Truly independent web browser
LibWeb: Report fetch CSP violations through a reporter function
Luke Wilde15fd00da8536
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cppindex 84690520710..a3865a86b7e 100644--- a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp+++ b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp@@ -87,10 +87,10 @@ namespace Web::ContentSecurityPolicy {}// https://w3c.github.io/webappsec-csp/#report-for-request-void report_content_security_policy_violations_for_request(JS::Realm& realm, GC::Ref<Fetch::Infrastructure::Request> request)+void report_content_security_policy_violations_for_request(Fetch::Infrastructure::Request const& request, ViolationReporter const& report_violation){// 1. Let CSP list be request’s policy container's CSP list.- auto const& csp_list = request->policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>()->csp_list;+ auto const& csp_list = request.policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>()->csp_list;// 2. For each policy of CSP list:for (auto const& policy : csp_list.policies()) {@@ -103,18 +103,24 @@ void report_content_security_policy_violations_for_request(JS::Realm& realm, GC:// 3. If violates is not "Does Not Violate", then execute § 5.5 Report a violation on the result of executing// § 2.4.2 Create a violation object for request, and policy. on request, and policy.- if (violates) {- auto violation = Violation::create_a_violation_object_for_request_and_policy(request, policy);- violation->report_a_violation(realm);- }+ if (violates)+ report_violation(policy);}}+ViolationReporter violation_reporter_for_request(JS::Realm& realm, GC::Ref<Fetch::Infrastructure::Request> request)+{+ return [&realm, request](NonnullRefPtr<Policy const> policy) {+ auto violation = Violation::create_a_violation_object_for_request_and_policy(request, move(policy));+ violation->report_a_violation(realm);+ };+}+// https://w3c.github.io/webappsec-csp/#should-block-request-Directives::Directive::Result should_request_be_blocked_by_content_security_policy(JS::Realm& realm, GC::Ref<Fetch::Infrastructure::Request> request)+Directives::Directive::Result should_request_be_blocked_by_content_security_policy(Fetch::Infrastructure::Request const& request, ViolationReporter const& report_violation){// 1. Let CSP list be request’s policy container's CSP list.- auto const& csp_list = request->policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>()->csp_list;+ auto const& csp_list = request.policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>()->csp_list;// 2. Let result be "Allowed".auto result = Directives::Directive::Result::Allowed;@@ -132,8 +138,7 @@ Directives::Directive::Result should_request_be_blocked_by_content_security_poliif (violates) {// 1. Execute § 5.5 Report a violation on the result of executing § 2.4.2 Create a violation object for// request, and policy. on request, and policy.- auto violation = Violation::create_a_violation_object_for_request_and_policy(request, policy);- violation->report_a_violation(realm);+ report_violation(policy);// 2. Set result to "Blocked".result = Directives::Directive::Result::Blocked;@@ -205,10 +210,10 @@ Directives::Directive::Result should_request_be_blocked_by_integrity_policy(GC::}// https://w3c.github.io/webappsec-csp/#should-block-response-Directives::Directive::Result should_response_to_request_be_blocked_by_content_security_policy(JS::Realm& realm, GC::Ref<Fetch::Infrastructure::Response> response, GC::Ref<Fetch::Infrastructure::Request> request)+Directives::Directive::Result should_response_to_request_be_blocked_by_content_security_policy(Fetch::Infrastructure::Response const& response, Fetch::Infrastructure::Request const& request, ViolationReporter const& report_violation){// 1. Let CSP list be request’s policy container's CSP list.- auto const& csp_list = request->policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>()->csp_list;+ auto const& csp_list = request.policy_container().get<NonnullRefPtr<HTML::PolicyContainer const>>()->csp_list;// 2. Let result be "Allowed".auto result = Directives::Directive::Result::Allowed;@@ -223,8 +228,7 @@ Directives::Directive::Result should_response_to_request_be_blocked_by_content_sif (Directives::post_request_check(directive, request, response, policy) == Directives::Directive::Result::Blocked) {// 1. Execute § 5.5 Report a violation on the result of executing § 2.4.2 Create a violation object for// request, and policy. on request, and policy.- auto violation = Violation::create_a_violation_object_for_request_and_policy(request, policy);- violation->report_a_violation(realm);+ report_violation(policy);// 2. If policy’s disposition is "enforce", then set result to "Blocked".if (policy->disposition() == Policy::Disposition::Enforce) {diff --git a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.hindex d0eb377d9f9..049cc6a6281 100644--- a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h+++ b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h@@ -6,6 +6,8 @@#pragma once+#include <AK/Function.h>+#include <AK/NonnullRefPtr.h>#include <AK/Utf16View.h>#include <LibJS/Runtime/VM.h>#include <LibURL/Forward.h>@@ -13,10 +15,15 @@namespace Web::ContentSecurityPolicy {-void report_content_security_policy_violations_for_request(JS::Realm&, GC::Ref<Fetch::Infrastructure::Request>);-Directives::Directive::Result should_request_be_blocked_by_content_security_policy(JS::Realm&, GC::Ref<Fetch::Infrastructure::Request>);+// Reports the violation for the request being checked and the given policy.+using ViolationReporter = Function<void(NonnullRefPtr<Policy const>)>;++void report_content_security_policy_violations_for_request(Fetch::Infrastructure::Request const&, ViolationReporter const&);+Directives::Directive::Result should_request_be_blocked_by_content_security_policy(Fetch::Infrastructure::Request const&, ViolationReporter const&);Directives::Directive::Result should_request_be_blocked_by_integrity_policy(GC::Ref<Fetch::Infrastructure::Request>);-Directives::Directive::Result should_response_to_request_be_blocked_by_content_security_policy(JS::Realm&, GC::Ref<Fetch::Infrastructure::Response>, GC::Ref<Fetch::Infrastructure::Request>);+Directives::Directive::Result should_response_to_request_be_blocked_by_content_security_policy(Fetch::Infrastructure::Response const&, Fetch::Infrastructure::Request const&, ViolationReporter const&);++[[nodiscard]] ViolationReporter violation_reporter_for_request(JS::Realm&, GC::Ref<Fetch::Infrastructure::Request>);Directives::Directive::Result should_navigation_request_of_type_be_blocked_by_content_security_policy(GC::Ref<Fetch::Infrastructure::Request> navigation_request, Directives::Directive::NavigationType navigation_type);Directives::Directive::Result should_navigation_response_to_navigation_request_of_type_in_target_be_blocked_by_content_security_policy(diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cppindex 10cbf003c2e..517d742c74e 100644--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp@@ -453,7 +453,8 @@ GC::Ptr<PendingResponse> main_fetch(JS::Realm& realm, Infrastructure::FetchParamresponse = Infrastructure::Response::network_error("Request with 'local-URLs-only' flag must have a local URL"_string);// 4. Run report Content Security Policy violations for request.- ContentSecurityPolicy::report_content_security_policy_violations_for_request(realm, request);+ auto report_violation = ContentSecurityPolicy::violation_reporter_for_request(realm, request);+ ContentSecurityPolicy::report_content_security_policy_violations_for_request(request, report_violation);// 5. Upgrade request to a potentially trustworthy URL, if appropriate.UpgradeInsecureRequests::upgrade_request_to_a_potentially_trustworthy_url_if_appropriate(request);@@ -466,7 +467,7 @@ GC::Ptr<PendingResponse> main_fetch(JS::Realm& realm, Infrastructure::FetchParam// returns blocked, then set response to a network error.if (Infrastructure::block_bad_port(request) == Infrastructure::RequestOrResponseBlocking::Blocked|| MixedContent::should_fetching_request_be_blocked_as_mixed_content(request) == Infrastructure::RequestOrResponseBlocking::Blocked- || ContentSecurityPolicy::should_request_be_blocked_by_content_security_policy(realm, request) == ContentSecurityPolicy::Directives::Directive::Result::Blocked+ || ContentSecurityPolicy::should_request_be_blocked_by_content_security_policy(request, report_violation) == ContentSecurityPolicy::Directives::Directive::Result::Blocked|| ContentSecurityPolicy::should_request_be_blocked_by_integrity_policy(request) == ContentSecurityPolicy::Directives::Directive::Result::Blocked) {response = Infrastructure::Response::network_error("Request was blocked"_string);}@@ -729,11 +730,12 @@ GC::Ptr<PendingResponse> main_fetch(JS::Realm& realm, Infrastructure::FetchParaminternal_response->set_timing_allow_passed(true);// 20. If response is not a network error and any of the following returns blocked+ auto report_violation = ContentSecurityPolicy::violation_reporter_for_request(realm, request);if (!response->is_network_error() && (// - should internalResponse to request be blocked as mixed contentMixedContent::should_response_to_request_be_blocked_as_mixed_content(request, internal_response) == Infrastructure::RequestOrResponseBlocking::Blocked// - should internalResponse to request be blocked by Content Security Policy- || ContentSecurityPolicy::should_response_to_request_be_blocked_by_content_security_policy(realm, internal_response, request) == ContentSecurityPolicy::Directives::Directive::Result::Blocked+ || ContentSecurityPolicy::should_response_to_request_be_blocked_by_content_security_policy(*internal_response, request, report_violation) == ContentSecurityPolicy::Directives::Directive::Result::Blocked// - should internalResponse to request be blocked due to its MIME type|| Infrastructure::should_response_to_request_be_blocked_due_to_its_mime_type(internal_response, request) == Infrastructure::RequestOrResponseBlocking::Blocked// - should internalResponse to request be blocked due to nosniffdiff --git a/Libraries/LibWeb/WebSockets/WebSocket.cpp b/Libraries/LibWeb/WebSockets/WebSocket.cppindex 4d4418828b4..63c6f67d717 100644--- a/Libraries/LibWeb/WebSockets/WebSocket.cpp+++ b/Libraries/LibWeb/WebSockets/WebSocket.cpp@@ -196,12 +196,12 @@ ErrorOr<void> WebSocket::establish_web_socket_connection(URL::URL const& url_rec// FIXME: Fetch request as per the remaining steps of https://websockets.spec.whatwg.org/#concept-websocket-establish// AD-HOC: Until the opening handshake goes through fetch, run the blocking checks of main fetch here.request->set_policy_container(client.policy_container()->clone());- auto& realm = client.realm();- ContentSecurityPolicy::report_content_security_policy_violations_for_request(realm, request);+ auto report_violation = ContentSecurityPolicy::violation_reporter_for_request(client.realm(), request);+ ContentSecurityPolicy::report_content_security_policy_violations_for_request(request, report_violation);UpgradeInsecureRequests::upgrade_request_to_a_potentially_trustworthy_url_if_appropriate(request);if (Fetch::Infrastructure::block_bad_port(request) == Fetch::Infrastructure::RequestOrResponseBlocking::Blocked|| MixedContent::should_fetching_request_be_blocked_as_mixed_content(request) == Fetch::Infrastructure::RequestOrResponseBlocking::Blocked- || ContentSecurityPolicy::should_request_be_blocked_by_content_security_policy(realm, request) == ContentSecurityPolicy::Directives::Directive::Result::Blocked) {+ || ContentSecurityPolicy::should_request_be_blocked_by_content_security_policy(request, report_violation) == ContentSecurityPolicy::Directives::Directive::Result::Blocked) {return Error::from_string_literal("WebSocket connection was blocked");}diff --git a/Meta/Linters/check_libweb_realm_mentions.py b/Meta/Linters/check_libweb_realm_mentions.pyindex 1c734680b6a..f2bb1afb450 100755--- a/Meta/Linters/check_libweb_realm_mentions.py+++ b/Meta/Linters/check_libweb_realm_mentions.py@@ -38,7 +38,7 @@ ALLOWED_REALM_MENTIONS = {"Animations": (5, 14, "animation callbacks/events and CSS value materialization still pass spec-selected realms"),"CSS": (9, 24, "CSS Typed OM, parser-facing reification, style-value JS materialization, and setlike/maplike glue"),"Clipboard": (4, 16, "clipboard item/data callbacks still materialize JS values in caller-selected realms"),- "ContentSecurityPolicy": (4, 14, "CSP violation/report objects and callbacks still materialize JS-facing values"),+ "ContentSecurityPolicy": (4, 10, "CSP violation/report objects and callbacks still materialize JS-facing values"),"CookieStore": (2, 19, "cookie-store async callbacks and promise/value materialization still thread realms"),"Crypto": (8, 383, "WebCrypto algorithms still use realms for buffer/key/promise materialization"),"DOM": (10, 33, "DOM abort plumbing and node helpers still have callback/materialization realm use"),