fix(security): anchor plugin file access on rust-side consent

This commit is contained in:
Jinpy authored and GitHub committed 2026-09-29 17:39:31 +08:00
1 parent 5c8c7e0192
commit 3db93307d0
5 files changed
+247 -28

No files matched your search

@@ -73,8 +73,10 @@ let bootCacheHit = false;
// keeps File objects and in-memory save buffers (`w<n>` ids). Handle ids are
// opaque strings end to end — never run them through Number(): ids above
// Number.MAX_SAFE_INTEGER silently round, and the registry then rejects every
// read with "unknown plugin file handle". Only paths that came from a native
// dialog or an OS drop reach plugin_file_open — never a plugin-supplied string.
// read with "unknown plugin file handle". Every path is consented to on the
// Rust side — dialogs open there (plugin_file_pick_files / plugin_file_save_as)
// and drops are registered by the native drag-drop pipeline before
// plugin_file_open accepts them — never a plugin-supplied string.
let webFileSequence = 0;
const webPickedFiles = new Map<string, File>();
@@ -104,6 +106,9 @@ async function tauriFileApi() {
return import("@/lib/backend/tauri");
}
// OS drops are the one flow where the renderer still names a path: the native
// drag-drop pipeline registered the dropped files for THIS webview, and
// plugin_file_open accepts exactly those (one open per dropped file).
async function openTauriPluginFile(pluginId: string, path: string, write: boolean): Promise<PluginFileHandleMeta> {
const { openPluginLocalFile } = await tauriFileApi();
const handle = await openPluginLocalFile(pluginId, path, write);
@@ -115,16 +120,15 @@ async function openTauriPluginFile(pluginId: string, path: string, write: boolea
async function pickPluginFiles(pluginId: string, options: PluginPickFilesOptions): Promise<PluginFileHandleMeta[]> {
if (isTauriRuntime()) {
const { open } = await import("@tauri-apps/plugin-dialog");
const selected = await open({ multiple: options.multiple === true });
const paths = Array.isArray(selected) ? selected : selected ? [selected] : [];
// The native dialog is opened on the Rust side: paths never round-trip
// through renderer-controlled arguments, the handles are the only result.
const { pickPluginLocalFiles } = await tauriFileApi();
const files: PluginFileHandleMeta[] = [];
for (const path of paths) {
try {
files.push(await openTauriPluginFile(pluginId, path, false));
} catch (error) {
console.warn("[DBX][plugin-workbench:pick]", error);
}
for (const handle of await pickPluginLocalFiles(pluginId, options.multiple === true)) {
// Track read AND write handles: unmount must reclaim both (leaked fds
// also burn the shared 64-handle registry quota).
openTauriHandles.add(handle.handleId);
files.push({ handleId: `${tauriHandlePrefix}${handle.handleId}`, name: handle.name, size: handle.size, contentType: handle.contentType });
}
return files;
}
@@ -205,19 +209,15 @@ async function deletePluginStorage(pluginId: string, key: string): Promise<void>
async function beginPluginFileSave(pluginId: string, request: { name?: string; contentType?: string; size?: number }): Promise<{ handleId: string; chunkBytes: number } | null> {
if (isTauriRuntime()) {
const { save } = await import("@tauri-apps/plugin-dialog");
const fileName = request.name || "download.bin";
const extension = fileName.includes(".") ? fileName.split(".").pop() : "";
const path = await save({
defaultPath: fileName,
filters: extension ? [{ name: extension.toUpperCase(), extensions: [extension] }] : undefined,
});
if (!path) return null;
// Route through openTauriPluginFile so the write handle joins
// openTauriHandles: a beginSave the plugin abandons must still be
// The save dialog runs on the Rust side and returns an already-consented
// write handle; only the suggested file name crosses the bridge. Track it
// in openTauriHandles: a beginSave the plugin abandons must still be
// reclaimed on unmount instead of burning the shared registry quota.
const handle = await openTauriPluginFile(pluginId, path, true);
return { handleId: handle.handleId, chunkBytes: PLUGIN_SAVE_CHUNK_BYTES };
const { savePluginLocalFileAs } = await tauriFileApi();
const handle = await savePluginLocalFileAs(pluginId, request.name || "download.bin");
if (!handle) return null;
openTauriHandles.add(handle.handleId);
return { handleId: `${tauriHandlePrefix}${handle.handleId}`, chunkBytes: PLUGIN_SAVE_CHUNK_BYTES };
}
const handleId = `${webHandlePrefix}${++webFileSequence}`;
webSaveBuffers.set(handleId, { name: request.name || "download.bin", contentType: request.contentType || "application/octet-stream", chunks: new Map() });