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.
This commit is contained in:
Shannon Booth 2026-05-30 13:54:22 +02:00 committed by Shannon Booth
parent cac30c8d73
commit 472b0a3baa
3 changed files with 21 additions and 6 deletions

View file

@ -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;
})();

View file

@ -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,

View file

@ -122,6 +122,11 @@ static ErrorOr<void> add_config_paths(StringView test_root_path, Vector<ByteStri
return {};
}
static ByteString unique_localhost_hostname(StringView prefix)
{
return ByteString::formatted("{}-{}.localhost", prefix, generate_random_uuid().to_byte_string());
}
static ErrorOr<void> 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();
}