Truly independent web browser
LibWeb: Report connect-src violations of worker imports in the owner
Luke Wildeba795a7b96a6
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cppindex 74e87beaf33..d639f5aeb04 100644--- a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp+++ b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp@@ -68,6 +68,31 @@ ViolationReporter violation_reporter_for_request(JS::Realm& realm, NonnullRefPtr};}+static bool policy_governs_directive(Policy const& policy, Utf16FlyString const& directive)+{+ auto governing_directives = Directives::get_fetch_directive_fallback_list(directive);+ if (governing_directives.is_empty())+ governing_directives.append(directive);+ return any_of(governing_directives, [&](auto const& governing_directive) {+ return policy.contains_directive_with_name(governing_directive);+ });+}++// AD-HOC: A fetch in another process found the violation of a policy of a client this process hosts. The report is+// that process's claim, so it is reported only for a policy of the client governing its directive.+void report_a_violation_found_in_another_process(HTML::EnvironmentSettingsObject& settings, u64 policy_index, Utf16FlyString const& directive, URL::URL resource)+{+ // NB: The other process's copy of the client's CSP list lists its policies in the same order, and the list only+ // grows while the client keeps its document, so while it does, policy_index names the violated policy.+ auto const& policies = settings.policy_container()->csp_list.policies();+ if (policy_index >= policies.size() || !policy_governs_directive(policies[policy_index], directive))+ return;++ auto violation = Violation::create_a_violation_object_for_global_policy_and_directive(settings.global_object(), policies[policy_index], directive.view().to_utf8_but_should_be_ported_to_utf16());+ violation->set_resource(move(resource));+ violation->report_a_violation(settings.realm());+}+// https://w3c.github.io/webappsec-csp/#should-block-navigation-requestDirectives::Directive::Result should_navigation_request_of_type_be_blocked_by_content_security_policy(NonnullRefPtr<Fetch::Infrastructure::Request> navigation_request, Directives::Directive::NavigationType navigation_type){diff --git a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.hindex ec7d1a3280c..e2c0331754a 100644--- a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h+++ b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h@@ -15,6 +15,7 @@namespace Web::ContentSecurityPolicy {[[nodiscard]] ViolationReporter violation_reporter_for_request(JS::Realm&, NonnullRefPtr<Fetch::Infrastructure::Request>);+void report_a_violation_found_in_another_process(HTML::EnvironmentSettingsObject&, u64 policy_index, Utf16FlyString const& directive, URL::URL resource);Directives::Directive::Result should_navigation_request_of_type_be_blocked_by_content_security_policy(NonnullRefPtr<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/HTML/WorkerAgentParent.cpp b/Libraries/LibWeb/HTML/WorkerAgentParent.cppindex 30ca74c2b25..e7d31bc115c 100644--- a/Libraries/LibWeb/HTML/WorkerAgentParent.cpp+++ b/Libraries/LibWeb/HTML/WorkerAgentParent.cpp@@ -9,9 +9,8 @@#include <LibGC/Heap.h>#include <LibWeb/Bindings/PrincipalHostDefined.h>#include <LibWeb/Bindings/WrapperWorld.h>+#include <LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h>#include <LibWeb/ContentSecurityPolicy/Directives/Names.h>-#include <LibWeb/ContentSecurityPolicy/PolicyList.h>-#include <LibWeb/ContentSecurityPolicy/Violation.h>#include <LibWeb/DOM/Document.h>#include <LibWeb/DOM/Event.h>#include <LibWeb/DOM/EventTarget.h>@@ -236,17 +235,12 @@ void WorkerAgentParent::dispatch_worker_exception(Utf16String message, Utf16Stri// worker's process. The violations of its policies are reported here, where outside settings' global object is.void WorkerAgentParent::report_content_security_policy_violation(u64 policy_index, String effective_directive, URL::URL resource){- // NB: The worker's snapshot of outside settings' CSP list lists its policies in the same order, and a CSP list- // only grows, so policy_index names the policy that was violated. The worker's process is less trusted than- // this one, so we only report violations of policies in outside settings' CSP list, and of worker-src, which- // is the effective directive of every worker script request.- auto const& policies = m_outside_settings->policy_container()->csp_list.policies();- if (policy_index >= policies.size() || ContentSecurityPolicy::Directives::Names::WorkerSrc != effective_directive.bytes_as_string_view())+ // NB: A worker's script requests can violate only worker-src, and a module worker's json and text imports+ // connect-src.+ auto directive = Utf16FlyString::from_utf8(effective_directive);+ if (directive != ContentSecurityPolicy::Directives::Names::WorkerSrc && directive != ContentSecurityPolicy::Directives::Names::ConnectSrc)return;-- auto violation = ContentSecurityPolicy::Violation::create_a_violation_object_for_global_policy_and_directive(&m_outside_settings->global_object(), policies[policy_index], move(effective_directive));- violation->set_resource(move(resource));- violation->report_a_violation(m_outside_settings->realm());+ ContentSecurityPolicy::report_a_violation_found_in_another_process(*m_outside_settings, policy_index, directive, move(resource));}WorkerAgentOwnerToken WorkerAgentParent::next_owner_token()diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.iniindex 3720a4a3668..e18c78c6a2b 100644--- a/Tests/LibWeb/TestConfig.ini+++ b/Tests/LibWeb/TestConfig.ini@@ -564,6 +564,7 @@ Text/input/Worker/Worker-importScripts.htmlText/input/Worker/Worker-location.htmlText/input/Worker/Worker-module.htmlText/input/Worker/Worker-performance.html+Text/input/Worker/Worker-src-violation-is-reported-in-the-owner.htmlText/input/wpt-import/workers/WorkerGlobalScope-close.htmlText/input/wpt-import/workers/constructors/Worker/AbstractWorker.onerror.htmlText/input/wpt-import/workers/interfaces/WorkerGlobalScope/close/incoming-message.htmldiff --git a/Tests/LibWeb/Text/expected/Worker/Worker-module-import-violation-is-reported-in-the-owner.txt b/Tests/LibWeb/Text/expected/Worker/Worker-module-import-violation-is-reported-in-the-owner.txtnew file mode 100644index 00000000000..2f1c616aedd--- /dev/null+++ b/Tests/LibWeb/Text/expected/Worker/Worker-module-import-violation-is-reported-in-the-owner.txt@@ -0,0 +1,5 @@+effective directive: connect-src+disposition: enforce+blocked URL matches: true+document URL matches: true+worker failed to loaddiff --git a/Tests/LibWeb/Text/expected/Worker/Worker-src-violation-is-reported-in-the-owner.txt b/Tests/LibWeb/Text/expected/Worker/Worker-src-violation-is-reported-in-the-owner.txtnew file mode 100644index 00000000000..9f444bb0042--- /dev/null+++ b/Tests/LibWeb/Text/expected/Worker/Worker-src-violation-is-reported-in-the-owner.txt@@ -0,0 +1,5 @@+effective directive: worker-src+disposition: enforce+blocked URL matches: true+document URL matches: true+worker failed to loaddiff --git a/Tests/LibWeb/Text/input/Worker/Worker-module-import-violation-is-reported-in-the-owner.html b/Tests/LibWeb/Text/input/Worker/Worker-module-import-violation-is-reported-in-the-owner.htmlnew file mode 100644index 00000000000..3d6a0cb45ff--- /dev/null+++ b/Tests/LibWeb/Text/input/Worker/Worker-module-import-violation-is-reported-in-the-owner.html@@ -0,0 +1,53 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // A module worker's imports are fetched by the worker's process, whose client is the worker's owner. The owner's+ // connect-src blocks the worker's json import, and the owner sees the violation as a securitypolicyviolation event.+ promiseTest(async () => {+ const httpServer = httpTestServer();+ const runId = crypto.randomUUID();++ const importUrl = await httpServer.createEcho("GET", `/module-import-violation-data-${runId}.json`, {+ status: 200,+ headers: { "Content-Type": "application/json" },+ body: `{ "imported": true }`,+ });+ const workerUrl = await httpServer.createEcho("GET", `/module-import-violation-worker-${runId}.mjs`, {+ status: 200,+ headers: { "Content-Type": "text/javascript" },+ body: `import data from ${JSON.stringify(importUrl)} with { type: "json" }; postMessage(data);`,+ });+ const ownerUrl = await httpServer.createEcho("GET", `/module-import-violation-owner-${runId}.html`, {+ status: 200,+ headers: { "Content-Type": "text/html", "Content-Security-Policy": "connect-src 'none'" },+ body: `<!DOCTYPE html><script>+ document.addEventListener("securitypolicyviolation", event => parent.postMessage([+ "effective directive: " + event.effectiveDirective,+ "disposition: " + event.disposition,+ "blocked URL matches: " + (event.blockedURI === ${JSON.stringify(importUrl)}),+ "document URL matches: " + (event.documentURI === location.href),+ ].join("\\n"), "*"));+ const worker = new Worker(${JSON.stringify(workerUrl)}, { type: "module" });+ worker.onerror = () => parent.postMessage("worker failed to load", "*");+ <\/script>`,+ });++ const messages = [];+ const received = new Promise(resolve => {+ addEventListener("message", event => {+ messages.push(event.data);+ if (messages.length === 2)+ resolve();+ });+ });+ const iframe = document.createElement("iframe");+ iframe.src = ownerUrl;+ document.body.appendChild(iframe);+ await received;++ // The two messages print in one order, whichever arrives first.+ messages.sort((a, b) => (a === "worker failed to load") - (b === "worker failed to load"));+ for (const message of messages)+ println(message);+ });+</script>diff --git a/Tests/LibWeb/Text/input/Worker/Worker-src-violation-is-reported-in-the-owner.html b/Tests/LibWeb/Text/input/Worker/Worker-src-violation-is-reported-in-the-owner.htmlnew file mode 100644index 00000000000..fedf7b87f11--- /dev/null+++ b/Tests/LibWeb/Text/input/Worker/Worker-src-violation-is-reported-in-the-owner.html@@ -0,0 +1,22 @@+<!DOCTYPE html>+<meta http-equiv="Content-Security-Policy" content="worker-src 'none'">+<script src="../include.js"></script>+<script>+ // The worker's script is fetched by the worker's process, whose client is this document. The document's+ // worker-src blocks the fetch, and the document sees the violation as a securitypolicyviolation event.+ promiseTest(async () => {+ const violation = new Promise(resolve => document.addEventListener("securitypolicyviolation", resolve, { once: true }));++ const worker = new Worker("worker.js");+ const failed = new Promise(resolve => (worker.onerror = resolve));++ const event = await violation;+ await failed;++ println(`effective directive: ${event.effectiveDirective}`);+ println(`disposition: ${event.disposition}`);+ println(`blocked URL matches: ${event.blockedURI === new URL("worker.js", location.href).href}`);+ println(`document URL matches: ${event.documentURI === location.href}`);+ println("worker failed to load");+ });+</script>