From 422908e78fe1cba5ef71a3636b70ad205801dd77 Mon Sep 17 00:00:00 2001 From: t8y2 Date: Sun, 27 Sep 2026 09:52:02 +0800 Subject: [PATCH] feat(plugins): add clipboard image and media URL APIs --- .../plugins/PluginWorkbenchHost.vue | 9 +- apps/desktop/src/lib/backend/http.ts | 8 + apps/desktop/src/lib/backend/tauri.ts | 8 + apps/desktop/src/lib/common/clipboard.ts | 40 ++++ .../src/lib/plugins/pluginHostBridge.spec.ts | 46 ++++- .../src/lib/plugins/pluginHostBridge.ts | 96 +++++++-- src-tauri/src/commands/mod.rs | 1 + src-tauri/src/commands/plugin_media.rs | 122 ++++++++++++ src-tauri/src/lib.rs | 3 + src-tauri/src/plugin_ui_protocol.rs | 184 +++++++++++++++++- 10 files changed, 494 insertions(+), 23 deletions(-) create mode 100644 src-tauri/src/commands/plugin_media.rs diff --git a/apps/desktop/src/components/plugins/PluginWorkbenchHost.vue b/apps/desktop/src/components/plugins/PluginWorkbenchHost.vue index 1ab650f7b0..09c651d22e 100644 --- a/apps/desktop/src/components/plugins/PluginWorkbenchHost.vue +++ b/apps/desktop/src/components/plugins/PluginWorkbenchHost.vue @@ -3,7 +3,7 @@ import { computed, inject, nextTick, onBeforeUnmount, onMounted, ref, watch } fr import { AlertTriangle, Loader2 } from "@lucide/vue"; import * as api from "@/lib/backend/api"; import { isTauriRuntime } from "@/lib/backend/tauriRuntime"; -import { copyToClipboard, readTextFromClipboard } from "@/lib/common/clipboard"; +import { copyToClipboard, readImageFromClipboard, readTextFromClipboard } from "@/lib/common/clipboard"; import { PluginHostBridge, pluginSandboxDocument, @@ -419,12 +419,19 @@ function createBridge() { // Permission-gated in the bridge (host.clipboard:read); the helper // prefers the Tauri clipboard plugin and falls back to the Web Clipboard. clipboardRead: (_pluginId) => readTextFromClipboard(), + clipboardReadImage: isTauriRuntime() ? (_pluginId) => readImageFromClipboard() : undefined, // Session consent for the first clipboard read: a native ask dialog naming // the plugin, so reads always have a human in the loop. On the web host // (no dialog surface) the callback is omitted and the bridge denies. confirmClipboardRead: isTauriRuntime() ? (_pluginId, pluginName) => import("@tauri-apps/plugin-dialog").then(({ ask }) => ask(t("pluginPlatform.clipboardReadConsent", { name: pluginName }), { title: t("pluginPlatform.clipboardReadConsentTitle"), kind: "warning" }).then((allowed) => allowed === true)) : undefined, + openMedia: isTauriRuntime() + ? (pluginId, method, params) => tauriFileApi().then(({ openPluginMedia }) => openPluginMedia(pluginId, method, params)) + : undefined, + closeMedia: isTauriRuntime() + ? (pluginId, token) => tauriFileApi().then(({ closePluginMedia }) => closePluginMedia(pluginId, token)) + : undefined, pickFiles: (pluginId, options) => pickPluginFiles(pluginId, options), readFileChunk: (pluginId, handleId, offset, length) => readPluginFileChunkById(pluginId, handleId, offset, length), beginFileSave: (pluginId, request) => beginPluginFileSave(pluginId, request), diff --git a/apps/desktop/src/lib/backend/http.ts b/apps/desktop/src/lib/backend/http.ts index 7504341d60..d44ae9f057 100644 --- a/apps/desktop/src/lib/backend/http.ts +++ b/apps/desktop/src/lib/backend/http.ts @@ -5372,6 +5372,14 @@ export async function closePluginLocalFile(_pluginId: string, _handleId: string) throw new Error("Plugin local file access is not available in the web backend"); } +export async function openPluginMedia(_pluginId: string, _method: string, _params: Record): Promise { + throw new Error("Plugin media URLs are not available in the web backend"); +} + +export async function closePluginMedia(_pluginId: string, _token: string): Promise { + throw new Error("Plugin media URLs are not available in the web backend"); +} + // Plugin UI storage goes through the Rust plugin-data tree on native hosts; // the web workbench host keeps its localStorage fallback in the component, so // these only exist to satisfy the shared backend surface. diff --git a/apps/desktop/src/lib/backend/tauri.ts b/apps/desktop/src/lib/backend/tauri.ts index c3bc1e814d..51ea83257b 100644 --- a/apps/desktop/src/lib/backend/tauri.ts +++ b/apps/desktop/src/lib/backend/tauri.ts @@ -2673,6 +2673,14 @@ export async function closePluginLocalFile(pluginId: string, handleId: string): return invoke("plugin_file_close", { pluginId, handleId }); } +export async function openPluginMedia(pluginId: string, method: string, params: Record): Promise { + return invoke("plugin_media_open", { pluginId, method, params }); +} + +export async function closePluginMedia(pluginId: string, token: string): Promise { + return invoke("plugin_media_close", { pluginId, token }); +} + export async function getPluginUiStorage(pluginId: string, key: string): Promise { return invoke("plugin_ui_storage_get", { pluginId, key }); } diff --git a/apps/desktop/src/lib/common/clipboard.ts b/apps/desktop/src/lib/common/clipboard.ts index 7f2de095e9..dab2576037 100644 --- a/apps/desktop/src/lib/common/clipboard.ts +++ b/apps/desktop/src/lib/common/clipboard.ts @@ -160,6 +160,46 @@ export async function readTextFromClipboard(env: ClipboardEnvironment = globalTh throw new Error("Clipboard API is not available"); } +export interface ClipboardImagePayload { + contentType: "image/png"; + dataBase64: string; + width: number; + height: number; +} + +export async function readImageFromClipboard(): Promise { + if (!isTauriRuntime()) throw new Error("Clipboard image reads require the desktop host"); + const { readImage } = await import("@tauri-apps/plugin-clipboard-manager"); + const image = await readImage(); + try { + const [{ width, height }, rgba] = await Promise.all([image.size(), image.rgba()]); + if (!width || !height || rgba.byteLength !== width * height * 4) throw new Error("Clipboard does not contain a valid image"); + const canvas = document.createElement("canvas"); + canvas.width = width; + canvas.height = height; + const context = canvas.getContext("2d"); + if (!context) throw new Error("Clipboard image conversion is unavailable"); + const pixels = new Uint8ClampedArray(rgba.byteLength); + pixels.set(rgba); + context.putImageData(new ImageData(pixels, width, height), 0, 0); + const blob = await new Promise((resolve, reject) => { + canvas.toBlob((value) => (value ? resolve(value) : reject(new Error("Clipboard image conversion failed"))), "image/png"); + }); + return { contentType: "image/png", dataBase64: bytesToBase64(new Uint8Array(await blob.arrayBuffer())), width, height }; + } finally { + await image.close(); + } +} + +function bytesToBase64(bytes: Uint8Array): string { + let binary = ""; + const chunkSize = 0x8000; + for (let offset = 0; offset < bytes.length; offset += chunkSize) { + binary += String.fromCharCode(...bytes.subarray(offset, offset + chunkSize)); + } + return btoa(binary); +} + export async function copyToClipboard(text: string, env: ClipboardEnvironment = globalThis as unknown as ClipboardEnvironment): Promise { text = clipboardLineEndings(text); diff --git a/apps/desktop/src/lib/plugins/pluginHostBridge.spec.ts b/apps/desktop/src/lib/plugins/pluginHostBridge.spec.ts index 57e844d431..0f2958a32e 100644 --- a/apps/desktop/src/lib/plugins/pluginHostBridge.spec.ts +++ b/apps/desktop/src/lib/plugins/pluginHostBridge.spec.ts @@ -1116,6 +1116,47 @@ describe("PluginHostBridge", () => { expect(result?.text).toHaveLength(2 * 1024 * 1024); }); + it("shares clipboard consent with image reads and validates the image payload", async () => { + const messages: unknown[] = []; + const target = { postMessage: (message: unknown) => messages.push(message) } as unknown as Window; + const send = (bridge: PluginHostBridge, id: string, method: string, params?: unknown) => bridge.handleWindowMessage({ source: target, data: { source: "dbx-plugin", version: 1, type: "request", id, method, params } } as MessageEvent); + const clipboardReadImage = vi.fn().mockResolvedValue({ contentType: "image/png", dataBase64: "AQIDBA==", width: 1, height: 1 }); + const bridge = new PluginHostBridge(plugin(["host.clipboard:read"]), workbench, {}, () => target, { + invoke: vi.fn(), + notify: vi.fn(), + sendBinary: vi.fn(), + readAsset: vi.fn(), + clipboardReadImage, + confirmClipboardRead: vi.fn().mockResolvedValue(true), + }); + send(bridge, "image-ok", "host.clipboardReadImage"); + await vi.waitFor(() => expect(messages).toHaveLength(1)); + expect(clipboardReadImage).toHaveBeenCalledWith("sample"); + expect(messages[0]).toMatchObject({ id: "image-ok", result: { contentType: "image/png", dataBase64: "AQIDBA==", width: 1, height: 1 } }); + }); + + it("opens and revokes plugin-scoped media URLs", async () => { + const messages: unknown[] = []; + const target = { postMessage: (message: unknown) => messages.push(message) } as unknown as Window; + const send = (bridge: PluginHostBridge, id: string, method: string, params?: unknown) => bridge.handleWindowMessage({ source: target, data: { source: "dbx-plugin", version: 1, type: "request", id, method, params } } as MessageEvent); + const openMedia = vi.fn().mockResolvedValue("550e8400-e29b-41d4-a716-446655440000"); + const closeMedia = vi.fn().mockResolvedValue(undefined); + const bridge = new PluginHostBridge(plugin(), workbench, {}, () => target, { + invoke: vi.fn(), + notify: vi.fn(), + sendBinary: vi.fn(), + readAsset: vi.fn(), + openMedia, + closeMedia, + }); + send(bridge, "media-open", "host.mediaOpen", { method: "filesystem/media/read", params: { uri: "s3://bucket/video.mp4" } }); + await vi.waitFor(() => expect(messages).toHaveLength(1)); + expect(openMedia).toHaveBeenCalledWith("sample", "filesystem/media/read", { uri: "s3://bucket/video.mp4" }); + send(bridge, "media-close", "host.mediaClose", { token: "550e8400-e29b-41d4-a716-446655440000" }); + await vi.waitFor(() => expect(messages).toHaveLength(2)); + expect(closeMedia).toHaveBeenCalledWith("sample", "550e8400-e29b-41d4-a716-446655440000"); + }); + it("asks session consent before the first clipboard read and remembers a denial for the bridge lifetime", async () => { const messages: unknown[] = []; const target = { postMessage: (message: unknown) => messages.push(message) } as unknown as Window; @@ -1345,11 +1386,14 @@ describe("PluginHostBridge", () => { expect(clipboardReadGateAllows(createClipboardReadGate(), 0)).toBe(true); }); - it("exposes the sandbox clipboard namespace mapping writeText to host.copy and readText to host.clipboardRead", () => { + it("exposes clipboard image reads and plugin media URLs in the sandbox SDK", () => { const source = pluginSdkSource(); expect(source).toContain("clipboard: Object.freeze({"); expect(source).toContain("writeText: (text) => request('host.copy', { text })"); expect(source).toContain("request('host.clipboardRead')"); + expect(source).toContain("readImage: async () => request('host.clipboardReadImage')"); + expect(source).toContain("request('host.mediaOpen', { method, params })"); + expect(source).toContain("request('host.mediaClose', { token })"); }); it("routes host.storage through the owning plugin, caps values, and needs the declared permission", async () => { diff --git a/apps/desktop/src/lib/plugins/pluginHostBridge.ts b/apps/desktop/src/lib/plugins/pluginHostBridge.ts index ad140e7cf7..d546634e3f 100644 --- a/apps/desktop/src/lib/plugins/pluginHostBridge.ts +++ b/apps/desktop/src/lib/plugins/pluginHostBridge.ts @@ -10,6 +10,7 @@ const HOST_MESSAGE_SOURCE = "dbx-host"; const BRIDGE_VERSION = 1; const MAX_BRIDGE_PAYLOAD_BYTES = 2 * 1024 * 1024; const MAX_BRIDGE_BINARY_BYTES = 8 * 1024 * 1024; +const MAX_CLIPBOARD_IMAGE_BASE64_BYTES = 24 * 1024 * 1024; // Distinct from the sidecar binary cap: saved files go straight from the // plugin iframe to disk and never traverse plugin frames. const MAX_BRIDGE_SAVE_BYTES = 512 * 1024 * 1024; @@ -34,7 +35,7 @@ export interface PluginClipboardAuditEntry { at: number; /** Request outcome: granted (content returned), denied (user or no consent surface). */ outcome: "granted" | "denied" | "rate-limited"; - /** Content length in UTF-16 code units; the content itself is never stored. */ + /** Returned payload size (text code units or decoded image bytes); content is never stored. */ length: number; } @@ -89,6 +90,13 @@ export interface PluginWorkbenchContext { [key: string]: unknown; } +export interface PluginClipboardImage { + contentType: "image/png"; + dataBase64: string; + width: number; + height: number; +} + export interface PluginSaveFileRequest { fileName?: string; contentType?: string; @@ -197,6 +205,8 @@ export interface PluginHostBridgeApi { * further user interaction, so it is permission-gated. */ clipboardRead?(pluginId: string): Promise; + /** Read and PNG-encode the current clipboard image. Shares the clipboard-read permission and consent gate. */ + clipboardReadImage?(pluginId: string): Promise; /** * Session consent prompt for the first clipboard read of a bridge lifetime. * Resolves true to allow (and remember for the workbench session), false to @@ -204,6 +214,10 @@ export interface PluginHostBridgeApi { * surface; a host that cannot ask must not silently allow. */ confirmClipboardRead?(pluginId: string, pluginName: string): Promise | boolean; + /** Register a short-lived, plugin-scoped custom-protocol media source. */ + openMedia?(pluginId: string, method: string, params: Record): Promise; + /** Revoke a media source token previously returned by openMedia. */ + closeMedia?(pluginId: string, token: string): Promise; /** Native open dialog; resolves opened read handles (null selection → empty list). */ pickFiles?(pluginId: string, options: PluginPickFilesOptions): Promise; /** Stream a chunk from an opened read handle. */ @@ -235,6 +249,7 @@ interface PluginRequestMessage { export class PluginHostBridge { private downloads = new Set(); + private mediaTokens = new Set(); private context: PluginWorkbenchContext; private locale: string; private theme?: PluginBridgeTheme; @@ -311,6 +326,8 @@ export class PluginHostBridge { if ((this.initSignals.load && this.initSignals.ready) || (signal === "load" && this.initSignals.load)) { for (const downloadId of this.downloads) void this.api.cancelDownload?.(this.plugin.manifest.id, downloadId).catch(() => undefined); this.downloads.clear(); + for (const token of this.mediaTokens) void this.api.closeMedia?.(this.plugin.manifest.id, token).catch(() => undefined); + this.mediaTokens.clear(); this.initGeneration += 1; this.initSignals = { load: false, ready: false }; this.initStarted = false; @@ -341,6 +358,8 @@ export class PluginHostBridge { this.disposed = true; for (const downloadId of this.downloads) void this.api.cancelDownload?.(this.plugin.manifest.id, downloadId).catch(() => undefined); this.downloads.clear(); + for (const token of this.mediaTokens) void this.api.closeMedia?.(this.plugin.manifest.id, token).catch(() => undefined); + this.mediaTokens.clear(); } private disposed = false; @@ -404,6 +423,8 @@ export class PluginHostBridge { // host omits these, and a web host has neither. clipboardWrite: !!this.api.copyText, clipboardRead: !!this.api.clipboardRead, + clipboardImageRead: !!this.api.clipboardReadImage, + mediaUrl: !!this.api.openMedia && !!this.api.closeMedia, }, context: snapshotPluginWorkbenchContext(this.context), }); @@ -517,6 +538,21 @@ export class PluginHostBridge { await this.api.sendBinary(this.plugin.manifest.id, channel, requireBase64(input.dataBase64)); return null; } + if (method === "host.mediaOpen") { + if (!this.api.openMedia || !this.api.closeMedia) throw new Error("Host media URLs are unavailable"); + const input = requireRecord(params, "media params"); + const backendMethod = requireProtocolName(input.method, "media backend method"); + const backendParams = requireRecord(input.params, "media backend params"); + const token = await this.api.openMedia(this.plugin.manifest.id, backendMethod, backendParams); + this.mediaTokens.add(token); + return { token }; + } + if (method === "host.mediaClose") { + const input = requireRecord(params, "media close params"); + const token = requireProtocolName(input.token, "media token"); + if (this.mediaTokens.delete(token)) await this.api.closeMedia?.(this.plugin.manifest.id, token); + return null; + } if (method === "ui.readAsset") { const input = requireRecord(params, "ui.readAsset params"); return this.api.readAsset(this.plugin.manifest.id, requireSafeAssetPath(input.path)); @@ -612,29 +648,26 @@ export class PluginHostBridge { // `host.clipboard:read` (writes stay on ungated host.copy). this.requirePermission("host.clipboard:read"); if (!this.api.clipboardRead) throw new Error("Host clipboard read is unavailable"); - const now = Date.now(); - if (!clipboardReadGateAllows(this.clipboardReadGate, now)) { - recordClipboardRead(this.clipboardReadGate, now, "rate-limited", 0); - throw new Error("Clipboard read rate limit exceeded; retry in a moment"); - } - // Session consent: the first read asks the user through the host's - // dialog surface; a denial is remembered for this workbench session (an - // iframe reload rebuilds the bridge and asks again). A host without a - // consent surface denies rather than silently allowing. - if (this.clipboardReadGate.consented === null) { - const answer = this.api.confirmClipboardRead ? await this.api.confirmClipboardRead(this.plugin.manifest.id, this.plugin.manifest.name) : false; - this.clipboardReadGate.consented = answer === true; - if (!this.clipboardReadGate.consented) { - recordClipboardRead(this.clipboardReadGate, now, "denied", 0); - throw new Error("Clipboard read was denied for this plugin session"); - } - } + const now = await this.requireClipboardRead(); const text = await this.api.clipboardRead(this.plugin.manifest.id); if (typeof text !== "string") throw new Error("Host clipboard read returned a non-string value"); const clamped = text.length > MAX_BRIDGE_PAYLOAD_BYTES ? text.slice(0, MAX_BRIDGE_PAYLOAD_BYTES) : text; recordClipboardRead(this.clipboardReadGate, now, "granted", clamped.length); return { text: clamped }; } + if (method === "host.clipboardReadImage") { + this.requirePermission("host.clipboard:read"); + if (!this.api.clipboardReadImage) throw new Error("Host clipboard image read is unavailable"); + const now = await this.requireClipboardRead(); + const image = await this.api.clipboardReadImage(this.plugin.manifest.id); + if (image.contentType !== "image/png" || !Number.isSafeInteger(image.width) || image.width <= 0 || !Number.isSafeInteger(image.height) || image.height <= 0) { + throw new Error("Host clipboard image returned invalid metadata"); + } + const dataBase64 = requireBase64(image.dataBase64); + if (dataBase64.length > MAX_CLIPBOARD_IMAGE_BASE64_BYTES) throw new Error("Clipboard image exceeds the 18 MiB upload limit"); + recordClipboardRead(this.clipboardReadGate, now, "granted", Math.floor(dataBase64.length * 0.75)); + return { ...image, dataBase64 }; + } if (method === "host.pickFiles") { // Same trust level as host.saveFile: the bytes only flow after the user // picked the files in the native dialog, so no manifest permission gate. @@ -709,6 +742,23 @@ export class PluginHostBridge { throw new Error(`Unsupported plugin host method '${method}'`); } + private async requireClipboardRead(): Promise { + const now = Date.now(); + if (!clipboardReadGateAllows(this.clipboardReadGate, now)) { + recordClipboardRead(this.clipboardReadGate, now, "rate-limited", 0); + throw new Error("Clipboard read rate limit exceeded; retry in a moment"); + } + if (this.clipboardReadGate.consented === null) { + const answer = this.api.confirmClipboardRead ? await this.api.confirmClipboardRead(this.plugin.manifest.id, this.plugin.manifest.name) : false; + this.clipboardReadGate.consented = answer === true; + if (!this.clipboardReadGate.consented) { + recordClipboardRead(this.clipboardReadGate, now, "denied", 0); + throw new Error("Clipboard read was denied for this plugin session"); + } + } + return now; + } + private requirePermission(permission: string): void { if (!this.hasPermission(permission)) throw new Error(`Plugin has not declared permission '${permission}'`); } @@ -1080,6 +1130,16 @@ export function pluginSdkSource(initialTheme?: PluginBridgeTheme): string { const result = await request('host.clipboardRead'); return (result && typeof result === 'object' && typeof result.text === 'string') ? result.text : ''; }, + readImage: async () => request('host.clipboardReadImage'), + }), + media: Object.freeze({ + open: async (method, params) => { + const result = await request('host.mediaOpen', { method, params }); + const token = result && typeof result === 'object' && typeof result.token === 'string' ? result.token : ''; + if (!token) throw new Error('Host returned an invalid media token'); + return { token, url: new URL('__media/' + encodeURIComponent(token), document.baseURI).href }; + }, + close: (token) => request('host.mediaClose', { token }), }), // Persistent per-plugin key-value state; gate on capabilities.storage // (older hosts omit it) and declare the host.storage permission. diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rs index 9243a6e520..25dd0f0c5a 100644 --- a/src-tauri/src/commands/mod.rs +++ b/src-tauri/src/commands/mod.rs @@ -41,6 +41,7 @@ pub mod nacos_cmd; pub mod plugin_download; pub mod plugin_download_file; pub mod plugin_file; +pub mod plugin_media; pub mod plugin_storage; pub mod plugins; pub mod prompt_template; diff --git a/src-tauri/src/commands/plugin_media.rs b/src-tauri/src/commands/plugin_media.rs new file mode 100644 index 0000000000..ec5977def2 --- /dev/null +++ b/src-tauri/src/commands/plugin_media.rs @@ -0,0 +1,122 @@ +use std::{ + collections::HashMap, + sync::{Arc, Mutex}, + time::{Duration, Instant}, +}; + +use serde_json::Value; +use tauri::State; +use uuid::Uuid; + +const MAX_MEDIA_SOURCES_PER_PLUGIN: usize = 32; +const MEDIA_SOURCE_TTL: Duration = Duration::from_secs(6 * 60 * 60); + +#[derive(Clone)] +pub struct PluginMediaSource { + pub plugin_id: String, + pub method: String, + pub params: Value, +} + +struct StoredPluginMediaSource { + source: PluginMediaSource, + created_at: Instant, +} + +#[derive(Clone, Default)] +pub struct PluginMediaState { + sources: Arc>>, +} + +impl PluginMediaState { + pub fn new() -> Self { + Self::default() + } + + fn purge_expired(sources: &mut HashMap, now: Instant) { + sources.retain(|_, source| now.duration_since(source.created_at) < MEDIA_SOURCE_TTL); + } + + pub(crate) fn get(&self, plugin_id: &str, token: &str) -> Option { + let now = Instant::now(); + let mut sources = self.sources.lock().ok()?; + Self::purge_expired(&mut sources, now); + let stored = sources.get(token)?; + (stored.source.plugin_id == plugin_id).then(|| stored.source.clone()) + } + + fn open(&self, plugin_id: String, method: String, params: Value) -> Result { + if method != "filesystem/media/read" { + return Err("Plugin media URLs only support filesystem/media/read".to_string()); + } + if !params.is_object() { + return Err("Plugin media parameters must be an object".to_string()); + } + let now = Instant::now(); + let mut sources = self.sources.lock().map_err(|_| "Plugin media registry is unavailable".to_string())?; + Self::purge_expired(&mut sources, now); + if sources.values().filter(|source| source.source.plugin_id == plugin_id).count() + >= MAX_MEDIA_SOURCES_PER_PLUGIN + { + return Err("Too many active plugin media URLs".to_string()); + } + let token = Uuid::new_v4().to_string(); + sources.insert( + token.clone(), + StoredPluginMediaSource { source: PluginMediaSource { plugin_id, method, params }, created_at: now }, + ); + Ok(token) + } + + fn close(&self, plugin_id: &str, token: &str) -> Result<(), String> { + let mut sources = self.sources.lock().map_err(|_| "Plugin media registry is unavailable".to_string())?; + if sources.get(token).is_some_and(|source| source.source.plugin_id == plugin_id) { + sources.remove(token); + } + Ok(()) + } +} + +#[tauri::command] +pub fn plugin_media_open( + state: State<'_, PluginMediaState>, + plugin_id: String, + method: String, + params: Value, +) -> Result { + state.open(plugin_id, method, params) +} + +#[tauri::command] +pub fn plugin_media_close(state: State<'_, PluginMediaState>, plugin_id: String, token: String) -> Result<(), String> { + state.close(&plugin_id, &token) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn media_tokens_are_scoped_to_the_owning_plugin() { + let state = PluginMediaState::new(); + let token = state + .open( + "sample".to_string(), + "filesystem/media/read".to_string(), + serde_json::json!({ "uri": "s3://bucket/video.mp4" }), + ) + .unwrap(); + assert!(state.get("sample", &token).is_some()); + assert!(state.get("other", &token).is_none()); + state.close("other", &token).unwrap(); + assert!(state.get("sample", &token).is_some()); + state.close("sample", &token).unwrap(); + assert!(state.get("sample", &token).is_none()); + } + + #[test] + fn media_registry_rejects_arbitrary_backend_methods() { + let state = PluginMediaState::new(); + assert!(state.open("sample".to_string(), "connection/connect".to_string(), serde_json::json!({})).is_err()); + } +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 8a996a51a8..20cef09570 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1551,6 +1551,7 @@ pub fn run() { builder .manage(CloseBehaviorState::new()) .manage(commands::plugin_file::PluginFileState::new()) + .manage(commands::plugin_media::PluginMediaState::new()) .manage(commands::plugin_storage::PluginUiStorageState::new()) .manage(AppLocaleState::new()) .on_page_load(|webview, payload| { @@ -1947,6 +1948,8 @@ pub fn run() { commands::plugin_file::plugin_file_read, commands::plugin_file::plugin_file_write, commands::plugin_file::plugin_file_close, + commands::plugin_media::plugin_media_open, + commands::plugin_media::plugin_media_close, commands::plugin_storage::plugin_ui_storage_get, commands::plugin_storage::plugin_ui_storage_set, commands::plugin_storage::plugin_ui_storage_delete, diff --git a/src-tauri/src/plugin_ui_protocol.rs b/src-tauri/src/plugin_ui_protocol.rs index 8f2aa5c550..4969c5100d 100644 --- a/src-tauri/src/plugin_ui_protocol.rs +++ b/src-tauri/src/plugin_ui_protocol.rs @@ -6,14 +6,17 @@ //! the WebView2-mapped form (`http://dbx-plugin.localhost/...`) a single //! constant origin, which the sandbox CSP can allow without wildcard hosts. -use std::sync::Arc; +use std::{sync::Arc, time::Duration}; +use base64::Engine; use dbx_core::plugins::PluginRegistry; use percent_encoding::percent_decode_str; +use serde::Deserialize; use tauri::http::{HeaderValue, Method, Response, StatusCode, Uri}; use tauri::{Manager, Runtime, UriSchemeContext, UriSchemeResponder}; +use uuid::Uuid; -use crate::commands::connection::AppState; +use crate::commands::{connection::AppState, plugin_media::PluginMediaState}; pub const PLUGIN_UI_SCHEME: &str = "dbx-plugin"; @@ -23,9 +26,24 @@ pub fn handle( request: tauri::http::Request>, responder: UriSchemeResponder, ) { - let registry = ctx.app_handle().try_state::>().map(|state| state.plugins.clone()); let method = request.method().clone(); let uri = request.uri().clone(); + if let Some((plugin_id, token)) = parse_plugin_media_uri(&uri) { + let app_state = ctx.app_handle().try_state::>().map(|state| state.inner().clone()); + let media_state = ctx.app_handle().try_state::().map(|state| state.inner().clone()); + let range = request.headers().get("range").and_then(|value| value.to_str().ok()).map(str::to_string); + tauri::async_runtime::spawn(async move { + let response = match (app_state, media_state) { + (Some(app_state), Some(media_state)) => { + serve_plugin_media(&app_state, &media_state, &plugin_id, &token, &method, range).await + } + _ => error_response(StatusCode::SERVICE_UNAVAILABLE, "Plugin media service is unavailable"), + }; + responder.respond(response); + }); + return; + } + let registry = ctx.app_handle().try_state::>().map(|state| state.plugins.clone()); tauri::async_runtime::spawn_blocking(move || { let response = match registry { Some(registry) => serve_plugin_ui_asset(®istry, &method, &uri), @@ -35,6 +53,130 @@ pub fn handle( }); } +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +struct PluginMediaChunk { + #[serde(default)] + data_base64: String, + size: u64, + #[serde(default)] + offset: u64, + #[serde(default)] + length: u64, + #[serde(default)] + partial: bool, + #[serde(default)] + range_not_satisfiable: bool, + #[serde(default)] + content_type: String, + #[serde(default)] + etag: String, +} + +async fn serve_plugin_media( + app_state: &Arc, + media_state: &PluginMediaState, + plugin_id: &str, + token: &str, + method: &Method, + range: Option, +) -> Response> { + if method != Method::GET && method != Method::HEAD { + return error_response(StatusCode::METHOD_NOT_ALLOWED, "Plugin media only supports GET and HEAD"); + } + let Some(source) = media_state.get(plugin_id, token) else { + return error_response(StatusCode::NOT_FOUND, "Plugin media URL expired or was closed"); + }; + let mut params = source.params; + let Some(values) = params.as_object_mut() else { + return error_response(StatusCode::BAD_REQUEST, "Plugin media parameters are invalid"); + }; + values.insert("head".to_string(), serde_json::Value::Bool(method == Method::HEAD)); + if let Some(range) = range { + values.insert("range".to_string(), serde_json::Value::String(range)); + } + let result = match app_state + .plugin_host + .invoke(plugin_id, &source.method, params, None, Some(Duration::from_secs(30))) + .await + { + Ok(result) => result, + Err(error) => { + log::warn!("[{PLUGIN_UI_SCHEME}] plugin media request failed for {plugin_id}: {error}"); + return error_response(StatusCode::BAD_GATEWAY, "Plugin media request failed"); + } + }; + let chunk: PluginMediaChunk = match serde_json::from_value(result) { + Ok(chunk) => chunk, + Err(error) => { + log::warn!("[{PLUGIN_UI_SCHEME}] invalid plugin media response for {plugin_id}: {error}"); + return error_response(StatusCode::BAD_GATEWAY, "Plugin media response was invalid"); + } + }; + if chunk.range_not_satisfiable { + let mut response = + error_response(StatusCode::RANGE_NOT_SATISFIABLE, "Requested media range is not satisfiable"); + if let Ok(value) = HeaderValue::from_str(&format!("bytes */{}", chunk.size)) { + response.headers_mut().insert("content-range", value); + } + return response; + } + let bytes = if method == Method::HEAD { + Vec::new() + } else { + match base64::engine::general_purpose::STANDARD.decode(&chunk.data_base64) { + Ok(bytes) => bytes, + Err(error) => { + log::warn!("[{PLUGIN_UI_SCHEME}] invalid plugin media bytes for {plugin_id}: {error}"); + return error_response(StatusCode::BAD_GATEWAY, "Plugin media bytes were invalid"); + } + } + }; + if method == Method::GET && bytes.len() as u64 != chunk.length { + return error_response(StatusCode::BAD_GATEWAY, "Plugin media response length was invalid"); + } + media_response(chunk, bytes, method == Method::HEAD) +} + +fn media_response(chunk: PluginMediaChunk, bytes: Vec, head: bool) -> Response> { + let partial = chunk.partial || (!head && chunk.length < chunk.size); + let mut response = Response::new(if head { Vec::new() } else { bytes }); + *response.status_mut() = if partial { StatusCode::PARTIAL_CONTENT } else { StatusCode::OK }; + let headers = response.headers_mut(); + headers.insert("access-control-allow-origin", HeaderValue::from_static("*")); + headers.insert("accept-ranges", HeaderValue::from_static("bytes")); + headers.insert("cache-control", HeaderValue::from_static("no-store")); + let content_type = if chunk.content_type.is_empty() { "application/octet-stream" } else { &chunk.content_type }; + headers.insert( + "content-type", + HeaderValue::from_str(content_type).unwrap_or_else(|_| HeaderValue::from_static("application/octet-stream")), + ); + let content_length = if head { chunk.size } else { chunk.length }; + if let Ok(value) = HeaderValue::from_str(&content_length.to_string()) { + headers.insert("content-length", value); + } + if partial && chunk.length > 0 { + let end = chunk.offset + chunk.length - 1; + if let Ok(value) = HeaderValue::from_str(&format!("bytes {}-{end}/{}", chunk.offset, chunk.size)) { + headers.insert("content-range", value); + } + } + if !chunk.etag.is_empty() { + if let Ok(value) = HeaderValue::from_str(&chunk.etag) { + headers.insert("etag", value); + } + } + response +} + +fn parse_plugin_media_uri(uri: &Uri) -> Option<(String, String)> { + let (plugin_id, asset_path) = parse_plugin_ui_uri(uri)?; + let token = + asset_path.strip_prefix("__media/").or_else(|| asset_path.rsplit_once("/__media/").map(|(_, token)| token))?; + Uuid::parse_str(token).ok()?; + Some((plugin_id, token.to_string())) +} + pub(crate) fn serve_plugin_ui_asset(registry: &PluginRegistry, method: &Method, uri: &Uri) -> Response> { if method != Method::GET && method != Method::HEAD { return error_response(StatusCode::METHOD_NOT_ALLOWED, "Plugin UI assets only support GET and HEAD"); @@ -211,4 +353,40 @@ mod tests { assert!(parse_plugin_ui_uri(&uri("dbx-plugin://localhost/a%20b/ui/index.html")).is_none()); assert!(parse_plugin_ui_uri(&uri("dbx-plugin://localhost/ok.id-1/ui/index.html")).is_some()); } + + #[test] + fn media_urls_require_scoped_uuid_tokens() { + let parsed = + parse_plugin_media_uri(&uri("dbx-plugin://localhost/sample/__media/550e8400-e29b-41d4-a716-446655440000")); + assert_eq!(parsed, Some(("sample".to_string(), "550e8400-e29b-41d4-a716-446655440000".to_string()))); + let nested = parse_plugin_media_uri(&uri( + "dbx-plugin://localhost/sample/ui/__media/550e8400-e29b-41d4-a716-446655440000", + )); + assert_eq!(nested, parsed); + assert!(parse_plugin_media_uri(&uri("dbx-plugin://localhost/sample/__media/not-a-token")).is_none()); + assert!(parse_plugin_media_uri(&uri("dbx-plugin://localhost/sample/ui/index.html")).is_none()); + } + + #[test] + fn media_response_sets_range_headers() { + let response = media_response( + PluginMediaChunk { + data_base64: String::new(), + size: 100, + offset: 10, + length: 20, + partial: true, + range_not_satisfiable: false, + content_type: "video/mp4".to_string(), + etag: "etag".to_string(), + }, + vec![0; 20], + false, + ); + assert_eq!(response.status(), StatusCode::PARTIAL_CONTENT); + assert_eq!(response.headers()["accept-ranges"], "bytes"); + assert_eq!(response.headers()["content-range"], "bytes 10-29/100"); + assert_eq!(response.headers()["content-length"], "20"); + assert_eq!(response.body().len(), 20); + } }