Skip to content

Truly independent web browser

LibWeb: Only let a focused document read the clipboard

Shannon Booth457b8063974e
diff --git a/Libraries/LibWeb/Clipboard/Clipboard.cpp b/Libraries/LibWeb/Clipboard/Clipboard.cpp
index 6757d0664e8..b97c5edba4c 100644
--- a/Libraries/LibWeb/Clipboard/Clipboard.cpp
+++ b/Libraries/LibWeb/Clipboard/Clipboard.cpp
@@ -168,6 +168,11 @@ static ClipboardReadPermission check_clipboard_read_permission(JS::Realm& realm,
auto& window = HTML::relevant_window(realm.global_object());
+ // NB: "This API must not be available unless the script is executing in the context of a document that has focus."
+ // https://w3c.github.io/clipboard-apis/#privacy-async
+ if (!window.associated_document().has_focus())
+ return ClipboardReadPermission::Denied;
+
// 1. Let hasGesture be true if the relevant global object of this has transient activation, false otherwise.
auto has_gesture = window.has_transient_activation();
diff --git a/Tests/LibWeb/Text/expected/Clipboard/clipboard-read-requires-paste.txt b/Tests/LibWeb/Text/expected/Clipboard/clipboard-read-requires-paste.txt
index 6c4ac2a5de0..21ddfc36500 100644
--- a/Tests/LibWeb/Text/expected/Clipboard/clipboard-read-requires-paste.txt
+++ b/Tests/LibWeb/Text/expected/Clipboard/clipboard-read-requires-paste.txt
@@ -9,5 +9,6 @@ read() in a browser paste: resolved with types []
The page's own data on the clipboard:
readText() after a click: resolved with "own text"
read() after a click: resolved with types [["text/plain"]]
+readText() after a click without focus: rejected with NotAllowedError
Another application's data replacing the page's:
read() after a click: rejected with NotAllowedError
diff --git a/Tests/LibWeb/Text/input/Clipboard/clipboard-read-requires-paste.html b/Tests/LibWeb/Text/input/Clipboard/clipboard-read-requires-paste.html
index e58d2e28b32..fecda34b70c 100644
--- a/Tests/LibWeb/Text/input/Clipboard/clipboard-read-requires-paste.html
+++ b/Tests/LibWeb/Text/input/Clipboard/clipboard-read-requires-paste.html
@@ -55,6 +55,10 @@
println(`readText() after a click: ${await readAfterClick(readText)}`);
println(`read() after a click: ${await readAfterClick(read)}`);
+ internals.setPageFocus(false);
+ println(`readText() after a click without focus: ${await readAfterClick(readText)}`);
+ internals.setPageFocus(true);
+
println("Another application's data replacing the page's:");
internals.setClipboardFile("other.txt", "text/plain", "other application");
println(`read() after a click: ${await readAfterClick(read)}`);