Skip to content

Truly independent web browser

LibWeb: Reject CSSStyleSheet.replace() before reading the owner node

Andreas Klingf88e07b599cf
diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/css_style_sheet.rs b/Libraries/LibWeb/Bindings/Rust/src/css/css_style_sheet.rs
index b2621bde193..a4f712a300e 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/css/css_style_sheet.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/css/css_style_sheet.rs
@@ -229,6 +229,18 @@ impl CSSStyleSheet {
// https://drafts.csswg.org/cssom/#dom-cssstylesheet-replace
pub(crate) fn replace(&self, _: &mut CallContext, text: UsvString) -> WebIdlResult<WebIdlPromise> {
+ // 1. Let promise be a promise.
+ // 2. If the constructed flag is not set, or the disallow modification flag is set, reject promise with a
+ // NotAllowedError DOMException and return promise.
+ // NB: The bindings reject a new promise with what this returns, as a sheet that is not constructed may have no
+ // owner node to find a realm through.
+ if !self.style_sheet.flag(NativeStyleSheetFlag::Constructed) {
+ return Err(not_allowed_error("Can't call replace() on non-constructed stylesheets"));
+ }
+ if self.style_sheet.flag(NativeStyleSheetFlag::DisallowModification) {
+ return Err(not_allowed_error("Can't call replace() on non-modifiable stylesheets"));
+ }
+
let text = text.into_utf16_string().into_raw();
// SAFETY: The cell is live, the text is handed over, and LibWeb returns a live promise.
Ok(unsafe { promise_from_abi(web_css_sheet_state_replace(self.state(), text)) })
diff --git a/Libraries/LibWeb/CSS/StyleSheetState.cpp b/Libraries/LibWeb/CSS/StyleSheetState.cpp
index 7d9d110ac98..ef50e85efe8 100644
--- a/Libraries/LibWeb/CSS/StyleSheetState.cpp
+++ b/Libraries/LibWeb/CSS/StyleSheetState.cpp
@@ -310,22 +310,12 @@ void StyleSheetState::remove_rule(u32 index)
// https://drafts.csswg.org/cssom/#dom-cssstylesheet-replace
GC::Ref<WebIDL::Promise> StyleSheetState::replace(Utf16String text)
{
- // Constructed sheets remember the document whose realm created them. A stylesheet attached to a
- // <style> element has no constructor document, so use its owner node's document instead.
- auto& realm = constructed()
- ? HTML::relevant_realm(*constructor_document())
- : HTML::relevant_realm(owner_node()->document());
- auto promise = constructed()
- ? WebIDL::create_promise_for(*constructor_document())
- : WebIDL::create_promise_for(owner_node()->document());
- if (!constructed()) {
- WebIDL::reject_promise(promise, WebIDL::NotAllowedError::create("Can't call replace() on non-constructed stylesheets"_utf16));
- return promise;
- }
- if (disallow_modification()) {
- WebIDL::reject_promise(promise, WebIDL::NotAllowedError::create("Can't call replace() on non-modifiable stylesheets"_utf16));
- return promise;
- }
+ // NB: The bindings rejected a sheet that is not constructed, or whose modification is disallowed. A constructed
+ // sheet remembers the document whose realm created it.
+ VERIFY(constructed());
+ VERIFY(!disallow_modification());
+ auto& realm = HTML::relevant_realm(*constructor_document());
+ auto promise = WebIDL::create_promise_for(*constructor_document());
set_disallow_modification(true);
Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(realm.heap(), [sheet = GC::Ref { cssom_sheet() }, &realm, text = move(text), promise = GC::Root(promise)] {
HTML::TemporaryExecutionContext execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
diff --git a/Tests/LibWeb/Text/expected/css/cssstylesheet-replace-without-owner-node.txt b/Tests/LibWeb/Text/expected/css/cssstylesheet-replace-without-owner-node.txt
new file mode 100644
index 00000000000..5d3ac0cd00b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/cssstylesheet-replace-without-owner-node.txt
@@ -0,0 +1,4 @@
+ownerNode: null
+replace() rejected: NotAllowedError
+second replace() rejected: NotAllowedError
+rules: 1
diff --git a/Tests/LibWeb/Text/input/css/cssstylesheet-replace-without-owner-node.html b/Tests/LibWeb/Text/input/css/cssstylesheet-replace-without-owner-node.html
new file mode 100644
index 00000000000..8f896a04f14
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/cssstylesheet-replace-without-owner-node.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<style id="style">a { color: red; }</style>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const sheet = style.sheet;
+ style.remove();
+ println(`ownerNode: ${sheet.ownerNode}`);
+
+ try {
+ await sheet.replace("b { color: green; }");
+ println("replace() resolved");
+ } catch (error) {
+ println(`replace() rejected: ${error.name}`);
+ }
+
+ const constructedSheet = new CSSStyleSheet();
+ const firstReplacement = constructedSheet.replace("c { color: blue; }");
+ try {
+ await constructedSheet.replace("d { color: blue; }");
+ println("second replace() resolved");
+ } catch (error) {
+ println(`second replace() rejected: ${error.name}`);
+ }
+ await firstReplacement;
+ println(`rules: ${constructedSheet.cssRules.length}`);
+ done();
+ });
+</script>