diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index 7bc428ea..431b4bff 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -62,7 +62,13 @@ 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", + # 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 dda95aa1..36cc6b8a 100644 --- a/tests/functional/proxy_test.py +++ b/tests/functional/proxy_test.py @@ -32,6 +32,20 @@ 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_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 c62efdbc..10f5b708 100644 --- a/tests/unit/viahtml/hooks/_headers_test.py +++ b/tests/unit/viahtml/hooks/_headers_test.py @@ -50,6 +50,30 @@ def test_modify_outbound_removes_blocked_items( assert blocked_header not in modified_headers assert expected_header in modified_headers + @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 one of these was taken out of the list. + # This test names the headers so that it would fail instead. + original_headers = [ + (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 header.lower() 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..86c7a159 100644 --- a/viahtml/hooks/_headers.py +++ b/viahtml/hooks/_headers.py @@ -26,6 +26,29 @@ 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", + # 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",