Skip to content

Truly independent web browser

LibWeb: Check the style attribute of an element imported from elsewhere

Andreas Kling300719142e43
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index efe667ab2a0..d9f1a81bebf 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -3292,9 +3292,10 @@ static void prefetch_inline_style_image_resources(CSS::CSSStyleProperties const&
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.
+ // NB: A source whose style attribute still awaits its check leaves the copy to make that check once connected. So
+ // does a source in another document, whose declarations were checked against another policy.
auto const* source_declarations = source.inline_style_declarations();
- if ((!m_inline_style && !m_inline_declarations) || !source_declarations || source.m_style_attribute_awaits_content_security_policy_check)
+ if ((!m_inline_style && !m_inline_declarations) || !source_declarations || source.m_style_attribute_awaits_content_security_policy_check || &source.document() != &document())
return;
if (m_inline_style)
m_inline_style->set_declarations_from(*source.inline_style());
diff --git a/Tests/LibWeb/Text/expected/content-security-policy/style-src-attr-blocked-import-node.txt b/Tests/LibWeb/Text/expected/content-security-policy/style-src-attr-blocked-import-node.txt
new file mode 100644
index 00000000000..22283693b31
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/content-security-policy/style-src-attr-blocked-import-node.txt
@@ -0,0 +1,5 @@
+Source: "red" rgb(255, 0, 0)
+Set while not connected: "" rgb(0, 0, 0)
+Imported: "" rgb(0, 0, 0)
+Imported CSSOM style: "" rgb(0, 0, 0)
+Clone of CSSOM style: "green" rgb(0, 128, 0)
diff --git a/Tests/LibWeb/Text/input/content-security-policy/style-src-attr-blocked-import-node.html b/Tests/LibWeb/Text/input/content-security-policy/style-src-attr-blocked-import-node.html
new file mode 100644
index 00000000000..6dfb1a962af
--- /dev/null
+++ b/Tests/LibWeb/Text/input/content-security-policy/style-src-attr-blocked-import-node.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<div id="source" style="color: red"></div>
+<div id="cssom"></div>
+<script>
+ asyncTest(done => {
+ cssom.style.color = "red";
+ const iframe = document.createElement("iframe");
+ iframe.srcdoc = `<!DOCTYPE html><meta http-equiv="Content-Security-Policy" content="style-src-attr 'none'"><body></body>`;
+ iframe.onload = () => {
+ const destination = iframe.contentDocument;
+ const destinationWindow = iframe.contentWindow;
+ const color = element => destinationWindow.getComputedStyle(element).color;
+
+ println(`Source: "${source.style.color}" ${getComputedStyle(source).color}`);
+
+ const detached = destination.createElement("div");
+ detached.setAttribute("style", "color: red");
+ destination.body.append(detached);
+ println(`Set while not connected: "${detached.style.color}" ${color(detached)}`);
+
+ const imported = destination.importNode(source);
+ destination.body.append(imported);
+ println(`Imported: "${imported.style.color}" ${color(imported)}`);
+
+ const importedCSSOM = destination.importNode(cssom);
+ destination.body.append(importedCSSOM);
+ println(`Imported CSSOM style: "${importedCSSOM.style.color}" ${color(importedCSSOM)}`);
+
+ const local = destination.createElement("div");
+ destination.body.append(local);
+ local.style.color = "green";
+ const localClone = local.cloneNode();
+ destination.body.append(localClone);
+ println(`Clone of CSSOM style: "${localClone.style.color}" ${color(localClone)}`);
+ done();
+ };
+ document.body.append(iframe);
+ });
+</script>