feat(plugins): bridge clipboard reads with consent gate

This commit is contained in:
Jinpy authored and GitHub committed 2026-09-24 16:16:49 +08:00
1 parent 1335ec032c
commit 92f92f6cdb
18 files changed
+424 -12

No files matched your search

+192 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from "vitest";
import { reactive, readonly } from "vue";
import { PluginHostBridge, pluginSandboxDocument, pluginSdkSource } from "./pluginHostBridge";
import { PLUGIN_CLIPBOARD_AUDIT_CAPACITY, PluginHostBridge, clipboardReadGateAllows, createClipboardReadGate, pluginSandboxDocument, pluginSdkSource, recordClipboardRead } from "./pluginHostBridge";
import type { InstalledPlugin, PluginResultViewContribution, PluginWorkbenchContribution } from "@/types/database";
function plugin(permissions: string[] = [], contributions: InstalledPlugin["manifest"]["contributions"] = []): InstalledPlugin {
@@ -1040,6 +1040,197 @@ describe("PluginHostBridge", () => {
expect(messages[0]).toMatchObject({ id: "nocopy", error: "Host clipboard is unavailable" });
});
it("serves host.clipboardRead only with the declared host.clipboard:read permission", async () => {
const messages: unknown[] = [];
const target = { postMessage: (message: unknown) => messages.push(message) } as unknown as Window;
const clipboardRead = vi.fn().mockResolvedValue("pasted text");
const send = (bridge: PluginHostBridge, id: string, method: string) => bridge.handleWindowMessage({ source: target, data: { source: "dbx-plugin", version: 1, type: "request", id, method } } as MessageEvent);
// Without the permission the read is refused before the host clipboard is touched.
const denied = new PluginHostBridge(plugin(), workbench, {}, () => target, {
invoke: vi.fn(),
notify: vi.fn(),
sendBinary: vi.fn(),
readAsset: vi.fn(),
clipboardRead,
});
denied.handleWindowMessage({
source: target,
data: { source: "dbx-plugin", version: 1, type: "request", id: "read-denied", method: "host.clipboardRead" },
} as MessageEvent);
await vi.waitFor(() => expect(messages).toHaveLength(1));
expect(clipboardRead).not.toHaveBeenCalled();
expect(messages[0]).toMatchObject({ id: "read-denied", error: "Plugin has not declared permission 'host.clipboard:read'" });
// With the permission (and session consent granted) the call is scoped to
// the owning plugin and unwraps to { text }.
const allowed = new PluginHostBridge(plugin(["host.clipboard:read"]), workbench, {}, () => target, {
invoke: vi.fn(),
notify: vi.fn(),
sendBinary: vi.fn(),
readAsset: vi.fn(),
clipboardRead,
confirmClipboardRead: vi.fn().mockResolvedValue(true),
});
allowed.sendInit();
expect((messages[1] as { capabilities?: { clipboardRead?: boolean } }).capabilities?.clipboardRead).toBe(true);
send(allowed, "read-ok", "host.clipboardRead");
await vi.waitFor(() => expect(messages.some((message) => (message as { id?: string }).id === "read-ok")).toBe(true));
expect(clipboardRead).toHaveBeenCalledWith("sample");
const byId = new Map(messages.map((message) => [(message as { id?: string }).id, message]));
expect(byId.get("read-ok")).toMatchObject({ id: "read-ok", result: { text: "pasted text" } });
});
it("rejects host.clipboardRead when the host cannot read the clipboard and caps oversized reads", async () => {
const messages: unknown[] = [];
const target = { postMessage: (message: unknown) => messages.push(message) } as unknown as Window;
const send = (bridge: PluginHostBridge, id: string, method: string) => bridge.handleWindowMessage({ source: target, data: { source: "dbx-plugin", version: 1, type: "request", id, method } } as MessageEvent);
// Legacy or web host: no clipboardRead in the API surface.
const noRead = new PluginHostBridge(plugin(["host.clipboard:read"]), workbench, {}, () => target, {
invoke: vi.fn(),
notify: vi.fn(),
sendBinary: vi.fn(),
readAsset: vi.fn(),
confirmClipboardRead: vi.fn().mockResolvedValue(true),
});
send(noRead, "read-missing", "host.clipboardRead");
await vi.waitFor(() => expect(messages).toHaveLength(1));
expect(messages[0]).toMatchObject({ id: "read-missing", error: "Host clipboard read is unavailable" });
// An oversized clipboard read is clamped to the bridge payload bound.
const clipboardRead = vi.fn().mockResolvedValue("x".repeat(2 * 1024 * 1024 + 1));
const clamping = new PluginHostBridge(plugin(["host.clipboard:read"]), workbench, {}, () => target, {
invoke: vi.fn(),
notify: vi.fn(),
sendBinary: vi.fn(),
readAsset: vi.fn(),
clipboardRead,
confirmClipboardRead: vi.fn().mockResolvedValue(true),
});
send(clamping, "read-huge", "host.clipboardRead");
await vi.waitFor(() => expect(messages.some((message) => (message as { id?: string }).id === "read-huge")).toBe(true));
expect(clipboardRead).toHaveBeenCalled();
const byId = new Map(messages.map((message) => [(message as { id?: string }).id, message]));
const result = (byId.get("read-huge") as { result?: { text?: string } }).result;
expect(result?.text).toHaveLength(2 * 1024 * 1024);
});
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;
const send = (bridge: PluginHostBridge, id: string, method: string) => bridge.handleWindowMessage({ source: target, data: { source: "dbx-plugin", version: 1, type: "request", id, method } } as MessageEvent);
// Denial on the consent prompt: the read rejects and the host clipboard is never touched.
const clipboardRead = vi.fn().mockResolvedValue("secret");
const deny = new PluginHostBridge(plugin(["host.clipboard:read"]), workbench, {}, () => target, {
invoke: vi.fn(),
notify: vi.fn(),
sendBinary: vi.fn(),
readAsset: vi.fn(),
clipboardRead,
confirmClipboardRead: vi.fn().mockResolvedValue(false),
});
send(deny, "consent-denied", "host.clipboardRead");
await vi.waitFor(() => expect(messages.some((message) => (message as { id?: string }).id === "consent-denied")).toBe(true));
expect(clipboardRead).not.toHaveBeenCalled();
const denialById = new Map(messages.map((message) => [(message as { id?: string }).id, message]));
expect(denialById.get("consent-denied")).toMatchObject({ id: "consent-denied", error: "Clipboard read was denied for this plugin session" });
expect(deny.clipboardAudit).toEqual([{ at: expect.any(Number), outcome: "denied", length: 0 }]);
// A denial is remembered: a second read rejects without asking again.
const confirm = (deny as unknown as { api: { confirmClipboardRead: ReturnType<typeof vi.fn> } }).api.confirmClipboardRead;
send(deny, "consent-denied-2", "host.clipboardRead");
await vi.waitFor(() => expect(messages.some((message) => (message as { id?: string }).id === "consent-denied-2")).toBe(true));
expect(confirm).toHaveBeenCalledTimes(1);
// Consent: the read proceeds, and later reads skip the prompt for the session.
const allow = new PluginHostBridge(plugin(["host.clipboard:read"]), workbench, {}, () => target, {
invoke: vi.fn(),
notify: vi.fn(),
sendBinary: vi.fn(),
readAsset: vi.fn(),
clipboardRead,
confirmClipboardRead: vi.fn().mockResolvedValue(true),
});
send(allow, "consent-1", "host.clipboardRead");
await vi.waitFor(() => expect(messages.some((message) => (message as { id?: string }).id === "consent-1" && (message as { result?: { text?: string } }).result).valueOf()).toBe(true));
// The rate gate demands PLUGIN_CLIPBOARD_READ_MIN_INTERVAL_MS between reads.
await new Promise((resolve) => setTimeout(resolve, 1_050));
send(allow, "consent-2", "host.clipboardRead");
await vi.waitFor(() => expect(messages.some((message) => (message as { id?: string }).id === "consent-2" && (message as { result?: { text?: string } }).result).valueOf()).toBe(true));
const allowConfirm = (allow as unknown as { api: { confirmClipboardRead: ReturnType<typeof vi.fn> } }).api.confirmClipboardRead;
expect(allowConfirm).toHaveBeenCalledTimes(1);
// Shared mock: the deny bridge never reached the clipboard, the allow bridge read twice.
expect(clipboardRead).toHaveBeenCalledTimes(2);
expect(readTextFromSharedBridgeAudit(allow)).toEqual(["granted", "granted"]);
});
function readTextFromSharedBridgeAudit(bridge: PluginHostBridge) {
return bridge.clipboardAudit.map((entry) => entry.outcome);
}
it("denies clipboard reads on hosts without a consent surface and rate-limits repeated reads", async () => {
const messages: unknown[] = [];
const target = { postMessage: (message: unknown) => messages.push(message) } as unknown as Window;
const send = (bridge: PluginHostBridge, id: string, method: string) => bridge.handleWindowMessage({ source: target, data: { source: "dbx-plugin", version: 1, type: "request", id, method } } as MessageEvent);
// No consent surface: deny rather than silently allow (option-a hardening).
const noSurface = new PluginHostBridge(plugin(["host.clipboard:read"]), workbench, {}, () => target, {
invoke: vi.fn(),
notify: vi.fn(),
sendBinary: vi.fn(),
readAsset: vi.fn(),
clipboardRead: vi.fn().mockResolvedValue("secret"),
});
send(noSurface, "no-surface", "host.clipboardRead");
await vi.waitFor(() => expect(messages.some((message) => (message as { id?: string }).id === "no-surface")).toBe(true));
const noSurfaceById = new Map(messages.map((message) => [(message as { id?: string }).id, message]));
expect(noSurfaceById.get("no-surface")).toMatchObject({ error: "Clipboard read was denied for this plugin session" });
// Rate gate: two back-to-back reads — the second is refused with a retry hint
// and recorded as rate-limited without touching the clipboard.
const clipboardRead = vi.fn().mockResolvedValue("text");
const readBridge = new PluginHostBridge(plugin(["host.clipboard:read"]), workbench, {}, () => target, {
invoke: vi.fn(),
notify: vi.fn(),
sendBinary: vi.fn(),
readAsset: vi.fn(),
clipboardRead,
confirmClipboardRead: vi.fn().mockResolvedValue(true),
});
send(readBridge, "rate-1", "host.clipboardRead");
await vi.waitFor(() => expect(messages.some((message) => (message as { id?: string }).id === "rate-1" && (message as { result?: unknown }).result).valueOf()).toBe(true));
send(readBridge, "rate-2", "host.clipboardRead");
await vi.waitFor(() => expect(messages.some((message) => (message as { id?: string }).id === "rate-2")).toBe(true));
const rateById = new Map(messages.map((message) => [(message as { id?: string }).id, message]));
expect(String((rateById.get("rate-2") as { error?: string }).error)).toContain("rate limit");
expect(readBridge.clipboardAudit.map((entry) => entry.outcome)).toEqual(["granted", "rate-limited"]);
expect(clipboardRead).toHaveBeenCalledTimes(1);
});
it("caps the clipboard read audit trail and exposes the pure rate helpers", () => {
expect(PLUGIN_CLIPBOARD_AUDIT_CAPACITY).toBeLessThanOrEqual(500);
const gate = createClipboardReadGate();
for (let index = 0; index < PLUGIN_CLIPBOARD_AUDIT_CAPACITY + 20; index += 1) {
recordClipboardRead(gate, index * 10_000, "granted", index);
}
expect(gate.audit).toHaveLength(PLUGIN_CLIPBOARD_AUDIT_CAPACITY);
expect(gate.audit[0].length).toBe(20);
// Exactly one read per min interval passes; within the window the gate refuses.
expect(clipboardReadGateAllows(gate, gate.audit[PLUGIN_CLIPBOARD_AUDIT_CAPACITY - 1].at + 999)).toBe(false);
expect(clipboardReadGateAllows(gate, gate.audit[PLUGIN_CLIPBOARD_AUDIT_CAPACITY - 1].at + 1_000)).toBe(true);
// A fresh gate (never read) always allows the first read.
expect(clipboardReadGateAllows(createClipboardReadGate(), 0)).toBe(true);
});
it("exposes the sandbox clipboard namespace mapping writeText to host.copy and readText to host.clipboardRead", () => {
const source = pluginSdkSource();
expect(source).toContain("clipboard: Object.freeze({");
expect(source).toContain("writeText: (text) => request('host.copy', { text })");
expect(source).toContain("request('host.clipboardRead')");
});
it("routes host.storage through the owning plugin, caps values, and needs the declared permission", async () => {
const messages: unknown[] = [];
const target = { postMessage: (message: unknown) => messages.push(message) } as unknown as Window;
@@ -15,6 +15,48 @@ const MAX_BRIDGE_SAVE_BYTES = 512 * 1024 * 1024;
// Mirrors MAX_PLUGIN_PLAN_NAME_CHARS in crates/dbx-core/src/query/plugin_plan.rs.
const MAX_PLUGIN_PLAN_IDENTIFIER_CHARS = 256;
// Clipboard reads are the one permission that hands environment data (the
// system clipboard) to plugin code with no user interaction on each call, so
// beyond the manifest permission gate the bridge adds: a per-session consent
// prompt before the first read, a bounded audit trail, and a read-rate cap.
// A plugin that trips the cap waits rather than being able to silently poll.
export const PLUGIN_CLIPBOARD_READ_MIN_INTERVAL_MS = 1_000;
export const PLUGIN_CLIPBOARD_AUDIT_CAPACITY = 200;
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. */
length: number;
}
export interface PluginClipboardReadGateState {
/** Consent for the current bridge lifetime; null = never asked. */
consented: boolean | null;
lastReadAt: number;
audit: PluginClipboardAuditEntry[];
}
export function createClipboardReadGate(): PluginClipboardReadGateState {
return { consented: null, lastReadAt: 0, audit: [] };
}
/**
* Rate gate: at most one read per PLUGIN_CLIPBOARD_READ_MIN_INTERVAL_MS.
* Returns true when the read may proceed; a denied (rate-limited) read is
* recorded by the caller.
*/
export function clipboardReadGateAllows(state: PluginClipboardReadGateState, now: number): boolean {
return state.lastReadAt <= 0 || now - state.lastReadAt >= PLUGIN_CLIPBOARD_READ_MIN_INTERVAL_MS;
}
export function recordClipboardRead(state: PluginClipboardReadGateState, now: number, outcome: PluginClipboardAuditEntry["outcome"], length: number): void {
if (outcome !== "rate-limited") state.lastReadAt = now;
state.audit.push({ at: now, outcome, length });
if (state.audit.length > PLUGIN_CLIPBOARD_AUDIT_CAPACITY) state.audit.splice(0, state.audit.length - PLUGIN_CLIPBOARD_AUDIT_CAPACITY);
}
/** Structured editor appearance: SQL editor settings that have no CSS-token
* carrier (font size is a number, the syntax theme an id). Font families are
* additionally mirrored as `--font-sans` / `--font-mono` root tokens. */
@@ -125,6 +167,20 @@ export interface PluginHostBridgeApi {
cancelDownload?(pluginId: string, downloadId: string): Promise<void>;
/** Write text to the system clipboard on behalf of the sandboxed plugin iframe. */
copyText?(pluginId: string, text: string): Promise<void>;
/**
* Read the system clipboard on behalf of the sandboxed plugin iframe.
* Requires the plugin to declare `host.clipboard:read`: unlike writes, a
* read hands arbitrary user data (passwords, tokens) to plugin code with no
* further user interaction, so it is permission-gated.
*/
clipboardRead?(pluginId: string): Promise<string>;
/**
* Session consent prompt for the first clipboard read of a bridge lifetime.
* Resolves true to allow (and remember for the workbench session), false to
* deny (the read request rejects). Optional on hosts without a dialog
* surface; a host that cannot ask must not silently allow.
*/
confirmClipboardRead?(pluginId: string, pluginName: string): Promise<boolean> | boolean;
/** Native open dialog; resolves opened read handles (null selection → empty list). */
pickFiles?(pluginId: string, options: PluginPickFilesOptions): Promise<PluginFileHandleMeta[]>;
/** Stream a chunk from an opened read handle. */
@@ -159,6 +215,13 @@ export class PluginHostBridge {
private context: PluginWorkbenchContext;
private locale: string;
private theme?: PluginBridgeTheme;
/** Consent + audit + rate state for clipboard reads; lives for the bridge lifetime. */
private clipboardReadGate = createClipboardReadGate();
/** Bounded audit trail of this session's clipboard read attempts (oldest first). */
get clipboardAudit(): readonly PluginClipboardAuditEntry[] {
return this.clipboardReadGate.audit;
}
/** Invoked once before each iframe load generation sends its init message. */
onReinit?: () => Promise<void> | void;
@@ -303,6 +366,10 @@ export class PluginHostBridge {
[PLUGIN_SCHEMA_METADATA_CAPABILITY]: !!this.api.getTableMetadata,
storage: !!this.api.storageGet && !!this.api.storageSet && !!this.api.storageDelete,
ai: !!this.api.openAiConversation,
// Additive with the same "absence means unsupported" contract: an older
// host omits these, and a web host has neither.
clipboardWrite: !!this.api.copyText,
clipboardRead: !!this.api.clipboardRead,
},
context: snapshotPluginWorkbenchContext(this.context),
});
@@ -485,6 +552,35 @@ export class PluginHostBridge {
await this.api.copyText(this.plugin.manifest.id, input.text);
return { success: true };
}
if (method === "host.clipboardRead") {
// Reads are the sensitive half of the clipboard surface: the payload is
// user data heading into plugin code, so the manifest must declare
// `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 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.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.
@@ -888,6 +984,17 @@ export function pluginSdkSource(initialTheme?: PluginBridgeTheme): string {
return request('host.saveFile', options, { transfer: bytes });
},
copy: (text) => request('host.copy', { text }),
// System clipboard surface: writeText rides the ungated host.copy path,
// readText is served by host.clipboardRead and requires the plugin to
// declare the host.clipboard:read permission (the bridge rejects
// otherwise, and capabilities.clipboardRead advertises support).
clipboard: Object.freeze({
writeText: (text) => request('host.copy', { text }),
readText: async () => {
const result = await request('host.clipboardRead');
return (result && typeof result === 'object' && typeof result.text === 'string') ? result.text : '';
},
}),
// Persistent per-plugin key-value state; gate on capabilities.storage
// (older hosts omit it) and declare the host.storage permission.
storage: Object.freeze({
@@ -0,0 +1,13 @@
/**
* Permission display classification for the plugin center. The badge shows the
* count; the detail surface lists the real permission strings so a user can see
* exactly what a plugin asks for before installing. Permissions in
* SENSITIVE_PLUGIN_PERMISSIONS hand user data or environment state to plugin
* code without an interaction per call (or open a direct environment channel),
* so they are highlighted instead of blended into the outline badge row.
*/
export const SENSITIVE_PLUGIN_PERMISSIONS: readonly string[] = ["host.clipboard:read"];
export function isSensitivePluginPermission(permission: string): boolean {
return SENSITIVE_PLUGIN_PERMISSIONS.includes(permission);
}