Skip to content

Truly independent web browser

LibWeb+LibWebView: Ask before giving a site the user’s location

sideshowbarker3cf83346e28d
diff --git a/Libraries/LibWeb/Geolocation/Geolocation.cpp b/Libraries/LibWeb/Geolocation/Geolocation.cpp
index e433cc4b5b3..560b3978733 100644
--- a/Libraries/LibWeb/Geolocation/Geolocation.cpp
+++ b/Libraries/LibWeb/Geolocation/Geolocation.cpp
@@ -219,61 +219,63 @@ void Geolocation::request_a_position(GC::Ref<WebIDL::CallbackType> success_callb
auto descriptor = PermissionsAPI::PermissionDescriptor { "geolocation"_utf16 };
// 7. In parallel:
- // AD-HOC: run_in_parallel_when_document_is_visible() already runs this in parallel.
+ // NB: The round trip to the UI process that asks the user for permission stands in for the parallel steps: the
+ // request goes out as an async IPC, and the rest of the steps run on the main thread once it answers.
{
// 1. Set permission to request permission to use descriptor.
- auto permission = Web::PermissionsAPI::request_permission(descriptor);
-
- // 2. If permission is "denied", then:
- if (permission == PermissionsAPI::PermissionState::Denied) {
- // 1. If watchId was passed, remove watchId from watchIDs.
- if (watch_id.has_value())
- remove_watch_id(watch_id.value());
-
- // 2. Call back with error passing errorCallback and PERMISSION_DENIED.
- call_back_with_error(error_callback, GeolocationPositionError::ErrorCode::PermissionDenied);
-
- // 3. Terminate this algorithm.
- return;
- }
-
- // FIXME: 3. Wait to acquire a position passing successCallback, errorCallback, options, and watchId.
- acquire_a_position(success_callback, error_callback, options, watch_id);
-
- // 4. If watchId was not passed, terminate this algorithm.
- if (!watch_id.has_value())
- return;
-
- if (!m_watch_ids.contains(*watch_id))
- return;
+ auto& settings = window().associated_document().relevant_settings_object();
+ PermissionsAPI::request_permission_with_user_prompt(descriptor, settings, GC::create_function(heap(), [this, watch_id, success_callback, error_callback, options](PermissionsAPI::PermissionState permission) {
+ // 2. If permission is "denied", then:
+ if (permission == PermissionsAPI::PermissionState::Denied) {
+ // 1. If watchId was passed, remove watchId from watchIDs.
+ if (watch_id.has_value())
+ remove_watch_id(watch_id.value());
+
+ // 2. Call back with error passing errorCallback and PERMISSION_DENIED.
+ call_back_with_error(error_callback, GeolocationPositionError::ErrorCode::PermissionDenied);
+
+ // 3. Terminate this algorithm.
+ return;
+ }
- // 5. While watchIDs contains watchId:
- // AD-HOC: The spec expresses this as a loop; register callbacks for significant position changes and
- // re-enter request a position from those callbacks.
- auto watch_position_data = m_watch_position_data.get(*watch_id);
- if (!watch_position_data.has_value() || watch_position_data->emulated_position_data_observer_id.has_value())
- return;
+ // FIXME: 3. Wait to acquire a position passing successCallback, errorCallback, options, and watchId.
+ acquire_a_position(success_callback, error_callback, options, watch_id);
- auto weak_document = GC::Weak<DOM::Document> { window().associated_document() };
- auto observer_id = window().page().register_emulated_position_data_observer(GC::create_function(heap(), [this, watch_id = *watch_id, weak_document] {
- if (!m_watch_ids.contains(watch_id))
+ // 4. If watchId was not passed, terminate this algorithm.
+ if (!watch_id.has_value())
return;
- auto document = weak_document.ptr();
- if (!document || !document->is_fully_active())
+ if (!m_watch_ids.contains(*watch_id))
return;
- if (m_pending_watch_position_request_ids.contains(watch_id))
+ // 5. While watchIDs contains watchId:
+ // AD-HOC: The spec expresses this as a loop; register callbacks for significant position changes and
+ // re-enter request a position from those callbacks.
+ auto watch_position_data = m_watch_position_data.get(*watch_id);
+ if (!watch_position_data.has_value() || watch_position_data->emulated_position_data_observer_id.has_value())
return;
- auto watch_position_data = m_watch_position_data.get(watch_id);
- if (!watch_position_data.has_value())
- return;
+ auto weak_document = GC::Weak<DOM::Document> { window().associated_document() };
+ auto observer_id = window().page().register_emulated_position_data_observer(GC::create_function(heap(), [this, watch_id = *watch_id, weak_document] {
+ if (!m_watch_ids.contains(watch_id))
+ return;
+
+ auto document = weak_document.ptr();
+ if (!document || !document->is_fully_active())
+ return;
+
+ if (m_pending_watch_position_request_ids.contains(watch_id))
+ return;
+
+ auto watch_position_data = m_watch_position_data.get(watch_id);
+ if (!watch_position_data.has_value())
+ return;
- // FIXME: If document is not fully active or visibility state is not "visible", wait for visibility.
- request_a_position(watch_position_data->success_callback, watch_position_data->error_callback, watch_position_data->options, watch_id);
+ // FIXME: If document is not fully active or visibility state is not "visible", wait for visibility.
+ request_a_position(watch_position_data->success_callback, watch_position_data->error_callback, watch_position_data->options, watch_id);
+ }));
+ watch_position_data->emulated_position_data_observer_id = observer_id;
}));
- watch_position_data->emulated_position_data_observer_id = observer_id;
}
}));
}
@@ -299,7 +301,8 @@ void Geolocation::acquire_a_position(GC::Ref<WebIDL::CallbackType> success_callb
// the device's position by running the following steps:
{
// 1. Let permission be get the current permission state of "geolocation".
- auto permission = Web::PermissionsAPI::permission_state(Bindings::PermissionDescriptor { "geolocation"_utf16 });
+ // NB: This can run outside any script (when the user's answer arrives, e.g.), so the settings object is passed.
+ auto permission = Web::PermissionsAPI::permission_state(Bindings::PermissionDescriptor { "geolocation"_utf16 }, window().associated_document().relevant_settings_object());
// 2. If permission is "denied":
if (permission == Bindings::PermissionState::Denied) {
@@ -372,10 +375,26 @@ void Geolocation::acquire_a_position(GC::Ref<WebIDL::CallbackType> success_callb
// 4. Otherwise, if position is not cachedPosition, try to acquire position data from the underlying system,
// optionally taking into consideration the value of options.enableHighAccuracy during acquisition.
acquire_position_from_page(success_callback, error_callback, options, watch_id, acquisition_time);
+ return;
}
+
+ // NB: While the permission is "prompt" (a one-time grant just ended, e.g.), neither step above applies, and the
+ // timeout task elapses without a position.
+ call_back_with_timeout_error(error_callback, options.timeout);
}
}
+void Geolocation::call_back_with_timeout_error(GC::Ptr<WebIDL::CallbackType> error_callback, WebIDL::UnsignedLong timeout)
+{
+ auto timer = Platform::Timer::create_single_shot(heap(), AK::Duration::from_milliseconds(min(timeout, NumericLimits<int>::max())), {});
+ m_timeout_timers.append(timer);
+ timer->on_timeout = GC::create_function(heap(), [this, timer, error_callback] {
+ m_timeout_timers.remove_first_matching([&](auto candidate) { return candidate == timer; });
+ call_back_with_error(error_callback, GeolocationPositionError::ErrorCode::Timeout);
+ });
+ timer->start();
+}
+
void Geolocation::acquire_position_from_page(GC::Ref<WebIDL::CallbackType> success_callback,
GC::Ptr<WebIDL::CallbackType> error_callback, PositionOptions const& options,
Optional<WebIDL::UnsignedLong> watch_id, HighResolutionTime::EpochTimeStamp acquisition_time)
@@ -478,7 +497,7 @@ void Geolocation::acquire_position_from_page(GC::Ref<WebIDL::CallbackType> succe
queue_success_callback(success_callback, position);
});
- request_state->request_id = page.request_geolocation_position(position_callback, watch_id.has_value() ? Page::GeolocationRequestType::Watch : Page::GeolocationRequestType::OneShot);
+ request_state->request_id = page.request_geolocation_position(document.id(), position_callback, watch_id.has_value() ? Page::GeolocationRequestType::Watch : Page::GeolocationRequestType::OneShot);
if (watch_id.has_value())
m_pending_watch_position_request_ids.set(*watch_id, request_state->request_id);
diff --git a/Libraries/LibWeb/Geolocation/Geolocation.h b/Libraries/LibWeb/Geolocation/Geolocation.h
index 1d39a2839e6..51913100427 100644
--- a/Libraries/LibWeb/Geolocation/Geolocation.h
+++ b/Libraries/LibWeb/Geolocation/Geolocation.h
@@ -54,6 +54,7 @@ private:
HTML::Window& window() const { return m_window; }
void acquire_a_position(GC::Ref<WebIDL::CallbackType>, GC::Ptr<WebIDL::CallbackType>, PositionOptions const&, Optional<WebIDL::UnsignedLong>);
+ void call_back_with_timeout_error(GC::Ptr<WebIDL::CallbackType> error_callback, WebIDL::UnsignedLong timeout);
void acquire_position_from_page(GC::Ref<WebIDL::CallbackType>, GC::Ptr<WebIDL::CallbackType>, PositionOptions const&, Optional<WebIDL::UnsignedLong>, HighResolutionTime::EpochTimeStamp);
void call_back_with_error(GC::Ptr<WebIDL::CallbackType>, GeolocationPositionError::ErrorCode) const;
EmulatedPositionData get_emulated_position_data() const;
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index 5d8a6457804..f77d27fe572 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -1524,20 +1524,20 @@ void Page::permission_request_completed(u64 request_id, Web::Permissions::Effect
callback.value()->function()(state);
}
-u64 Page::request_geolocation_position(GeolocationPositionCallback callback, GeolocationRequestType type)
+u64 Page::request_geolocation_position(HTML::CrossProcessId requesting_document_id, GeolocationPositionCallback callback, GeolocationRequestType type)
{
// This is the browser-process bridge for the Geolocation spec's "try to acquire position data from the underlying system" step.
auto request_id = m_next_geolocation_request_id++;
- m_pending_geolocation_requests.set(request_id, { callback, type });
+ m_pending_geolocation_requests.set(request_id, { callback, type, requesting_document_id });
if (type == GeolocationRequestType::Watch) {
- client().page_did_start_geolocation_position_watch(request_id);
+ client().page_did_start_geolocation_position_watch(request_id, requesting_document_id);
return request_id;
}
if (!m_active_geolocation_request_id.has_value()) {
m_active_geolocation_request_id = request_id;
- client().page_did_request_geolocation_position(request_id);
+ client().page_did_request_geolocation_position(request_id, requesting_document_id);
}
return request_id;
@@ -1562,7 +1562,7 @@ void Page::cancel_geolocation_position_request(u64 request_id)
for (auto const& entry : m_pending_geolocation_requests) {
if (entry.value.type == GeolocationRequestType::OneShot) {
m_active_geolocation_request_id = entry.key;
- client().page_did_request_geolocation_position(entry.key);
+ client().page_did_request_geolocation_position(entry.key, entry.value.requesting_document_id);
return;
}
}
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index 6b08e1cc353..e4ea8f42640 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -344,7 +344,7 @@ public:
OneShot,
Watch,
};
- u64 request_geolocation_position(GeolocationPositionCallback, GeolocationRequestType = GeolocationRequestType::OneShot);
+ u64 request_geolocation_position(HTML::CrossProcessId requesting_document_id, GeolocationPositionCallback, GeolocationRequestType = GeolocationRequestType::OneShot);
void cancel_geolocation_position_request(u64 request_id);
void receive_geolocation_position(u64 request_id, GeolocationPositionResult);
@@ -577,6 +577,7 @@ private:
struct PendingGeolocationRequest {
GeolocationPositionCallback callback;
GeolocationRequestType type;
+ HTML::CrossProcessId requesting_document_id;
};
HashMap<u64, PendingGeolocationRequest> m_pending_geolocation_requests;
u64 m_next_geolocation_request_id { 0 };
@@ -874,9 +875,9 @@ public:
virtual void page_did_request_color_picker([[maybe_unused]] Color current_color) { }
virtual void page_did_request_file_picker([[maybe_unused]] HTML::FileFilter const& accepted_file_types, Web::HTML::AllowMultipleFiles) { }
virtual void page_did_request_select_dropdown([[maybe_unused]] HTML::CrossProcessId local_root_id, [[maybe_unused]] Web::CSSPixelPoint content_position, [[maybe_unused]] Web::CSSPixels minimum_width, [[maybe_unused]] Vector<Web::HTML::SelectItem> items) { }
- virtual void page_did_request_geolocation_position([[maybe_unused]] u64 request_id) { }
+ virtual void page_did_request_geolocation_position([[maybe_unused]] u64 request_id, [[maybe_unused]] HTML::CrossProcessId requesting_document_id) { }
virtual void page_did_cancel_geolocation_position_request([[maybe_unused]] u64 request_id) { }
- virtual void page_did_start_geolocation_position_watch([[maybe_unused]] u64 request_id) { }
+ virtual void page_did_start_geolocation_position_watch([[maybe_unused]] u64 request_id, [[maybe_unused]] HTML::CrossProcessId requesting_document_id) { }
virtual void page_did_stop_geolocation_position_watch([[maybe_unused]] u64 request_id) { }
virtual void page_did_request_permission([[maybe_unused]] u64 request_id, [[maybe_unused]] HTML::CrossProcessId requesting_document_id, [[maybe_unused]] Utf16String const& name) { }
virtual Optional<Web::Permissions::PermissionSnapshot> page_did_set_permission_for_testing([[maybe_unused]] HTML::CrossProcessId document_id, [[maybe_unused]] String const& name, [[maybe_unused]] String const& state) { return {}; }
diff --git a/Libraries/LibWeb/PermissionsAPI/Permissions.cpp b/Libraries/LibWeb/PermissionsAPI/Permissions.cpp
index 1dbf7514a14..2f8e7f87c63 100644
--- a/Libraries/LibWeb/PermissionsAPI/Permissions.cpp
+++ b/Libraries/LibWeb/PermissionsAPI/Permissions.cpp
@@ -78,36 +78,6 @@ static void store_permission_decision(PermissionDescriptor const& descriptor, HT
}));
}
-// https://w3c.github.io/permissions/#dfn-request-permission-to-use
-PermissionState request_permission(PermissionDescriptor const& descriptor)
-{
- // 1. Let current state be the descriptor's permission state.
- auto current_state = permission_state(descriptor);
-
- // 2. If current state is not "prompt", return current state and abort these steps.
- if (current_state != PermissionState::Prompt)
- return current_state;
-
- // FIXME: 3. Ask the user for express permission for the calling algorithm to use the powerful feature described by descriptor.
-
- // 4. If the user gives express permission to use the powerful feature, set current state to "granted"; otherwise to "denied".
- // The user's interaction may provide new information about the user's intent for the origin.
- if (HTML::Window::in_test_mode()) {
- current_state = PermissionState::Granted;
- } else {
- current_state = PermissionState::Denied;
- }
-
- // 5. Let settings be the current settings object.
- // 6. Let key be the result of generating a permission key for descriptor with settings's top-level origin and settings's origin.
- // 7. Queue a task on the current settings object's responsible event loop to set a permission store entry with descriptor, key, and current state.
- // NB: store_permission_decision() carries out steps 6 and 7 for both request algorithms.
- store_permission_decision(descriptor, HTML::current_settings_object(), current_state);
-
- // 8. Return current state.
- return current_state;
-}
-
// https://w3c.github.io/permissions/#dfn-request-permission-to-use
void request_permission_with_user_prompt(PermissionDescriptor const& descriptor, HTML::EnvironmentSettingsObject& settings, PermissionDecisionCallback on_decided)
{
diff --git a/Libraries/LibWeb/PermissionsAPI/Permissions.h b/Libraries/LibWeb/PermissionsAPI/Permissions.h
index 186dd2fdf03..b56a0a4f24c 100644
--- a/Libraries/LibWeb/PermissionsAPI/Permissions.h
+++ b/Libraries/LibWeb/PermissionsAPI/Permissions.h
@@ -26,8 +26,6 @@ bool is_permission_supported(Utf16View);
PermissionState permission_state(PermissionDescriptor descriptor, Optional<HTML::EnvironmentSettingsObject&> settings = {});
-PermissionState request_permission(PermissionDescriptor const& descriptor);
-
// Asks the user when the state is "prompt"; the callback runs once a state other than "prompt" is known.
using PermissionDecisionCallback = GC::Ref<GC::Function<void(PermissionState)>>;
void request_permission_with_user_prompt(PermissionDescriptor const&, HTML::EnvironmentSettingsObject&, PermissionDecisionCallback);
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index 67ac0875b5b..990d671539f 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -3048,11 +3048,11 @@ void ViewImplementation::initialize_tab(Web::HTML::VisibilityState system_visibi
VERIFY_NOT_REACHED();
};
- auto make_geolocation_success_handler = [this](WebContentPage& request_page, u64 request_id, bool is_watch) {
+ auto make_geolocation_success_handler = [this](WebContentPage& request_page, u64 request_id, Web::HTML::CrossProcessId document_id, bool is_watch) {
auto weak_this = make_weak_ptr();
auto key = GeolocationRequestKey { request_page, request_id };
- return [weak_this, request_page = NonnullRefPtr<WebContentPage>(request_page), request_id, key, is_watch](Core::GeolocationCoordinates coords) {
+ return [weak_this, request_page = NonnullRefPtr<WebContentPage>(request_page), request_id, document_id, key, is_watch](Core::GeolocationCoordinates coords) {
auto* view = weak_this.ptr();
if (!view || !request_page->is_open())
return;
@@ -3064,7 +3064,8 @@ void ViewImplementation::initialize_tab(Web::HTML::VisibilityState system_visibi
return;
}
- if (!Application::the().sites_can_ask_for_location()) {
+ // The permission is checked again for every position, since the user may have taken it away meanwhile.
+ if (!Application::the().sites_can_ask_for_location() || !request_page->document_may_use(document_id, Web::Permissions::PermissionName::Geolocation)) {
if (auto provider_watch_id = view->m_geolocation_watch_ids.take(key); provider_watch_id.has_value())
Application::the().stop_watching_geolocation_position(*provider_watch_id);
request_page->async_geolocation_position_response(request_id, {}, to_underlying(GeolocationErrorCode::PermissionDenied));
@@ -3103,7 +3104,7 @@ void ViewImplementation::initialize_tab(Web::HTML::VisibilityState system_visibi
};
};
- on_request_geolocation_position = [this, geolocation_error_code, make_geolocation_success_handler, make_geolocation_error_handler](WebContentPage& requesting_page, u64 request_id) {
+ on_request_geolocation_position = [this, geolocation_error_code, make_geolocation_success_handler, make_geolocation_error_handler](WebContentPage& requesting_page, u64 request_id, Web::HTML::CrossProcessId document_id) {
if (!Application::the().sites_can_ask_for_location()) {
requesting_page.async_geolocation_position_response(request_id, {}, to_underlying(GeolocationErrorCode::PermissionDenied));
return;
@@ -3113,7 +3114,7 @@ void ViewImplementation::initialize_tab(Web::HTML::VisibilityState system_visibi
Application::the().cancel_geolocation_position_request(*previous_provider_request_id);
auto provider_request_id = Application::the().request_geolocation_position(
- make_geolocation_success_handler(requesting_page, request_id, false),
+ make_geolocation_success_handler(requesting_page, request_id, document_id, false),
make_geolocation_error_handler(requesting_page, request_id, false));
if (provider_request_id.is_error()) {
requesting_page.async_geolocation_position_response(request_id, {}, to_underlying(geolocation_error_code(provider_request_id.error())));
@@ -3129,7 +3130,7 @@ void ViewImplementation::initialize_tab(Web::HTML::VisibilityState system_visibi
Application::the().cancel_geolocation_position_request(*provider_request_id);
};
- on_start_geolocation_position_watch = [this, geolocation_error_code, make_geolocation_success_handler, make_geolocation_error_handler](WebContentPage& requesting_page, u64 request_id) {
+ on_start_geolocation_position_watch = [this, geolocation_error_code, make_geolocation_success_handler, make_geolocation_error_handler](WebContentPage& requesting_page, u64 request_id, Web::HTML::CrossProcessId document_id) {
if (!Application::the().sites_can_ask_for_location()) {
requesting_page.async_geolocation_position_response(request_id, {}, to_underlying(GeolocationErrorCode::PermissionDenied));
return;
@@ -3139,7 +3140,7 @@ void ViewImplementation::initialize_tab(Web::HTML::VisibilityState system_visibi
Application::the().stop_watching_geolocation_position(*previous_provider_watch_id);
auto provider_watch_id = Application::the().start_watching_geolocation_position(
- make_geolocation_success_handler(requesting_page, request_id, true),
+ make_geolocation_success_handler(requesting_page, request_id, document_id, true),
make_geolocation_error_handler(requesting_page, request_id, true));
if (provider_watch_id.is_error()) {
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index f00048fce6e..b957b421c57 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -594,9 +594,9 @@ public:
Function<void()> on_fullscreen_window;
Function<void()> on_exit_fullscreen_window;
Function<void(Color current_color)> on_request_color_picker;
- Function<void(WebContentPage& requesting_page, u64 request_id)> on_request_geolocation_position;
+ Function<void(WebContentPage& requesting_page, u64 request_id, Web::HTML::CrossProcessId document_id)> on_request_geolocation_position;
Function<void(WebContentPage& requesting_page, u64 request_id)> on_cancel_geolocation_position_request;
- Function<void(WebContentPage& requesting_page, u64 request_id)> on_start_geolocation_position_watch;
+ Function<void(WebContentPage& requesting_page, u64 request_id, Web::HTML::CrossProcessId document_id)> on_start_geolocation_position_watch;
Function<void(WebContentPage& requesting_page, u64 request_id)> on_stop_geolocation_position_watch;
Function<void(PermissionPromptRequest const&, PermissionPromptCallback)> on_request_permission;
Function<void(PermissionPromptRequest const&)> on_cancel_permission_prompt;
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 3162d75abf8..c0180adeec5 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -1599,6 +1599,26 @@ void WebContentPage::did_request_color_picker(Color current_color)
view().did_request_color_picker({}, *this, current_color);
}
+static Optional<Web::Permissions::PermissionName> permission_name_from(Utf16String const& name)
+{
+ if (name == "geolocation"_utf16)
+ return Web::Permissions::PermissionName::Geolocation;
+ if (name == "microphone"_utf16)
+ return Web::Permissions::PermissionName::Microphone;
+ return {};
+}
+
+static Web::PermissionsPolicy::PolicyControlledFeature policy_controlled_feature_for(Web::Permissions::PermissionName name)
+{
+ switch (name) {
+ case Web::Permissions::PermissionName::Geolocation:
+ return Web::PermissionsPolicy::PolicyControlledFeature::Geolocation;
+ case Web::Permissions::PermissionName::Microphone:
+ return Web::PermissionsPolicy::PolicyControlledFeature::Microphone;
+ }
+ VERIFY_NOT_REACHED();
+}
+
bool WebContentPage::hosts_a_document_allowed_to_use(Web::PermissionsPolicy::PolicyControlledFeature feature) const
{
auto is_allowed = false;
@@ -1613,34 +1633,30 @@ bool WebContentPage::hosts_a_document_allowed_to_use(Web::PermissionsPolicy::Pol
return is_allowed;
}
-void WebContentPage::did_request_geolocation_position(u64 request_id)
+bool WebContentPage::document_may_use(Web::HTML::CrossProcessId document_id, Web::Permissions::PermissionName name) const
{
- if (!hosts_a_document_allowed_to_use(Web::PermissionsPolicy::PolicyControlledFeature::Geolocation)) {
- async_geolocation_position_response(request_id, {}, to_underlying(Web::Geolocation::GeolocationPositionErrorCode::PermissionDenied));
- return;
- }
- if (view().on_request_geolocation_position)
- view().on_request_geolocation_position(*this, request_id);
-}
-
-static Optional<Web::Permissions::PermissionName> permission_name_from(Utf16String const& name)
-{
- if (name == "geolocation"_utf16)
- return Web::Permissions::PermissionName::Geolocation;
- if (name == "microphone"_utf16)
- return Web::Permissions::PermissionName::Microphone;
- return {};
+ auto document = hosted_document_with_id(document_id);
+ if (!document)
+ return false;
+ auto top_level_origin = document->top_level_origin();
+ if (!top_level_origin.has_value())
+ return false;
+ if (Web::PermissionsPolicy::check_permissions_policy(document->permissions_policy(), policy_controlled_feature_for(name), document->origin(), document->origin()) != Web::PermissionsPolicy::FeatureValue::Enabled)
+ 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;
}
-static Web::PermissionsPolicy::PolicyControlledFeature policy_controlled_feature_for(Web::Permissions::PermissionName name)
+void WebContentPage::did_request_geolocation_position(u64 request_id, Web::HTML::CrossProcessId document_id)
{
- switch (name) {
- case Web::Permissions::PermissionName::Geolocation:
- return Web::PermissionsPolicy::PolicyControlledFeature::Geolocation;
- case Web::Permissions::PermissionName::Microphone:
- return Web::PermissionsPolicy::PolicyControlledFeature::Microphone;
+ // The UI process hands out a position only to a document it knows the user let have one.
+ if (!document_may_use(document_id, Web::Permissions::PermissionName::Geolocation)) {
+ async_geolocation_position_response(request_id, {}, to_underlying(Web::Geolocation::GeolocationPositionErrorCode::PermissionDenied));
+ return;
}
- VERIFY_NOT_REACHED();
+ if (view().on_request_geolocation_position)
+ view().on_request_geolocation_position(*this, request_id, document_id);
}
RefPtr<CanonicalDocument> WebContentPage::hosted_document_with_id(Web::HTML::CrossProcessId document_id) const
@@ -1717,6 +1733,8 @@ void WebContentPage::did_request_permission(u64 request_id, Web::HTML::CrossProc
state = Web::Permissions::EffectiveState::Granted;
else if (outcome == Web::Permissions::PromptOutcome::Block)
state = Web::Permissions::EffectiveState::Denied;
+ if (state == Web::Permissions::EffectiveState::Granted)
+ page->m_file_permission_grants.append({ document_id, permission_name });
if (state == Web::Permissions::EffectiveState::Granted && permission_name == Web::Permissions::PermissionName::Microphone) {
if (auto result = page->client().grant_audio_capture_to_file(page->view().url()); result.is_error())
warnln("Unable to grant audio capture: {}", result.error());
@@ -1800,10 +1818,14 @@ void WebContentPage::did_cancel_geolocation_position_request(u64 request_id)
view().on_cancel_geolocation_position_request(*this, request_id);
}
-void WebContentPage::did_start_geolocation_position_watch(u64 request_id)
+void WebContentPage::did_start_geolocation_position_watch(u64 request_id, Web::HTML::CrossProcessId document_id)
{
+ if (!document_may_use(document_id, Web::Permissions::PermissionName::Geolocation)) {
+ async_geolocation_position_response(request_id, {}, to_underlying(Web::Geolocation::GeolocationPositionErrorCode::PermissionDenied));
+ return;
+ }
if (view().on_start_geolocation_position_watch)
- view().on_start_geolocation_position_watch(*this, request_id);
+ view().on_start_geolocation_position_watch(*this, request_id, document_id);
}
void WebContentPage::did_stop_geolocation_position_watch(u64 request_id)
diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.h
index b785f694e1f..3fd974b0d1f 100644
--- a/Libraries/LibWebView/WebContentPage.h
+++ b/Libraries/LibWebView/WebContentPage.h
@@ -119,6 +119,7 @@ public:
// Whether a document this page hosts may use the feature. The process hosting the page can act for any of them.
bool hosts_a_document_allowed_to_use(Web::PermissionsPolicy::PolicyControlledFeature) const;
RefPtr<CanonicalDocument> hosted_document_with_id(Web::HTML::CrossProcessId) const;
+ bool document_may_use(Web::HTML::CrossProcessId, Web::Permissions::PermissionName) const;
Web::Permissions::PermissionSnapshot set_permission_for_testing(Web::HTML::CrossProcessId document_id, String const& name, String const& state);
void set_next_permission_prompt_outcome_for_testing(String const& name, String const& outcome, bool held);
@@ -271,9 +272,9 @@ private:
virtual void did_change_picture_in_picture_player_controls_visibility(bool shown) override;
virtual void did_request_file(ByteString path, i32 request_id) override;
virtual void did_request_color_picker(Color current_color) override;
- virtual void did_request_geolocation_position(u64 request_id) override;
+ virtual void did_request_geolocation_position(u64 request_id, Web::HTML::CrossProcessId document_id) override;
virtual void did_cancel_geolocation_position_request(u64 request_id) override;
- virtual void did_start_geolocation_position_watch(u64 request_id) override;
+ 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_file_picker(Web::HTML::FileFilter accepted_file_types, Web::HTML::AllowMultipleFiles allow_multiple_files) override;
@@ -406,6 +407,13 @@ private:
HashTable<u64> m_renderer_owned_downloads;
HashTable<OwedReply> m_owed_replies;
Optional<MonotonicTime> m_last_user_activation;
+
+ // The grants a file: page's documents got, which have no origin to be kept for.
+ struct FilePermissionGrant {
+ Web::HTML::CrossProcessId document_id;
+ Web::Permissions::PermissionName name;
+ };
+ Vector<FilePermissionGrant> m_file_permission_grants;
};
}
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index 53863728272..20200839bb2 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -1301,9 +1301,9 @@ void PageClient::geolocation_position_response(u64 request_id, WebView::Geolocat
page().receive_geolocation_position(request_id, Web::Geolocation::GeolocationPositionError::ErrorCode::PositionUnavailable);
}
-void PageClient::page_did_request_geolocation_position(u64 request_id)
+void PageClient::page_did_request_geolocation_position(u64 request_id, Web::HTML::CrossProcessId requesting_document_id)
{
- client().async_did_request_geolocation_position(m_id, request_id);
+ client().async_did_request_geolocation_position(m_id, request_id, requesting_document_id);
}
void PageClient::page_did_request_permission(u64 request_id, Web::HTML::CrossProcessId requesting_document_id, Utf16String const& name)
@@ -1355,9 +1355,9 @@ void PageClient::page_did_cancel_geolocation_position_request(u64 request_id)
client().async_did_cancel_geolocation_position_request(m_id, request_id);
}
-void PageClient::page_did_start_geolocation_position_watch(u64 request_id)
+void PageClient::page_did_start_geolocation_position_watch(u64 request_id, Web::HTML::CrossProcessId requesting_document_id)
{
- client().async_did_start_geolocation_position_watch(m_id, request_id);
+ client().async_did_start_geolocation_position_watch(m_id, request_id, requesting_document_id);
}
void PageClient::page_did_stop_geolocation_position_watch(u64 request_id)
diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.h
index 0150f75f5d9..5374aeddeba 100644
--- a/Services/WebContent/PageClient.h
+++ b/Services/WebContent/PageClient.h
@@ -346,9 +346,9 @@ private:
virtual void page_did_request_color_picker(Color current_color) override;
virtual void page_did_request_file_picker(Web::HTML::FileFilter const& accepted_file_types, Web::HTML::AllowMultipleFiles) override;
virtual void page_did_request_select_dropdown(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint content_position, Web::CSSPixels minimum_width, Vector<Web::HTML::SelectItem> items) override;
- virtual void page_did_request_geolocation_position(u64 request_id) override;
+ virtual void page_did_request_geolocation_position(u64 request_id, Web::HTML::CrossProcessId requesting_document_id) override;
virtual void page_did_cancel_geolocation_position_request(u64 request_id) override;
- virtual void page_did_start_geolocation_position_watch(u64 request_id) override;
+ virtual void page_did_start_geolocation_position_watch(u64 request_id, Web::HTML::CrossProcessId requesting_document_id) override;
virtual void page_did_stop_geolocation_position_watch(u64 request_id) override;
virtual void page_did_request_permission(u64 request_id, Web::HTML::CrossProcessId requesting_document_id, Utf16String const& name) override;
virtual Optional<Web::Permissions::PermissionSnapshot> page_did_set_permission_for_testing(Web::HTML::CrossProcessId document_id, String const& name, String const& state) override;
diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipc
index 6e86e529172..cc80d50b465 100644
--- a/Services/WebContent/WebContentClient.ipc
+++ b/Services/WebContent/WebContentClient.ipc
@@ -234,9 +234,9 @@ endpoint WebContentClient routed by Web::PageId page_id
did_change_picture_in_picture_player_controls_visibility(Web::PageId page_id, bool shown) =|
did_request_file(Web::PageId page_id, ByteString path, i32 request_id) =|
did_request_color_picker(Web::PageId page_id, Color current_color) =|
- did_request_geolocation_position(Web::PageId page_id, u64 request_id) =|
+ did_request_geolocation_position(Web::PageId page_id, u64 request_id, Web::HTML::CrossProcessId document_id) =|
did_cancel_geolocation_position_request(Web::PageId page_id, u64 request_id) =|
- did_start_geolocation_position_watch(Web::PageId page_id, u64 request_id) =|
+ did_start_geolocation_position_watch(Web::PageId page_id, u64 request_id, Web::HTML::CrossProcessId document_id) =|
did_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) =|
did_request_file_picker(Web::PageId page_id, Web::HTML::FileFilter accepted_file_types, Web::HTML::AllowMultipleFiles allow_multiple_files) =|
diff --git a/Tests/LibWeb/Text/expected/PermissionsAPI/geolocation-iframe-without-allow-is-denied-silently.txt b/Tests/LibWeb/Text/expected/PermissionsAPI/geolocation-iframe-without-allow-is-denied-silently.txt
new file mode 100644
index 00000000000..8ea817aadbb
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/PermissionsAPI/geolocation-iframe-without-allow-is-denied-silently.txt
@@ -0,0 +1,4 @@
+frame 1: error 1
+frame 2: error 1
+frame 3: error 1
+top level: error 1, query prompt
diff --git a/Tests/LibWeb/Text/expected/PermissionsAPI/geolocation-prompt-answers.txt b/Tests/LibWeb/Text/expected/PermissionsAPI/geolocation-prompt-answers.txt
new file mode 100644
index 00000000000..be8d3770d7f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/PermissionsAPI/geolocation-prompt-answers.txt
@@ -0,0 +1,4 @@
+dismissed: error 1, query prompt
+blocked: error 1, query denied
+asked again: error 1
+allowed: position 1.25, 2.5, query granted
diff --git a/Tests/LibWeb/Text/input/PermissionsAPI/geolocation-iframe-without-allow-is-denied-silently.html b/Tests/LibWeb/Text/input/PermissionsAPI/geolocation-iframe-without-allow-is-denied-silently.html
new file mode 100644
index 00000000000..cc934955439
--- /dev/null
+++ b/Tests/LibWeb/Text/input/PermissionsAPI/geolocation-iframe-without-allow-is-denied-silently.html
@@ -0,0 +1,32 @@
+<!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", "*"),
+ error => parent.postMessage(\`error \${error.code}\`, "*"));
+ <\/script>`;
+
+ // Each refused request would count as a dismissal if it reached a prompt, and three would embargo the site.
+ internals.setNextPermissionPromptOutcome("geolocation", "dismiss");
+ for (let i = 1; i <= 3; ++i) {
+ const iframe = document.createElement("iframe");
+ iframe.setAttribute("allow", "geolocation 'none'");
+ iframe.srcdoc = childSource;
+ const result = await new Promise(resolve => {
+ addEventListener("message", event => resolve(event.data), { once: true });
+ document.body.appendChild(iframe);
+ });
+ println(`frame ${i}: ${result}`);
+ iframe.remove();
+ }
+
+ // The dismissal set up above was never used, so the site's own request gets it.
+ const own = await new Promise(resolve => navigator.geolocation.getCurrentPosition(
+ () => resolve("position"), error => resolve(`error ${error.code}`)));
+ println(`top level: ${own}, query ${(await navigator.permissions.query({ name: "geolocation" })).state}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/PermissionsAPI/geolocation-prompt-answers.html b/Tests/LibWeb/Text/input/PermissionsAPI/geolocation-prompt-answers.html
new file mode 100644
index 00000000000..9db49fb1b9d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/PermissionsAPI/geolocation-prompt-answers.html
@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ internals.setGeolocationEmulatedPosition(1.25, 2.5, 100);
+ const position = () => new Promise(resolve => navigator.geolocation.getCurrentPosition(
+ position => resolve(`position ${position.coords.latitude}, ${position.coords.longitude}`),
+ error => resolve(`error ${error.code}`)));
+ const query = async () => (await navigator.permissions.query({ name: "geolocation" })).state;
+
+ internals.setNextPermissionPromptOutcome("geolocation", "dismiss");
+ println(`dismissed: ${await position()}, query ${await query()}`);
+
+ internals.setNextPermissionPromptOutcome("geolocation", "block");
+ println(`blocked: ${await position()}, query ${await query()}`);
+
+ // A blocked site isn't asked again, so this answer goes unused.
+ internals.setNextPermissionPromptOutcome("geolocation", "allow");
+ println(`asked again: ${await position()}`);
+
+ await internals.setPermission("geolocation", "prompt");
+ println(`allowed: ${await position()}, query ${await query()}`);
+ done();
+ });
+</script>