Skip to content

Truly independent web browser

LibWeb: Keep the full URL of cross-origin scripts in CSP violations

Tim Ledbetter0c1b621541ed
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp
index 353544e46ce..67a3637910b 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp
@@ -61,6 +61,8 @@ GC::Ref<Violation> Violation::create_a_violation_object_for_global_policy_and_di
// and column number from the global, set violation’s source file, line number, and column number
// accordingly.
// SPEC ISSUE 1: Is this kind of thing specified anywhere? I didn’t see anything that looked useful in [ECMA262].
+ // NOTE: User agents need to ensure that the source file is the URL requested by the page, pre-redirects. If that’s
+ // not possible, user agents need to strip the URL down to an origin to avoid unintentional leakage.
// NB: We use the innermost stack frame that has a source location.
for (auto const& frame : JS::VM::the().stack_trace()) {
if (!frame.source_range.has_value())
@@ -70,18 +72,6 @@ GC::Ref<Violation> Violation::create_a_violation_object_for_global_policy_and_di
if (!source_file.has_value())
break;
- // NB: The URL of a classic script is its response URL, which can reveal where a cross-origin request was
- // redirected to. Per the note in the spec, we strip such URLs down to their origin. Redirects only lead
- // to HTTP(S) URLs, so URLs with an opaque origin, such as file: URLs, are kept as they are.
- if (global_object) {
- auto source_origin = source_file->origin();
- if (!source_origin.is_opaque() && !source_origin.is_same_origin(HTML::relevant_settings_object(*global_object).origin())) {
- source_file = URL::Parser::basic_parse(source_origin.serialize());
- if (!source_file.has_value())
- break;
- }
- }
-
violation->m_source_file = source_file.release_value();
violation->m_line_number = source_range.start.line;
violation->m_column_number = source_range.start.column;
diff --git a/Tests/LibWeb/Text/expected/ContentSecurityPolicy/violation-source-file-of-redirected-script.txt b/Tests/LibWeb/Text/expected/ContentSecurityPolicy/violation-source-file-of-redirected-script.txt
new file mode 100644
index 00000000000..1a9b46a5b22
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/ContentSecurityPolicy/violation-source-file-of-redirected-script.txt
@@ -0,0 +1 @@
+sourceFile: the requested URL
diff --git a/Tests/LibWeb/Text/input/ContentSecurityPolicy/violation-source-file-of-redirected-script.html b/Tests/LibWeb/Text/input/ContentSecurityPolicy/violation-source-file-of-redirected-script.html
new file mode 100644
index 00000000000..ef1c20b6f97
--- /dev/null
+++ b/Tests/LibWeb/Text/input/ContentSecurityPolicy/violation-source-file-of-redirected-script.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<meta http-equiv="Content-Security-Policy" content="script-src 'unsafe-inline' file: http:">
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const server = httpTestServer();
+
+ const target = new URL(await server.createEcho("GET", "/csp-redirect-target", {
+ status: 200,
+ headers: { "Content-Type": "text/javascript" },
+ body: `try { eval(""); } catch {}`,
+ }));
+ target.hostname = "127.0.0.1";
+ const start = await server.createEcho("GET", "/csp-redirect-start", {
+ status: 302,
+ headers: { "Location": target.href },
+ });
+
+ document.addEventListener("securitypolicyviolation", event => {
+ println(`sourceFile: ${event.sourceFile === start ? "the requested URL" : event.sourceFile}`);
+ done();
+ });
+
+ const script = document.createElement("script");
+ script.src = start;
+ document.body.appendChild(script);
+ });
+</script>