diff --git a/tests/conftest.py b/tests/conftest.py index 87d437b2..2537a1bf 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,6 +14,33 @@ SOURCE = ROOT / "vendor/data/plugins/jiang_image" +def _chromium_socket_launch_options(arguments): + disabled = [arg for arg in arguments if arg.startswith("--disable-features=")] + if len(disabled) > 1: + raise RuntimeError("Expected at most one Chromium --disable-features default") + features = disabled[0].split("=", 1)[1].split(",") if disabled else [] + features = list(dict.fromkeys([*filter(None, features), "TcpPortRandomizationWin"])) + return {"ignore_default_args": disabled, "args": ["--disable-features=" + ",".join(features)]} + + +@pytest.fixture(scope="session") +def manager_browser_launch_options(): + if sys.platform != "win32": + return {} + from playwright.sync_api import sync_playwright + + # Backport https://github.com/microsoft/playwright/pull/43605 without + # replacing Playwright's other disabled features or reading driver internals. + # A single short-lived probe captures the installed version's real defaults. + with sync_playwright() as runtime: + instance = runtime.chromium.launch(args=["--enable-automation"]) + try: + arguments = instance.new_browser_cdp_session().send("Browser.getBrowserCommandLine")["arguments"] + finally: + instance.close() + return _chromium_socket_launch_options(arguments) + + @pytest.fixture(autouse=True) def manager_browser_failure_evidence(request, monkeypatch): """Keep startup evidence even when an early assertion skips final error checks.""" diff --git a/tests/test_browser_launch.py b/tests/test_browser_launch.py new file mode 100644 index 00000000..fc22447e --- /dev/null +++ b/tests/test_browser_launch.py @@ -0,0 +1,57 @@ +"""Windows loopback mitigation preserves Chromium's existing launch defaults.""" +import sys + +import pytest +from playwright.sync_api import sync_playwright + + +@pytest.mark.parametrize("arguments,features", [ + (["--headless", "--disable-features=FeatureA,FeatureB"], ["FeatureA", "FeatureB", "TcpPortRandomizationWin"]), + (["--disable-features=FeatureA,TcpPortRandomizationWin"], ["FeatureA", "TcpPortRandomizationWin"]), + (["--headless"], ["TcpPortRandomizationWin"]), +]) +def test_socket_options_keep_existing_disabled_features(arguments, features): + from conftest import _chromium_socket_launch_options + + original = list(arguments) + options = _chromium_socket_launch_options(arguments) + assert options["ignore_default_args"] == [arg for arg in arguments if arg.startswith("--disable-features=")] + assert options["args"] == ["--disable-features=" + ",".join(features)] + assert arguments == original + + +def test_socket_options_reject_ambiguous_default_switches(): + from conftest import _chromium_socket_launch_options + + with pytest.raises(RuntimeError, match="disable-features"): + _chromium_socket_launch_options(["--disable-features=FeatureA", "--disable-features=FeatureB"]) + + +@pytest.mark.parametrize("platform", ["linux", "darwin"]) +def test_other_platforms_keep_default_launch_options(monkeypatch, platform): + import conftest + + monkeypatch.setattr(conftest.sys, "platform", platform) + assert conftest.manager_browser_launch_options.__wrapped__() == {} + + +def test_real_chromium_keeps_defaults_and_disables_randomized_windows_ports(manager_browser_launch_options): + if sys.platform != "win32": + assert manager_browser_launch_options == {} + return + options = manager_browser_launch_options + assert len(options["ignore_default_args"]) == 1 + original = options["ignore_default_args"][0].split("=", 1)[1].split(",") + # This CDP inspection requires the automation switch; normal manager tests + # keep their launch defaults and only replace the combined feature argument. + with sync_playwright() as runtime: + instance = runtime.chromium.launch(**{**options, "args": [*options["args"], "--enable-automation"]}) + try: + arguments = instance.new_browser_cdp_session().send("Browser.getBrowserCommandLine")["arguments"] + disabled = [arg for arg in arguments if arg.startswith("--disable-features=")] + assert disabled == options["args"] + features = disabled[0].split("=", 1)[1].split(",") + assert set(original) <= set(features) + assert features.count("TcpPortRandomizationWin") == 1 + finally: + instance.close() diff --git a/tests/test_manager_startup.py b/tests/test_manager_startup.py index 865611df..600f456b 100644 --- a/tests/test_manager_startup.py +++ b/tests/test_manager_startup.py @@ -34,9 +34,9 @@ class PreviewHandler(_handler(service, manager_assets=_asset_root())): @pytest.fixture(scope="module") -def browser(): +def browser(manager_browser_launch_options): with sync_playwright() as runtime: - instance = runtime.chromium.launch() + instance = runtime.chromium.launch(**manager_browser_launch_options) try: yield instance finally: