Skip to content

Truly independent web browser

LibWeb: Exempt user agent content from CSP on inline styles

Andreas Kling94ce90549896
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp
index 7d4e4a6cbd3..c15c7ff571d 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp
@@ -16,6 +16,7 @@
#include <LibWeb/ContentSecurityPolicy/Violation.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
+#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/Fetch/Fetching/ClientContextSnapshots.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/ResponseView.h>
@@ -24,6 +25,7 @@
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Page/Page.h>
+#include <LibWeb/SVG/SVGUseElement.h>
#include <LibWeb/TrustedTypes/RequireTrustedTypesForDirective.h>
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
#include <LibWeb/WebAssembly/WebAssembly.h>
@@ -309,6 +311,14 @@ Directives::Directive::Result should_navigation_response_to_navigation_request_o
return result;
}
+// NB: The content of a user agent shadow tree belongs to the user agent, not to the document and its policies. The
+// exception is the shadow tree of a use element, which holds clones of the document's own content.
+static bool is_in_shadow_tree_with_user_agent_content(DOM::Element& element)
+{
+ auto shadow_root = element.containing_shadow_root();
+ return shadow_root && shadow_root->is_user_agent_internal() && !is<SVG::SVGUseElement>(shadow_root->host());
+}
+
// https://w3c.github.io/webappsec-csp/#should-block-inline
Directives::Directive::Result should_elements_inline_type_behavior_be_blocked_by_content_security_policy(GC::Ref<DOM::Element> element, Directives::Directive::InlineType type, Utf16View source)
{
@@ -318,6 +328,10 @@ Directives::Directive::Result should_elements_inline_type_behavior_be_blocked_by
// 1. Assert: element is not null.
// NOTE: Already done by only accepting a GC::Ref.
+ // AD-HOC: The spec has no notion of the user agent's own content.
+ if (is_in_shadow_tree_with_user_agent_content(*element))
+ return Directives::Directive::Result::Allowed;
+
// 2. Let result be "Allowed".
auto result = Directives::Directive::Result::Allowed;
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index c3e1c6c03c1..634cafb7f09 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -3017,8 +3017,6 @@ double Element::current_css_zoom() const
static bool style_attribute_is_blocked_by_content_security_policy(Element& element, Utf16View value)
{
- if (auto shadow_root = element.containing_shadow_root(); shadow_root && shadow_root->is_user_agent_internal())
- return false;
return ContentSecurityPolicy::should_elements_inline_type_behavior_be_blocked_by_content_security_policy(element, ContentSecurityPolicy::Directives::Directive::InlineType::StyleAttribute, value) == ContentSecurityPolicy::Directives::Directive::Result::Blocked;
}
diff --git a/Tests/LibWeb/Ref/expected/csp-blocks-styles-cloned-into-use-element-ref.html b/Tests/LibWeb/Ref/expected/csp-blocks-styles-cloned-into-use-element-ref.html
new file mode 100644
index 00000000000..038b815af07
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/csp-blocks-styles-cloned-into-use-element-ref.html
@@ -0,0 +1,5 @@
+<!DOCTYPE html>
+<svg width="300" height="100">
+ <rect width="100" height="100" />
+ <rect x="120" width="100" height="100" />
+</svg>
diff --git a/Tests/LibWeb/Ref/input/csp-blocks-styles-cloned-into-use-element.html b/Tests/LibWeb/Ref/input/csp-blocks-styles-cloned-into-use-element.html
new file mode 100644
index 00000000000..faf24ec4695
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/csp-blocks-styles-cloned-into-use-element.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/csp-blocks-styles-cloned-into-use-element-ref.html">
+<svg width="300" height="100">
+ <defs>
+ <g id="style-element">
+ <style>.green { fill: green; }</style>
+ <rect class="green" width="100" height="100" />
+ </g>
+ <g id="style-attribute">
+ <rect style="fill: green;" x="120" width="100" height="100" />
+ </g>
+ </defs>
+ <use href="#style-element" />
+ <use href="#style-attribute" />
+</svg>
diff --git a/Tests/LibWeb/Ref/input/csp-blocks-styles-cloned-into-use-element.html.headers b/Tests/LibWeb/Ref/input/csp-blocks-styles-cloned-into-use-element.html.headers
new file mode 100644
index 00000000000..798ab1c6a88
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/csp-blocks-styles-cloned-into-use-element.html.headers
@@ -0,0 +1,2 @@
+Content-Type: text/html
+Content-Security-Policy: style-src 'none'
diff --git a/Tests/LibWeb/Text/expected/ContentSecurityPolicy/style-element-in-user-agent-shadow-tree.txt b/Tests/LibWeb/Text/expected/ContentSecurityPolicy/style-element-in-user-agent-shadow-tree.txt
index ae00c09b95a..80178c178b4 100644
--- a/Tests/LibWeb/Text/expected/ContentSecurityPolicy/style-element-in-user-agent-shadow-tree.txt
+++ b/Tests/LibWeb/Text/expected/ContentSecurityPolicy/style-element-in-user-agent-shadow-tree.txt
@@ -1,3 +1 @@
-Blocked style in VIDEO
-Blocked style in DETAILS
Blocked style in STYLE