Skip to content

Truly independent web browser

LibWeb: Allow cross-origin script focus without user activation

Callum Law4df566a73eea
diff --git a/Libraries/LibWeb/HTML/HTMLOrSVGOrMathMLElement.cpp b/Libraries/LibWeb/HTML/HTMLOrSVGOrMathMLElement.cpp
index 4f1ba0f1498..ee58abfab1a 100644
--- a/Libraries/LibWeb/HTML/HTMLOrSVGOrMathMLElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLOrSVGOrMathMLElement.cpp
@@ -205,6 +205,13 @@ void HTMLOrSVGOrMathMLElement<ElementBase>::inserted()
if (!target.allow_focus())
return;
+ // AD-HOC: Match Chromium, Firefox, and WebKit by excluding autofocus candidates with a cross-origin ancestor.
+ for (auto ancestor = target.navigable()->parent(); ancestor; ancestor = ancestor->parent()) {
+ auto ancestor_origin = ancestor->active_document_origin();
+ if (!ancestor_origin.has_value() || !target.origin().is_same_origin(*ancestor_origin))
+ return;
+ }
+
// 6. Let topDocument be target's node navigable's top-level traversable's active document.
// FIXME: The candidates of a top-level document hosted by another process are kept there. Only an element same
// origin with that document is flushed, which a document isolated from it never is, save for a
diff --git a/Libraries/LibWebCommon/PermissionsPolicy/PermissionsPolicy.cpp b/Libraries/LibWebCommon/PermissionsPolicy/PermissionsPolicy.cpp
index 1298ad270bd..2d836fd94c6 100644
--- a/Libraries/LibWebCommon/PermissionsPolicy/PermissionsPolicy.cpp
+++ b/Libraries/LibWebCommon/PermissionsPolicy/PermissionsPolicy.cpp
@@ -63,8 +63,6 @@ DefaultAllowlist default_allowlist(PolicyControlledFeature feature)
case PolicyControlledFeature::CrossOriginIsolated:
// https://w3c.github.io/encrypted-media/#permissions-policy-integration
case PolicyControlledFeature::EncryptedMedia:
- // https://html.spec.whatwg.org/multipage/infrastructure.html#focus-without-user-activation-feature
- case PolicyControlledFeature::FocusWithoutUserActivation:
// https://fullscreen.spec.whatwg.org/#permissions-policy-integration
case PolicyControlledFeature::Fullscreen:
// https://w3c.github.io/geolocation/#permissions-policy
@@ -74,6 +72,9 @@ DefaultAllowlist default_allowlist(PolicyControlledFeature feature)
// https://w3c.github.io/window-management/#api-permission-policy-integration
case PolicyControlledFeature::WindowManagement:
return DefaultAllowlist::Self;
+ // https://html.spec.whatwg.org/multipage/infrastructure.html#focus-without-user-activation-feature
+ // AD-HOC: Allow cross-origin script focus as in Chromium and Firefox; the specification's default is "self".
+ case PolicyControlledFeature::FocusWithoutUserActivation:
// https://w3c.github.io/gamepad/#permission-policy
case PolicyControlledFeature::Gamepad:
// https://w3c.github.io/picture-in-picture/#permissions-policy
diff --git a/Tests/LibWeb/Text/expected/SiteIsolation/iframe/script-focus-without-user-activation.txt b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/script-focus-without-user-activation.txt
new file mode 100644
index 00000000000..dd35aa480c6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SiteIsolation/iframe/script-focus-without-user-activation.txt
@@ -0,0 +1,2 @@
+focused=true, hasFocus=true, activated=false
+parent frame active=true
diff --git a/Tests/LibWeb/Text/input/SiteIsolation/iframe/autofocus-does-not-cross-an-isolated-iframe.html b/Tests/LibWeb/Text/input/SiteIsolation/iframe/autofocus-does-not-cross-an-isolated-iframe.html
index 72f1eebd24f..b53997f8096 100644
--- a/Tests/LibWeb/Text/input/SiteIsolation/iframe/autofocus-does-not-cross-an-isolated-iframe.html
+++ b/Tests/LibWeb/Text/input/SiteIsolation/iframe/autofocus-does-not-cross-an-isolated-iframe.html
@@ -1,9 +1,8 @@
-<!DOCTYPE html>
+<!doctype html>
<script src="../../include.js"></script>
<script>
- // The "focus-without-user-activation" feature has a default allowlist of "self", so it is disabled in a document
- // with a cross-origin ancestor, however that ancestor is hosted. A grandchild same origin with the top document
- // under an isolated iframe therefore does not autofocus.
+ // An autofocus candidate with a cross-origin ancestor is excluded, however that ancestor is hosted. A grandchild
+ // same origin with the top document under an isolated iframe therefore does not autofocus.
// This page is served over HTTP so that the grandchild can be same origin with it.
promiseTest(async () => {
const httpServer = httpTestServer();
diff --git a/Tests/LibWeb/Text/input/SiteIsolation/iframe/script-focus-without-user-activation.html b/Tests/LibWeb/Text/input/SiteIsolation/iframe/script-focus-without-user-activation.html
new file mode 100644
index 00000000000..a5bc3256292
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SiteIsolation/iframe/script-focus-without-user-activation.html
@@ -0,0 +1,33 @@
+<!doctype html>
+<script src="../../include.js"></script>
+<iframe id="frame"></iframe>
+<script>
+ promiseTest(async () => {
+ const url = new URL(
+ await httpTestServer().createEcho("GET", "/startup-script-focus", {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<!DOCTYPE html><input id="field"><script>
+ addEventListener("load", () => {
+ field.focus();
+ parent.postMessage({
+ focused: document.activeElement === field,
+ hasFocus: document.hasFocus(),
+ activated: navigator.userActivation.hasBeenActive,
+ }, "*");
+ });
+ <\/script>`,
+ })
+ );
+ url.hostname = uniqueLocalhostHostname("startup-script-focus");
+
+ const frame = document.getElementById("frame");
+ const report = new Promise(resolve =>
+ window.addEventListener("message", event => resolve(event.data), { once: true })
+ );
+ frame.src = url;
+ const result = await report;
+ println(`focused=${result.focused}, hasFocus=${result.hasFocus}, activated=${result.activated}`);
+ println(`parent frame active=${document.activeElement === frame}`);
+ });
+</script>