From 472b0a3baa5f801addcb610808e3c780adff46fd Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 30 May 2026 13:54:22 +0200 Subject: [PATCH] Tests/LibWeb: Use unique localhost hosts for echo-served tests Load tests that require the echo server through a per-test *.localhost hostname instead of plain localhost. This prevents shared per-host browser state, from leaking between parallel or repeated test runs. Update HTTPTestServer to create echo URLs on the current origin when a test is already loaded from the echo server, preserving same-origin behavior for tests that inspect echo-served iframes. --- Tests/LibWeb/Text/input/include.js | 11 +++++++++-- .../Text/input/wpt-import/common/get-host-info.sub.js | 9 ++++++--- Tests/LibWeb/test-web/main.cpp | 7 ++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Tests/LibWeb/Text/input/include.js b/Tests/LibWeb/Text/input/include.js index 46a8c624eb..3df8e766eb 100644 --- a/Tests/LibWeb/Text/input/include.js +++ b/Tests/LibWeb/Text/input/include.js @@ -126,8 +126,15 @@ class HTTPTestServer { } const __httpTestServer = (function () { - if (globalThis.internals && globalThis.internals.getEchoServerPort) - return new HTTPTestServer(`http://localhost:${internals.getEchoServerPort()}`); + if (globalThis.internals && globalThis.internals.getEchoServerPort) { + const echoServerPort = internals.getEchoServerPort(); + const isLoadedFromEchoServer = location.protocol === "http:" && location.port === String(echoServerPort); + + // Tests loaded through the echo server should create echo URLs on their current origin, + // so same-origin iframe/fetch checks keep working with unique localhost hostnames. + const baseURL = isLoadedFromEchoServer ? location.origin : `http://localhost:${echoServerPort}`; + return new HTTPTestServer(baseURL); + } return null; })(); diff --git a/Tests/LibWeb/Text/input/wpt-import/common/get-host-info.sub.js b/Tests/LibWeb/Text/input/wpt-import/common/get-host-info.sub.js index 73f9641068..7bc953bd98 100644 --- a/Tests/LibWeb/Text/input/wpt-import/common/get-host-info.sub.js +++ b/Tests/LibWeb/Text/input/wpt-import/common/get-host-info.sub.js @@ -18,10 +18,13 @@ function get_host_info() { var HTTP_PORT2_ELIDED = HTTP_PORT2 == "80" ? "" : (":" + HTTP_PORT2); var HTTPS_PORT_ELIDED = HTTPS_PORT == "443" ? "" : (":" + HTTPS_PORT); var PORT_ELIDED = IS_HTTPS ? HTTPS_PORT_ELIDED : HTTP_PORT_ELIDED; - var ORIGINAL_HOST = 'localhost'; - var REMOTE_HOST = (ORIGINAL_HOST === 'localhost') ? '127.0.0.1' : ('www1.' + ORIGINAL_HOST); + // test-web serves HTTP-loaded tests from per-test *.localhost hosts, so the + // "original" host must match the current page rather than hard-code localhost. + var ORIGINAL_HOST = self.location.hostname; + var USE_LOOPBACK_REMOTE_HOST = (ORIGINAL_HOST === 'localhost') || ORIGINAL_HOST.endsWith('.localhost'); + var REMOTE_HOST = USE_LOOPBACK_REMOTE_HOST ? '127.0.0.1' : ('www1.' + ORIGINAL_HOST); var OTHER_HOST = 'www2.wpt.live'; - var NOTSAMESITE_HOST = (ORIGINAL_HOST === 'localhost') ? '127.0.0.1' : ('not-wpt.live'); + var NOTSAMESITE_HOST = USE_LOOPBACK_REMOTE_HOST ? '127.0.0.1' : ('not-wpt.live'); return { HTTP_PORT: HTTP_PORT, diff --git a/Tests/LibWeb/test-web/main.cpp b/Tests/LibWeb/test-web/main.cpp index d723045c9f..efd870a4c6 100644 --- a/Tests/LibWeb/test-web/main.cpp +++ b/Tests/LibWeb/test-web/main.cpp @@ -122,6 +122,11 @@ static ErrorOr add_config_paths(StringView test_root_path, Vector load_test_config(StringView test_root_path) { auto config_path = LexicalPath::join(test_root_path, "TestConfig.ini"sv); @@ -1090,7 +1095,7 @@ static void run_test(TestWebView& view, TestRunContext& context, size_t test_ind VERIFY(echo_server_port.has_value()); auto relative_path = LexicalPath::relative_path(real_path, app.test_root_path); VERIFY(relative_path.has_value()); - url = URL::Parser::basic_parse(ByteString::formatted("http://localhost:{}/static/{}", echo_server_port.value(), relative_path.value())).release_value(); + url = URL::Parser::basic_parse(ByteString::formatted("http://{}:{}/static/{}", unique_localhost_hostname("test-web"sv), echo_server_port.value(), relative_path.value())).release_value(); } else { url = URL::create_with_file_scheme(real_path).release_value(); }