mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-02 19:42:51 +08:00
feat(skin): bind plugin approval to package digest
This commit is contained in:
1 parent
81c35356b4
commit
c243f272fe
8 files changed
+453
-4
No files matched your search
@@ -0,0 +1,99 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import {
|
||||
computeSkinRuntimePluginDigest,
|
||||
createSkinRuntimePluginApproval,
|
||||
isSkinRuntimePluginApproved,
|
||||
parseSkinRuntimePluginApproval,
|
||||
SkinRuntimePluginApprovalError,
|
||||
} from "./skin-runtime-plugin-approval.js";
|
||||
import { parseSkinRuntimePluginDocument, type SkinRuntimePlugin } from "./skin-runtime-plugins.js";
|
||||
|
||||
function plugin(overrides: Record<string, unknown> = {}): SkinRuntimePlugin {
|
||||
return 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",
|
||||
style: "plugins/voice-toolbar/style.css",
|
||||
assets: ["plugins/voice-toolbar/assets/icon.png"],
|
||||
permissions: ["session.status.read"],
|
||||
...overrides,
|
||||
}],
|
||||
}).plugins[0];
|
||||
}
|
||||
|
||||
function files(entry = "export default () => {};", style = ".toolbar { color: teal; }", icon = "icon-v1") {
|
||||
return {
|
||||
"plugins/voice-toolbar/index.js": new Blob([entry]),
|
||||
"plugins/voice-toolbar/style.css": new Blob([style]),
|
||||
"plugins/voice-toolbar/assets/icon.png": new Blob([icon]),
|
||||
};
|
||||
}
|
||||
|
||||
test("plugin digest is deterministic and binds all declared source, style, assets, and descriptor fields", async () => {
|
||||
const definition = plugin();
|
||||
const packageFiles = files();
|
||||
const first = await computeSkinRuntimePluginDigest(definition, packageFiles);
|
||||
assert.equal(first, await computeSkinRuntimePluginDigest(definition, packageFiles));
|
||||
assert.match(first, /^[a-f0-9]{64}$/);
|
||||
assert.notEqual(first, await computeSkinRuntimePluginDigest(definition, files("export default () => 1;")));
|
||||
assert.notEqual(first, await computeSkinRuntimePluginDigest(definition, files(undefined, ".toolbar { color: red; }")));
|
||||
assert.notEqual(first, await computeSkinRuntimePluginDigest(definition, files(undefined, undefined, "icon-v2")));
|
||||
assert.notEqual(first, await computeSkinRuntimePluginDigest(plugin({ permissions: [] }), packageFiles));
|
||||
assert.notEqual(first, await computeSkinRuntimePluginDigest(plugin({ mode: "surface", permissions: ["ui.surface.replace"] }), packageFiles));
|
||||
});
|
||||
|
||||
test("plugin approval is pinned to skin and plugin versions, content digest, and permission policy", async () => {
|
||||
const definition = plugin();
|
||||
const digest = await computeSkinRuntimePluginDigest(definition, files());
|
||||
const approval = createSkinRuntimePluginApproval("community.sample", "2.0.0", definition, digest);
|
||||
assert.equal(isSkinRuntimePluginApproved(approval, "community.sample", "2.0.0", definition, digest), true);
|
||||
assert.equal(isSkinRuntimePluginApproved(approval, "community.sample", "2.0.0", definition, "0".repeat(64)), false);
|
||||
assert.equal(isSkinRuntimePluginApproved(approval, "community.sample", "2.1.0", definition, digest), false);
|
||||
assert.equal(isSkinRuntimePluginApproved(approval, "other.skin", "2.0.0", definition, digest), false);
|
||||
assert.equal(isSkinRuntimePluginApproved(approval, "community.sample", "2.0.0", plugin({ version: "1.1.0" }), digest), false);
|
||||
assert.deepEqual(parseSkinRuntimePluginApproval(approval), approval);
|
||||
|
||||
const stalePolicy = parseSkinRuntimePluginApproval({
|
||||
...approval,
|
||||
permissions: { "session.status.read": 99 },
|
||||
});
|
||||
assert.equal(isSkinRuntimePluginApproved(stalePolicy, "community.sample", "2.0.0", definition, digest), false);
|
||||
});
|
||||
|
||||
test("plugin approval parser rejects malformed identities, digests, and unrecognized grants", async () => {
|
||||
const definition = plugin();
|
||||
const digest = await computeSkinRuntimePluginDigest(definition, files());
|
||||
const approval = createSkinRuntimePluginApproval("community.sample", "2.0.0", definition, digest);
|
||||
for (const candidate of [
|
||||
{ ...approval, digest: "short" },
|
||||
{ ...approval, pluginId: "../other" },
|
||||
{ ...approval, permissions: { "filesystem.read": 1 } },
|
||||
{ ...approval, extra: true },
|
||||
]) {
|
||||
assert.throws(() => parseSkinRuntimePluginApproval(candidate), (error: unknown) =>
|
||||
error instanceof SkinRuntimePluginApprovalError && error.code === "SKIN_RUNTIME_PLUGIN_APPROVAL_INVALID");
|
||||
}
|
||||
assert.throws(() => createSkinRuntimePluginApproval("", "2.0.0", definition, digest), { code: "SKIN_RUNTIME_PLUGIN_APPROVAL_INVALID" });
|
||||
});
|
||||
|
||||
test("plugin digest refuses missing declared files and packages beyond the ZIP expansion bound", async () => {
|
||||
const definition = plugin();
|
||||
await assert.rejects(
|
||||
computeSkinRuntimePluginDigest(definition, {}),
|
||||
(error: unknown) => error instanceof SkinRuntimePluginApprovalError && error.code === "SKIN_RUNTIME_PLUGIN_DIGEST_FILE_MISSING",
|
||||
);
|
||||
await assert.rejects(
|
||||
computeSkinRuntimePluginDigest(definition, {
|
||||
...files(),
|
||||
"plugins/voice-toolbar/assets/icon.png": new Blob([new Uint8Array(50 * 1024 * 1024)]),
|
||||
}),
|
||||
(error: unknown) => error instanceof SkinRuntimePluginApprovalError && error.code === "SKIN_RUNTIME_PLUGIN_DIGEST_SIZE",
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,176 @@
|
||||
import { SKIN_PLUGIN_PERMISSIONS, type SkinPluginPermission } from "./skin-plugin.js";
|
||||
import { parseSkinRuntimePluginDocument, type SkinRuntimePlugin } from "./skin-runtime-plugins.js";
|
||||
|
||||
export const SKIN_RUNTIME_PLUGIN_APPROVAL_SCHEMA_VERSION = 1 as const;
|
||||
export const SKIN_RUNTIME_PLUGIN_DIGEST_ALGORITHM = "SHA-256" as const;
|
||||
export const SKIN_RUNTIME_PLUGIN_DIGEST_TOTAL_LIMIT_BYTES = 50 * 1024 * 1024;
|
||||
|
||||
export interface SkinRuntimePluginApproval {
|
||||
schemaVersion: typeof SKIN_RUNTIME_PLUGIN_APPROVAL_SCHEMA_VERSION;
|
||||
skinId: string;
|
||||
skinVersion: string;
|
||||
pluginId: string;
|
||||
pluginVersion: string;
|
||||
apiVersion: 1;
|
||||
digest: string;
|
||||
permissions: Readonly<Partial<Record<SkinPluginPermission, number>>>;
|
||||
}
|
||||
|
||||
export class SkinRuntimePluginApprovalError extends Error {
|
||||
constructor(readonly code: string, message: string) {
|
||||
super(message);
|
||||
this.name = "SkinRuntimePluginApprovalError";
|
||||
}
|
||||
}
|
||||
|
||||
function invalid(code: string, message: string): never {
|
||||
throw new SkinRuntimePluginApprovalError(code, message);
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
||||
const prototype = Object.getPrototypeOf(value);
|
||||
return prototype === Object.prototype || prototype === null;
|
||||
}
|
||||
|
||||
function validDigest(value: unknown): value is string {
|
||||
return typeof value === "string" && /^[a-f0-9]{64}$/.test(value);
|
||||
}
|
||||
|
||||
function sha256(bytes: Uint8Array): Promise<string> {
|
||||
if (!globalThis.crypto?.subtle) {
|
||||
return Promise.reject(new SkinRuntimePluginApprovalError("SKIN_RUNTIME_PLUGIN_DIGEST_UNAVAILABLE", "Secure plugin approval requires Web Crypto."));
|
||||
}
|
||||
const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) as ArrayBuffer;
|
||||
return globalThis.crypto.subtle.digest(SKIN_RUNTIME_PLUGIN_DIGEST_ALGORITHM, buffer).then((digest) =>
|
||||
Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join(""));
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds local consent to the exact plugin descriptor and every declared package file.
|
||||
* This is an integrity key for local approval, not a publisher signature.
|
||||
*/
|
||||
export async function computeSkinRuntimePluginDigest(
|
||||
input: SkinRuntimePlugin,
|
||||
files: Readonly<Record<string, Blob>>,
|
||||
): Promise<string> {
|
||||
if (!files || typeof files !== "object" || Array.isArray(files)) {
|
||||
invalid("SKIN_RUNTIME_PLUGIN_DIGEST_FILE_MISSING", "The declared plugin files are unavailable.");
|
||||
}
|
||||
const plugin = parseSkinRuntimePluginDocument({ schemaVersion: 1, plugins: [input] }).plugins[0];
|
||||
const paths = [plugin.entry, ...(plugin.style ? [plugin.style] : []), ...plugin.assets].sort();
|
||||
let totalBytes = 0;
|
||||
const fileDigests: Array<{ path: string; size: number; digest: string }> = [];
|
||||
|
||||
for (const path of paths) {
|
||||
if (!Object.hasOwn(files, path) || !(files[path] instanceof Blob)) {
|
||||
invalid("SKIN_RUNTIME_PLUGIN_DIGEST_FILE_MISSING", "A declared plugin file is missing from the digest input.");
|
||||
}
|
||||
const blob = files[path];
|
||||
totalBytes += blob.size;
|
||||
if (totalBytes > SKIN_RUNTIME_PLUGIN_DIGEST_TOTAL_LIMIT_BYTES) {
|
||||
invalid("SKIN_RUNTIME_PLUGIN_DIGEST_SIZE", "The declared plugin files exceed the skin package size limit.");
|
||||
}
|
||||
fileDigests.push({ path, size: blob.size, digest: await sha256(new Uint8Array(await blob.arrayBuffer())) });
|
||||
}
|
||||
|
||||
const canonicalPlugin = {
|
||||
id: plugin.id,
|
||||
name: plugin.name,
|
||||
version: plugin.version,
|
||||
apiVersion: plugin.apiVersion,
|
||||
page: plugin.page,
|
||||
mode: plugin.mode,
|
||||
entry: plugin.entry,
|
||||
...(plugin.style ? { style: plugin.style } : {}),
|
||||
assets: [...plugin.assets].sort(),
|
||||
permissions: [...plugin.permissions].sort(),
|
||||
};
|
||||
const canonical = JSON.stringify({
|
||||
domain: "webspeak.skin-runtime-plugin-approval",
|
||||
schemaVersion: SKIN_RUNTIME_PLUGIN_APPROVAL_SCHEMA_VERSION,
|
||||
plugin: canonicalPlugin,
|
||||
files: fileDigests,
|
||||
});
|
||||
return sha256(new TextEncoder().encode(canonical));
|
||||
}
|
||||
|
||||
export function createSkinRuntimePluginApproval(
|
||||
skinId: string,
|
||||
skinVersion: string,
|
||||
input: SkinRuntimePlugin,
|
||||
digest: string,
|
||||
): SkinRuntimePluginApproval {
|
||||
if (typeof skinId !== "string" || !skinId.trim() || skinId.length > 120
|
||||
|| typeof skinVersion !== "string" || !skinVersion.trim() || skinVersion.length > 32
|
||||
|| !validDigest(digest)) {
|
||||
invalid("SKIN_RUNTIME_PLUGIN_APPROVAL_INVALID", "The host approval identity or plugin digest is invalid.");
|
||||
}
|
||||
const plugin = parseSkinRuntimePluginDocument({ schemaVersion: 1, plugins: [input] }).plugins[0];
|
||||
const permissions: Partial<Record<SkinPluginPermission, number>> = {};
|
||||
for (const permission of plugin.permissions) permissions[permission] = SKIN_PLUGIN_PERMISSIONS[permission].policyVersion;
|
||||
return Object.freeze({
|
||||
schemaVersion: SKIN_RUNTIME_PLUGIN_APPROVAL_SCHEMA_VERSION,
|
||||
skinId,
|
||||
skinVersion,
|
||||
pluginId: plugin.id,
|
||||
pluginVersion: plugin.version,
|
||||
apiVersion: plugin.apiVersion,
|
||||
digest,
|
||||
permissions: Object.freeze(permissions),
|
||||
});
|
||||
}
|
||||
|
||||
export function parseSkinRuntimePluginApproval(input: unknown): SkinRuntimePluginApproval {
|
||||
const fields = ["schemaVersion", "skinId", "skinVersion", "pluginId", "pluginVersion", "apiVersion", "digest", "permissions"];
|
||||
if (!isRecord(input) || Object.keys(input).length !== fields.length || fields.some((field) => !Object.hasOwn(input, field))) {
|
||||
invalid("SKIN_RUNTIME_PLUGIN_APPROVAL_INVALID", "The stored plugin approval has an invalid shape.");
|
||||
}
|
||||
if (input.schemaVersion !== SKIN_RUNTIME_PLUGIN_APPROVAL_SCHEMA_VERSION
|
||||
|| typeof input.skinId !== "string" || !input.skinId.trim() || input.skinId.length > 120
|
||||
|| typeof input.skinVersion !== "string" || !input.skinVersion.trim() || input.skinVersion.length > 32
|
||||
|| typeof input.pluginId !== "string" || !/^[a-z][a-z0-9-]{0,63}$/.test(input.pluginId)
|
||||
|| typeof input.pluginVersion !== "string" || input.pluginVersion.length > 32
|
||||
|| input.apiVersion !== 1 || !validDigest(input.digest) || !isRecord(input.permissions)) {
|
||||
invalid("SKIN_RUNTIME_PLUGIN_APPROVAL_INVALID", "The stored plugin approval has invalid identity fields.");
|
||||
}
|
||||
const permissions: Partial<Record<SkinPluginPermission, number>> = {};
|
||||
for (const [permission, policyVersion] of Object.entries(input.permissions)) {
|
||||
if (!Object.hasOwn(SKIN_PLUGIN_PERMISSIONS, permission)
|
||||
|| !Number.isSafeInteger(policyVersion) || (policyVersion as number) < 1) {
|
||||
invalid("SKIN_RUNTIME_PLUGIN_APPROVAL_INVALID", "The stored plugin approval contains an unsupported permission.");
|
||||
}
|
||||
permissions[permission as SkinPluginPermission] = policyVersion as number;
|
||||
}
|
||||
return Object.freeze({
|
||||
schemaVersion: SKIN_RUNTIME_PLUGIN_APPROVAL_SCHEMA_VERSION,
|
||||
skinId: input.skinId,
|
||||
skinVersion: input.skinVersion,
|
||||
pluginId: input.pluginId,
|
||||
pluginVersion: input.pluginVersion,
|
||||
apiVersion: 1,
|
||||
digest: input.digest,
|
||||
permissions: Object.freeze(permissions),
|
||||
});
|
||||
}
|
||||
|
||||
export function isSkinRuntimePluginApproved(
|
||||
approval: SkinRuntimePluginApproval,
|
||||
skinId: string,
|
||||
skinVersion: string,
|
||||
input: SkinRuntimePlugin,
|
||||
digest: string,
|
||||
): boolean {
|
||||
let plugin: SkinRuntimePlugin;
|
||||
try { plugin = parseSkinRuntimePluginDocument({ schemaVersion: 1, plugins: [input] }).plugins[0]; }
|
||||
catch { return false; }
|
||||
return approval.skinId === skinId
|
||||
&& approval.skinVersion === skinVersion
|
||||
&& approval.pluginId === plugin.id
|
||||
&& approval.pluginVersion === plugin.version
|
||||
&& approval.apiVersion === plugin.apiVersion
|
||||
&& approval.digest === digest
|
||||
&& validDigest(digest)
|
||||
&& plugin.permissions.every((permission) =>
|
||||
approval.permissions[permission] === SKIN_PLUGIN_PERMISSIONS[permission].policyVersion);
|
||||
}
|
||||
Reference in new issue
Block a user