Skip to content

Truly independent web browser

LibWebView: Don’t offer to view the source of a page’s source

sideshowbarker8a2fb6725f24
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 2da938a959d..4ae323c5a10 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -3269,6 +3269,7 @@ void Application::update_actions_for_active_web_view()
update_editing_history_actions();
update_report_website_issue_action();
update_force_dark_action();
+ update_view_source_action();
}
void Application::bookmarks_changed(Badge<ApplicationBookmarkStoreObserver>)
@@ -3739,6 +3740,12 @@ void Application::update_report_website_issue_action()
m_report_website_issue_action->set_enabled(view.has_value() && view->can_report_website_issue());
}
+void Application::update_view_source_action()
+{
+ auto view = active_web_view();
+ m_view_source_action->set_enabled(!view.has_value() || view->can_view_source());
+}
+
void Application::update_editing_history_actions()
{
auto view = active_web_view();
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index 878a60d05d6..a9ae3343563 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -416,6 +416,7 @@ public:
Menu& inspect_menu() { return *m_inspect_menu; }
Action& view_source_action() { return *m_view_source_action; }
+ void update_view_source_action();
Action& report_website_issue_action() { return *m_report_website_issue_action; }
void update_report_website_issue_action();
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index 8f25a30435c..fa098dfbe0b 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -30,6 +30,7 @@
#include <LibWebCommon/WebView/SiteIsolation.h>
#include <LibWebView/Application.h>
#include <LibWebView/BookmarkStore.h>
+#include <LibWebView/CanonicalSessionHistoryEntry.h>
#include <LibWebView/CrashReportReview.h>
#include <LibWebView/ErrorHTML.h>
#include <LibWebView/FaviconStore.h>
@@ -38,6 +39,7 @@
#include <LibWebView/HistoryStore.h>
#include <LibWebView/Menu.h>
#include <LibWebView/PermissionManager.h>
+#include <LibWebView/SessionHistory.h>
#include <LibWebView/TabPerformanceMonitor.h>
#include <LibWebView/URL.h>
#include <LibWebView/UserAgent.h>
@@ -202,9 +204,22 @@ URL::URL ViewImplementation::url() const
void ViewImplementation::did_update_url()
{
+ // The source arrives as an about:srcdoc document, after the about:blank of a new tab; any other URL means the view
+ // moved on before the source arrived.
+ if (m_page_source_pending) {
+ auto url = this->url();
+ if (url.equals(URL::about_srcdoc(), URL::ExcludeFragment::Yes)) {
+ m_page_source_document_state = traversable().session_history().current_entry().document_state->id;
+ m_page_source_pending = false;
+ } else if (url != URL::about_blank()) {
+ m_page_source_pending = false;
+ }
+ }
+
update_bookmark_action();
update_report_website_issue_action();
Application::the().update_force_dark_action();
+ update_view_source_action();
if (auto page = traversable().display_page(); page && page->routed_connection())
page->routed_connection()->update_audio_capture_permission();
update_displayed_url();
@@ -1844,9 +1859,27 @@ void ViewImplementation::find_in_page_end()
void ViewImplementation::get_source()
{
+ if (!can_view_source())
+ return;
page().async_get_source();
}
+bool ViewImplementation::can_view_source() const
+{
+ if (m_page_source_pending)
+ return false;
+ if (!m_page_source_document_state.has_value())
+ return true;
+ return traversable().session_history().current_entry().document_state->id != *m_page_source_document_state;
+}
+
+void ViewImplementation::did_start_showing_page_source()
+{
+ m_page_source_pending = true;
+ m_page_source_document_state.clear();
+ update_view_source_action();
+}
+
// Only a website the page is showing can be reported, so not a browser page or a page that crashed.
bool ViewImplementation::can_report_website_issue() const
{
@@ -1865,6 +1898,11 @@ void ViewImplementation::update_report_website_issue_action()
Application::the().update_report_website_issue_action();
}
+void ViewImplementation::update_view_source_action()
+{
+ Application::the().update_view_source_action();
+}
+
void ViewImplementation::inspect_dom_tree()
{
page().async_inspect_dom_tree();
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index 13fe4479e32..3ec918913d0 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -46,6 +46,7 @@
#include <LibWebCommon/Forward.h>
#include <LibWebCommon/HTML/ActivateTab.h>
#include <LibWebCommon/HTML/ColorPickerUpdateState.h>
+#include <LibWebCommon/HTML/CrossProcessId.h>
#include <LibWebCommon/HTML/FileFilter.h>
#include <LibWebCommon/HTML/HistoryOperation.h>
#include <LibWebCommon/HTML/MuteState.h>
@@ -363,6 +364,8 @@ public:
void find_in_page_end();
void get_source();
+ bool can_view_source() const;
+ void did_start_showing_page_source();
bool can_report_website_issue() const;
void report_website_issue();
@@ -832,6 +835,7 @@ protected:
void add_text_edit_actions(Menu&, bool is_editable, bool has_selected_text);
void add_look_up_action(Menu&, Optional<DictionaryLookup> const&, Gfx::IntPoint content_position);
void update_report_website_issue_action();
+ void update_view_source_action();
void handle_external_url(URL::URL, URL::Origin, bool has_transient_activation, Function<void()> on_handler_unavailable = {});
void handle_external_url_from_user_input(URL::URL const&, Function<void()> on_handler_unavailable = {});
void complete_external_url_request();
@@ -1017,6 +1021,12 @@ protected:
Optional<WebDriverNavigationObservation> m_webdriver_navigation_observation;
Optional<NavigationToRetry> m_last_stopped_navigation;
+ // A view that shows the source of a page offers no source of its own to view: Each view of the source of a source
+ // page would wrap the one before in another layer of markup, without end. The source is the document of one
+ // document state, which a fragment navigation keeps and history traversal brings back.
+ bool m_page_source_pending { false };
+ Optional<Web::HTML::CrossProcessId> m_page_source_document_state;
+
size_t m_crash_count = 0;
RefPtr<Core::Timer> m_repeated_crash_timer;
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 75c5a5eef8d..d1ad4e1c9aa 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -2925,8 +2925,10 @@ void WebContentPage::did_request_media_context_menu(Web::HTML::CrossProcessId lo
void WebContentPage::did_get_highlighted_source(String html)
{
- if (auto new_tab = Application::the().open_blank_new_tab(Web::HTML::ActivateTab::Yes); new_tab.has_value())
+ if (auto new_tab = Application::the().open_blank_new_tab(Web::HTML::ActivateTab::Yes); new_tab.has_value()) {
+ new_tab->did_start_showing_page_source();
new_tab->load_html(html);
+ }
}
void WebContentPage::did_get_debugger_environments(u64 request_id, Optional<String> error, Vector<DebuggerEnvironment> environments)
diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txt
index beefe9767ef..b185ac8dd9e 100644
--- a/Tests/LibWebView/CMakeLists.txt
+++ b/Tests/LibWebView/CMakeLists.txt
@@ -267,6 +267,7 @@ if (NOT WIN32)
TestTabPerformanceMonitor.cpp
TestUserActivation.cpp
TestViewDestruction.cpp
+ TestViewSource.cpp
TestWebCompatReport.cpp
TestWebCompatReportPage.cpp
)
diff --git a/Tests/LibWebView/TestViewSource.cpp b/Tests/LibWebView/TestViewSource.cpp
new file mode 100644
index 00000000000..1a794492054
--- /dev/null
+++ b/Tests/LibWebView/TestViewSource.cpp
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "HeadlessTest.h"
+#include <AK/LexicalPath.h>
+#include <AK/Vector.h>
+#include <LibCore/EventLoop.h>
+#include <LibGfx/SystemTheme.h>
+#include <LibTest/TestCase.h>
+#include <LibURL/Parser.h>
+#include <LibWebView/Application.h>
+#include <LibWebView/HeadlessWebView.h>
+#include <LibWebView/Menu.h>
+#include <LibWebView/Utilities.h>
+
+namespace {
+
+Core::AnonymousBuffer load_theme()
+{
+ auto theme_path = LexicalPath::join(WebView::s_ladybird_resource_root, "themes"sv, "Default.ini"sv);
+ return MUST(Gfx::load_system_theme(theme_path.string()));
+}
+
+// The source of a page opens in a new tab, which becomes the active one.
+class TestApplication : public Test::HeadlessApplication {
+ WEB_VIEW_APPLICATION(TestApplication)
+
+public:
+ explicit TestApplication(Optional<ByteString> ladybird_binary_path)
+ : Test::HeadlessApplication(move(ladybird_binary_path))
+ {
+ }
+
+ virtual Optional<WebView::ViewImplementation&> open_blank_new_tab(Web::HTML::ActivateTab) const override
+ {
+ m_opened_tabs.append(WebView::HeadlessWebView::create(load_theme(), { 800, 600 }));
+ auto& tab = m_opened_tabs.last();
+ tab->on_load_finish = [this](auto const&) { ++m_opened_tab_loads; };
+ m_active_view = tab.ptr();
+ return *tab;
+ }
+
+ virtual Optional<WebView::ViewImplementation&> active_web_view() const override
+ {
+ if (!m_active_view)
+ return {};
+ return *m_active_view;
+ }
+
+ void activate(WebView::ViewImplementation& view)
+ {
+ m_active_view = &view;
+ update_actions_for_active_web_view();
+ }
+
+ size_t opened_tab_count() const { return m_opened_tabs.size(); }
+ WebView::HeadlessWebView& opened_tab(size_t index) { return *m_opened_tabs[index]; }
+ size_t opened_tab_loads() const { return m_opened_tab_loads; }
+
+private:
+ mutable Vector<NonnullOwnPtr<WebView::HeadlessWebView>> m_opened_tabs;
+ mutable WebView::ViewImplementation* m_active_view { nullptr };
+ mutable size_t m_opened_tab_loads { 0 };
+};
+
+}
+
+// Viewing the source of a page opens a tab that shows it. That tab offers no source of its own to view, so the source
+// of a source page can't be viewed over and over, each time wrapped in another layer of markup. A source tab that
+// moves on to another page offers its source again.
+TEST_CASE(view_source_of_source_page)
+{
+ Test::HeadlessTestEnvironment environment { "TestViewSource"sv };
+
+ auto app = TRY_OR_FAIL(environment.create_application<TestApplication>());
+
+ auto view = WebView::HeadlessWebView::create(load_theme(), { 800, 600 });
+
+ size_t loads_finished = 0;
+ view->on_load_finish = [&](auto const&) { ++loads_finished; };
+ Core::EventLoop::current().spin_until([&]() { return loads_finished >= 1; });
+
+ view->load_html("<!DOCTYPE html><p>A page</p>"sv);
+ Core::EventLoop::current().spin_until([&]() { return loads_finished >= 2; });
+
+ app->activate(*view);
+ VERIFY(view->can_view_source());
+ VERIFY(app->view_source_action().enabled());
+
+ app->view_source_action().activate();
+ Core::EventLoop::current().spin_until([&]() {
+ return app->opened_tab_count() == 1 && app->opened_tab(0).url() == URL::about_srcdoc();
+ });
+
+ auto& source_tab = app->opened_tab(0);
+ VERIFY(!source_tab.can_view_source());
+ VERIFY(!app->view_source_action().enabled());
+
+ source_tab.get_source();
+ VERIFY(app->opened_tab_count() == 1);
+
+ // A fragment navigation keeps the source page's document, so the restriction stays.
+ source_tab.run_javascript("location.hash = 'fragment'"_string);
+ Core::EventLoop::current().spin_until([&]() { return source_tab.url().fragment() == "fragment"sv; });
+ VERIFY(!source_tab.can_view_source());
+
+ app->activate(*view);
+ VERIFY(app->view_source_action().enabled());
+
+ app->activate(source_tab);
+ VERIFY(!app->view_source_action().enabled());
+
+ auto loads_before_navigation = app->opened_tab_loads();
+ source_tab.load(URL::Parser::basic_parse("data:text/html,<p>Another page</p>"sv).release_value());
+ Core::EventLoop::current().spin_until([&]() { return app->opened_tab_loads() > loads_before_navigation; });
+
+ VERIFY(source_tab.can_view_source());
+ VERIFY(app->view_source_action().enabled());
+
+ // History traversal brings the source page back, and with it the restriction.
+ source_tab.traverse_the_history_by_delta(-1);
+ Core::EventLoop::current().spin_until([&]() { return source_tab.url().equals(URL::about_srcdoc(), URL::ExcludeFragment::Yes); });
+ VERIFY(!source_tab.can_view_source());
+ VERIFY(!app->view_source_action().enabled());
+
+ source_tab.traverse_the_history_by_delta(1);
+ Core::EventLoop::current().spin_until([&]() { return !source_tab.url().equals(URL::about_srcdoc(), URL::ExcludeFragment::Yes); });
+ VERIFY(source_tab.can_view_source());
+ VERIFY(app->view_source_action().enabled());
+
+ outln("PASS: A tab showing the source of a page offers no source of its own, while it shows that source");
+}