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): serve MIME-controlled nosniff scripts

ldm04473d42935d3
diff --git a/moli-benchmark/moli_benchmark/wpt_cross/server.py b/moli-benchmark/moli_benchmark/wpt_cross/server.py
index 830d3b092..275ecf00e 100644
--- a/moli-benchmark/moli_benchmark/wpt_cross/server.py
+++ b/moli-benchmark/moli_benchmark/wpt_cross/server.py
@@ -1964,6 +1964,24 @@ def _form_echo_response(body: bytes) -> bytes:
return b" ".join(f"{byte:02x}".encode("ascii") for byte in body)
+def _nosniff_javascript_response(query: str) -> tuple[str | None, bytes]:
+ """Model fetch/nosniff/resources/js.py's MIME-controlled script body."""
+
+ params = parse_qsl(query, keep_blank_values=True)
+ outcome = next(
+ (value for name, value in params if name == "outcome"),
+ "f",
+ )
+ content_type = next(
+ (value for name, value in params if name == "type"),
+ None,
+ )
+ type_label = content_type if content_type is not None else "Content-Type missing"
+ result_call = "log('FAIL: " + type_label + "')" if outcome == "f" else "p()"
+ body = ("// nothing to see here\n" + result_call).encode()
+ return content_type, body
+
+
def _make_handler(
wpt_root: Path,
results_store: "ResultsStore",
@@ -2697,6 +2715,9 @@ requestExecutor("{executor_uuid}", {start_on_js});
self.send_error(500, "Invalid WPT template or pipe")
def _serve_response(self, *, emit_body: bool) -> None:
+ if unquote(urlsplit(self.path).path) == "/fetch/nosniff/resources/js.py":
+ self._serve_nosniff_javascript(urlsplit(self.path).query, emit_body=emit_body)
+ return
if unquote(urlsplit(self.path).path) == FORM_ECHO_PATH:
self._send_bytes("text/plain", _form_echo_response(b""), emit_body=emit_body)
return
@@ -3621,6 +3642,21 @@ requestExecutor("{executor_uuid}", {start_on_js});
)
+ def _serve_nosniff_javascript(self, query: str, *, emit_body: bool) -> None:
+ content_type, body = _nosniff_javascript_response(query)
+ self.send_response(200)
+ self.send_header("X-Content-Type-Options", "nosniff")
+ self.send_header("Content-Length", str(len(body)))
+ if content_type is not None:
+ self.send_header("Content-Type", content_type)
+ self.end_headers()
+ if emit_body:
+ try:
+ self.wfile.write(body)
+ except (BrokenPipeError, ConnectionResetError):
+ return
+
+
def _serve_xhr_delay(self, query: str, *, emit_body: bool) -> None:
delay_seconds = _wpt_delay_seconds(query)
if delay_seconds is None:
diff --git a/moli-benchmark/tests/test_wpt_cross_fixture_responses.py b/moli-benchmark/tests/test_wpt_cross_fixture_responses.py
index acf5836e9..ed84d5f2e 100644
--- a/moli-benchmark/tests/test_wpt_cross_fixture_responses.py
+++ b/moli-benchmark/tests/test_wpt_cross_fixture_responses.py
@@ -643,3 +643,52 @@ class WptCrossFixtureResponsesTests(WptCrossTestCase):
("POST", 200, b"00 09 7f 80 ff", "text/plain"),
],
)
+
+ def test_fixture_server_models_fetch_nosniff_javascript_handler(self) -> None:
+ self.assertEqual(
+ _nosniff_javascript_response(""),
+ (
+ None,
+ b"// nothing to see here\nlog('FAIL: Content-Type missing')",
+ ),
+ )
+ self.assertEqual(
+ _nosniff_javascript_response("type=text%2Fjavascript&outcome=p"),
+ ("text/javascript", b"// nothing to see here\np()"),
+ )
+
+ with tempfile.TemporaryDirectory() as root:
+ root_path = Path(root)
+ (root_path / "resources").mkdir()
+ (root_path / "resources" / "testharness.js").write_text(
+ "// testharness", encoding="utf-8"
+ )
+ with WptFixtureServer(root_path) as server:
+ base_url = f"{server.base_url}/fetch/nosniff/resources/js.py"
+ responses = []
+ for query in (
+ "",
+ "?type=",
+ "?type=text%2Fjavascript&outcome=p",
+ ):
+ with urlopen(base_url + query, timeout=2) as response:
+ responses.append(
+ (
+ response.headers.get("Content-Type"),
+ response.headers["X-Content-Type-Options"],
+ response.read(),
+ )
+ )
+
+ self.assertEqual(
+ responses,
+ [
+ (
+ None,
+ "nosniff",
+ b"// nothing to see here\nlog('FAIL: Content-Type missing')",
+ ),
+ ("", "nosniff", b"// nothing to see here\nlog('FAIL: ')"),
+ ("text/javascript", "nosniff", b"// nothing to see here\np()"),
+ ],
+ )
diff --git a/moli-benchmark/tests/wpt_cross_test_support.py b/moli-benchmark/tests/wpt_cross_test_support.py
index 562f9efea..227652158 100644
--- a/moli-benchmark/tests/wpt_cross_test_support.py
+++ b/moli-benchmark/tests/wpt_cross_test_support.py
@@ -110,6 +110,7 @@ from moli_benchmark.wpt_cross.scheduler import (
build_run_schedule,
)
from moli_benchmark.wpt_cross.server import (
+ _nosniff_javascript_response,
_workers_url_encoding_response,
_fetch_status_response,
BENCH_REPORT_BRIDGE,