|
| 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