Truly independent web browser
LibWebView: Write only page formats to the clipboard for a renderer
sideshowbarker4410802e5a06
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cppindex 2102abf9d2b..7936ef3be81 100644--- a/Libraries/LibWebView/WebContentPage.cpp+++ b/Libraries/LibWebView/WebContentPage.cpp@@ -2210,6 +2210,18 @@ void WebContentPage::did_insert_clipboard_item(Web::Clipboard::SystemClipboardIt{if (!has_transient_user_activation())return;++ // A page writes only text, HTML, and PNG data, so drop any other format a process sends. Some carry more than data:+ // A list of file URLs, e.g., is one the next paste opens, handing the page the files it names. The internals object+ // writes files as another application would, so a process exposing it to tests writes 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.txtindex 38dd2565a29..012992d257a 100644--- a/Tests/LibWebView/CMakeLists.txt+++ b/Tests/LibWebView/CMakeLists.txt@@ -160,6 +160,7 @@ if (NOT WIN32)TestPictureInPictureWindowControls.cppTestPictureInPictureWindowLifetime.cppTestRendererAttentionClaims.cpp+ TestRendererClipboardFormats.cppTestRendererCookieAccess.cppTestRendererOriginClaims.cppTestRendererResponseBodyClaims.cppdiff --git a/Tests/LibWebView/TestRendererClipboardFormats.cpp b/Tests/LibWebView/TestRendererClipboardFormats.cppnew file mode 100644index 00000000000..02688a8f488--- /dev/null+++ b/Tests/LibWebView/TestRendererClipboardFormats.cpp@@ -0,0 +1,102 @@+/*+ * 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 {++// Neither the test harness nor the internals object stands in for other applications here, so the UI process takes from+// a renderer only the formats a page writes.+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;+ }+};++}++// The system clipboard is shared with every other application, and some of its formats are more than data: The next+// paste opens each file a list of file URLs names, and hands it to the page. So a renderer writes only text, HTML, and+// PNG data, whatever it sends.++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, which only needs the view's page to be assigned.+ 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 page writes plain data only, so a file goes, 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));++ write({ { "text/plain"_string, file() } });+ EXPECT(!has_format("text/plain"sv));+ EXPECT(has_format("text/html"sv));+}