Skip to content

Truly independent web browser

LibWeb: Withhold geolocation where the permissions policy disables it

Zaggy102427979393ca69
diff --git a/Libraries/LibWeb/Geolocation/Geolocation.cpp b/Libraries/LibWeb/Geolocation/Geolocation.cpp
index 2d85eca2457..e433cc4b5b3 100644
--- a/Libraries/LibWeb/Geolocation/Geolocation.cpp
+++ b/Libraries/LibWeb/Geolocation/Geolocation.cpp
@@ -181,10 +181,10 @@ void Geolocation::request_a_position(GC::Ref<WebIDL::CallbackType> success_callb
// 1. Let watchIDs be geolocation's [[watchIDs]].
// 2. Let document be the geolocation's relevant global object's associated Document.
- [[maybe_unused]] auto& document = window().associated_document();
+ auto& document = window().associated_document();
- // FIXME: 3. If document is not allowed to use the "geolocation" feature:
- if (false) {
+ // 3. If document is not allowed to use the "geolocation" feature:
+ if (!document.is_allowed_to_use_feature(DOM::PolicyControlledFeature::Geolocation)) {
// 1. If watchId was passed, remove watchId from watchIDs.
if (watch_id.has_value())
remove_watch_id(watch_id.value());
diff --git a/Libraries/LibWebCommon/PermissionsPolicy/PermissionsPolicy.cpp b/Libraries/LibWebCommon/PermissionsPolicy/PermissionsPolicy.cpp
index 3b6d472951a..1298ad270bd 100644
--- a/Libraries/LibWebCommon/PermissionsPolicy/PermissionsPolicy.cpp
+++ b/Libraries/LibWebCommon/PermissionsPolicy/PermissionsPolicy.cpp
@@ -30,6 +30,8 @@ StringView policy_controlled_feature_token(PolicyControlledFeature feature)
return "fullscreen"sv;
case PolicyControlledFeature::Gamepad:
return "gamepad"sv;
+ case PolicyControlledFeature::Geolocation:
+ return "geolocation"sv;
case PolicyControlledFeature::Microphone:
return "microphone"sv;
case PolicyControlledFeature::PictureInPicture:
@@ -65,6 +67,8 @@ DefaultAllowlist default_allowlist(PolicyControlledFeature feature)
case PolicyControlledFeature::FocusWithoutUserActivation:
// https://fullscreen.spec.whatwg.org/#permissions-policy-integration
case PolicyControlledFeature::Fullscreen:
+ // https://w3c.github.io/geolocation/#permissions-policy
+ case PolicyControlledFeature::Geolocation:
// https://w3c.github.io/mediacapture-main/#permissions-policy-integration
case PolicyControlledFeature::Microphone:
// https://w3c.github.io/window-management/#api-permission-policy-integration
diff --git a/Libraries/LibWebCommon/PermissionsPolicy/PermissionsPolicy.h b/Libraries/LibWebCommon/PermissionsPolicy/PermissionsPolicy.h
index 054f914e1fc..d8a01defc97 100644
--- a/Libraries/LibWebCommon/PermissionsPolicy/PermissionsPolicy.h
+++ b/Libraries/LibWebCommon/PermissionsPolicy/PermissionsPolicy.h
@@ -29,6 +29,7 @@ enum class PolicyControlledFeature : u8 {
FocusWithoutUserActivation,
Fullscreen,
Gamepad,
+ Geolocation,
Microphone,
PictureInPicture,
WindowManagement,
diff --git a/Tests/LibWeb/Text/expected/geolocation-iframe-permissions-policy.txt b/Tests/LibWeb/Text/expected/geolocation-iframe-permissions-policy.txt
new file mode 100644
index 00000000000..f3d62523614
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/geolocation-iframe-permissions-policy.txt
@@ -0,0 +1,2 @@
+without-allow: position 1.25, 2.5
+allow-none: error 1
diff --git a/Tests/LibWeb/Text/input/SiteIsolation/iframe/isolated-iframe-gets-emulated-geolocation.html b/Tests/LibWeb/Text/input/SiteIsolation/iframe/isolated-iframe-gets-emulated-geolocation.html
index 4ae90475545..61f5ad027de 100644
--- a/Tests/LibWeb/Text/input/SiteIsolation/iframe/isolated-iframe-gets-emulated-geolocation.html
+++ b/Tests/LibWeb/Text/input/SiteIsolation/iframe/isolated-iframe-gets-emulated-geolocation.html
@@ -19,6 +19,7 @@
const result = new Promise(resolve => addEventListener("message", event => resolve(event.data), { once: true }));
const iframe = document.createElement("iframe");
+ iframe.allow = "geolocation";
iframe.src = childUrl;
document.body.appendChild(iframe);
diff --git a/Tests/LibWeb/Text/input/geolocation-iframe-permissions-policy.html b/Tests/LibWeb/Text/input/geolocation-iframe-permissions-policy.html
new file mode 100644
index 00000000000..eb2444fa0e4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/geolocation-iframe-permissions-policy.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script>
+ asyncTest(async done => {
+ internals.setGeolocationEmulatedPosition(1.25, 2.5, 100);
+
+ const childSource = `<!DOCTYPE html><script>
+ navigator.geolocation.getCurrentPosition(
+ position => parent.postMessage(\`position \${position.coords.latitude}, \${position.coords.longitude}\`, "*"),
+ error => parent.postMessage(\`error \${error.code}\`, "*"));
+ <\/script>`;
+
+ const frame = async (name, allow) => {
+ const iframe = document.createElement("iframe");
+ if (allow !== undefined)
+ iframe.setAttribute("allow", allow);
+ iframe.srcdoc = childSource;
+ const result = await new Promise(resolve => {
+ addEventListener("message", event => resolve(event.data), { once: true });
+ document.body.appendChild(iframe);
+ });
+ println(`${name}: ${result}`);
+ iframe.remove();
+ };
+
+ await frame("without-allow");
+ await frame("allow-none", "geolocation 'none'");
+
+ done();
+ });
+</script>