Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(wpt): keep loopback host templates tied to the primary domain

ldm0967c9de88868
diff --git a/moli-benchmark/moli_benchmark/wpt_cross/__main__.py b/moli-benchmark/moli_benchmark/wpt_cross/__main__.py
index 552ccddde..90d3e86f2 100644
--- a/moli-benchmark/moli_benchmark/wpt_cross/__main__.py
+++ b/moli-benchmark/moli_benchmark/wpt_cross/__main__.py
@@ -691,7 +691,7 @@ def main(argv: list[str] | None = None) -> int:
engine_results: dict[str, list[dict[str, Any]]] = {}
engine_metadata: dict[str, dict[str, Any]] = {}
- with WptFixtureServer(args.wpt_root) as server:
+ with WptFixtureServer(args.wpt_root, primary_hostname="web-platform.localhost") as server:
meta_path = output_dir / "fixture-server.json"
meta_path.write_text(
json.dumps(
diff --git a/moli-benchmark/moli_benchmark/wpt_cross/cli_runner.py b/moli-benchmark/moli_benchmark/wpt_cross/cli_runner.py
index a245c6cb6..80f7ac64d 100644
--- a/moli-benchmark/moli_benchmark/wpt_cross/cli_runner.py
+++ b/moli-benchmark/moli_benchmark/wpt_cross/cli_runner.py
@@ -305,7 +305,7 @@ def _run_cli_case_worker(job: _CliCaseWorkerInput) -> CaseResult:
)
binary = Path(job.binary)
- with WptFixtureServer(Path(job.wpt_root)) as server:
+ with WptFixtureServer(Path(job.wpt_root), primary_hostname="web-platform.localhost") as server:
server.set_harness_timeout_multipliers(
{job.case_path: job.harness_timeout_multiplier},
default_multiplier=job.harness_timeout_multiplier,
diff --git a/moli-benchmark/moli_benchmark/wpt_cross/server.py b/moli-benchmark/moli_benchmark/wpt_cross/server.py
index fbb8abd4e..ded28b2eb 100644
--- a/moli-benchmark/moli_benchmark/wpt_cross/server.py
+++ b/moli-benchmark/moli_benchmark/wpt_cross/server.py
@@ -1287,6 +1287,10 @@ def _substitute_wpt_template_variables(
alternate_port = port
if remote_port is None:
remote_port = alternate_port
+ template_primary_hostname = primary_hostname or "localhost"
+ if ":" in template_primary_hostname:
+ template_primary_hostname = "localhost"
+ template_primary_host = template_primary_hostname.encode("utf-8")
if primary_hostname is None:
primary_hostname = request_hostname
request_path_bytes = request_path.encode("utf-8", errors="replace")
@@ -1340,16 +1344,16 @@ def _substitute_wpt_template_variables(
b"http://" + request_url_host_bytes + b":" + str(remote_port).encode("ascii")
),
b"https://{{hosts[][www]}}:{{ports[https][0]}}": (
- b"http://www.localhost:" + str(port).encode("ascii")
+ b"http://www." + template_primary_host + b":" + str(port).encode("ascii")
),
b"https://{{hosts[][www]}}:{{ports[https][1]}}": (
- b"http://www.localhost:" + str(alternate_port).encode("ascii")
+ b"http://www." + template_primary_host + b":" + str(alternate_port).encode("ascii")
),
b"https://{{hosts[][]}}:{{ports[https][0]}}": (
- b"http://localhost:" + str(port).encode("ascii")
+ b"http://" + template_primary_host + b":" + str(port).encode("ascii")
),
b"https://{{hosts[][]}}:{{ports[https][1]}}": (
- b"http://localhost:" + str(alternate_port).encode("ascii")
+ b"http://" + template_primary_host + b":" + str(alternate_port).encode("ascii")
),
b"https://{{hosts[alt][]}}:{{ports[https][0]}}": (
b"http://alt.localhost:" + str(port).encode("ascii")
@@ -1450,7 +1454,7 @@ def _substitute_wpt_template_variables(
subdomain = match.group("subdomain")
namespace = match.group("namespace")
prefix = _WPT_SUBDOMAIN_PREFIXES.get(subdomain)
- host = {b"": b"localhost", b"alt": b"alt.localhost"}.get(namespace)
+ host = {b"": template_primary_host, b"alt": b"alt.localhost"}.get(namespace)
if prefix is None or host is None:
return match.group(0)
return prefix + host
@@ -3658,7 +3662,10 @@ class WptFixtureServer:
refuse loopback fixtures (Obscura).
"""
- def __init__(self, wpt_root: Path) -> None:
+ def __init__(self, wpt_root: Path, *, primary_hostname: str = "localhost") -> None:
+ self.primary_hostname = primary_hostname.encode("idna").decode("ascii").lower()
+ if self.primary_hostname != "localhost" and not self.primary_hostname.endswith(".localhost"):
+ raise ValueError("loopback fixture hostname must be localhost or a .localhost subdomain")
self.wpt_root = wpt_root.resolve()
if not self.wpt_root.exists():
raise RuntimeError(f"WPT root does not exist: {self.wpt_root}")
@@ -3685,7 +3692,7 @@ class WptFixtureServer:
httpd,
primary_port=self.port,
alternate_port=self.alternate_port,
- primary_hostname="localhost",
+ primary_hostname=self.primary_hostname,
)
self.thread = threading.Thread(
target=self.httpd.serve_forever,
@@ -3773,7 +3780,7 @@ class WptFixtureServer:
@property
def base_url(self) -> str:
- return f"http://localhost:{self.port}"
+ return f"http://{self.primary_hostname}:{self.port}"
@property
def external_base_url(self) -> str | None:
@@ -3795,7 +3802,7 @@ class WptFixtureServer:
@property
def alternate_base_url(self) -> str:
- return f"http://localhost:{self.alternate_port}"
+ return f"http://{self.primary_hostname}:{self.alternate_port}"
def _configure_httpd_defaults(
self,
diff --git a/moli-benchmark/tests/test_wpt_cross_case_lists.py b/moli-benchmark/tests/test_wpt_cross_case_lists.py
index 5543f95d4..4d401b697 100644
--- a/moli-benchmark/tests/test_wpt_cross_case_lists.py
+++ b/moli-benchmark/tests/test_wpt_cross_case_lists.py
@@ -350,7 +350,8 @@ class WptCrossCaseListsTests(WptCrossTestCase):
calls = []
class FakeServer:
- def __init__(self, wpt_root: Path) -> None:
+ def __init__(self, wpt_root: Path, *, primary_hostname: str) -> None:
+ assert primary_hostname == "web-platform.localhost"
self.wpt_root = Path(wpt_root)
self.base_url = "http://127.0.0.1:8000"
self.alternate_base_url = "http://127.0.0.1:8001"
diff --git a/moli-benchmark/tests/test_wpt_cross_cli_runner.py b/moli-benchmark/tests/test_wpt_cross_cli_runner.py
index a0edb8a42..f78f1a762 100644
--- a/moli-benchmark/tests/test_wpt_cross_cli_runner.py
+++ b/moli-benchmark/tests/test_wpt_cross_cli_runner.py
@@ -192,7 +192,8 @@ class WptCrossCliRunnerTests(WptCrossTestCase):
return None
class FakeServer:
- def __init__(self, wpt_root: Path) -> None:
+ def __init__(self, wpt_root: Path, *, primary_hostname: str) -> None:
+ assert primary_hostname == "web-platform.localhost"
self.wpt_root = Path(wpt_root)
self.results = FakeResults()
self.external_host = None
diff --git a/moli-benchmark/tests/test_wpt_cross_fixture_resources.py b/moli-benchmark/tests/test_wpt_cross_fixture_resources.py
index 4b477548f..13ec26083 100644
--- a/moli-benchmark/tests/test_wpt_cross_fixture_resources.py
+++ b/moli-benchmark/tests/test_wpt_cross_fixture_resources.py
@@ -532,7 +532,7 @@ class WptCrossFixtureResourcesTests(WptCrossTestCase):
self,
) -> None:
body = (
- b"host={{host}} "
+ b"host={{host}} domain={{domains[]}} "
b"location={{location[hostname]}} "
b"HTTP_ORIGIN: 'http://' + ORIGINAL_HOST + HTTP_PORT_ELIDED,"
)
@@ -544,7 +544,7 @@ class WptCrossFixtureResourcesTests(WptCrossTestCase):
request_hostname="www1.localhost",
primary_hostname="localhost",
),
- b"host=localhost location=www1.localhost "
+ b"host=localhost domain=localhost location=www1.localhost "
b"HTTP_ORIGIN: 'http://' + ORIGINAL_HOST + HTTP_PORT_ELIDED,",
)
def test_host_header_hostname_preserves_ipv6_brackets(self) -> None:
@@ -653,3 +653,75 @@ class WptCrossFixtureResourcesTests(WptCrossTestCase):
b"('http://foo:bar@' + REMOTE_HOST + ':34567') : "
b"('http://foo:bar@' + REMOTE_HOST + HTTP_PORT2_ELIDED),",
)
+
+ def test_fixture_server_uses_configured_primary_hostname_for_aliases(self) -> None:
+ with tempfile.TemporaryDirectory() as root:
+ root_path = Path(root)
+ (root_path / "resources").mkdir()
+ (root_path / "resources" / "testharness.js").write_text("", encoding="utf-8")
+ (root_path / "hosts.sub.txt").write_text(
+ "{{host}}|{{domains[]}}|{{hosts[][]}}|{{domains[www1]}}|"
+ "{{hosts[][élève]}}|{{hosts[alt][www2]}}|{{location[hostname]}}",
+ encoding="utf-8",
+ )
+ (root_path / "hosts.sub.txt.sub.headers").write_text(
+ "Access-Control-Allow-Origin: http://{{hosts[][]}}:{{ports[http][0]}}\n",
+ encoding="utf-8",
+ )
+ with WptFixtureServer(root_path, primary_hostname="web-platform.localhost") as server:
+ self.assertEqual(server.base_url, f"http://web-platform.localhost:{server.port}")
+ self.assertEqual(
+ server.alternate_base_url,
+ f"http://web-platform.localhost:{server.alternate_port}",
+ )
+ for hostname in ("web-platform.localhost", "www1.web-platform.localhost"):
+ with self.subTest(hostname=hostname):
+ connection = HTTPConnection("127.0.0.1", server.port, timeout=2)
+ try:
+ connection.request(
+ "GET", "/hosts.sub.txt",
+ headers={"Host": f"{hostname}:{server.port}"},
+ )
+ response = connection.getresponse()
+ self.assertEqual(response.status, 200)
+ self.assertEqual(
+ response.read().decode(),
+ "web-platform.localhost|web-platform.localhost|web-platform.localhost|"
+ "www1.web-platform.localhost|xn--lve-6lad.web-platform.localhost|"
+ f"www2.alt.localhost|{hostname}",
+ )
+ self.assertEqual(
+ response.getheader("Access-Control-Allow-Origin"), server.base_url,
+ )
+ finally:
+ connection.close()
+
+ def test_fixture_server_substitutes_primary_domain_in_cors_sidecar(self) -> None:
+ with tempfile.TemporaryDirectory() as root:
+ fixture = Path(root) / "style.css"
+ fixture.write_text("body {}", encoding="utf-8")
+ fixture.with_name("style.css.sub.headers").write_text(
+ "Access-Control-Allow-Origin: {{location[scheme]}}://{{domains[]}}{{GET[acao_port]}}\n"
+ "Access-Control-Allow-Credentials: true\n",
+ encoding="utf-8",
+ )
+
+ for port in (80, 12345):
+ with self.subTest(port=port):
+ suffix = "" if port == 80 else f":{port}"
+ query = "" if port == 80 else f"acao_port=%3A{port}"
+ self.assertEqual(
+ _static_response_headers(
+ fixture,
+ query,
+ port=port,
+ request_hostname="www.example.test",
+ primary_hostname="example.test",
+ ),
+ [
+ ("Access-Control-Allow-Origin", f"http://example.test{suffix}"),
+ ("Access-Control-Allow-Credentials", "true"),
+ ],
+ )
+
+
diff --git a/moli-benchmark/tests/test_wpt_cross_runner_orchestration.py b/moli-benchmark/tests/test_wpt_cross_runner_orchestration.py
index ed7f3bd4c..946a5a884 100644
--- a/moli-benchmark/tests/test_wpt_cross_runner_orchestration.py
+++ b/moli-benchmark/tests/test_wpt_cross_runner_orchestration.py
@@ -8,7 +8,8 @@ class WptCrossRunnerOrchestrationTests(WptCrossTestCase):
captured: dict[str, object] = {}
class FakeServer:
- def __init__(self, wpt_root: Path) -> None:
+ def __init__(self, wpt_root: Path, *, primary_hostname: str) -> None:
+ assert primary_hostname == "web-platform.localhost"
self.wpt_root = Path(wpt_root)
self.base_url = "http://127.0.0.1:8000"
self.alternate_base_url = "http://127.0.0.1:8001"
@@ -132,7 +133,8 @@ class WptCrossRunnerOrchestrationTests(WptCrossTestCase):
calls: list[dict[str, object]] = []
class FakeServer:
- def __init__(self, wpt_root: Path) -> None:
+ def __init__(self, wpt_root: Path, *, primary_hostname: str) -> None:
+ assert primary_hostname == "web-platform.localhost"
self.wpt_root = Path(wpt_root)
self.base_url = "http://127.0.0.1:8000"
self.alternate_base_url = "http://127.0.0.1:8001"
@@ -243,7 +245,8 @@ class WptCrossRunnerOrchestrationTests(WptCrossTestCase):
captured: dict[str, object] = {}
class FakeServer:
- def __init__(self, wpt_root: Path) -> None:
+ def __init__(self, wpt_root: Path, *, primary_hostname: str) -> None:
+ assert primary_hostname == "web-platform.localhost"
self.wpt_root = Path(wpt_root)
self.base_url = "http://127.0.0.1:8000"
self.alternate_base_url = "http://127.0.0.1:8001"
@@ -333,7 +336,8 @@ class WptCrossRunnerOrchestrationTests(WptCrossTestCase):
calls = []
class FakeServer:
- def __init__(self, wpt_root: Path) -> None:
+ def __init__(self, wpt_root: Path, *, primary_hostname: str) -> None:
+ assert primary_hostname == "web-platform.localhost"
self.wpt_root = Path(wpt_root)
self.base_url = "http://127.0.0.1:8000"
self.alternate_base_url = "http://127.0.0.1:8001"
diff --git a/moli-benchmark/tests/wpt_cross_test_support.py b/moli-benchmark/tests/wpt_cross_test_support.py
index a4cccf180..b4e268ecb 100644
--- a/moli-benchmark/tests/wpt_cross_test_support.py
+++ b/moli-benchmark/tests/wpt_cross_test_support.py
@@ -167,7 +167,8 @@ class WptCrossTestCase(unittest.TestCase):
extra_args: list[str] | None = None,
) -> int:
class FakeServer:
- def __init__(self, wpt_root: Path) -> None:
+ def __init__(self, wpt_root: Path, *, primary_hostname: str) -> None:
+ assert primary_hostname == "web-platform.localhost"
self.wpt_root = Path(wpt_root)
self.base_url = "http://127.0.0.1:8000"
self.alternate_base_url = "http://127.0.0.1:8001"