Skip to content

Commit c8a69a8

Browse files
mbeckley17dekanbro
authored andcommitted
desktop: enable getUserMedia in the Linux WebKitGTK webview (block#3607)
Microphone/camera capture works on macOS (WKWebView) and Windows (WebView2) but fails on Linux with `NotAllowedError`. WebKitGTK ships with `enable-media-stream` off and a default `permission-request` handler that denies every request. This reaches the underlying `webkit2gtk::WebView` from `on_webview_ready` and enables `enable-media-stream`, then installs a **deny-by-default** `permission-request` handler: a `UserMedia` request is allowed only from a trusted app origin (`tauri://localhost` in prod, the Vite dev origin in debug) **and** when it targets an audio/video device — everything else is denied. No-op on macOS/Windows. - `webkit2gtk` is pinned to the version wry already uses (`=2.0.2`) so there's a single shared copy of the native binding. --------- Signed-off-by: Beckley <[email protected]> (cherry picked from commit c9aa555) Signed-off-by: Dekan Brown <[email protected]>
1 parent d0a24bc commit c8a69a8

4 files changed

Lines changed: 152 additions & 0 deletions

File tree

‎desktop/src-tauri/Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎desktop/src-tauri/Cargo.toml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ keyring = { version = "3.6.3", default-features = false, features = ["sync-secre
4040
# connection is dropped, which the plugin does immediately. Default features
4141
# keep the pure-Rust zbus backend, matching the plugin (no libdbus needed).
4242
notify-rust = "4"
43+
# Enable getUserMedia in the WebKitGTK webview (see src/linux_media.rs). Pinned
44+
# to the exact version wry links so both resolve to one webkit2gtk-sys and we
45+
# don't get duplicate symbols; bump in lockstep with wry.
46+
webkit2gtk = { version = "=2.0.2", features = ["v2_22"] }
4347

4448
[target.'cfg(target_os = "macos")'.dependencies]
4549
objc2 = { version = "0.6.4", default-features = false }

‎desktop/src-tauri/src/lib.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod deep_link;
77
mod event_sync;
88
mod events;
99
mod huddle;
10+
mod linux_media;
1011
mod managed_agents;
1112
mod media_proxy;
1213
#[cfg(feature = "mesh-llm")]
@@ -199,6 +200,11 @@ pub fn run() {
199200
return;
200201
}
201202

203+
// Linux/WebKitGTK needs media-stream settings and a
204+
// permission-request handler for getUserMedia; no-op
205+
// on macOS/Windows.
206+
linux_media::enable_media_capture(&webview);
207+
202208
// macOS applies the restored geometry asynchronously. Wait
203209
// for several identical outer bounds and for React to
204210
// commit the startup surface before revealing it.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
//! Linux-only: enable media capture (`getUserMedia`) in the WebKitGTK webview.
2+
//!
3+
//! On macOS (WKWebView) and Windows (WebView2) the media-permission prompt is
4+
//! routed to the OS automatically, so microphone/camera capture "just works".
5+
//! WebKitGTK is different on two counts, and both must be handled or capture
6+
//! fails on Linux only:
7+
//!
8+
//! * `enable-media-stream` is **off by default**, so `navigator.mediaDevices`
9+
//! never exposes a working `getUserMedia`; and
10+
//! * the default `permission-request` handler **denies every request**, so even
11+
//! with media-stream on, the call rejects with `NotAllowedError`.
12+
//!
13+
//! This module reaches the underlying `webkit2gtk::WebView` via
14+
//! [`tauri::Webview::with_webview`], enables media-stream, and installs a
15+
//! `permission-request` handler that is **deny-by-default**: a `UserMedia`
16+
//! request is allowed only when it comes from a trusted app origin and asks for
17+
//! an audio and/or video device. Tauri does not restrict navigation by default,
18+
//! so without the origin check any document that ended up in this webview would
19+
//! inherit silent mic/camera access for the process lifetime.
20+
//!
21+
//! Buzz's AppImage pins `GDK_BACKEND=x11` (see [`crate::webkit_rendering`]),
22+
//! which is the backend WebKitGTK media capture is reliable on.
23+
24+
/// The origin Tauri serves the packaged app from on Linux.
25+
const PROD_ORIGIN: &str = "tauri://localhost";
26+
27+
/// The Vite dev-server origin (`devUrl` in `tauri.conf.json`, `strictPort`
28+
/// 1420 in `vite.config.ts`). Only trusted in debug builds.
29+
#[cfg(debug_assertions)]
30+
const DEV_ORIGIN: &str = "http://localhost:1420";
31+
32+
/// Whether `uri` (the webview's current document URI) is a trusted app origin
33+
/// allowed to use mic/camera. Matches the origin exactly or as a path prefix so
34+
/// `tauri://localhost.evil.com` and `http://localhost:14200` do not slip
35+
/// through. Pure and platform-independent so it can be unit-tested everywhere.
36+
fn is_trusted_media_origin(uri: &str) -> bool {
37+
fn matches(uri: &str, origin: &str) -> bool {
38+
uri == origin
39+
|| uri
40+
.strip_prefix(origin)
41+
.is_some_and(|rest| rest.starts_with('/'))
42+
}
43+
44+
if matches(uri, PROD_ORIGIN) {
45+
return true;
46+
}
47+
#[cfg(debug_assertions)]
48+
if matches(uri, DEV_ORIGIN) {
49+
return true;
50+
}
51+
false
52+
}
53+
54+
/// Enable microphone/camera capture for `webview` if it is running on
55+
/// WebKitGTK. A no-op on every non-Linux target, so callers can invoke it
56+
/// unconditionally from shared startup code.
57+
#[cfg(target_os = "linux")]
58+
pub fn enable_media_capture<R: tauri::Runtime>(webview: &tauri::Webview<R>) {
59+
use webkit2gtk::{
60+
glib::prelude::Cast, PermissionRequestExt, SettingsExt, UserMediaPermissionRequest,
61+
UserMediaPermissionRequestExt, WebViewExt,
62+
};
63+
64+
// `with_webview` runs the closure on the UI thread, which GTK calls
65+
// require. It errors only if the platform webview is unavailable.
66+
let result = webview.with_webview(|platform_webview| {
67+
// On Linux this is the underlying `webkit2gtk::WebView`.
68+
let webview = platform_webview.inner();
69+
70+
if let Some(settings) = WebViewExt::settings(&webview) {
71+
settings.set_enable_media_stream(true);
72+
}
73+
74+
// Deny-by-default: allow only mic/camera requests from a trusted app
75+
// origin; deny everything else (still returning `true` so WebKit's
76+
// auto-deny default does not also run). Non-`UserMedia` requests return
77+
// `false` and keep their default handling.
78+
webview.connect_permission_request(|wv, request| {
79+
let Some(request) = request.downcast_ref::<UserMediaPermissionRequest>() else {
80+
return false;
81+
};
82+
83+
let uri = wv.uri().map(|u| u.to_string()).unwrap_or_default();
84+
let for_device = request.is_for_audio_device() || request.is_for_video_device();
85+
86+
if for_device && is_trusted_media_origin(&uri) {
87+
request.allow();
88+
} else {
89+
request.deny();
90+
}
91+
true
92+
});
93+
});
94+
95+
if let Err(error) = result {
96+
eprintln!("buzz-desktop: could not enable WebKitGTK media capture: {error}");
97+
}
98+
}
99+
100+
/// No-op stub so shared startup code can call [`enable_media_capture`] on every
101+
/// platform. macOS and Windows route media permissions through the OS.
102+
#[cfg(not(target_os = "linux"))]
103+
pub fn enable_media_capture<R: tauri::Runtime>(_webview: &tauri::Webview<R>) {}
104+
105+
#[cfg(test)]
106+
mod tests {
107+
use super::is_trusted_media_origin;
108+
109+
#[test]
110+
fn allows_production_app_origin() {
111+
assert!(is_trusted_media_origin("tauri://localhost"));
112+
assert!(is_trusted_media_origin(
113+
"tauri://localhost/channels/general"
114+
));
115+
}
116+
117+
#[test]
118+
fn denies_untrusted_origins() {
119+
assert!(!is_trusted_media_origin(""));
120+
assert!(!is_trusted_media_origin("https://evil.example.com"));
121+
// Prefix look-alikes must not slip through.
122+
assert!(!is_trusted_media_origin("tauri://localhost.evil.com"));
123+
assert!(!is_trusted_media_origin("tauri://localhostfoo"));
124+
}
125+
126+
#[cfg(debug_assertions)]
127+
#[test]
128+
fn allows_dev_origin_in_debug_only() {
129+
assert!(is_trusted_media_origin("http://localhost:1420"));
130+
assert!(is_trusted_media_origin("http://localhost:1420/"));
131+
// A different localhost port is still untrusted.
132+
assert!(!is_trusted_media_origin("http://localhost:14200"));
133+
assert!(!is_trusted_media_origin("http://localhost:3000"));
134+
}
135+
136+
#[cfg(not(debug_assertions))]
137+
#[test]
138+
fn denies_dev_origin_in_release() {
139+
assert!(!is_trusted_media_origin("http://localhost:1420"));
140+
}
141+
}

0 commit comments

Comments
 (0)