Truly independent web browser
LibWebView+WebContent: Show a notification only for an allowed document
sideshowbarker5e46799eb2cc
diff --git a/Libraries/LibWeb/NotificationsAPI/Notification.cpp b/Libraries/LibWeb/NotificationsAPI/Notification.cppindex de8f68dd9b3..662adecdba4 100644--- a/Libraries/LibWeb/NotificationsAPI/Notification.cpp+++ b/Libraries/LibWeb/NotificationsAPI/Notification.cpp@@ -487,7 +487,7 @@ void Notification::display()// across pages, replaces it on the platform, and reports its close back through notification_closed().// Here, the notification joins this page's part of the list and is sent off for display.m_state = State::Showing;- m_page->client().page_did_request_show_notification(m_id, data(), m_icon_resource);+ m_page->client().page_did_request_show_notification(m_id, document->id(), data(), m_icon_resource);// 8. If notification is a non-persistent notification, then queue a task to fire an event named show on the// Notification object representing notification.diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.hindex 6afccb8ae38..ca854e5e086 100644--- a/Libraries/LibWeb/Page/Page.h+++ b/Libraries/LibWeb/Page/Page.h@@ -910,7 +910,7 @@ public:virtual void page_did_set_next_permission_prompt_outcome_for_testing([[maybe_unused]] String const& name, [[maybe_unused]] String const& outcome, [[maybe_unused]] bool held) { }virtual void page_did_release_held_permission_prompt_for_testing([[maybe_unused]] u32 waiters) { }virtual Optional<Web::Permissions::PermissionSnapshot> page_did_advance_permission_clock_for_testing([[maybe_unused]] u64 milliseconds) { return {}; }- virtual void page_did_request_show_notification([[maybe_unused]] u64 notification_id, [[maybe_unused]] NotificationsAPI::NotificationData const&, [[maybe_unused]] RefPtr<Gfx::Bitmap const> icon) { }+ virtual void page_did_request_show_notification([[maybe_unused]] u64 notification_id, [[maybe_unused]] HTML::CrossProcessId document_id, [[maybe_unused]] NotificationsAPI::NotificationData const&, [[maybe_unused]] RefPtr<Gfx::Bitmap const> icon) { }virtual void page_did_request_close_notification([[maybe_unused]] u64 notification_id) { }virtual void page_did_abandon_notification([[maybe_unused]] u64 notification_id) { }virtual void page_did_handle_notification_click([[maybe_unused]] u64 notification_id, [[maybe_unused]] bool into_focus) { }diff --git a/Libraries/LibWebView/NotificationManager.cpp b/Libraries/LibWebView/NotificationManager.cppindex 06b6786a57e..c4ff0677521 100644--- a/Libraries/LibWebView/NotificationManager.cpp+++ b/Libraries/LibWebView/NotificationManager.cpp@@ -41,7 +41,7 @@ void NotificationManager::install_backend_callbacks()m_backend->on_closed = [this](u64 id, NotificationBackend::ByUser by_user) { backend_did_close(id, by_user); };}-void NotificationManager::show(WebContentPage& page, u64 page_notification_id, Web::NotificationsAPI::NotificationData data, RefPtr<Gfx::Bitmap const> icon)+void NotificationManager::show(WebContentPage& page, u64 page_notification_id, URL::Origin top_level_origin, Web::NotificationsAPI::NotificationData data, RefPtr<Gfx::Bitmap const> icon){// Only the page that shows the tab's document may notify for that document. A page hosting a cross-site frame// in the tab has its own process, and its renderer refuses the notification; one that asks anyway is misbehaving,@@ -55,11 +55,11 @@ void NotificationManager::show(WebContentPage& page, u64 page_notification_id, Wif (icon && (icon->width() > icon_maximum_side || icon->height() > icon_maximum_side))icon = nullptr;- // The notification belongs to the origin the browser knows the page's top-level document by, never to one the- // page reports. (The document's origin, not the URL's: An opaque origin, which file: pages have, is the same- // one for the life of the document, where a URL would make a fresh one on every ask.)+ // The notification belongs to the top-level origin the browser knows its document by, never to one the page+ // reports. (An origin, not a URL's: An opaque origin, which file: pages have, is the same one for the life of the+ // document, where a URL would make a fresh one on every ask.)auto url = page.view().url();- auto origin = page.traversable().active_document().origin();+ auto origin = move(top_level_origin);Record record {.id = m_next_id++,diff --git a/Libraries/LibWebView/NotificationManager.h b/Libraries/LibWebView/NotificationManager.hindex 6218606feaa..efd9d90f214 100644--- a/Libraries/LibWebView/NotificationManager.h+++ b/Libraries/LibWebView/NotificationManager.h@@ -37,7 +37,8 @@ public:// The longest side an icon may have by the time it reaches a backend; a renderer sending more is misbehaving.static constexpr int icon_maximum_side = 128;- void show(WebContentPage&, u64 page_notification_id, Web::NotificationsAPI::NotificationData, RefPtr<Gfx::Bitmap const> icon);+ // The origin is the top-level origin of the document the notification came from, as the UI process knows it.+ void show(WebContentPage&, u64 page_notification_id, URL::Origin top_level_origin, Web::NotificationsAPI::NotificationData, RefPtr<Gfx::Bitmap const> icon);void close(WebContentPage&, u64 page_notification_id);// The page's document is gone: The notification stays up, and a click on it opens the site in a new tab.void abandon(WebContentPage&, u64 page_notification_id);diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cppindex 6276999cef3..bb16472c958 100644--- a/Libraries/LibWebView/WebContentPage.cpp+++ b/Libraries/LibWebView/WebContentPage.cpp@@ -1666,6 +1666,15 @@ void WebContentPage::for_each_navigable_with_a_hosted_active_document(Function<I});}+// Notifications has no policy-controlled feature, so the frame check is ours: only a document whose origin is the+// top-level origin, the site the user sees in the location bar, gets to ask for it or to show one.+static bool permission_reaches_document(Web::Permissions::PermissionName name, CanonicalDocument const& document, URL::Origin const& top_level_origin)+{+ if (name != Web::Permissions::PermissionName::Notifications)+ return true;+ return document.origin().is_same_origin(top_level_origin);+}+bool WebContentPage::document_may_use(Web::HTML::CrossProcessId document_id, Web::Permissions::PermissionName name) const{auto document = hosted_document_with_id(document_id);@@ -1676,6 +1685,8 @@ bool WebContentPage::document_may_use(Web::HTML::CrossProcessId document_id, Webreturn false;if (auto feature = policy_controlled_feature_for(name); feature.has_value() && Web::PermissionsPolicy::check_permissions_policy(document->permissions_policy(), *feature, document->origin(), document->origin()) != Web::PermissionsPolicy::FeatureValue::Enabled)return false;+ if (!permission_reaches_document(name, *document, *top_level_origin))+ return false;if (top_level_origin->is_opaque())return m_file_permission_grants.find_first_index_if([&](auto const& grant) { return grant.document_id == document_id && grant.name == name; }).has_value();return view().session().permission_manager->effective_state(name, *top_level_origin) == Web::Permissions::EffectiveState::Granted;@@ -1747,6 +1758,10 @@ void WebContentPage::did_request_permission(u64 request_id, Web::HTML::CrossProcreturn;}}+ if (!permission_reaches_document(*permission_name, *document, *top_level_origin)) {+ deny();+ return;+ }// A file: page has an opaque origin, and it's the only one that gets to ask. Its answer lasts as long as the page.auto is_file = top_level_origin->is_opaque() && view().url().scheme() == "file"sv;@@ -1768,6 +1783,13 @@ void WebContentPage::did_request_permission(u64 request_id, Web::HTML::CrossProc}}+ // A page asks for notifications only from a gesture, and the UI process holds it to the activation it delivered+ // itself, consuming it as the renderer does: a renderer that skipped its own check gets no prompt.+ if (*permission_name == Web::Permissions::PermissionName::Notifications && !consume_transient_user_activation()) {+ deny();+ return;+ }+auto generation_when_asked = manager.decision_generation(*permission_name, *top_level_origin);// When the system hasn't yet let the browser find the user's location either, the prompt says the system will ask next.auto show_os_hint = *permission_name == Web::Permissions::PermissionName::Geolocation && Application::the().geolocation_authorization() == Core::GeolocationAuthorization::NotDetermined;@@ -1863,12 +1885,21 @@ Web::Permissions::PermissionSnapshot WebContentPage::advance_permission_clock_foreturn manager.snapshot_for(client().permission_origins());}-void WebContentPage::did_request_show_notification(u64 notification_id, Web::NotificationsAPI::NotificationData data, Optional<Gfx::ShareableBitmap> icon)+void WebContentPage::did_request_show_notification(u64 notification_id, Web::HTML::CrossProcessId document_id, Web::NotificationsAPI::NotificationData data, Optional<Gfx::ShareableBitmap> icon){+ // The UI process shows a notification only for a document it knows the user let notify, read from its own store+ // as the notification arrives: A decision made while the page was still fetching the icon counts, and a renderer+ // that skipped its own check gets nothing.+ auto document = hosted_document_with_id(document_id);+ if (!document || !document_may_use(document_id, Web::Permissions::PermissionName::Notifications)) {+ async_notification_show_failed(notification_id);+ return;+ }+RefPtr<Gfx::Bitmap const> icon_bitmap;if (icon.has_value() && icon->is_valid())icon_bitmap = icon->bitmap();- NotificationManager::the().show(*this, notification_id, move(data), move(icon_bitmap));+ NotificationManager::the().show(*this, notification_id, *document->top_level_origin(), move(data), move(icon_bitmap));}void WebContentPage::did_request_close_notification(u64 notification_id)diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.hindex 4ead28f11e1..91df2b552a9 100644--- a/Libraries/LibWebView/WebContentPage.h+++ b/Libraries/LibWebView/WebContentPage.h@@ -282,7 +282,7 @@ private:virtual void did_start_geolocation_position_watch(u64 request_id, Web::HTML::CrossProcessId document_id) override;virtual void did_stop_geolocation_position_watch(u64 request_id) override;virtual void did_request_permission(u64 request_id, Web::HTML::CrossProcessId document_id, Utf16String name) override;- virtual void did_request_show_notification(u64 notification_id, Web::NotificationsAPI::NotificationData data, Optional<Gfx::ShareableBitmap> icon) override;+ virtual void did_request_show_notification(u64 notification_id, Web::HTML::CrossProcessId document_id, Web::NotificationsAPI::NotificationData data, Optional<Gfx::ShareableBitmap> icon) override;virtual void did_request_close_notification(u64 notification_id) override;virtual void did_abandon_notification(u64 notification_id) override;virtual void did_handle_notification_click(u64 notification_id, bool into_focus) override;diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cppindex 6a5ece8315d..b2c1d5cec0e 100644--- a/Services/WebContent/PageClient.cpp+++ b/Services/WebContent/PageClient.cpp@@ -1307,12 +1307,12 @@ void PageClient::page_did_request_permission(u64 request_id, Web::HTML::CrossProclient().async_did_request_permission(m_id, request_id, requesting_document_id, name);}-void PageClient::page_did_request_show_notification(u64 notification_id, Web::NotificationsAPI::NotificationData const& data, RefPtr<Gfx::Bitmap const> icon)+void PageClient::page_did_request_show_notification(u64 notification_id, Web::HTML::CrossProcessId document_id, Web::NotificationsAPI::NotificationData const& data, RefPtr<Gfx::Bitmap const> icon){Optional<Gfx::ShareableBitmap> shareable_icon;if (icon)shareable_icon = icon->to_shareable_bitmap();- client().async_did_request_show_notification(m_id, notification_id, data, move(shareable_icon));+ client().async_did_request_show_notification(m_id, notification_id, document_id, data, move(shareable_icon));}void PageClient::page_did_request_close_notification(u64 notification_id)diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.hindex d43bef7289c..f6e9146a08e 100644--- a/Services/WebContent/PageClient.h+++ b/Services/WebContent/PageClient.h@@ -363,7 +363,7 @@ private:virtual void page_did_set_next_permission_prompt_outcome_for_testing(String const& name, String const& outcome, bool held) override;virtual void page_did_release_held_permission_prompt_for_testing(u32 waiters) override;virtual Optional<Web::Permissions::PermissionSnapshot> page_did_advance_permission_clock_for_testing(u64 milliseconds) override;- virtual void page_did_request_show_notification(u64 notification_id, Web::NotificationsAPI::NotificationData const&, RefPtr<Gfx::Bitmap const> icon) override;+ virtual void page_did_request_show_notification(u64 notification_id, Web::HTML::CrossProcessId document_id, Web::NotificationsAPI::NotificationData const&, RefPtr<Gfx::Bitmap const> icon) override;virtual void page_did_request_close_notification(u64 notification_id) override;virtual void page_did_abandon_notification(u64 notification_id) override;virtual void page_did_handle_notification_click(u64 notification_id, bool into_focus) override;diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipcindex f5877269474..cde41596d2f 100644--- a/Services/WebContent/WebContentClient.ipc+++ b/Services/WebContent/WebContentClient.ipc@@ -242,7 +242,7 @@ endpoint WebContentClient routed by Web::PageId page_iddid_stop_geolocation_position_watch(Web::PageId page_id, u64 request_id) =|did_request_permission(Web::PageId page_id, u64 request_id, Web::HTML::CrossProcessId document_id, Utf16String name) =|[NotRouted] did_request_permission_snapshot(Web::PageId page_id, URL::Origin top_level_origin) => (Web::Permissions::PermissionSnapshot snapshot)- did_request_show_notification(Web::PageId page_id, u64 notification_id, Web::NotificationsAPI::NotificationData data, Optional<Gfx::ShareableBitmap> icon) =|+ did_request_show_notification(Web::PageId page_id, u64 notification_id, Web::HTML::CrossProcessId document_id, Web::NotificationsAPI::NotificationData data, Optional<Gfx::ShareableBitmap> icon) =|did_request_close_notification(Web::PageId page_id, u64 notification_id) =|did_abandon_notification(Web::PageId page_id, u64 notification_id) =|did_handle_notification_click(Web::PageId page_id, u64 notification_id, bool into_focus) =|diff --git a/Tests/LibWeb/Text/expected/notifications/block-during-icon-fetch-errors.txt b/Tests/LibWeb/Text/expected/notifications/block-during-icon-fetch-errors.txtnew file mode 100644index 00000000000..c23695a04d3--- /dev/null+++ b/Tests/LibWeb/Text/expected/notifications/block-during-icon-fetch-errors.txt@@ -0,0 +1,2 @@+outcome: error+shown: 0diff --git a/Tests/LibWeb/Text/expected/notifications/ui-refuses-prompt-from-cross-origin-frame.txt b/Tests/LibWeb/Text/expected/notifications/ui-refuses-prompt-from-cross-origin-frame.txtnew file mode 100644index 00000000000..a3f3729eccf--- /dev/null+++ b/Tests/LibWeb/Text/expected/notifications/ui-refuses-prompt-from-cross-origin-frame.txt@@ -0,0 +1,2 @@+frame's request: denied+top-level permission: defaultdiff --git a/Tests/LibWeb/Text/input/notifications/block-during-icon-fetch-errors.html b/Tests/LibWeb/Text/input/notifications/block-during-icon-fetch-errors.htmlnew file mode 100644index 00000000000..96e01b54437--- /dev/null+++ b/Tests/LibWeb/Text/input/notifications/block-during-icon-fetch-errors.html@@ -0,0 +1,29 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // The UI process checks the permission when the notification reaches it, not when the page created it: a site+ // blocked while its notification was still fetching its icon gets error, and nothing is shown.+ asyncTest(async done => {+ await internals.setPermission("notifications", "granted");+ const server = httpTestServer();+ const slowIcon = await server.createEcho("GET", "/notifications/slow-icon-blocked.png", {+ status: 200,+ headers: { "Content-Type": "image/png" },+ body: "",+ delay_ms: 1000,+ });++ const notification = new Notification("Blocked mid-fetch", { icon: slowIcon });+ const outcome = new Promise(resolve => {+ notification.onshow = () => resolve("show");+ notification.onerror = () => resolve("error");+ });+ // The permission check at creation runs in a task; let it pass, and the icon fetch start, before blocking.+ await new Promise(resolve => setTimeout(resolve, 100));+ await internals.setPermission("notifications", "denied");+ println(`outcome: ${await outcome}`);+ const state = JSON.parse(internals.notificationsStateForTesting());+ println(`shown: ${state.shown.length}`);+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/notifications/ui-refuses-prompt-from-cross-origin-frame.html b/Tests/LibWeb/Text/input/notifications/ui-refuses-prompt-from-cross-origin-frame.htmlnew file mode 100644index 00000000000..44c01a5d307--- /dev/null+++ b/Tests/LibWeb/Text/input/notifications/ui-refuses-prompt-from-cross-origin-frame.html@@ -0,0 +1,31 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>iframe { position: absolute; top: 0; left: 0; width: 200px; height: 100px; border: 0; }</style>+<script>+ // The UI process refuses a notifications prompt for a document whose origin isn't its top-level origin, whatever+ // the renderer says: the frame asks through the Permissions request directly, skipping the Notification API's own+ // refusals, and still gets no prompt and no grant for the top-level site.+ asyncTest(async done => {+ const server = httpTestServer();+ const frameURL = new URL(await server.createEcho("GET", "/notifications/ui-gate-frame.html", {+ status: 200,+ headers: { "Content-Type": "text/html" },+ body: `<!DOCTYPE html><script>+ internals.requestPermission("notifications").then(state => parent.postMessage(state, "*"));+ <\/script>`,+ }));+ frameURL.hostname = uniqueLocalhostHostname("notifications-ui-gate-cross-origin");++ internals.setNextPermissionPromptOutcome("notifications", "allow");+ const iframe = document.createElement("iframe");+ iframe.src = frameURL.href;+ const result = await new Promise(resolve => {+ setTimeout(() => resolve("no answer"), 2000);+ addEventListener("message", event => resolve(event.data));+ document.body.appendChild(iframe);+ });+ println(`frame's request: ${result}`);+ println(`top-level permission: ${Notification.permission}`);+ done();+ });+</script>