From e82cde9fad24abdae1783b777188d76e727dad3f Mon Sep 17 00:00:00 2001 From: Santiago Regusci Date: Thu, 12 Nov 2026 01:10:10 -0300 Subject: [PATCH 1/2] Remove Cross-Origin-Opener-Policy from proxied responses The Hypothesis client logs people in through a popup window, and the page that h shows in the popup sends the authorization code back to the client with `window.opener.postMessage()`. When a proxied site sends `Cross-Origin-Opener-Policy: same-origin` the browser cuts the link between the page and the popups it opens, so `window.opener` is null in the popup and the code never reaches the client. The person presses "Log in", approves, and nothing happens. pywb passes this header through unchanged and Via HTML did not remove it. Add it to BLOCKED_OUTBOUND, next to Content-Security-Policy. Tests: - Unit: a test that names the header, in mixed, lower and upper case. The existing parametrized test only covers what is already in BLOCKED_OUTBOUND, so it could not fail for a header that is missing from the list. - Functional: the test upstream site now sends the header and proxy_test.py checks that it is not in the proxied response. Co-Authored-By: Claude Fable 5.2 --- tests/functional/conftest.py | 7 ++++++- tests/functional/proxy_test.py | 6 ++++++ tests/unit/viahtml/hooks/_headers_test.py | 17 +++++++++++++++++ viahtml/hooks/_headers.py | 12 ++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index 7bc428ea..35ffaf1a 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -62,7 +62,12 @@ def upstream_website(): with serve_content( # pylint: disable=not-context-manager minimal_valid_html, port=8197, - extra_headers={"Cache-Control": "public, max-age=60"}, + extra_headers={ + "Cache-Control": "public, max-age=60", + # A header that Via HTML has to remove, so the tests can check + # that it does not reach the browser. + "Cross-Origin-Opener-Policy": "same-origin", + }, ): yield diff --git a/tests/functional/proxy_test.py b/tests/functional/proxy_test.py index dda95aa1..be4b82d5 100644 --- a/tests/functional/proxy_test.py +++ b/tests/functional/proxy_test.py @@ -32,6 +32,12 @@ def test_we_do_not_have_a_csp_header(self, proxied_content): assert policy == "*MISSING*" + def test_we_do_not_have_a_coop_header(self, proxied_content): + # The upstream site sends this header, see `upstream_website()`. + policy = proxied_content.headers.get("Cross-Origin-Opener-Policy", "*MISSING*") + + assert policy == "*MISSING*" + def test_we_dont_rewrite_links_by_default(self, proxied_content): assert 'link' in proxied_content diff --git a/tests/unit/viahtml/hooks/_headers_test.py b/tests/unit/viahtml/hooks/_headers_test.py index c62efdbc..3d6612c3 100644 --- a/tests/unit/viahtml/hooks/_headers_test.py +++ b/tests/unit/viahtml/hooks/_headers_test.py @@ -50,6 +50,23 @@ def test_modify_outbound_removes_blocked_items( assert blocked_header not in modified_headers assert expected_header in modified_headers + def test_modify_outbound_removes_cross_origin_opener_policy( + self, headers, string_case + ): + # The test above only covers the headers that are in BLOCKED_OUTBOUND, + # so it would still pass if this header was taken out of the list. + # This test names the header so that it would fail instead. + original_headers = [ + (string_case("Cross-Origin-Opener-Policy"), "same-origin"), + ("Other-Header", "ok"), + ] + + modified_headers = headers.modify_outbound(original_headers) + + header_names = [name.lower() for name, _value in modified_headers] + assert "cross-origin-opener-policy" not in header_names + assert "other-header" in header_names + def test_cache_control_header_is_set_to_no_store(self, headers): modified_headers = headers.modify_outbound(tuple()) diff --git a/viahtml/hooks/_headers.py b/viahtml/hooks/_headers.py index cf9cb5a5..a6caa9c4 100644 --- a/viahtml/hooks/_headers.py +++ b/viahtml/hooks/_headers.py @@ -26,6 +26,18 @@ class Headers: BLOCKED_OUTBOUND = { # Disable the Content-Security-Policy which blocks our embed "Content-Security-Policy", + # The Hypothesis client logs people in through a popup window. The + # page that h shows in the popup sends the result of the login back to + # the client with `window.opener.postMessage()`. + # + # Sites can use the Cross-Origin-Opener-Policy header to tell the + # browser to cut the link between a page and the popups it opens (for + # example: Cross-Origin-Opener-Policy: same-origin). The popup's + # `window.opener` is then null, the result never reaches the client, + # and the person stays logged out after pressing "Log in" and + # approving. We block Cross-Origin-Opener-Policy headers to prevent + # that. + "Cross-Origin-Opener-Policy", # Various headers added by `pywb` relating to archival "Memento-Datetime", "Link", From 486f98134f0c83d76b8498e6a88f17a928e393c8 Mon Sep 17 00:00:00 2001 From: Santiago Regusci Date: Thu, 12 Nov 2026 01:24:37 -0300 Subject: [PATCH 2/2] Remove Cross-Origin-Embedder-Policy from proxied responses too A site that sends Cross-Origin-Embedder-Policy tells the browser to refuse anything from another site that has not opted in. The Hypothesis client's script and the frame its sidebar lives in both come from another site and neither opts in, so on such a page there is no Hypothesis at all. Tested on a local page that embeds the public client: - no header: the client loads and the sidebar works; - require-corp: the browser blocks the client's own script (ERR_BLOCKED_BY_RESPONSE.NotSameOriginAfterDefaultedToSameOriginByCoep); - credentialless: the script loads and the sidebar's frame is blocked. The header only does its job together with Cross-Origin-Opener-Policy, which the previous commit removes, so leaving it would protect nothing and only block the client. The unit test now names both headers, and the functional test site sends both. The new cases failed before the change (3 unit failures). Co-Authored-By: Claude Fable 5.2 --- tests/functional/conftest.py | 5 +++-- tests/functional/proxy_test.py | 8 ++++++++ tests/unit/viahtml/hooks/_headers_test.py | 19 +++++++++++++------ viahtml/hooks/_headers.py | 11 +++++++++++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index 35ffaf1a..431b4bff 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -64,9 +64,10 @@ def upstream_website(): port=8197, extra_headers={ "Cache-Control": "public, max-age=60", - # A header that Via HTML has to remove, so the tests can check - # that it does not reach the browser. + # Headers that Via HTML has to remove, so the tests can check + # that they do not reach the browser. "Cross-Origin-Opener-Policy": "same-origin", + "Cross-Origin-Embedder-Policy": "require-corp", }, ): yield diff --git a/tests/functional/proxy_test.py b/tests/functional/proxy_test.py index be4b82d5..36cc6b8a 100644 --- a/tests/functional/proxy_test.py +++ b/tests/functional/proxy_test.py @@ -38,6 +38,14 @@ def test_we_do_not_have_a_coop_header(self, proxied_content): assert policy == "*MISSING*" + def test_we_do_not_have_a_coep_header(self, proxied_content): + # The upstream site sends this header, see `upstream_website()`. + policy = proxied_content.headers.get( + "Cross-Origin-Embedder-Policy", "*MISSING*" + ) + + assert policy == "*MISSING*" + def test_we_dont_rewrite_links_by_default(self, proxied_content): assert 'link' in proxied_content diff --git a/tests/unit/viahtml/hooks/_headers_test.py b/tests/unit/viahtml/hooks/_headers_test.py index 3d6612c3..10f5b708 100644 --- a/tests/unit/viahtml/hooks/_headers_test.py +++ b/tests/unit/viahtml/hooks/_headers_test.py @@ -50,21 +50,28 @@ def test_modify_outbound_removes_blocked_items( assert blocked_header not in modified_headers assert expected_header in modified_headers - def test_modify_outbound_removes_cross_origin_opener_policy( - self, headers, string_case + @pytest.mark.parametrize( + "header,value", + ( + ("Cross-Origin-Opener-Policy", "same-origin"), + ("Cross-Origin-Embedder-Policy", "require-corp"), + ), + ) + def test_modify_outbound_removes_the_cross_origin_policies( + self, headers, string_case, header, value ): # The test above only covers the headers that are in BLOCKED_OUTBOUND, - # so it would still pass if this header was taken out of the list. - # This test names the header so that it would fail instead. + # so it would still pass if one of these was taken out of the list. + # This test names the headers so that it would fail instead. original_headers = [ - (string_case("Cross-Origin-Opener-Policy"), "same-origin"), + (string_case(header), value), ("Other-Header", "ok"), ] modified_headers = headers.modify_outbound(original_headers) header_names = [name.lower() for name, _value in modified_headers] - assert "cross-origin-opener-policy" not in header_names + assert header.lower() not in header_names assert "other-header" in header_names def test_cache_control_header_is_set_to_no_store(self, headers): diff --git a/viahtml/hooks/_headers.py b/viahtml/hooks/_headers.py index a6caa9c4..86c7a159 100644 --- a/viahtml/hooks/_headers.py +++ b/viahtml/hooks/_headers.py @@ -38,6 +38,17 @@ class Headers: # approving. We block Cross-Origin-Opener-Policy headers to prevent # that. "Cross-Origin-Opener-Policy", + # Sites can use the Cross-Origin-Embedder-Policy header to tell the + # browser to refuse anything from another site that has not opted in + # (for example: Cross-Origin-Embedder-Policy: require-corp). The + # Hypothesis client's script and the frame its sidebar lives in both + # come from another site and do not opt in, so the browser blocks them + # and the page shows without Hypothesis. + # + # The two headers only do their job together, so with + # Cross-Origin-Opener-Policy removed above this one protects nothing + # and only blocks the client. + "Cross-Origin-Embedder-Policy", # Various headers added by `pywb` relating to archival "Memento-Datetime", "Link",