Skip to content

Truly independent web browser

LibWeb+LibWebView: Revoke live access when a permission is taken away

sideshowbarkere1bf97ec50de
diff --git a/Libraries/LibWeb/HTML/Navigator.h b/Libraries/LibWeb/HTML/Navigator.h
index 1b577412b24..bf12037006d 100644
--- a/Libraries/LibWeb/HTML/Navigator.h
+++ b/Libraries/LibWeb/HTML/Navigator.h
@@ -79,6 +79,7 @@ public:
GC::Ref<MediaCapabilitiesAPI::MediaCapabilities> media_capabilities();
GC::Ref<MediaCapture::MediaDevices> media_devices();
+ GC::Ptr<MediaCapture::MediaDevices> media_devices_if_created() const { return m_media_devices; }
static WebIDL::Long max_touch_points();
diff --git a/Libraries/LibWeb/HTML/Window.h b/Libraries/LibWeb/HTML/Window.h
index bb70d13fa73..6ce69f476d3 100644
--- a/Libraries/LibWeb/HTML/Window.h
+++ b/Libraries/LibWeb/HTML/Window.h
@@ -215,6 +215,7 @@ public:
WebIDL::ExceptionOr<GC::Ptr<WindowProxy>> open(Optional<Utf16String> const& url, Optional<Utf16String> const& target, Optional<Utf16String> const& features);
[[nodiscard]] GC::Ref<Navigator> navigator();
+ GC::Ptr<Navigator> navigator_if_created() const { return m_navigator; }
[[nodiscard]] GC::Ref<CloseWatcherManager> close_watcher_manager();
[[nodiscard]] GC::Ref<CookieStore::CookieStore> cookie_store();
[[nodiscard]] GC::Ref<Speech::SpeechSynthesis> speech_synthesis();
diff --git a/Libraries/LibWeb/MediaCapture/MediaDevices.cpp b/Libraries/LibWeb/MediaCapture/MediaDevices.cpp
index da462daefb5..a543baaddd2 100644
--- a/Libraries/LibWeb/MediaCapture/MediaDevices.cpp
+++ b/Libraries/LibWeb/MediaCapture/MediaDevices.cpp
@@ -823,6 +823,26 @@ void MediaDevices::end_tracks_of_removed_devices()
end_tracks(move(tracks_to_end));
}
+// https://w3c.github.io/mediacapture-main/#dfn-device-permission-revocation-algorithm
+void MediaDevices::end_tracks_without_permission()
+{
+ // NB: A track's source is the microphone, the one device Ladybird captures from, so every live track ends once the
+ // document may no longer use it.
+ auto& settings = HTML::relevant_settings_object(*m_window);
+ if (PermissionsAPI::permission_state(PermissionsAPI::PermissionDescriptor { "microphone"_utf16 }, settings) == PermissionsAPI::PermissionState::Granted)
+ return;
+
+ GC::RootVector<GC::Ref<MediaStreamTrack>> tracks_to_end;
+ m_media_stream_track_sources.remove_all_matching([&](GC::Weak<MediaStreamTrack> const& weak_track) {
+ auto track = weak_track.ptr();
+ if (!track || track->track_ready_state() == MediaStreamTrackState::Ended)
+ return true;
+ tracks_to_end.append(*track);
+ return true;
+ });
+ end_tracks(move(tracks_to_end));
+}
+
void MediaDevices::end_tracks(GC::RootVector<GC::Ref<MediaStreamTrack>> tracks_to_end)
{
if (tracks_to_end.is_empty())
diff --git a/Libraries/LibWeb/MediaCapture/MediaDevices.h b/Libraries/LibWeb/MediaCapture/MediaDevices.h
index 54b52ce8c3a..ebc9dc56bd8 100644
--- a/Libraries/LibWeb/MediaCapture/MediaDevices.h
+++ b/Libraries/LibWeb/MediaCapture/MediaDevices.h
@@ -36,6 +36,9 @@ class MediaDevices final : public DOM::EventTarget {
GC_DECLARE_ALLOCATOR(MediaDevices);
public:
+ // Ends every live track once the document may no longer use the microphone.
+ void end_tracks_without_permission();
+
[[nodiscard]] static GC::Ref<MediaDevices> create(HTML::Window&);
MediaTrackSupportedConstraints get_supported_constraints();
diff --git a/Libraries/LibWeb/PermissionsAPI/PermissionStore.cpp b/Libraries/LibWeb/PermissionsAPI/PermissionStore.cpp
index 238d6680c3f..529a87bd097 100644
--- a/Libraries/LibWeb/PermissionsAPI/PermissionStore.cpp
+++ b/Libraries/LibWeb/PermissionsAPI/PermissionStore.cpp
@@ -5,6 +5,11 @@
*/
#include <LibGC/Root.h>
+#include <LibWeb/DOM/Document.h>
+#include <LibWeb/HTML/LocalNavigable.h>
+#include <LibWeb/HTML/Navigator.h>
+#include <LibWeb/HTML/Window.h>
+#include <LibWeb/MediaCapture/MediaDevices.h>
#include <LibWeb/PermissionsAPI/PermissionStatus.h>
#include <LibWeb/PermissionsAPI/PermissionStore.h>
@@ -92,6 +97,17 @@ void PermissionStore::apply_snapshot(Web::Permissions::PermissionSnapshot snapsh
}
for (auto const& status : statuses)
status->update_from_store();
+
+ // A document whose microphone permission went away loses its live microphone tracks.
+ for (auto navigable : HTML::all_local_navigables()) {
+ auto document = navigable->active_document();
+ if (!document || !document->window())
+ continue;
+ if (auto navigator = document->window()->navigator_if_created(); navigator) {
+ if (auto media_devices = navigator->media_devices_if_created())
+ media_devices->end_tracks_without_permission();
+ }
+ }
}
void PermissionStore::register_status(PermissionStatus& status)
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 47a8f5484ba..8ab4eee0e2b 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -2968,6 +2968,18 @@ bool Application::sites_can_ask_for_location() const
void Application::permissions_changed(Badge<ApplicationPermissionManagerObserver>, PermissionManager const& manager)
{
+ // A revoked site loses what it was using before its pages hear of the change.
+ ViewImplementation::for_each_view([&](ViewImplementation& view) {
+ if (view.session().permission_manager.ptr() == &manager)
+ view.revoke_geolocation_without_permission();
+ return IterationDecision::Continue;
+ });
+ WebContentClient::for_each_client([&](WebContentClient& client) {
+ if (client.session().permission_manager.ptr() == &manager)
+ client.update_audio_capture_permission();
+ return IterationDecision::Continue;
+ });
+
auto snapshot = manager.snapshot();
WebContentClient::for_each_client([&](WebContentClient& client) {
if (client.session().permission_manager.ptr() == &manager)
@@ -3014,6 +3026,22 @@ void Application::location_policy_changed(Badge<ApplicationSettingsObserver>)
update_permission_defaults();
}
+void Application::update_os_location_authorization()
+{
+ auto denied = m_geolocation_provider && m_geolocation_provider->authorization() == Core::GeolocationAuthorization::Denied;
+ m_default_session->permission_manager->set_os_denies_geolocation(denied);
+}
+
+Core::GeolocationAuthorization Application::geolocation_authorization()
+{
+ if (!sites_can_ask_for_location())
+ return Core::GeolocationAuthorization::Unknown;
+ auto provider = ensure_geolocation_provider();
+ if (provider.is_error())
+ return Core::GeolocationAuthorization::Unknown;
+ return provider.value()->authorization();
+}
+
void Application::update_permission_defaults()
{
// NB: A private session reads its defaults from the ordinary one.
@@ -3230,8 +3258,11 @@ void Application::set_spell_checker(OwnPtr<SpellChecker> spell_checker)
ErrorOr<NonnullRawPtr<Core::GeolocationProvider>> Application::ensure_geolocation_provider()
{
- if (!m_geolocation_provider)
+ if (!m_geolocation_provider) {
m_geolocation_provider = TRY(Core::GeolocationProvider::create());
+ m_geolocation_provider->on_authorization_change = [this] { update_os_location_authorization(); };
+ update_os_location_authorization();
+ }
return NonnullRawPtr { *m_geolocation_provider };
}
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index 2d4cd0916d9..5a4f9fffbf1 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -173,6 +173,7 @@ public:
// True unless the user has turned location off, or there's no way to find it.
bool sites_can_ask_for_location() const;
+ Core::GeolocationAuthorization geolocation_authorization();
void location_policy_changed(Badge<ApplicationSettingsObserver>);
void permissions_changed(Badge<ApplicationPermissionManagerObserver>, PermissionManager const&);
@@ -493,6 +494,7 @@ private:
void finish_current_content_blocker_list_update();
ErrorOr<NonnullRawPtr<Core::GeolocationProvider>> ensure_geolocation_provider();
void update_permission_defaults();
+ void update_os_location_authorization();
void schedule_permission_expiry();
void initialize_actions();
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index 990d671539f..4737ee73c9f 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -3122,6 +3122,7 @@ void ViewImplementation::initialize_tab(Web::HTML::VisibilityState system_visibi
}
m_geolocation_position_request_ids.set({ requesting_page, request_id }, provider_request_id.release_value());
+ m_geolocation_request_documents.set({ requesting_page, request_id }, document_id);
};
on_cancel_geolocation_position_request = [this](WebContentPage& requesting_page, u64 request_id) {
@@ -3149,6 +3150,7 @@ void ViewImplementation::initialize_tab(Web::HTML::VisibilityState system_visibi
}
m_geolocation_watch_ids.set({ requesting_page, request_id }, provider_watch_id.release_value());
+ m_geolocation_request_documents.set({ requesting_page, request_id }, document_id);
};
on_stop_geolocation_position_watch = [this](WebContentPage& requesting_page, u64 request_id) {
@@ -4314,6 +4316,30 @@ void ViewImplementation::global_privacy_control_changed()
traversable().for_each_hosting_page(send_global_privacy_control);
}
+void ViewImplementation::revoke_geolocation_without_permission()
+{
+ using ErrorCode = Web::Geolocation::GeolocationPositionErrorCode;
+
+ m_geolocation_request_documents.remove_all_matching([&](auto const& key, auto const&) {
+ return !m_geolocation_position_request_ids.contains(key) && !m_geolocation_watch_ids.contains(key);
+ });
+
+ Vector<GeolocationRequestKey> revoked;
+ for (auto const& it : m_geolocation_request_documents) {
+ if (!it.key.page->document_may_use(it.value, Web::Permissions::PermissionName::Geolocation))
+ revoked.append(it.key);
+ }
+
+ for (auto const& key : revoked) {
+ m_geolocation_request_documents.remove(key);
+ if (auto provider_request_id = m_geolocation_position_request_ids.take(key); provider_request_id.has_value())
+ Application::the().cancel_geolocation_position_request(*provider_request_id);
+ if (auto provider_watch_id = m_geolocation_watch_ids.take(key); provider_watch_id.has_value())
+ Application::the().stop_watching_geolocation_position(*provider_watch_id);
+ key.page->async_geolocation_position_response(key.request_id, {}, to_underlying(ErrorCode::PermissionDenied));
+ }
+}
+
void ViewImplementation::geolocation_settings_changed()
{
using ErrorCode = Web::Geolocation::GeolocationPositionErrorCode;
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index b957b421c57..891ac044f0a 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -134,6 +134,9 @@ public:
void release_held_permission_prompt_for_testing(u32 waiters);
void reset_permission_prompts_for_testing();
+ // Ends every location request and watch whose document the user has since taken the permission away from.
+ void revoke_geolocation_without_permission();
+
u64 view_id() const { return m_view_id; }
pid_t web_content_pid() const;
@@ -1125,6 +1128,7 @@ protected:
HashMap<GeolocationRequestKey, Core::GeolocationProvider::RequestId> m_geolocation_position_request_ids;
HashMap<GeolocationRequestKey, Core::GeolocationProvider::WatchId> m_geolocation_watch_ids;
+ HashMap<GeolocationRequestKey, Web::HTML::CrossProcessId> m_geolocation_request_documents;
struct PendingPermissionPrompt {
PermissionPromptRequest request;
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index c0180adeec5..a7a66e8a316 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -1721,7 +1721,9 @@ void WebContentPage::did_request_permission(u64 request_id, Web::HTML::CrossProc
}
auto generation_when_asked = manager.decision_generation(*permission_name, *top_level_origin);
- PermissionPromptRequest request { *permission_name, *top_level_origin, is_file };
+ // 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;
+ PermissionPromptRequest request { *permission_name, *top_level_origin, is_file, show_os_hint };
view().request_permission_prompt(move(request), [page = NonnullRefPtr(*this), request_id, document_id, permission_name = *permission_name, top_level_origin = *top_level_origin, is_file, generation_when_asked](Web::Permissions::PromptOutcome outcome) {
if (!page->is_open())
diff --git a/Tests/LibWeb/Text/expected/PermissionsAPI/geolocation-watch-ends-on-revoke.txt b/Tests/LibWeb/Text/expected/PermissionsAPI/geolocation-watch-ends-on-revoke.txt
new file mode 100644
index 00000000000..08e595e8a48
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/PermissionsAPI/geolocation-watch-ends-on-revoke.txt
@@ -0,0 +1 @@
+position 1, error 1
diff --git a/Tests/LibWeb/Text/input/PermissionsAPI/geolocation-watch-ends-on-revoke.html b/Tests/LibWeb/Text/input/PermissionsAPI/geolocation-watch-ends-on-revoke.html
new file mode 100644
index 00000000000..d3c78269e33
--- /dev/null
+++ b/Tests/LibWeb/Text/input/PermissionsAPI/geolocation-watch-ends-on-revoke.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ internals.setGeolocationEmulatedPosition(1, 1, 100);
+ const results = [];
+ let wake = null;
+ const record = result => {
+ results.push(result);
+ if (wake)
+ wake();
+ };
+ const next = () => new Promise(resolve => wake = resolve);
+
+ navigator.geolocation.watchPosition(
+ position => record(`position ${position.coords.latitude}`),
+ error => record(`error ${error.code}`));
+ await next();
+
+ await internals.setPermission("geolocation", "denied");
+ internals.setGeolocationEmulatedPosition(2, 2, 100);
+ await next();
+ // A watch that ended doesn't hear of later positions.
+ internals.setGeolocationEmulatedPosition(3, 3, 100);
+ await new Promise(resolve => setTimeout(resolve, 50));
+
+ println(results.join(", "));
+ done();
+ });
+</script>