feat(skin): bind plugin approval to package digest

This commit is contained in:
illusia committed 2026-10-10 15:53:49 +08:00
1 parent 81c35356b4
commit c243f272fe
8 files changed
+453 -4

No files matched your search

@@ -0,0 +1,79 @@
import assert from "node:assert/strict";
import test from "node:test";
import {
approveSkinRuntimePlugin,
getMissingSkinRuntimePluginApprovals,
getSkinRuntimePluginApproval,
revokeSkinRuntimePluginApprovals,
} from "./skin-runtime-plugin-approval.js";
import { computeSkinRuntimePluginDigest } from "../../../src/shared/skin-runtime-plugin-approval.js";
import { parseSkinRuntimePluginDocument } from "../../../src/shared/skin-runtime-plugins.js";
const STORAGE_KEY = "webspeak:skin-runtime-plugin-approvals";
function memoryStorage(initial?: string) {
const values = new Map<string, string>(initial === undefined ? [] : [[STORAGE_KEY, initial]]);
return {
getItem: (key: string) => values.get(key) ?? null,
setItem: (key: string, value: string) => { values.set(key, value); },
read: () => values.get(STORAGE_KEY),
};
}
const plugin = parseSkinRuntimePluginDocument({
schemaVersion: 1,
plugins: [{
id: "voice-toolbar",
name: "Voice toolbar",
version: "1.0.0",
apiVersion: 1,
page: "voice",
mode: "widget",
entry: "plugins/voice-toolbar/index.js",
assets: [],
permissions: ["session.status.read"],
}],
}).plugins[0];
test("local plugin grants require explicit approval and match the complete package digest", async () => {
const storage = memoryStorage();
const files = { "plugins/voice-toolbar/index.js": new Blob(["export default () => 1;"]) };
const digest = await computeSkinRuntimePluginDigest(plugin, files);
assert.deepEqual(getMissingSkinRuntimePluginApprovals("community.sample", "2.0.0", [plugin], { [plugin.id]: digest }, storage), [plugin]);
assert.equal(getSkinRuntimePluginApproval("community.sample", "2.0.0", plugin, digest, storage), null);
approveSkinRuntimePlugin("community.sample", "2.0.0", plugin, digest, storage);
assert.deepEqual(getMissingSkinRuntimePluginApprovals("community.sample", "2.0.0", [plugin], { [plugin.id]: digest }, storage), []);
assert.equal(getSkinRuntimePluginApproval("community.sample", "2.0.0", plugin, digest, storage)?.digest, digest);
assert.deepEqual(getMissingSkinRuntimePluginApprovals("community.sample", "2.0.0", [plugin], { [plugin.id]: "0".repeat(64) }, storage), [plugin]);
assert.deepEqual(getMissingSkinRuntimePluginApprovals("community.sample", "2.1.0", [plugin], { [plugin.id]: digest }, storage), [plugin]);
});
test("plugin approval revocation can target a skin plugin or every plugin in a skin", async () => {
const storage = memoryStorage();
const other = parseSkinRuntimePluginDocument({
schemaVersion: 1,
plugins: [{ ...plugin, id: "other-widget", name: "Other widget", entry: "plugins/other-widget/index.js" }],
}).plugins[0];
const digest = await computeSkinRuntimePluginDigest(plugin, { [plugin.entry]: new Blob(["one"]) });
const otherDigest = await computeSkinRuntimePluginDigest(other, { [other.entry]: new Blob(["two"]) });
approveSkinRuntimePlugin("community.sample", "2.0.0", plugin, digest, storage);
approveSkinRuntimePlugin("community.sample", "2.0.0", other, otherDigest, storage);
approveSkinRuntimePlugin("community.other", "2.0.0", plugin, digest, storage);
revokeSkinRuntimePluginApprovals("community.sample", plugin.id, storage);
assert.equal(getSkinRuntimePluginApproval("community.sample", "2.0.0", plugin, digest, storage), null);
assert.ok(getSkinRuntimePluginApproval("community.sample", "2.0.0", other, otherDigest, storage));
assert.ok(getSkinRuntimePluginApproval("community.other", "2.0.0", plugin, digest, storage));
revokeSkinRuntimePluginApprovals("community.sample", undefined, storage);
assert.equal(getSkinRuntimePluginApproval("community.sample", "2.0.0", other, otherDigest, storage), null);
assert.ok(getSkinRuntimePluginApproval("community.other", "2.0.0", plugin, digest, storage));
});
test("corrupt local grant data fails closed and failed persistence cannot create an in-memory grant", async () => {
const corrupt = memoryStorage("not json");
const digest = await computeSkinRuntimePluginDigest(plugin, { [plugin.entry]: new Blob(["code"]) });
assert.equal(getSkinRuntimePluginApproval("community.sample", "2.0.0", plugin, digest, corrupt), null);
const readonlyStorage = { getItem: () => null, setItem: () => { throw new Error("quota"); } };
approveSkinRuntimePlugin("community.sample", "2.0.0", plugin, digest, readonlyStorage);
assert.equal(getSkinRuntimePluginApproval("community.sample", "2.0.0", plugin, digest, readonlyStorage), null);
});
@@ -0,0 +1,93 @@
import {
createSkinRuntimePluginApproval,
isSkinRuntimePluginApproved,
parseSkinRuntimePluginApproval,
type SkinRuntimePluginApproval,
} from "../../../src/shared/skin-runtime-plugin-approval.js";
import type { SkinRuntimePlugin } from "../../../src/shared/skin-runtime-plugins.js";
const STORAGE_KEY = "webspeak:skin-runtime-plugin-approvals";
interface ApprovalStore { schemaVersion: 1; grants: Record<string, SkinRuntimePluginApproval> }
type ApprovalStorage = Pick<Storage, "getItem" | "setItem">;
const unavailableStorage: ApprovalStorage = { getItem: () => null, setItem: () => undefined };
function pluginKey(skinId: string, skinVersion: string, pluginId: string): string {
return JSON.stringify([skinId, skinVersion, pluginId]);
}
function readStore(storage: ApprovalStorage): ApprovalStore {
try {
const raw: unknown = JSON.parse(storage.getItem(STORAGE_KEY) ?? "null");
if (!raw || typeof raw !== "object" || (raw as Record<string, unknown>).schemaVersion !== 1
|| !(raw as Record<string, unknown>).grants || typeof (raw as Record<string, unknown>).grants !== "object") {
return { schemaVersion: 1, grants: {} };
}
const grants: Record<string, SkinRuntimePluginApproval> = Object.create(null) as Record<string, SkinRuntimePluginApproval>;
for (const [key, value] of Object.entries((raw as ApprovalStore).grants)) {
try { grants[key] = parseSkinRuntimePluginApproval(value); } catch { /* Ignore corrupt local grants. */ }
}
return { schemaVersion: 1, grants };
} catch {
return { schemaVersion: 1, grants: {} };
}
}
function writeStore(storage: ApprovalStorage, store: ApprovalStore): void {
try { storage.setItem(STORAGE_KEY, JSON.stringify(store)); } catch { /* Approval persistence is optional. */ }
}
function defaultStorage(): ApprovalStorage {
try { return window.localStorage; } catch { return unavailableStorage; }
}
export function getSkinRuntimePluginApproval(
skinId: string,
skinVersion: string,
plugin: SkinRuntimePlugin,
digest: string,
storage: ApprovalStorage = defaultStorage(),
): SkinRuntimePluginApproval | null {
const grant = readStore(storage).grants[pluginKey(skinId, skinVersion, plugin.id)];
return grant && isSkinRuntimePluginApproved(grant, skinId, skinVersion, plugin, digest) ? grant : null;
}
/** Called only by the host consent UI after a user explicitly approves this exact package. */
export function approveSkinRuntimePlugin(
skinId: string,
skinVersion: string,
plugin: SkinRuntimePlugin,
digest: string,
storage: ApprovalStorage = defaultStorage(),
): void {
const store = readStore(storage);
const key = pluginKey(skinId, skinVersion, plugin.id);
store.grants[key] = createSkinRuntimePluginApproval(skinId, skinVersion, plugin, digest);
writeStore(storage, store);
}
export function getMissingSkinRuntimePluginApprovals(
skinId: string,
skinVersion: string,
plugins: readonly SkinRuntimePlugin[],
digests: Readonly<Record<string, string>>,
storage: ApprovalStorage = defaultStorage(),
): SkinRuntimePlugin[] {
const grants = readStore(storage).grants;
return plugins.filter((plugin) => {
const digest = digests[plugin.id];
const grant = grants[pluginKey(skinId, skinVersion, plugin.id)];
return !digest || !grant || !isSkinRuntimePluginApproved(grant, skinId, skinVersion, plugin, digest);
});
}
export function revokeSkinRuntimePluginApprovals(
skinId: string,
pluginId?: string,
storage: ApprovalStorage = defaultStorage(),
): void {
const store = readStore(storage);
for (const [key, grant] of Object.entries(store.grants)) {
if (grant.skinId === skinId && (!pluginId || grant.pluginId === pluginId)) delete store.grants[key];
}
writeStore(storage, store);
}