Skip to content

Truly independent web browser

LibWeb: Record the script location of CSP violations

Andreas Klingb1b516b8bf49
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp
index d1e532682ba..86ba2f654db 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp
@@ -6,6 +6,7 @@
#include <AK/ByteBuffer.h>
#include <LibGC/Heap.h>
+#include <LibJS/Runtime/VM.h>
#include <LibURL/Parser.h>
#include <LibURL/URL.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
@@ -56,10 +57,36 @@ GC::Ref<Violation> Violation::create_a_violation_object_for_global_policy_and_di
// directive, and resource is null.
auto violation = GC::Heap::the().allocate<Violation>(global_scope_from_global_object(global_object), policy, move(directive));
- // FIXME: 2. If the user agent is currently executing script, and can extract a source file’s URL, line number,
- // and column number from the global, set violation’s source file, line number, and column number
- // accordingly.
+ // 2. If the user agent is currently executing script, and can extract a source file’s URL, line number,
+ // 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].
+ // 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())
+ continue;
+ auto const& source_range = *frame.source_range;
+ auto source_file = URL::Parser::basic_parse(source_range.filename().utf16_view());
+ 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;
+ break;
+ }
// 3. If global is a Window object, set violation’s referrer to global’s document's referrer.
if (auto* global = global_object.ptr()) {
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-connected-after-set.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-connected-after-set.txt
index df96375dde8..fc9161b8a38 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-connected-after-set.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-connected-after-set.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail targeting-for-inline-handler-connected-after-set
\ No newline at end of file
+1 Pass
+Pass targeting-for-inline-handler-connected-after-set
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-in-markup-disconnected-after-set.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-in-markup-disconnected-after-set.txt
index 091fd87da2b..e544d0e9e83 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-in-markup-disconnected-after-set.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-in-markup-disconnected-after-set.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail targeting-for-inline-handler-in-markup-disconnected-after-set
\ No newline at end of file
+1 Pass
+Pass targeting-for-inline-handler-in-markup-disconnected-after-set
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-in-shadow-dom.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-in-shadow-dom.txt
index 340422ecda5..3441f473a0c 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-in-shadow-dom.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-in-shadow-dom.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail targeting-for-inline-handler-in-shadow-dom
\ No newline at end of file
+1 Pass
+Pass targeting-for-inline-handler-in-shadow-dom
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-on-disconnected-element.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-on-disconnected-element.txt
index 6e3e0abd92a..3c2e9294398 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-on-disconnected-element.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-on-disconnected-element.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail targeting-for-inline-handler-on-disconnected-element
\ No newline at end of file
+1 Pass
+Pass targeting-for-inline-handler-on-disconnected-element
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-on-element-in-disconnected-document-fragment.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-on-element-in-disconnected-document-fragment.txt
index d3f226d5c0e..166141a3d3b 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-on-element-in-disconnected-document-fragment.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-on-element-in-disconnected-document-fragment.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail targeting-for-inline-handler-on-element-in-disconnected-document-fragment
\ No newline at end of file
+1 Pass
+Pass targeting-for-inline-handler-on-element-in-disconnected-document-fragment
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-on-main-frame-element-moved-to-subframe.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-on-main-frame-element-moved-to-subframe.txt
index d1b8436ae3f..b2460087ab4 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-on-main-frame-element-moved-to-subframe.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/securitypolicyviolation/targeting-for-inline-handler-on-main-frame-element-moved-to-subframe.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail targeting-for-inline-handler-on-main-frame-element-moved-to-subframe
\ No newline at end of file
+1 Pass
+Pass targeting-for-inline-handler-on-main-frame-element-moved-to-subframe
\ No newline at end of file