Skip to content

Truly independent web browser

LibWeb: Stop writing to the clipboard after a write() is rejected

Shannon Boothec99b4ee2fde
diff --git a/Libraries/LibWeb/Bindings/Rust/src/clipboard/clipboard.rs b/Libraries/LibWeb/Bindings/Rust/src/clipboard/clipboard.rs
index 40f8da4b654..046788eb2b9 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/clipboard/clipboard.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/clipboard/clipboard.rs
@@ -998,10 +998,11 @@ fn write_task(vm: &Vm, data: Gc<EmbedderCell<WriteData>>) {
// https://github.com/w3c/clipboard-apis/issues/237
spin_until_promise_is_settled(reaction);
- if reaction
+ // NB: The reaction promise is fulfilled even when the steps above abort, so check representationDataPromise.
+ if representation_data_promise
.promise()
.downcast::<Promise>()
- .expect("a reaction is a Promise")
+ .expect("a representation's data is a Promise")
.state()
== PromiseState::Rejected
{
diff --git a/Tests/LibWeb/Text/expected/Clipboard/clipboard-write-rejected-representation.txt b/Tests/LibWeb/Text/expected/Clipboard/clipboard-write-rejected-representation.txt
new file mode 100644
index 00000000000..8fe1410f3fb
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Clipboard/clipboard-write-rejected-representation.txt
@@ -0,0 +1,2 @@
+write() rejected with NotAllowedError: Writing to the clipboard failed: [object Error]
+Clipboard text: before
diff --git a/Tests/LibWeb/Text/input/Clipboard/clipboard-write-rejected-representation.html b/Tests/LibWeb/Text/input/Clipboard/clipboard-write-rejected-representation.html
new file mode 100644
index 00000000000..5d5f92fac1c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Clipboard/clipboard-write-rejected-representation.html
@@ -0,0 +1,24 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ internals.dispatchUserActivatedEvent(document.body, new Event("mousedown"));
+ await navigator.clipboard.writeText("before");
+
+ internals.dispatchUserActivatedEvent(document.body, new Event("mousedown"));
+ const item = new ClipboardItem({
+ "text/plain": "after",
+ "text/html": Promise.reject(new Error("no data")),
+ });
+ try {
+ await navigator.clipboard.write([item]);
+ println("FAIL: write() resolved");
+ } catch (error) {
+ println(`write() rejected with ${error.name}: ${error.message}`);
+ }
+
+ await new Promise(resolve => setTimeout(resolve, 0));
+ const text = await readClipboardFromPaste(document.body, clipboard => clipboard.readText());
+ println(`Clipboard text: ${text}`);
+ });
+</script>