mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 20:12:52 +08:00
feat(skin): support approved Wasm plugin prototype
This commit is contained in:
1 parent
26b74c5be4
commit
7993b38eee
13 files changed
+321
-24
No files matched your search
@@ -5,6 +5,7 @@ import { strToU8, zipSync } from "fflate";
|
||||
import { importSkinPack, resolveSkinCssAssets, SkinPackError } from "./skin-pack.js";
|
||||
import { scopeBuiltinThemeForCustomSkin } from "./skin-cascade.js";
|
||||
import type { SkinPluginDocument } from "../../../src/shared/skin-plugin.js";
|
||||
import { createSkinExtensionWasmPrefixByteImmediateProbe } from "../../test/skin-extension-wasm-fixture.js";
|
||||
|
||||
const manifest = {
|
||||
schemaVersion: 1,
|
||||
@@ -416,6 +417,39 @@ test("the distributable KAAK v3 package validates end to end", async () => {
|
||||
assert.ok(skin.css.includes('data-ws-plugin-part="kaak-members-sidebar.member-row-content"'));
|
||||
});
|
||||
|
||||
test("v4 skin packages validate Wasm entry ABI and cache it as application/wasm", async () => {
|
||||
const wasmPlugin = {
|
||||
schemaVersion: 1,
|
||||
plugins: [{
|
||||
id: "voice-toolbar",
|
||||
name: "Voice toolbar",
|
||||
version: "1.0.0",
|
||||
apiVersion: 1,
|
||||
runtime: "wasm",
|
||||
page: "voice",
|
||||
mode: "widget",
|
||||
entry: "plugins/voice-toolbar/index.wasm",
|
||||
style: "plugins/voice-toolbar/style.css",
|
||||
assets: ["plugins/voice-toolbar/assets/icon.png"],
|
||||
permissions: [],
|
||||
}],
|
||||
};
|
||||
const skin = await importSkinPack(makeSkinV4(wasmPlugin, {
|
||||
entryPath: "plugins/voice-toolbar/index.wasm",
|
||||
entryBytes: createSkinExtensionWasmPrefixByteImmediateProbe(),
|
||||
}));
|
||||
assert.equal(skin.runtimePlugins?.plugins[0].runtime, "wasm");
|
||||
assert.equal(skin.runtimePluginFiles?.["plugins/voice-toolbar/index.wasm"]?.type, "application/wasm");
|
||||
|
||||
await assert.rejects(
|
||||
importSkinPack(makeSkinV4(wasmPlugin, {
|
||||
entryPath: "plugins/voice-toolbar/index.wasm",
|
||||
entryBytes: new Uint8Array([0, 1, 2, 3]),
|
||||
})),
|
||||
(error: unknown) => error instanceof SkinPackError && error.code === "SKIN_RUNTIME_PLUGIN_WASM_INVALID",
|
||||
);
|
||||
});
|
||||
|
||||
test("the full voice surface example packages home, voice, and host-owned controls", async () => {
|
||||
const archive = await readFile(new URL("../../../docs/examples/open-voice-surface.wskin", import.meta.url));
|
||||
const file = new File([archive], "open-voice-surface.wskin", { type: "application/octet-stream" });
|
||||
@@ -478,7 +512,7 @@ function makeSkinV4(
|
||||
permissions: ["session.channels.read"],
|
||||
}],
|
||||
},
|
||||
options: { includeEntry?: boolean; entryBytes?: Uint8Array; extra?: Record<string, Uint8Array> } = {},
|
||||
options: { includeEntry?: boolean; entryBytes?: Uint8Array; entryPath?: string; extra?: Record<string, Uint8Array> } = {},
|
||||
): File {
|
||||
const packageFiles: Record<string, Uint8Array> = {
|
||||
"manifest.json": strToU8(JSON.stringify({ ...manifest, schemaVersion: 4, packageType: "open-skin", plugins: "plugins.json" })),
|
||||
@@ -488,7 +522,8 @@ function makeSkinV4(
|
||||
"plugins/voice-toolbar/assets/icon.png": new Uint8Array([1, 2, 3, 4]),
|
||||
...options.extra,
|
||||
};
|
||||
if (options.includeEntry !== false) packageFiles["plugins/voice-toolbar/index.js"] = options.entryBytes ?? strToU8("export const render = () => document.createElement('button');");
|
||||
const entryPath = options.entryPath ?? "plugins/voice-toolbar/index.js";
|
||||
if (options.includeEntry !== false) packageFiles[entryPath] = options.entryBytes ?? strToU8("export const render = () => document.createElement('button');");
|
||||
const bytes = zipSync(packageFiles);
|
||||
return new File([bytes.slice().buffer as ArrayBuffer], "skin-v4.wskin", { type: "application/octet-stream" });
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import valueParser from "postcss-value-parser";
|
||||
import { unzipSync } from "fflate";
|
||||
import { parseSkinLayoutJson, type SkinLayoutDocument } from "../../../src/shared/skin-layout.js";
|
||||
import { listSkinPluginAssetPaths, parseSkinPluginJson, type SkinPluginDocument } from "../../../src/shared/skin-plugin.js";
|
||||
import { validateSkinExtensionWasmBytes, validateSkinExtensionWasmImportsAndExports } from "./skin-extension-wasm-policy.js";
|
||||
import {
|
||||
listSkinRuntimePluginFilePaths,
|
||||
parseSkinRuntimePluginJson,
|
||||
@@ -191,6 +192,17 @@ export async function importSkinPack(file: File): Promise<InstalledSkin> {
|
||||
if (bytes.byteLength > SKIN_RUNTIME_PLUGIN_SOURCE_LIMIT_BYTES) throw new SkinPackError("Plugin entry files must be smaller than 256 KiB.", "SKIN_RUNTIME_PLUGIN_SOURCE_SIZE");
|
||||
try { decoder.decode(bytes); } catch { throw new SkinPackError("Plugin entry files must contain valid UTF-8.", "SKIN_RUNTIME_PLUGIN_ENCODING_INVALID"); }
|
||||
runtimePluginFiles[pluginPath] = new Blob([bytes], { type: "text/javascript" });
|
||||
} else if (lowerPath.endsWith(".wasm")) {
|
||||
if (bytes.byteLength > SKIN_RUNTIME_PLUGIN_SOURCE_LIMIT_BYTES) throw new SkinPackError("Wasm plugin entry files must be smaller than 256 KiB.", "SKIN_RUNTIME_PLUGIN_SOURCE_SIZE");
|
||||
const plugin = runtimePlugins?.plugins.find((candidate) => candidate.entry === pluginPath);
|
||||
if (!plugin || plugin.runtime !== "wasm") throw new SkinPackError("A Wasm entry must be declared as a Wasm plugin runtime.", "SKIN_RUNTIME_PLUGIN_WASM_INVALID");
|
||||
try {
|
||||
validateSkinExtensionWasmBytes(bytes);
|
||||
validateSkinExtensionWasmImportsAndExports(bytes, plugin.permissions);
|
||||
} catch (error) {
|
||||
throw new SkinPackError(`The Wasm plugin entry is invalid: ${error instanceof Error ? error.message : "unknown validation error"}`, "SKIN_RUNTIME_PLUGIN_WASM_INVALID");
|
||||
}
|
||||
runtimePluginFiles[pluginPath] = new Blob([bytes], { type: "application/wasm" });
|
||||
} else if (lowerPath.endsWith(".css")) {
|
||||
if (bytes.byteLength > SKIN_RUNTIME_PLUGIN_STYLE_LIMIT_BYTES) throw new SkinPackError("Plugin style files must be smaller than 512 KiB.", "SKIN_RUNTIME_PLUGIN_STYLE_SIZE");
|
||||
try { decoder.decode(bytes); } catch { throw new SkinPackError("Plugin style files must contain valid UTF-8.", "SKIN_RUNTIME_PLUGIN_ENCODING_INVALID"); }
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import {
|
||||
computeSkinRuntimePluginDigest,
|
||||
createSkinRuntimePluginApproval,
|
||||
} from "../../../src/shared/skin-runtime-plugin-approval.js";
|
||||
import { parseSkinRuntimePluginDocument } from "../../../src/shared/skin-runtime-plugins.js";
|
||||
import { createSkinExtensionWasmPrefixByteImmediateProbe } from "../../test/skin-extension-wasm-fixture.js";
|
||||
import {
|
||||
createSkinRuntimePluginWasmSandboxPrototype,
|
||||
SkinRuntimePluginWasmError,
|
||||
} from "./skin-runtime-plugin-wasm.js";
|
||||
|
||||
const plugin = parseSkinRuntimePluginDocument({
|
||||
schemaVersion: 1,
|
||||
plugins: [{
|
||||
id: "bounded-widget",
|
||||
name: "Bounded widget",
|
||||
version: "1.0.0",
|
||||
apiVersion: 1,
|
||||
runtime: "wasm",
|
||||
page: "voice",
|
||||
mode: "widget",
|
||||
entry: "plugins/bounded-widget/index.wasm",
|
||||
assets: [],
|
||||
permissions: [],
|
||||
}],
|
||||
}).plugins[0];
|
||||
|
||||
async function approvedFiles(source = createSkinExtensionWasmPrefixByteImmediateProbe()) {
|
||||
const files = { [plugin.entry]: new Blob([source]) };
|
||||
const digest = await computeSkinRuntimePluginDigest(plugin, files);
|
||||
return { files, digest, approval: createSkinRuntimePluginApproval("community.sample", "2.0.0", plugin, digest) };
|
||||
}
|
||||
|
||||
test("Wasm plugin startup rechecks the exact digest approval before creating a Worker", async () => {
|
||||
const { files, approval } = await approvedFiles();
|
||||
const staleApproval = { ...approval, digest: "0".repeat(64) };
|
||||
await assert.rejects(createSkinRuntimePluginWasmSandboxPrototype({
|
||||
skinId: "community.sample",
|
||||
skinVersion: "2.0.0",
|
||||
plugin,
|
||||
approval: staleApproval,
|
||||
files,
|
||||
readSessionStatus: () => ({ connected: false, channelName: null, memberCount: 0 }),
|
||||
prototypeOnly: true,
|
||||
}), (error: unknown) => error instanceof SkinRuntimePluginWasmError && error.code === "SKIN_EXTENSION_WASM_APPROVAL_INVALID");
|
||||
});
|
||||
|
||||
test("Wasm prototype refuses v4 capabilities it does not implement", async () => {
|
||||
const channelPlugin = parseSkinRuntimePluginDocument({
|
||||
schemaVersion: 1,
|
||||
plugins: [{ ...plugin, permissions: ["session.channels.read"] }],
|
||||
}).plugins[0];
|
||||
const files = { [channelPlugin.entry]: new Blob([createSkinExtensionWasmPrefixByteImmediateProbe()]) };
|
||||
const digest = await computeSkinRuntimePluginDigest(channelPlugin, files);
|
||||
const approval = createSkinRuntimePluginApproval("community.sample", "2.0.0", channelPlugin, digest);
|
||||
await assert.rejects(createSkinRuntimePluginWasmSandboxPrototype({
|
||||
skinId: "community.sample",
|
||||
skinVersion: "2.0.0",
|
||||
plugin: channelPlugin,
|
||||
approval,
|
||||
files,
|
||||
readSessionStatus: () => ({ connected: false, channelName: null, memberCount: 0 }),
|
||||
prototypeOnly: true,
|
||||
}), (error: unknown) => error instanceof SkinRuntimePluginWasmError
|
||||
&& error.code === "SKIN_EXTENSION_WASM_PERMISSION_UNSUPPORTED");
|
||||
});
|
||||
|
||||
test("approved Wasm entries run as disposable workers with bounded interpreter output", async () => {
|
||||
const { files, approval } = await approvedFiles();
|
||||
const previousWorker = Object.getOwnPropertyDescriptor(globalThis, "Worker");
|
||||
const previousWindow = Object.getOwnPropertyDescriptor(globalThis, "window");
|
||||
let terminated = 0;
|
||||
class FakeWorker {
|
||||
onmessage: ((event: MessageEvent<unknown>) => void) | null = null;
|
||||
onmessageerror: (() => void) | null = null;
|
||||
onerror: (() => void) | null = null;
|
||||
postMessage(): void {
|
||||
queueMicrotask(() => {
|
||||
this.onmessage?.({ data: { type: "running" } } as MessageEvent<unknown>);
|
||||
this.onmessage?.({ data: {
|
||||
type: "complete", result: 7, linearMemoryBytes: 65_536, statusReadCount: 0, uiInputReadCount: 0, uiOutput: null,
|
||||
} } as MessageEvent<unknown>);
|
||||
});
|
||||
}
|
||||
terminate(): void { terminated += 1; }
|
||||
}
|
||||
Object.defineProperty(globalThis, "Worker", { configurable: true, value: FakeWorker });
|
||||
Object.defineProperty(globalThis, "window", { configurable: true, value: { setTimeout, clearTimeout } });
|
||||
try {
|
||||
const handle = await createSkinRuntimePluginWasmSandboxPrototype({
|
||||
skinId: "community.sample",
|
||||
skinVersion: "2.0.0",
|
||||
plugin,
|
||||
approval,
|
||||
files,
|
||||
readSessionStatus: () => ({ connected: false, channelName: null, memberCount: 0 }),
|
||||
prototypeOnly: true,
|
||||
});
|
||||
assert.deepEqual(await handle.result, {
|
||||
result: 7,
|
||||
linearMemoryBytes: 65_536,
|
||||
statusReadCount: 0,
|
||||
uiInputReadCount: 0,
|
||||
uiOutput: null,
|
||||
});
|
||||
assert.equal(handle.closeReason, "completed");
|
||||
assert.equal(terminated, 1);
|
||||
} finally {
|
||||
if (previousWorker) Object.defineProperty(globalThis, "Worker", previousWorker);
|
||||
else Reflect.deleteProperty(globalThis, "Worker");
|
||||
if (previousWindow) Object.defineProperty(globalThis, "window", previousWindow);
|
||||
else Reflect.deleteProperty(globalThis, "window");
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,81 @@
|
||||
import {
|
||||
createSkinExtensionApproval,
|
||||
parseSkinExtensionManifest,
|
||||
type SkinExtensionSessionStatus,
|
||||
} from "../../../src/shared/skin-extension.js";
|
||||
import {
|
||||
computeSkinRuntimePluginDigest,
|
||||
isSkinRuntimePluginApproved,
|
||||
parseSkinRuntimePluginApproval,
|
||||
type SkinRuntimePluginApproval,
|
||||
} from "../../../src/shared/skin-runtime-plugin-approval.js";
|
||||
import { parseSkinRuntimePluginDocument, type SkinRuntimePlugin } from "../../../src/shared/skin-runtime-plugins.js";
|
||||
import {
|
||||
createSkinExtensionWasmSandboxPrototype,
|
||||
type SkinExtensionWasmSandboxHandle,
|
||||
} from "./skin-extension-wasm-sandbox.js";
|
||||
|
||||
export class SkinRuntimePluginWasmError extends Error {
|
||||
constructor(readonly code: string, message: string) {
|
||||
super(message);
|
||||
this.name = "SkinRuntimePluginWasmError";
|
||||
}
|
||||
}
|
||||
|
||||
export interface SkinRuntimePluginWasmOptions {
|
||||
skinId: string;
|
||||
skinVersion: string;
|
||||
plugin: SkinRuntimePlugin;
|
||||
approval: unknown;
|
||||
files: Readonly<Record<string, Blob>>;
|
||||
uiInput?: unknown;
|
||||
readSessionStatus: (signal: AbortSignal) => SkinExtensionSessionStatus | Promise<SkinExtensionSessionStatus>;
|
||||
prototypeOnly: true;
|
||||
}
|
||||
|
||||
const WASM_PROTOTYPE_PERMISSIONS = new Set(["session.status.read"]);
|
||||
|
||||
/** Starts a Wasm entry only after rechecking the exact package digest and local approval. */
|
||||
export async function createSkinRuntimePluginWasmSandboxPrototype(
|
||||
options: SkinRuntimePluginWasmOptions,
|
||||
): Promise<SkinExtensionWasmSandboxHandle> {
|
||||
if (options.prototypeOnly !== true) throw new SkinRuntimePluginWasmError("SKIN_EXTENSION_WASM_PROTOTYPE_ONLY", "The Wasm runner is a development-only security prototype.");
|
||||
const plugin = parseSkinRuntimePluginDocument({ schemaVersion: 1, plugins: [options.plugin] }).plugins[0];
|
||||
if (plugin.runtime !== "wasm") throw new SkinRuntimePluginWasmError("SKIN_EXTENSION_WASM_ENTRY_INVALID", "Only a Wasm plugin entry can use the bounded Wasm runner.");
|
||||
|
||||
const unsupportedPermission = plugin.permissions.find((permission) => !WASM_PROTOTYPE_PERMISSIONS.has(permission));
|
||||
if (unsupportedPermission) {
|
||||
throw new SkinRuntimePluginWasmError("SKIN_EXTENSION_WASM_PERMISSION_UNSUPPORTED", `The Wasm prototype does not implement ${unsupportedPermission}.`);
|
||||
}
|
||||
|
||||
const digest = await computeSkinRuntimePluginDigest(plugin, options.files);
|
||||
let approval: SkinRuntimePluginApproval;
|
||||
try { approval = parseSkinRuntimePluginApproval(options.approval); }
|
||||
catch { throw new SkinRuntimePluginWasmError("SKIN_EXTENSION_WASM_APPROVAL_INVALID", "The Wasm plugin does not have a valid local approval."); }
|
||||
if (!isSkinRuntimePluginApproved(approval, options.skinId, options.skinVersion, plugin, digest)) {
|
||||
throw new SkinRuntimePluginWasmError("SKIN_EXTENSION_WASM_APPROVAL_INVALID", "The Wasm plugin approval does not match this skin, version, plugin, or package digest.");
|
||||
}
|
||||
|
||||
const entry = options.files[plugin.entry];
|
||||
if (!(entry instanceof Blob)) throw new SkinRuntimePluginWasmError("SKIN_EXTENSION_WASM_ENTRY_INVALID", "The approved Wasm entry is unavailable.");
|
||||
const wasmBytes = new Uint8Array(await entry.arrayBuffer());
|
||||
const manifest = parseSkinExtensionManifest({
|
||||
schemaVersion: 1,
|
||||
packageType: "extension",
|
||||
id: plugin.id,
|
||||
name: plugin.name,
|
||||
version: plugin.version,
|
||||
apiVersion: plugin.apiVersion,
|
||||
permissions: [...plugin.permissions],
|
||||
});
|
||||
|
||||
// This legacy approval object is created only after the v4 digest-bound host approval above.
|
||||
return createSkinExtensionWasmSandboxPrototype({
|
||||
manifest,
|
||||
approval: createSkinExtensionApproval(manifest),
|
||||
wasmBytes,
|
||||
uiInput: options.uiInput,
|
||||
readSessionStatus: options.readSessionStatus,
|
||||
prototypeOnly: true,
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user