Skip to content

Truly independent web browser

LibWebView: End one-time permission grants when the site goes away

sideshowbarkerd5adb8dedf16
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index 4737ee73c9f..d8ab7c4c264 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -37,6 +37,7 @@
#include <LibWebView/HistoryDebug.h>
#include <LibWebView/HistoryStore.h>
#include <LibWebView/Menu.h>
+#include <LibWebView/PermissionManager.h>
#include <LibWebView/TabPerformanceMonitor.h>
#include <LibWebView/URL.h>
#include <LibWebView/UserAgent.h>
@@ -114,6 +115,7 @@ ViewImplementation::~ViewImplementation()
// The frontend may be torn down already, so clearing the ongoing navigation must not report a URL change to it.
on_url_change = nullptr;
TabPerformanceMonitor::forget_view(view_id());
+ session().permission_manager->remove_view(view_id());
if (m_top_level_traversable)
m_top_level_traversable->clear_ongoing_navigation();
cancel_all_native_geolocation_requests();
@@ -321,6 +323,7 @@ void ViewImplementation::did_change_active_document(Badge<CanonicalTraversable>)
{
// A prompt the document that asked has left behind goes unanswered: the user ignored it.
ignore_pending_permission_prompts();
+ report_permission_presence();
set_title(Utf16String::from_utf8(url().serialize()));
set_favicon({});
did_update_url();
@@ -451,6 +454,16 @@ void ViewImplementation::set_system_visibility_state(Web::HTML::VisibilityState
traversable().set_system_visibility_state(visibility_state);
Application::the().update_compositor_context_visibility(page().compositor_context_id(), visibility_state);
+ report_permission_presence();
+}
+
+// One-time grants last only while some view shows their site, so each view tells the permission manager what it shows.
+void ViewImplementation::report_permission_presence()
+{
+ if (!m_top_level_traversable)
+ return;
+ auto visible = traversable().system_visibility_state() == Web::HTML::VisibilityState::Visible;
+ session().permission_manager->set_view_presence(view_id(), url().origin(), visible);
}
void ViewImplementation::set_presents_from_cpu_memory(bool presents_from_cpu_memory)
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index 891ac044f0a..aa4c58a837c 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -690,6 +690,7 @@ protected:
void did_update_url();
void settle_permission_prompt(PermissionPromptRequest const&, Web::Permissions::PromptOutcome);
+ void report_permission_presence();
void ignore_pending_permission_prompts();
void set_title(Utf16String);
void set_favicon(Optional<Gfx::Bitmap const&>);
diff --git a/Tests/LibWeb/Text/expected/PermissionsAPI/permission-allow-once-lasts-while-the-site-is-shown.txt b/Tests/LibWeb/Text/expected/PermissionsAPI/permission-allow-once-lasts-while-the-site-is-shown.txt
new file mode 100644
index 00000000000..d6804b00faa
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/PermissionsAPI/permission-allow-once-lasts-while-the-site-is-shown.txt
@@ -0,0 +1,3 @@
+allowed this time: granted
+after a same-origin navigation: granted
+after the site was left: prompt
diff --git a/Tests/LibWeb/Text/input/PermissionsAPI/permission-allow-once-lasts-while-the-site-is-shown.html b/Tests/LibWeb/Text/input/PermissionsAPI/permission-allow-once-lasts-while-the-site-is-shown.html
new file mode 100644
index 00000000000..620cbd73e6c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/PermissionsAPI/permission-allow-once-lasts-while-the-site-is-shown.html
@@ -0,0 +1,51 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // A one-time grant lasts while some tab shows the site: through same-origin navigations, and no further than the
+ // moment the last of them moves on to another site. The site here is a host of its own, shown in a popup.
+ asyncTest(async done => {
+ const httpServer = httpTestServer();
+ const runId = crypto.randomUUID();
+ const htmlHeaders = { "Content-Type": "text/html" };
+ const site = uniqueLocalhostHostname("allow-once-site");
+ const elsewhere = uniqueLocalhostHostname("allow-once-elsewhere");
+
+ const echo = async (host, name, body) => {
+ const url = new URL(await httpServer.createEcho("GET", `/${name}-${runId}`, {
+ status: 200,
+ headers: htmlHeaders,
+ body: `<!DOCTYPE html><script>${body}<\/script>`,
+ }));
+ url.hostname = host;
+ return url.href;
+ };
+ const report = `const report = message => opener.postMessage(message, "*");
+ const state = async () => (await navigator.permissions.query({ name: "microphone" })).state;`;
+
+ const leftUrl = await echo(elsewhere, "left", `${report} report("left");`);
+ const secondPageUrl = await echo(site, "second", `${report}
+ state().then(value => { report(\`after a same-origin navigation: \${value}\`); location.href = ${JSON.stringify(leftUrl)}; });`);
+ const firstPageUrl = await echo(site, "first", `${report}
+ internals.setNextPermissionPromptOutcome("microphone", "allow-once");
+ internals.requestPermission("microphone").then(value => {
+ report(\`allowed this time: \${value}\`);
+ location.href = ${JSON.stringify(secondPageUrl)};
+ });`);
+ const returnUrl = await echo(site, "return", `${report}
+ state().then(value => report(\`after the site was left: \${value}\`));`);
+
+ const popups = [];
+ addEventListener("message", event => {
+ if (event.data === "left") {
+ popups.push(window.open(returnUrl));
+ return;
+ }
+ println(event.data);
+ if (event.data.startsWith("after the site was left")) {
+ popups.forEach(popup => popup.close());
+ done();
+ }
+ });
+ popups.push(window.open(firstPageUrl));
+ });
+</script>