Skip to content

Truly independent web browser

LibWebView: Write only page formats to the clipboard for a renderer

sideshowbarkerdbfd49127882
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 1951a702ad1..2215c3ca24a 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -2216,6 +2216,17 @@ void WebContentPage::did_insert_clipboard_item(Web::Clipboard::SystemClipboardIt
{
if (!has_transient_user_activation())
return;
+
+ // A page writes only text, HTML and PNG data. Any other format can carry more, like a list of file URLs that the
+ // next paste opens for the page. The internals object stands in for other applications, so tests write any format.
+ if (!WebContentClient::renderers_may_synthesize_input_for_testing()) {
+ item.system_clipboard_representations.remove_all_matching([](Web::Clipboard::SystemClipboardRepresentation const& representation) {
+ return !representation.data.has<ByteString>() || !representation.name.is_one_of("text/plain"sv, "text/html"sv, "image/png"sv);
+ });
+ if (item.system_clipboard_representations.is_empty())
+ return;
+ }
+
if (writing_document_id.has_value()) {
if (auto document = hosted_document_with_id(*writing_document_id))
view().did_insert_clipboard_item_written_by(document->origin(), item);
diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txt
index aa49ad05af0..e6c9b65f69c 100644
--- a/Tests/LibWebView/CMakeLists.txt
+++ b/Tests/LibWebView/CMakeLists.txt
@@ -163,6 +163,7 @@ if (NOT WIN32)
TestPictureInPictureWindowControls.cpp
TestPictureInPictureWindowLifetime.cpp
TestRendererAttentionClaims.cpp
+ TestRendererClipboardFormats.cpp
TestRendererCookieAccess.cpp
TestRendererOriginClaims.cpp
TestRendererResponseBodyClaims.cpp
diff --git a/Tests/LibWebView/TestRendererClipboardFormats.cpp b/Tests/LibWebView/TestRendererClipboardFormats.cpp
new file mode 100644
index 00000000000..41eda27521b
--- /dev/null
+++ b/Tests/LibWebView/TestRendererClipboardFormats.cpp
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "HeadlessTest.h"
+#include <AK/AnyOf.h>
+#include <AK/LexicalPath.h>
+#include <LibGfx/SystemTheme.h>
+#include <LibTest/TestCase.h>
+#include <LibWebCommon/Clipboard/SystemClipboard.h>
+#include <LibWebCommon/HTML/SelectedFile.h>
+#include <LibWebView/Application.h>
+#include <LibWebView/HeadlessWebView.h>
+#include <LibWebView/Utilities.h>
+#include <LibWebView/WebContentClient.h>
+#include <LibWebView/WebContentPage.h>
+
+namespace {
+
+// Outside test mode and without the internals object, a renderer's clipboard writes are filtered.
+class TestApplication : public Test::HeadlessApplication {
+ WEB_VIEW_APPLICATION(TestApplication)
+
+public:
+ explicit TestApplication(Optional<ByteString> ladybird_binary_path)
+ : Test::HeadlessApplication(move(ladybird_binary_path))
+ {
+ }
+
+ virtual void create_platform_options(WebView::BrowserOptions& browser_options, WebView::RequestServerOptions&, WebView::WebContentOptions& web_content_options) override
+ {
+ browser_options.headless_mode = WebView::HeadlessMode::Test;
+ browser_options.disable_sql_database = WebView::DisableSQLDatabase::Yes;
+ web_content_options.is_test_mode = WebView::IsTestMode::No;
+ web_content_options.expose_internals_object = WebView::ExposeInternalsObject::No;
+ }
+};
+
+}
+
+TEST_CASE(renderer_clipboard_formats)
+{
+ Test::HeadlessTestEnvironment environment { "TestRendererClipboardFormats"sv };
+
+ auto app = TRY_OR_FAIL(environment.create_application<TestApplication>());
+
+ auto theme_path = LexicalPath::join(WebView::s_ladybird_resource_root, "themes"sv, "Default.ini"sv);
+ auto theme = TRY_OR_FAIL(Gfx::load_system_theme(theme_path.string()));
+
+ // The message goes straight to the UI process's handler, so a stub client is enough.
+ auto view = WebView::HeadlessWebView::create(theme, { 800, 600 });
+ auto& stub = static_cast<WebContentClientStub&>(view->page().client());
+
+ auto has_format = [&](StringView name) {
+ return any_of(app->clipboard_item().system_clipboard_representations, [&](auto const& representation) {
+ return representation.name == name;
+ });
+ };
+
+ auto write = [&](Vector<Web::Clipboard::SystemClipboardRepresentation> representations) {
+ view->page().give_user_activation();
+ stub.did_insert_clipboard_item(view->page().id(), { move(representations) }, "unspecified"_string, {});
+ };
+
+ write({
+ { "text/uri-list"_string, ByteString { "file:///etc/hosts"sv } },
+ { "text/plain"_string, ByteString { "text"sv } },
+ { "text/html"_string, ByteString { "<b>html</b>"sv } },
+ { "image/png"_string, ByteString { "png"sv } },
+ });
+ EXPECT(!has_format("text/uri-list"sv));
+ EXPECT(has_format("text/plain"sv));
+ EXPECT(has_format("text/html"sv));
+ EXPECT(has_format("image/png"sv));
+
+ // A write with no format a page writes leaves the clipboard as it was.
+ write({ { "text/uri-list"_string, ByteString { "file:///etc/hosts"sv } } });
+ EXPECT(!has_format("text/uri-list"sv));
+ EXPECT(has_format("text/plain"sv));
+
+ // A file is dropped, even under a format a page writes.
+ auto file = [] {
+ return Web::HTML::SelectedFile { "file.txt"_utf16, MUST(ByteBuffer::copy("file"sv.bytes())) };
+ };
+ write({
+ { "text/plain"_string, file() },
+ { "text/html"_string, ByteString { "<b>html</b>"sv } },
+ });
+ EXPECT(!has_format("text/plain"sv));
+ EXPECT(has_format("text/html"sv));
+
+ // A write left with nothing leaves the clipboard as it was.
+ write({ { "text/plain"_string, file() } });
+ EXPECT(!has_format("text/plain"sv));
+ EXPECT(has_format("text/html"sv));
+}