Skip to content

Truly independent web browser

LibWeb: Apply Content Security Policy to style attributes

Andreas Klingebb0f953c4e4
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index abdb9936bd5..f70cc06dfa2 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -61,6 +61,7 @@
#include <LibWeb/CSS/StyleValues/StyleValueList.h>
#include <LibWeb/CSS/VisualViewport.h>
#include <LibWeb/ComputedValuesRustFFI.h>
+#include <LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h>
#include <LibWeb/DOM/AbstractElement.h>
#include <LibWeb/DOM/Attr.h>
#include <LibWeb/DOM/DOMTokenList.h>
@@ -3219,10 +3220,40 @@ double Element::current_css_zoom() const
return 1.0;
}
+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;
+}
+
+static void prefetch_inline_style_image_resources(CSS::CSSStyleProperties const&, Document&);
+
+void Element::clone_inline_style_from(Element const& source)
+{
+ // NB: A source whose style attribute still awaits its check leaves the copy to make that check once connected.
+ auto const* source_rare_data = source.element_rare_data();
+ if (!m_inline_style || !source.m_inline_style || (source_rare_data && source_rare_data->style_attribute_awaits_content_security_policy_check))
+ return;
+ m_inline_style->set_declarations_from(*source.m_inline_style);
+ if (auto* rare_data = element_rare_data())
+ rare_data->style_attribute_awaits_content_security_policy_check = false;
+}
+
void Element::inserted()
{
Base::inserted();
+ if (auto* rare_data = element_rare_data(); rare_data && rare_data->style_attribute_awaits_content_security_policy_check && is_connected()) {
+ rare_data->style_attribute_awaits_content_security_policy_check = false;
+ if (auto value = attribute(HTML::AttributeNames::style); value.has_value() && m_inline_style) {
+ if (style_attribute_is_blocked_by_content_security_policy(*this, value->utf16_view()))
+ m_inline_style->set_declarations_from_text({});
+ else
+ prefetch_inline_style_image_resources(*m_inline_style, document());
+ }
+ }
+
if (is_connected()) {
if (m_style_attribute_is_dirty)
document().mark_style_attribute_dirty(*this);
@@ -6478,7 +6509,25 @@ void Element::attribute_changed(Utf16FlyString const& local_name, Optional<Utf16
document().flush_deferred_style_change_event();
if (!m_inline_style)
m_inline_style = CSS::CSSStyleProperties::create_element_inline_style({ *this });
- m_inline_style->set_declarations_from_text(value_or_empty);
+ // https://html.spec.whatwg.org/multipage/dom.html#the-style-attribute
+ // However, if the Should element's inline behavior be blocked by Content Security Policy? algorithm returns
+ // "Blocked" when executed upon the attribute's element, "style attribute", and the attribute's value, then
+ // the style rules defined in the attribute's value must not be applied to the element.
+ // NB: The internal elements of a user agent shadow tree get their style attribute before they are inserted
+ // into it, and are styled by the user agent rather than the document. So the check is made when an
+ // element that is not connected becomes connected, and the resources its declarations use are fetched then.
+ if (value.has_value() && !is_connected()) {
+ ensure_element_rare_data().style_attribute_awaits_content_security_policy_check = true;
+ m_inline_style->set_declarations_from_text(*value);
+ return;
+ }
+ if (auto* rare_data = element_rare_data())
+ rare_data->style_attribute_awaits_content_security_policy_check = false;
+ if (!value.has_value()) {
+ m_inline_style->set_declarations_from_text({});
+ return;
+ }
+ m_inline_style->set_declarations_from_text(style_attribute_is_blocked_by_content_security_policy(*this, *value) ? Utf16View {} : *value);
prefetch_inline_style_image_resources(*m_inline_style, document());
} else if (local_name == HTML::AttributeNames::dir || local_name == HTML::AttributeNames::lang) {
bool const is_dir = local_name == HTML::AttributeNames::dir;
diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h
index 3d19c8dd268..35e7ea56e37 100644
--- a/Libraries/LibWeb/DOM/Element.h
+++ b/Libraries/LibWeb/DOM/Element.h
@@ -510,6 +510,7 @@ public:
void prepare_for_inline_style_change();
bool can_defer_inline_style_attribute_update() const;
void did_update_inline_style();
+ void clone_inline_style_from(Element const&);
GC::Ref<CSS::CSSStyleProperties> style();
GC::Ref<CSS::StylePropertyMap> attribute_style_map();
diff --git a/Libraries/LibWeb/DOM/ElementRareData.h b/Libraries/LibWeb/DOM/ElementRareData.h
index c2be3cf19d2..f39732bc2ab 100644
--- a/Libraries/LibWeb/DOM/ElementRareData.h
+++ b/Libraries/LibWeb/DOM/ElementRareData.h
@@ -93,6 +93,10 @@ struct Element::RareData
// flag is set.
bool had_duplicate_attribute_during_tokenization { false };
+ // Whether the style attribute was set while the element was not connected, so that whether Content Security
+ // Policy blocks it is decided when the element is inserted.
+ bool style_attribute_awaits_content_security_policy_check { false };
+
// https://drafts.csswg.org/css-contain/#proximity-to-the-viewport
ProximityToTheViewport proximity_to_the_viewport { ProximityToTheViewport::NotDetermined };
diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp
index e4aa0d29d8a..90b46badfaf 100644
--- a/Libraries/LibWeb/DOM/Node.cpp
+++ b/Libraries/LibWeb/DOM/Node.cpp
@@ -2260,6 +2260,10 @@ WebIDL::ExceptionOr<GC::Ref<Node>> Node::clone_single_node(Document& document, G
element_copy->append_attribute(name, Utf16String::from_utf16(value));
});
+ // INTEROP: The copy takes the inline style declarations of node instead of having Content Security Policy
+ // check the style attribute it was given again, so that styles set through CSSOM survive cloning.
+ element_copy->clone_inline_style_from(*element);
+
copy = move(element_copy);
}
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 50b5b29a92e..9a063a124a5 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -620,6 +620,13 @@ Text/input/wpt-import/content-security-policy/unsafe-eval/eval-scripts-setTimeou
; Its meta policy allows script-src *, which a file:// testharness does not match.
Text/input/wpt-import/content-security-policy/media-src/media-src-7_2.html
+; Their meta policies allow script-src 'self', style-src 'self' or img-src *, which file:// resources do not match.
+Text/input/wpt-import/content-security-policy/style-src/inline-style-allowed-while-cloning-objects.sub.html
+Text/input/wpt-import/content-security-policy/style-src/style-src-inline-style-attribute-allowed.html
+Text/input/wpt-import/content-security-policy/style-src/style-src-inline-style-attribute-blocked.html
+Text/input/wpt-import/content-security-policy/style-src/style-src-inline-style-with-csstext.html
+Text/input/wpt-import/content-security-policy/unsafe-hashes/style_attribute_allowed.html
+
; scroll-to-text-fragment tests all rely on loading/fetching other pages.
Text/input/scroll-to-text-fragment/history.html
Text/input/scroll-to-text-fragment/searchable-text-visibility.html
diff --git a/Tests/LibWeb/Text/expected/content-security-policy/style-src-attr-blocked.txt b/Tests/LibWeb/Text/expected/content-security-policy/style-src-attr-blocked.txt
new file mode 100644
index 00000000000..73dee3be362
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/content-security-policy/style-src-attr-blocked.txt
@@ -0,0 +1,14 @@
+Parsed: ""
+Set while not connected: ""
+Set while connected: ""
+Set through innerHTML: ""
+Set through CSSOM: "green" "green"
+Clone of CSSOM style: "green" "green"
+Input display: inline-block
+Violations before connecting nested: 4
+Set in a parent that is not connected: ""
+style-src-attr on parsed, sample "color: red"
+style-src-attr on detached, sample "color: red"
+style-src-attr on connected, sample "color: red"
+style-src-attr on inner, sample "color: red"
+style-src-attr on nested, sample "color: red"
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src-attr-elem/style-src-attr-allowed-src-blocked.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src-attr-elem/style-src-attr-allowed-src-blocked.txt
new file mode 100644
index 00000000000..7866908c1af
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src-attr-elem/style-src-attr-allowed-src-blocked.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Should apply the style attribute
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src-attr-elem/style-src-attr-blocked-src-allowed.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src-attr-elem/style-src-attr-blocked-src-allowed.txt
new file mode 100644
index 00000000000..173362b6f5b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src-attr-elem/style-src-attr-blocked-src-allowed.txt
@@ -0,0 +1,7 @@
+Harness status: OK
+
+Found 2 tests
+
+2 Pass
+Pass Should fire a security policy violation event
+Pass The attribute style should not be applied
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src-attr-elem/style-src-elem-allowed-attr-blocked.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src-attr-elem/style-src-elem-allowed-attr-blocked.txt
new file mode 100644
index 00000000000..b6997b075bb
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src-attr-elem/style-src-elem-allowed-attr-blocked.txt
@@ -0,0 +1,7 @@
+Harness status: OK
+
+Found 2 tests
+
+2 Pass
+Pass Should fire a security policy violation for the attribute
+Pass The attribute style should not be applied and the inline style should be applied
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src-attr-elem/style-src-elem-blocked-attr-allowed.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src-attr-elem/style-src-elem-blocked-attr-allowed.txt
new file mode 100644
index 00000000000..2b1964b0d13
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src-attr-elem/style-src-elem-blocked-attr-allowed.txt
@@ -0,0 +1,7 @@
+Harness status: OK
+
+Found 2 tests
+
+2 Pass
+Pass Should fire a security policy violation for the inline block
+Pass The inline style should not be applied and the attribute style should be applied
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src/inline-style-allowed-while-cloning-objects.sub.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src/inline-style-allowed-while-cloning-objects.sub.txt
new file mode 100644
index 00000000000..35bd128d609
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src/inline-style-allowed-while-cloning-objects.sub.txt
@@ -0,0 +1,31 @@
+Harness status: OK
+
+Found 25 tests
+
+22 Pass
+3 Fail
+Pass Test that violation report event was fired
+Pass inline-style-allowed-while-cloning-objects
+Pass inline-style-allowed-while-cloning-objects 1
+Pass inline-style-allowed-while-cloning-objects 2
+Pass inline-style-allowed-while-cloning-objects 3
+Pass inline-style-allowed-while-cloning-objects 4
+Pass inline-style-allowed-while-cloning-objects 5
+Pass inline-style-allowed-while-cloning-objects 6
+Pass inline-style-allowed-while-cloning-objects 7
+Pass inline-style-allowed-while-cloning-objects 8
+Pass inline-style-allowed-while-cloning-objects 9
+Pass inline-style-allowed-while-cloning-objects 10
+Pass inline-style-allowed-while-cloning-objects 11
+Pass inline-style-allowed-while-cloning-objects 12
+Pass inline-style-allowed-while-cloning-objects 13
+Pass inline-style-allowed-while-cloning-objects 14
+Pass inline-style-allowed-while-cloning-objects 15
+Pass inline-style-allowed-while-cloning-objects 16
+Pass inline-style-allowed-while-cloning-objects 17
+Pass inline-style-allowed-while-cloning-objects 18
+Fail inline-style-allowed-while-cloning-objects 19
+Fail inline-style-allowed-while-cloning-objects 20
+Pass inline-style-allowed-while-cloning-objects 21
+Pass inline-style-allowed-while-cloning-objects 22
+Fail non-HTML namespace
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src/style-src-inline-style-attribute-allowed.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src/style-src-inline-style-attribute-allowed.txt
new file mode 100644
index 00000000000..91127cebaa1
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src/style-src-inline-style-attribute-allowed.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Inline style attribute should apply with 'unsafe-inline'
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src/style-src-inline-style-attribute-blocked.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src/style-src-inline-style-attribute-blocked.txt
new file mode 100644
index 00000000000..e6242fbd8b2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src/style-src-inline-style-attribute-blocked.txt
@@ -0,0 +1,7 @@
+Harness status: OK
+
+Found 2 tests
+
+2 Pass
+Pass Inline style attribute should not be applied without 'unsafe-inline'
+Pass Should fire a securitypolicyviolation event
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src/style-src-inline-style-with-csstext.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src/style-src-inline-style-with-csstext.txt
new file mode 100644
index 00000000000..e19eebe1d6d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/style-src/style-src-inline-style-with-csstext.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Manipulating cssText should be allowed with 'self'
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/unsafe-hashes/style_attribute_allowed.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/unsafe-hashes/style_attribute_allowed.txt
new file mode 100644
index 00000000000..b379ea8ef54
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/unsafe-hashes/style_attribute_allowed.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test that the inline style attribute is loaded
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/unsafe-hashes/style_attribute_denied_missing_unsafe_hashes.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/unsafe-hashes/style_attribute_denied_missing_unsafe_hashes.txt
new file mode 100644
index 00000000000..8daa3e719bd
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/unsafe-hashes/style_attribute_denied_missing_unsafe_hashes.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test that the inline style attribute is blocked
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/unsafe-hashes/style_attribute_denied_wrong_hash.txt b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/unsafe-hashes/style_attribute_denied_wrong_hash.txt
new file mode 100644
index 00000000000..8daa3e719bd
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/content-security-policy/unsafe-hashes/style_attribute_denied_wrong_hash.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test that the inline style attribute is blocked
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/content-security-policy/style-src-attr-blocked.html b/Tests/LibWeb/Text/input/content-security-policy/style-src-attr-blocked.html
new file mode 100644
index 00000000000..f403226c75e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/content-security-policy/style-src-attr-blocked.html
@@ -0,0 +1,64 @@
+<!DOCTYPE html>
+<meta http-equiv="Content-Security-Policy" content="style-src-attr 'none' 'report-sample'">
+<script src="../include.js"></script>
+<script>
+ const violations = [];
+ document.addEventListener("securitypolicyviolation", event => {
+ violations.push(`${event.violatedDirective} on ${event.target.id}, sample "${event.sample}"`);
+ });
+</script>
+<div id="parsed" style="color: red"></div>
+<script>
+ asyncTest(done => {
+ println(`Parsed: "${parsed.style.color}"`);
+
+ const detached = document.createElement("div");
+ detached.id = "detached";
+ detached.setAttribute("style", "color: red");
+ document.body.append(detached);
+ println(`Set while not connected: "${detached.style.color}"`);
+
+ const connected = document.createElement("div");
+ connected.id = "connected";
+ document.body.append(connected);
+ connected.setAttribute("style", "color: red");
+ println(`Set while connected: "${connected.style.color}"`);
+ connected.removeAttribute("style");
+
+ const container = document.createElement("div");
+ document.body.append(container);
+ container.innerHTML = `<span id="inner" style="color: red"></span>`;
+ println(`Set through innerHTML: "${inner.style.color}"`);
+
+ const cssom = document.createElement("div");
+ cssom.id = "cssom";
+ cssom.style.color = "green";
+ document.body.append(cssom);
+ cssom.style.backgroundColor = "green";
+ println(`Set through CSSOM: "${cssom.style.color}" "${cssom.style.backgroundColor}"`);
+ const clone = cssom.cloneNode();
+ document.body.append(clone);
+ println(`Clone of CSSOM style: "${clone.style.color}" "${clone.style.backgroundColor}"`);
+
+ const input = document.createElement("input");
+ document.body.append(input);
+ println(`Input display: ${getComputedStyle(input).display}`);
+
+ const detachedParent = document.createElement("div");
+ const nested = document.createElement("div");
+ nested.id = "nested";
+ nested.setAttribute("style", "color: red");
+ detachedParent.append(nested);
+
+ requestAnimationFrame(() => requestAnimationFrame(() => {
+ println(`Violations before connecting nested: ${violations.length}`);
+ document.body.append(detachedParent);
+ println(`Set in a parent that is not connected: "${nested.style.color}"`);
+ requestAnimationFrame(() => requestAnimationFrame(() => {
+ for (const violation of violations)
+ println(violation);
+ done();
+ }));
+ }));
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src-attr-elem/style-src-attr-allowed-src-blocked.html b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src-attr-elem/style-src-attr-allowed-src-blocked.html
new file mode 100644
index 00000000000..5c300d45eb6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src-attr-elem/style-src-attr-allowed-src-blocked.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html>
+
+<head>
+ <meta http-equiv="Content-Security-Policy" content="style-src-attr 'unsafe-inline';
+ style-src 'none';">
+ <script src="../../resources/testharness.js"></script>
+ <script src="../../resources/testharnessreport.js"></script>
+ <script>
+ var t = async_test("Should apply the style attribute");
+ window.addEventListener('securitypolicyviolation', t.unreached_func("Should not have fired a spv event"));
+ </script>
+</head>
+
+<body style="background: green">
+ <script>
+ t.step(function() {
+ assert_true(document.body.style.length > 0);
+ t.done();
+ });
+ </script>
+</body>
+
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src-attr-elem/style-src-attr-blocked-src-allowed.html b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src-attr-elem/style-src-attr-blocked-src-allowed.html
new file mode 100644
index 00000000000..7cd968f37d2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src-attr-elem/style-src-attr-blocked-src-allowed.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<html>
+
+<head>
+ <meta http-equiv="Content-Security-Policy" content="style-src-attr 'none';
+ style-src 'unsafe-inline';">
+ <script src="../../resources/testharness.js"></script>
+ <script src="../../resources/testharnessreport.js"></script>
+ <script>
+ var t = async_test("Should fire a security policy violation event");
+ window.addEventListener('securitypolicyviolation', t.step_func_done(function(e) {
+ assert_equals(e.violatedDirective, 'style-src-attr');
+ assert_equals(e.blockedURI, 'inline');
+ }));
+ </script>
+</head>
+
+<body style="background: green">
+ <script>
+ async_test(function(test) {
+ assert_equals(document.body.style.length, 0);
+ test.done();
+ }, "The attribute style should not be applied");
+ </script>
+</body>
+
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src-attr-elem/style-src-elem-allowed-attr-blocked.html b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src-attr-elem/style-src-elem-allowed-attr-blocked.html
new file mode 100644
index 00000000000..fdde9b3283f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src-attr-elem/style-src-elem-allowed-attr-blocked.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<html>
+
+<head>
+ <meta http-equiv="Content-Security-Policy" content="style-src-elem 'unsafe-inline';
+ style-src-attr 'none';">
+ <script src="../../resources/testharness.js"></script>
+ <script src="../../resources/testharnessreport.js"></script>
+ <script>
+ var t = async_test("Should fire a security policy violation for the attribute");
+ window.addEventListener('securitypolicyviolation', t.step_func_done(function(e) {
+ assert_equals(e.violatedDirective, 'style-src-attr');
+ assert_equals(e.blockedURI, 'inline');
+ }));
+ </script>
+</head>
+
+<body style="background: green">
+ <style>
+ body {background: blue;}
+ </style>
+
+ <script>
+ async_test(function(test) {
+ assert_equals(document.body.style.length, 0);
+ assert_equals(document.styleSheets.length, 1);
+ test.done();
+ }, "The attribute style should not be applied and the inline style should be applied");
+ </script>
+
+</body>
+
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src-attr-elem/style-src-elem-blocked-attr-allowed.html b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src-attr-elem/style-src-elem-blocked-attr-allowed.html
new file mode 100644
index 00000000000..f53566c3e2d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src-attr-elem/style-src-elem-blocked-attr-allowed.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<html>
+
+<head>
+ <meta http-equiv="Content-Security-Policy" content="style-src-elem 'none';
+ script-src-attr 'unsafe-inline'">
+ <script src="../../resources/testharness.js"></script>
+ <script src="../../resources/testharnessreport.js"></script>
+ <script>
+ var t = async_test("Should fire a security policy violation for the inline block");
+ window.addEventListener('securitypolicyviolation', t.step_func_done(function(e) {
+ assert_equals(e.violatedDirective, 'style-src-elem');
+ assert_equals(e.blockedURI, 'inline');
+ }));
+ </script>
+</head>
+
+<body style="background: green">
+ <style>
+ body {background: blue;}
+ </style>
+
+ <script>
+ async_test(function(test) {
+ assert_true(document.body.style.length > 0);
+ assert_equals(document.styleSheets.length, 0);
+ test.done();
+ }, "The inline style should not be applied and the attribute style should be applied");
+ </script>
+
+</body>
+
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src/inline-style-allowed-while-cloning-objects.sub.html b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src/inline-style-allowed-while-cloning-objects.sub.html
new file mode 100644
index 00000000000..7afa6fbe232
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src/inline-style-allowed-while-cloning-objects.sub.html
@@ -0,0 +1,138 @@
+<!DOCTYPE html>
+<html>
+
+<head>
+ <!-- Programmatically converted from a WebKit Reftest, please forgive resulting idiosyncracies.-->
+ <meta http-equiv="Content-Security-Policy" content="style-src 'self'; script-src 'self' 'unsafe-inline'; connect-src 'self';">
+ <title>inline-style-allowed-while-cloning-objects</title>
+ <script src="../../resources/testharness.js"></script>
+ <script src="../../resources/testharnessreport.js"></script>
+ <script>
+ var t = async_test("Test that violation report event was fired");
+ window.addEventListener("securitypolicyviolation", t.step_func_done(function(e) {
+ assert_equals(e.violatedDirective, "style-src-attr");
+ }));
+ window.onload = function() {
+ window.nodes = document.getElementById('nodes');
+ window.node1 = document.getElementById('node1');
+ window.node1.style.background = "yellow";
+ window.node1.style.color = "red";
+ window.node2 = document.getElementById('node1').cloneNode(true);
+ window.node2.id = "node2";
+ window.node3 = document.getElementById('node3');
+ window.node3.style.background = "blue";
+ window.node3.style.color = "green";
+ window.node4 = document.getElementById('node3').cloneNode(false);
+ window.node4.id = "node4";
+ window.node4.innerHTML = "Node #4";
+ nodes.appendChild(node1);
+ nodes.appendChild(node2);
+ nodes.appendChild(node3);
+ nodes.appendChild(node4);
+ test(function() {
+ assert_equals(node1.style.background.match(/yellow/)[0], "yellow")
+ });
+ test(function() {
+ assert_equals(node2.style.background.match(/yellow/)[0], "yellow")
+ });
+ test(function() {
+ assert_equals(node3.style.background.match(/blue/)[0], "blue")
+ });
+ test(function() {
+ assert_equals(node4.style.background.match(/blue/)[0], "blue")
+ });
+ test(function() {
+ assert_equals(node1.style.color, "red")
+ });
+ test(function() {
+ assert_equals(node2.style.color, "red")
+ });
+ test(function() {
+ assert_equals(node3.style.color, "green")
+ });
+ test(function() {
+ assert_equals(node4.style.color, "green")
+ });
+ test(function() {
+ assert_equals(window.getComputedStyle(node1).background, window.getComputedStyle(node2).background)
+ });
+ test(function() {
+ assert_equals(window.getComputedStyle(node3).background, window.getComputedStyle(node4).background)
+ });
+ test(function() {
+ assert_equals(window.getComputedStyle(node1).color, window.getComputedStyle(node2).color)
+ });
+ test(function() {
+ assert_equals(window.getComputedStyle(node3).color, window.getComputedStyle(node4).color)
+ });
+ window.ops = document.getElementById('ops');
+ ops.style.color = 'red';
+ window.clonedOps = ops.cloneNode(true);
+ window.violetOps = document.getElementById('violetOps');
+ violetOps.style.background = 'rgb(238, 130, 238)';
+ document.getElementsByTagName('body')[0].appendChild(clonedOps);
+ test(function() {
+ assert_equals(ops.style.background, "")
+ });
+ test(function() {
+ assert_equals(ops.style.color, "red")
+ });
+ test(function() {
+ assert_equals(clonedOps.style.background, "")
+ });
+ test(function() {
+ assert_equals(clonedOps.style.color, "red")
+ });
+ test(function() {
+ assert_equals(violetOps.style.background.match(/rgb\(238, 130, 238\)/)[0], "rgb(238, 130, 238)")
+ });
+ test(function() {
+ assert_equals(window.getComputedStyle(clonedOps).background, window.getComputedStyle(ops).background)
+ });
+ test(function() {
+ assert_equals(window.getComputedStyle(clonedOps).color, window.getComputedStyle(ops).color)
+ });
+ test(function() {
+ assert_equals(window.getComputedStyle(ops).background, window.getComputedStyle(violetOps).background)
+ });
+ test(function() {
+ assert_equals(window.getComputedStyle(clonedOps).background, window.getComputedStyle(violetOps).background)
+ });
+ test(function() {
+ assert_equals(ops.id, "ops")
+ });
+ test(function() {
+ assert_equals(ops.id, clonedOps.id)
+ });
+ test(function() {
+ let el = document.getElementById("svg");
+ assert_equals(el.getAttribute("style"), "");
+ el.style.background = violetOps.style.background;
+ assert_not_equals(el.style.background, "");
+ let clone = el.cloneNode(true);
+ assert_equals(el.style.background, clone.style.background)
+ }, "non-HTML namespace");
+ };
+ </script>
+</head>
+
+<body>
+ <p>
+ This test ensures that styles can be set by object.cloneNode()
+ </p>
+ <div id="nodes">
+ This is a div (nodes)
+ <div id="node1"> This is a div. (node 1 or 2)</div>
+ <div id="node3"> This is a div. (node 3 or 4)</div>
+ </div>
+ <div id="ops" style="background: rgb(238, 130, 238)">
+ Yet another div.
+ </div>
+ <div id="violetOps">
+ Yet another div.
+ </div>
+ <svg id="svg" style="background: rgb(238, 130, 238)"></svg>
+ <div id="log"></div>
+</body>
+
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src/style-src-inline-style-attribute-allowed.html b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src/style-src-inline-style-attribute-allowed.html
new file mode 100644
index 00000000000..f2483b3ddd0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src/style-src-inline-style-attribute-allowed.html
@@ -0,0 +1,24 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline';">
+ <script src='../../resources/testharness.js'></script>
+ <script src='../../resources/testharnessreport.js'></script>
+
+ <script>
+ var t = async_test("Inline style attribute should apply with 'unsafe-inline'");
+ document.addEventListener("securitypolicyviolation", t.unreached_func("Should not trigger a security policy violation"));
+ onload = t.step_func_done(function(e) {
+ var contentEl = document.getElementById("content");
+ var marginLeftVal = getComputedStyle(contentEl).getPropertyValue('margin-left');
+ assert_equals(marginLeftVal, "2px");
+ });
+ </script>
+</head>
+<body>
+ <div id='log'></div>
+
+ <div id="content" style="margin-left: 2px">Lorem ipsum</div>
+
+</body>
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src/style-src-inline-style-attribute-blocked.html b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src/style-src-inline-style-attribute-blocked.html
new file mode 100644
index 00000000000..075b4ad025a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src/style-src-inline-style-attribute-blocked.html
@@ -0,0 +1,28 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline'; style-src 'self';">
+ <script src='../../resources/testharness.js'></script>
+ <script src='../../resources/testharnessreport.js'></script>
+
+ <script>
+ var t = async_test("Inline style attribute should not be applied without 'unsafe-inline'");
+ var t_spv = async_test("Should fire a securitypolicyviolation event");
+
+ document.addEventListener("securitypolicyviolation", t_spv.step_func_done(function(e) {
+ assert_equals("style-src-attr", e.violatedDirective);
+ }));
+ onload = t.step_func_done(function(e) {
+ var contentEl = document.getElementById("content");
+ var marginLeftVal = getComputedStyle(contentEl).getPropertyValue('margin-left');
+ assert_not_equals(marginLeftVal, "2px");
+ });
+ </script>
+</head>
+<body>
+ <div id='log'></div>
+
+ <div id="content" style="margin-left: 2px">Lorem ipsum</div>
+
+</body>
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src/style-src-inline-style-with-csstext.html b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src/style-src-inline-style-with-csstext.html
new file mode 100644
index 00000000000..e3ad15489bf
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/style-src/style-src-inline-style-with-csstext.html
@@ -0,0 +1,29 @@
+<!doctype html>
+<html>
+<head>
+ <meta http-equiv="Content-Security-Policy" content="style-src 'self';">
+ <script src="../../resources/testharness.js"></script>
+ <script src="../../resources/testharnessreport.js"></script>
+
+ <script>
+ var t = async_test("Manipulating cssText should be allowed with 'self'");
+ document.addEventListener("securitypolicyviolation", t.unreached_func("Should not trigger a security policy violation"));
+ </script>
+</head>
+<body>
+ <div id='log'></div>
+
+ <div id="content">Lorem ipsum</div>
+
+ <script>
+ t.step(function() {
+ var contentEl = document.getElementById("content");
+ contentEl.style.cssText = 'margin-left: 2px;';
+ var marginLeftVal = getComputedStyle(contentEl).getPropertyValue('margin-left');
+ assert_equals(marginLeftVal, "2px");
+ t.done();
+ });
+ </script>
+
+</body>
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/content-security-policy/support/pass.png b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/support/pass.png
new file mode 100644
index 00000000000..2fa1e0ac066
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/support/pass.png differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/content-security-policy/unsafe-hashes/style_attribute_allowed.html b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/unsafe-hashes/style_attribute_allowed.html
new file mode 100644
index 00000000000..73355c3eb7f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/unsafe-hashes/style_attribute_allowed.html
@@ -0,0 +1,30 @@
+<!DOCTYPE HTML>
+<html>
+
+<head>
+ <meta http-equiv="Content-Security-Policy" content="img-src *;
+ style-src 'unsafe-hashes' 'sha256-S0VSqEOmzmyOifPfat2sJ7ELOgkldAEbaXlvi5iMqjc=';">
+ <!--
+ 'sha256-S0VSqEOmzmyOifPfat2sJ7ELOgkldAEbaXlvi5iMqjc=' ==> 'background: green'
+ -->
+ <title>Event handlers should be allowed if a matching hash and 'unsafe-hashes' are present</title>
+ <script src='../../resources/testharness.js'></script>
+ <script src='../../resources/testharnessreport.js'></script>
+</head>
+
+<body>
+ <div id='log'></div>
+ <script>
+ var t1 = async_test("Test that the inline style attribute is loaded");
+
+ self.check_for_style = t1.step_func_done(function() {
+ assert_equals("green", document.getElementById('test').style.background);
+ });
+
+ window.addEventListener('securitypolicyviolation', t1.unreached_func("Should have not raised any event"));
+ </script>
+ <img src='../support/pass.png' id='test' style='background: green'
+ onload='check_for_style()'>
+</body>
+
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/content-security-policy/unsafe-hashes/style_attribute_denied_missing_unsafe_hashes.html b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/unsafe-hashes/style_attribute_denied_missing_unsafe_hashes.html
new file mode 100644
index 00000000000..716a765bf12
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/unsafe-hashes/style_attribute_denied_missing_unsafe_hashes.html
@@ -0,0 +1,29 @@
+<!DOCTYPE HTML>
+<html>
+
+<head>
+ <meta http-equiv="Content-Security-Policy" content="img-src *;
+ style-src 'sha256-S0VSqEOmzmyOifPfat2sJ7ELOgkldAEbaXlvi5iMqjc=';">
+ <!--
+ 'sha256-S0VSqEOmzmyOifPfat2sJ7ELOgkldAEbaXlvi5iMqjc=' ==> 'background: green'
+ -->
+ <title>Event handlers should be allowed if a matching hash and 'unsafe-hashes' are present</title>
+ <script src='../../resources/testharness.js'></script>
+ <script src='../../resources/testharnessreport.js'></script>
+</head>
+
+<body>
+ <div id='log'></div>
+ <script>
+ var t1 = async_test("Test that the inline style attribute is blocked");
+
+ window.addEventListener('securitypolicyviolation', t1.step_func_done(function(e) {
+ assert_equals(e.violatedDirective, 'style-src-attr');
+ assert_equals(e.blockedURI, 'inline');
+ }));
+
+ </script>
+ <img src='../support/pass.png' id='test' style='background: green'>
+</body>
+
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/content-security-policy/unsafe-hashes/style_attribute_denied_wrong_hash.html b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/unsafe-hashes/style_attribute_denied_wrong_hash.html
new file mode 100644
index 00000000000..db7c000baca
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/content-security-policy/unsafe-hashes/style_attribute_denied_wrong_hash.html
@@ -0,0 +1,29 @@
+<!DOCTYPE HTML>
+<html>
+
+<head>
+ <meta http-equiv="Content-Security-Policy" content="img-src *;
+ style-src 'unsafe-hashes' 'sha256-UI8QfroYhb0WX073XBuM+RTPntpjZfkyFLsMw5vQfd0=';">
+ <!--
+ 'sha256-S0VSqEOmzmyOifPfat2sJ7ELOgkldAEbaXlvi5iMqjc=' ==> 'background: green'
+ -->
+ <title>Event handlers should be allowed if a matching hash and 'unsafe-hashes' are present</title>
+ <script src='../../resources/testharness.js'></script>
+ <script src='../../resources/testharnessreport.js'></script>
+</head>
+
+<body>
+ <div id='log'></div>
+ <script>
+ var t1 = async_test("Test that the inline style attribute is blocked");
+
+ window.addEventListener('securitypolicyviolation', t1.step_func_done(function(e) {
+ assert_equals(e.violatedDirective, 'style-src-attr');
+ assert_equals(e.blockedURI, 'inline');
+ }));
+
+ </script>
+ <img src='../support/pass.png' id='test' style='background: green'>
+</body>
+
+</html>