feat(skin): add v3 declarative plugin components

This commit is contained in:
illusia committed 2026-10-10 15:53:48 +08:00
1 parent 02441410a0
commit eabc78f36a
30 files changed
+2483 -308

No files matched your search

+43
View File
@@ -134,6 +134,49 @@ test("server accepts validated declarative v2 layouts and rejects permissions or
await assert.rejects(registry.save(unknownComponentArchive, "sample-skin"), (error: unknown) => error instanceof SkinRegistryError && error.code === "SKIN_LAYOUT_INVALID");
});
test("server accepts v3 declarative components and rejects unapproved capability declarations", async (context) => {
const directory = await mkdtemp(path.join(os.tmpdir(), "webspeak-skin-v3-"));
context.after(() => rm(directory, { recursive: true, force: true }));
const registry = new SkinRegistry(directory);
const manifestV3 = {
...manifest,
schemaVersion: 3,
packageType: "open-skin",
content: undefined,
preview: undefined,
components: "components.json",
};
const components = {
schemaVersion: 1,
components: [{
id: "channels",
name: "Channels",
page: "voice",
accessibleName: "TeamSpeak channels",
permissions: ["session.channels.read", "session.channel.join"],
actions: { join: { type: "voice.joinChannel", args: { channelId: "{{channel.id}}" } } },
root: { tag: "nav", repeat: { path: "session.channels", as: "channel" }, children: [{ tag: "button", events: { dblclick: "join" }, children: [{ text: "{{channel.name}}" }] }] },
}],
};
const archive = createZip([
["manifest.json", Buffer.from(JSON.stringify(manifestV3))],
["skin.css", Buffer.from('[data-ws-part="voice.channel-group"] { display: none; }')],
["components.json", Buffer.from(JSON.stringify(components))],
]);
const saved = await registry.save(archive, "sample-skin");
assert.equal(saved.id, "sample-skin");
assert.deepEqual(await registry.readArchive("sample-skin"), archive);
const unapproved = structuredClone(components) as typeof components;
unapproved.components[0].permissions = ["network.fetch"];
const unsafeArchive = createZip([
["manifest.json", Buffer.from(JSON.stringify(manifestV3))],
["skin.css", Buffer.from("[data-ws-part=app] { color: red; }")],
["components.json", Buffer.from(JSON.stringify(unapproved))],
]);
await assert.rejects(registry.save(unsafeArchive, "sample-skin"), (error: unknown) => error instanceof SkinRegistryError && error.code === "SKIN_PLUGIN_INVALID");
});
test("skin registry rejects path traversal and mismatched local ZIP headers", async (context) => {
const directory = await mkdtemp(path.join(os.tmpdir(), "webspeak-skin-invalid-"));
context.after(() => rm(directory, { recursive: true, force: true }));
+26 -11
View File
@@ -1,5 +1,6 @@
import type { SkinCatalogEntry as SharedSkinCatalogEntry } from "../shared/skin-catalog.js";
import { parseSkinLayoutJson } from "../shared/skin-layout.js";
import { listSkinPluginAssetPaths, parseSkinPluginJson, type SkinPluginDocument } from "../shared/skin-plugin.js";
import { randomBytes } from "node:crypto";
import { mkdir, readFile, readdir, rename, stat, unlink, writeFile } from "node:fs/promises";
import path from "node:path";
@@ -41,11 +42,12 @@ interface SkinArchiveEntry {
}
type ParsedSkinManifest = Omit<SkinCatalogEntry, "installedAt" | "previewUrl" | "previewMimeType" | "enabled"> & {
schemaVersion: 1 | 2;
packageType?: "layout-skin";
schemaVersion: 1 | 2 | 3;
packageType?: "layout-skin" | "open-skin";
entry: string;
layout?: string;
content?: string;
components?: string;
preview?: string;
};
@@ -226,7 +228,7 @@ function validateSkinArchive(bytes: Buffer, expectedId?: string): { manifest: Pa
let previewBytes: Buffer | undefined;
let previewMime: string | undefined;
for (const [packagePath, data] of fileBytes) {
if (["manifest.json", manifest.entry, manifest.layout, manifest.content, manifest.preview].includes(packagePath)) continue;
if (["manifest.json", manifest.entry, manifest.layout, manifest.content, manifest.components, manifest.preview].includes(packagePath)) continue;
const mimeType = imageMime(packagePath) ?? (packagePath.toLowerCase().endsWith(".woff2") ? "font/woff2" : null);
if (!mimeType) throw new SkinRegistryError(`Unsupported package file: ${packagePath}`, "SKIN_FILE_UNSUPPORTED");
if (mimeType.startsWith("image/") && data.byteLength > MAX_IMAGE_BYTES) throw new SkinRegistryError("Each image must be smaller than 16 MiB.", "SKIN_IMAGE_SIZE");
@@ -245,6 +247,16 @@ function validateSkinArchive(bytes: Buffer, expectedId?: string): { manifest: Pa
try { parseSkinLayoutJson(decodeUtf8(layout, "layout.json is not valid UTF-8.")); }
catch (error) { throw new SkinRegistryError("layout.json is invalid: " + (error instanceof Error ? error.message : "unknown layout error"), "SKIN_LAYOUT_INVALID"); }
}
if (manifest.components) {
const components = fileBytes.get(manifest.components);
if (!components || components.byteLength > MAX_CONTENT_BYTES) throw new SkinRegistryError("components.json is missing or exceeds 256 KiB.", "SKIN_PLUGIN_SIZE");
let document: SkinPluginDocument;
try { document = parseSkinPluginJson(decodeUtf8(components, "components.json is not valid UTF-8.")); }
catch (error) { throw new SkinRegistryError("components.json is invalid: " + (error instanceof Error ? error.message : "unknown plugin error"), "SKIN_PLUGIN_INVALID"); }
for (const assetPath of listSkinPluginAssetPaths(document)) {
if (!fileBytes.has(assetPath) || !imageMime(assetPath)) throw new SkinRegistryError(`Component image asset is missing or unsupported: ${assetPath}`, "SKIN_PLUGIN_ASSET_INVALID");
}
}
if (manifest.preview) {
previewBytes = fileBytes.get(manifest.preview);
previewMime = imageMime(manifest.preview) ?? undefined;
@@ -377,9 +389,10 @@ function parseManifest(bytes: Buffer): ParsedSkinManifest {
if (!raw || typeof raw !== "object" || Array.isArray(raw)) throw new SkinRegistryError("manifest.json must be an object.", "SKIN_MANIFEST_INVALID");
const value = raw as Record<string, unknown>;
const schemaVersion = value.schemaVersion;
if (schemaVersion !== 1 && schemaVersion !== 2) throw new SkinRegistryError("The skin schema version or ID is unsupported.", "SKIN_SCHEMA_VERSION");
if (schemaVersion === 2) {
if (schemaVersion !== 1 && schemaVersion !== 2 && schemaVersion !== 3) throw new SkinRegistryError("The skin schema version or ID is unsupported.", "SKIN_SCHEMA_VERSION");
if (schemaVersion >= 2) {
const fields = new Set(["schemaVersion", "packageType", "id", "name", "version", "author", "license", "description", "entry", "layout", "permissions", "content", "minAppVersion", "preview"]);
if (schemaVersion === 3) { fields.delete("permissions"); fields.add("components"); }
const unknown = Object.keys(value).find((key) => !fields.has(key));
if (unknown) throw new SkinRegistryError(`Version 2 skin manifest contains an unsupported field: ${unknown}.`, "SKIN_MANIFEST_INVALID");
}
@@ -393,7 +406,8 @@ function parseManifest(bytes: Buffer): ParsedSkinManifest {
if (schemaVersion === 2 && (value.packageType !== "layout-skin" || !Array.isArray(value.permissions) || value.permissions.length !== 0)) {
throw new SkinRegistryError("Layout skin packages must declare the layout-skin type and no executable permissions.", "SKIN_PERMISSION_INVALID");
}
if (schemaVersion === 1 && (value.layout !== undefined || value.packageType !== undefined || value.permissions !== undefined)) {
if (schemaVersion === 3 && value.packageType !== "open-skin") throw new SkinRegistryError("Version 3 skin packages must declare the open-skin type.", "SKIN_MANIFEST_INVALID");
if (schemaVersion === 1 && (value.layout !== undefined || value.packageType !== undefined || value.permissions !== undefined || value.components !== undefined)) {
throw new SkinRegistryError("Version 1 skin packages cannot declare layout or plugin fields.", "SKIN_SCHEMA_VERSION");
}
const version = read("version", 32);
@@ -401,15 +415,16 @@ function parseManifest(bytes: Buffer): ParsedSkinManifest {
if (!/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/.test(version) || !/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/.test(minAppVersion)) throw new SkinRegistryError("Skin and minimum app versions must use semantic version format.", "SKIN_MANIFEST_INVALID");
const entry = read("entry", 120);
const readOptional = (key: string, max: number): string | undefined => value[key] == null ? undefined : read(key, max);
const layout = schemaVersion === 2 ? read("layout", 120) : undefined;
const layout = schemaVersion === 2 ? read("layout", 120) : schemaVersion === 3 && value.layout != null ? read("layout", 120) : undefined;
const content = readOptional("content", 120);
const components = schemaVersion === 3 ? read("components", 120) : undefined;
const preview = readOptional("preview", 120);
for (const candidate of [entry, layout, content, preview].filter((item): item is string => Boolean(item))) if (!isSafePackagePath(candidate)) throw new SkinRegistryError("The manifest references an unsafe path.", "SKIN_PATH_INVALID");
if (!entry.toLowerCase().endsWith(".css") || (layout && !layout.toLowerCase().endsWith(".json")) || (content && !content.toLowerCase().endsWith(".json")) || (preview && !imageMime(preview))) throw new SkinRegistryError("The CSS entry, layout, content, or preview path is invalid.", "SKIN_MANIFEST_INVALID");
const declaredFiles = ["manifest.json", entry, ...(layout ? [layout] : []), ...(content ? [content] : []), ...(preview ? [preview] : [])].map((item) => item.toLowerCase());
for (const candidate of [entry, layout, content, components, preview].filter((item): item is string => Boolean(item))) if (!isSafePackagePath(candidate)) throw new SkinRegistryError("The manifest references an unsafe path.", "SKIN_PATH_INVALID");
if (!entry.toLowerCase().endsWith(".css") || (layout && !layout.toLowerCase().endsWith(".json")) || (content && !content.toLowerCase().endsWith(".json")) || (components && !components.toLowerCase().endsWith(".json")) || (preview && !imageMime(preview))) throw new SkinRegistryError("The CSS entry, layout, content, plugin components, or preview path is invalid.", "SKIN_MANIFEST_INVALID");
const declaredFiles = ["manifest.json", entry, ...(layout ? [layout] : []), ...(content ? [content] : []), ...(components ? [components] : []), ...(preview ? [preview] : [])].map((item) => item.toLowerCase());
if (new Set(declaredFiles).size !== declaredFiles.length) throw new SkinRegistryError("Manifest files must use separate package paths.", "SKIN_MANIFEST_INVALID");
const description = readOptional("description", 400);
return { schemaVersion, ...(schemaVersion === 2 ? { packageType: "layout-skin" as const } : {}), id, name: read("name", 80), version, author: read("author", 80), license: read("license", 80), minAppVersion, entry, ...(layout ? { layout } : {}), ...(content ? { content } : {}), ...(preview ? { preview } : {}), ...(description ? { description } : {}) };
return { schemaVersion, ...(schemaVersion === 2 ? { packageType: "layout-skin" as const } : schemaVersion === 3 ? { packageType: "open-skin" as const } : {}), id, name: read("name", 80), version, author: read("author", 80), license: read("license", 80), minAppVersion, entry, ...(layout ? { layout } : {}), ...(content ? { content } : {}), ...(components ? { components } : {}), ...(preview ? { preview } : {}), ...(description ? { description } : {}) };
}
function singleRootPrefix(paths: string[]): string {
+122
View File
@@ -0,0 +1,122 @@
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import {
parseSkinPluginDocument,
parseSkinPluginJson,
type SkinPluginDocument,
type SkinPluginNode,
} from "./skin-plugin.js";
function validDocument(): SkinPluginDocument {
return {
schemaVersion: 1,
components: [{
id: "channel-list",
name: "Channel list",
page: "voice",
accessibleName: "TeamSpeak channels",
permissions: ["session.channels.read", "session.channel.join"],
actions: {
join: { type: "voice.joinChannel", args: { channelId: "{{channel.id}}" } },
},
root: {
tag: "nav",
repeat: { path: "session.channels", as: "channel" },
children: [{
tag: "button",
attributes: { type: "button", "aria-label": "{{channel.name}}" },
events: { dblclick: "join" },
children: [{ text: "{{channel.name}}" }],
}],
},
}],
};
}
function errorCode(run: () => unknown, expected: string): void {
assert.throws(run, (error: unknown) => error instanceof Error
&& "code" in error && error.code === expected);
}
test("skin plugin accepts a bounded channel list with a double-click host action", () => {
const parsed = parseSkinPluginDocument(validDocument());
assert.equal(parsed.components[0].root.repeat?.path, "session.channels");
assert.equal(parsed.components[0].root.children?.[0].events?.dblclick, "join");
});
test("KAAK v3 example uses only validated public session data and an explicit join action", async () => {
const source = await readFile(new URL("../../docs/examples/kaak-voice/components.json", import.meta.url), "utf8");
const parsed = parseSkinPluginJson(source);
const sidebar = parsed.components[0];
assert.equal(sidebar.id, "kaak-room-sidebar");
assert.deepEqual(sidebar.permissions, ["session.channels.read", "session.members.read", "session.channel.join"]);
assert.equal(sidebar.actions["join-channel"].type, "voice.joinChannel");
assert.equal(sidebar.root.children?.[1].children?.[0].repeat?.path, "session.channels");
});
test("skin plugin binds form controls only to declared local state", () => {
const document = validDocument();
document.components[0].state = { search: "" };
document.components[0].root.children = [{
tag: "input",
bindValue: "search",
events: { input: "filter" },
}];
document.components[0].actions.filter = { type: "ui.setState", args: { key: "search", value: "{{state.search}}" } };
assert.equal(parseSkinPluginDocument(document).components[0].state?.search, "");
});
test("skin plugin rejects undeclared permissions, arbitrary paths, and executable markup", () => {
const missingPermission = validDocument();
missingPermission.components[0].permissions = ["session.channel.join"];
errorCode(() => parseSkinPluginDocument(missingPermission), "SKIN_PLUGIN_PERMISSION_MISSING");
const hiddenPath = validDocument();
hiddenPath.components[0].root.children![0].children = [{ text: "{{session.credentials.privateKey}}" }];
errorCode(() => parseSkinPluginDocument(hiddenPath), "SKIN_PLUGIN_BINDING_INVALID");
const executable = validDocument();
executable.components[0].root.children![0].attributes = { onclick: "alert(1)" };
errorCode(() => parseSkinPluginDocument(executable), "SKIN_PLUGIN_ATTRIBUTE_INVALID");
const unsafeTag = validDocument();
unsafeTag.components[0].root.children![0].tag = "iframe";
errorCode(() => parseSkinPluginDocument(unsafeTag), "SKIN_PLUGIN_ELEMENT_INVALID");
const externalSvgPaint = validDocument();
externalSvgPaint.components[0].root.children![0].attributes = { fill: "url(https://example.invalid/payload.svg#paint)" };
errorCode(() => parseSkinPluginDocument(externalSvgPaint), "SKIN_PLUGIN_ATTRIBUTE_INVALID");
});
test("skin plugin restricts repeated fields to the selected collection", () => {
const document = validDocument();
document.components[0].root.repeat = { path: "session.channels", as: "channel" };
document.components[0].root.children = [{ text: "{{channel.author}}" }];
errorCode(() => parseSkinPluginDocument(document), "SKIN_PLUGIN_BINDING_INVALID");
});
test("skin plugin never dispatches host actions from typing or selection changes", () => {
const document = validDocument();
document.components[0].root.children![0].events = { change: "join" };
errorCode(() => parseSkinPluginDocument(document), "SKIN_PLUGIN_EVENT_INVALID");
});
test("skin plugin JSON parser enforces a bounded document size and valid JSON", () => {
assert.throws(() => parseSkinPluginJson("{"));
assert.throws(() => parseSkinPluginJson(" ".repeat(256 * 1024 + 1)));
assert.equal(parseSkinPluginJson(JSON.stringify(validDocument())).components.length, 1);
});
test("skin plugin enforces each component's node and nesting limits", () => {
const broad = validDocument();
broad.components[0].root.children = Array.from({ length: 101 }, () => ({ text: "item" }));
errorCode(() => parseSkinPluginDocument(broad), "SKIN_PLUGIN_TREE_INVALID");
const tooMany = validDocument();
const branch = (depth: number): SkinPluginNode => depth === 0
? { tag: "span", children: [{ text: "item" }] }
: { tag: "span", children: Array.from({ length: 3 }, () => branch(depth - 1)) };
tooMany.components[0].root.children = Array.from({ length: 3 }, () => branch(5));
errorCode(() => parseSkinPluginDocument(tooMany), "SKIN_PLUGIN_COMPLEXITY_LIMIT");
});
+364
View File
@@ -0,0 +1,364 @@
/** Safe, declarative UI extensions shipped inside a v3 skin package.
* Extension authors provide a bounded component tree and action declarations,
* never executable HTML, JavaScript, or browser APIs.
*/
export const SKIN_PLUGIN_SCHEMA_VERSION = 1 as const;
export const SKIN_PLUGIN_DOCUMENT_LIMIT_BYTES = 256 * 1024;
export const SKIN_PLUGIN_COMPONENT_LIMIT = 32;
export const SKIN_PLUGIN_NODE_LIMIT = 256;
export const SKIN_PLUGIN_TREE_DEPTH_LIMIT = 16;
export const SKIN_PLUGIN_REPEAT_LIMIT = 100;
export const SKIN_PLUGIN_PERMISSIONS = Object.freeze({
"session.status.read": Object.freeze({ policyVersion: 1, description: "Read the current connection and channel summary." }),
"session.channels.read": Object.freeze({ policyVersion: 1, description: "Read the public TeamSpeak channel tree; the demo page uses synthetic sample data." }),
"session.members.read": Object.freeze({ policyVersion: 1, description: "Read visible member names and speaking states; the demo page uses synthetic sample data." }),
"chat.channel.read": Object.freeze({ policyVersion: 1, description: "Read the current public text channel; the demo page uses synthetic sample data." }),
"chat.channel.send": Object.freeze({ policyVersion: 1, description: "Send to the current public text channel; the demo page only simulates this action." }),
"favorites.read": Object.freeze({ policyVersion: 1, description: "Read favorite labels and opaque local identifiers, without server addresses or credentials." }),
"favorites.switch": Object.freeze({ policyVersion: 1, description: "Select a saved TeamSpeak server using the host's connection flow." }),
"session.channel.join": Object.freeze({ policyVersion: 1, description: "Request joining a visible TeamSpeak channel through the host; the demo page only changes simulated state." }),
} as const);
export type SkinPluginPermission = keyof typeof SKIN_PLUGIN_PERMISSIONS;
export type SkinPluginPage = "home" | "voice" | "demo";
export type SkinPluginEvent = "click" | "dblclick" | "change" | "input" | "submit" | "keydown";
export type SkinPluginAction = "ui.setState" | "voice.joinChannel" | "favorites.switch" | "chat.sendMessage";
export interface SkinPluginActionDefinition {
type: SkinPluginAction;
args: Record<string, string | number | boolean>;
}
export interface SkinPluginNode {
tag?: string;
text?: string;
part?: string;
className?: string;
attributes?: Record<string, string | number | boolean>;
asset?: string;
bindValue?: string;
repeat?: { path: string; as: string };
when?: { path: string; equals?: string | number | boolean };
events?: Partial<Record<SkinPluginEvent, string>>;
children?: SkinPluginNode[];
}
export interface SkinPluginComponent {
id: string;
name: string;
page: SkinPluginPage;
accessibleName: string;
permissions: SkinPluginPermission[];
state?: Record<string, string | number | boolean>;
actions: Record<string, SkinPluginActionDefinition>;
root: SkinPluginNode;
}
export interface SkinPluginDocument {
schemaVersion: typeof SKIN_PLUGIN_SCHEMA_VERSION;
components: SkinPluginComponent[];
}
export interface SkinPluginApproval {
pluginId: string;
policyVersions: Partial<Record<SkinPluginPermission, number>>;
}
export class SkinPluginValidationError extends Error {
constructor(readonly code: string, message: string) {
super(message);
this.name = "SkinPluginValidationError";
}
}
const ELEMENTS = new Set([
"article", "aside", "button", "circle", "code", "dd", "details", "div", "dl", "dt", "em", "figcaption", "figure", "footer", "form",
"g", "h1", "h2", "h3", "h4", "header", "hr", "img", "input", "label", "li", "line", "main", "nav", "ol", "option",
"output", "p", "path", "polyline", "rect", "section", "select", "small", "span", "strong", "summary", "svg", "textarea", "time", "ul",
]);
const VOID_ELEMENTS = new Set(["circle", "hr", "img", "input", "line", "path", "polyline", "rect"]);
const ATTRIBUTES = new Set([
"alt", "aria-current", "aria-expanded", "aria-hidden", "aria-label", "aria-pressed", "aria-selected", "class", "colspan", "fill",
"height", "max", "min", "name", "placeholder", "role", "rowspan", "stroke", "stroke-linecap", "stroke-linejoin", "stroke-width",
"tabindex", "title", "type", "value", "viewbox", "width", "x", "x1", "x2", "y", "y1", "y2",
]);
const EVENTS = new Set<string>(["click", "dblclick", "change", "input", "submit", "keydown"]);
const ACTIONS = new Set<string>(["ui.setState", "voice.joinChannel", "favorites.switch", "chat.sendMessage"]);
const PERMISSIONS = new Set<string>(Object.keys(SKIN_PLUGIN_PERMISSIONS));
const ACTION_ARGS: Record<SkinPluginAction, ReadonlySet<string>> = {
"ui.setState": new Set(["key", "value"]),
"voice.joinChannel": new Set(["channelId"]),
"favorites.switch": new Set(["favoriteId"]),
"chat.sendMessage": new Set(["text"]),
};
const ACTION_PERMISSION: Record<SkinPluginAction, SkinPluginPermission | null> = {
"ui.setState": null,
"voice.joinChannel": "session.channel.join",
"favorites.switch": "favorites.switch",
"chat.sendMessage": "chat.channel.send",
};
const COLLECTION_PERMISSION: Record<string, SkinPluginPermission> = {
"session.channels": "session.channels.read",
"session.members": "session.members.read",
"favorites.items": "favorites.read",
"chat.messages": "chat.channel.read",
};
const COLLECTION_FIELDS: Record<string, ReadonlySet<string>> = {
"session.channels": new Set(["id", "name", "parentId", "depth", "memberCount", "current"]),
"session.members": new Set(["id", "name", "channelId", "status", "speaking", "self"]),
"favorites.items": new Set(["id", "label", "current", "kind"]),
"chat.messages": new Set(["id", "author", "text", "time", "kind", "channelId"]),
};
const SESSION_STATUS_FIELDS = new Set(["connected", "connecting", "serverLabel", "channelId", "channelName", "userName"]);
function fail(code: string, message: string): never { throw new SkinPluginValidationError(code, message); }
function isRecord(value: unknown): value is Record<string, unknown> { return Boolean(value) && typeof value === "object" && !Array.isArray(value); }
function onlyKeys(value: Record<string, unknown>, allowed: ReadonlySet<string>, context: string): void {
const unknown = Object.keys(value).find((key) => !allowed.has(key));
if (unknown) fail("SKIN_PLUGIN_FIELD_INVALID", `${context} contains an unsupported field: ${unknown}.`);
}
function boundedText(value: unknown, field: string, max = 160): string {
if (typeof value !== "string" || !value.trim() || value.length > max || value !== value.trim()) fail("SKIN_PLUGIN_FIELD_INVALID", `${field} must be a non-empty bounded string.`);
return value;
}
function identifier(value: unknown, field: string): string {
const result = boundedText(value, field, 64);
if (!/^[a-z][a-z0-9-]*$/.test(result)) fail("SKIN_PLUGIN_FIELD_INVALID", `${field} must use a lowercase identifier.`);
return result;
}
function bindingPermissions(value: string, aliases: ReadonlyMap<string, string>, stateKeys: ReadonlySet<string>): SkinPluginPermission[] {
const found = new Set<SkinPluginPermission>();
for (const match of value.matchAll(/\{\{\s*([a-z][a-z0-9]*(?:\.[a-z][a-zA-Z0-9]*){0,5})\s*\}\}/g)) {
const path = match[1].split(".");
if (path[0] === "state") {
if (path.length !== 2 || !stateKeys.has(path[1])) fail("SKIN_PLUGIN_BINDING_INVALID", "A state binding must reference a declared local state key.");
continue;
}
const aliasCollection = aliases.get(path[0]);
if (aliasCollection) {
if (path.length !== 2 || !COLLECTION_FIELDS[aliasCollection]?.has(path[1])) fail("SKIN_PLUGIN_BINDING_INVALID", "A repeated item binding references an unsupported public field.");
continue;
}
if (path[0] === "session" && path.length === 3 && path[1] === "status" && SESSION_STATUS_FIELDS.has(path[2])) {
found.add("session.status.read");
continue;
}
const collectionPath = path.slice(0, 2).join(".");
const permission = COLLECTION_PERMISSION[collectionPath];
if (permission && (path.length === 2 || (path.length === 3 && COLLECTION_FIELDS[collectionPath].has(path[2])))) {
found.add(permission);
continue;
}
fail("SKIN_PLUGIN_BINDING_INVALID", `The data path ${match[1]} is not available to skin components.`);
}
if (/\{\{[^}]*\}\}/.test(value.replace(/\{\{\s*[a-z][a-z0-9]*(?:\.[a-z][a-zA-Z0-9]*){0,5}\s*\}\}/g, ""))) {
fail("SKIN_PLUGIN_BINDING_INVALID", "A component binding must use a simple registered data path.");
}
return [...found];
}
export function parseSkinPluginDocument(input: unknown): SkinPluginDocument {
if (!isRecord(input)) fail("SKIN_PLUGIN_DOCUMENT_INVALID", "components.json must contain an object.");
onlyKeys(input, new Set(["schemaVersion", "components"]), "components.json");
if (input.schemaVersion !== SKIN_PLUGIN_SCHEMA_VERSION || !Array.isArray(input.components)
|| !input.components.length || input.components.length > SKIN_PLUGIN_COMPONENT_LIMIT) {
fail("SKIN_PLUGIN_DOCUMENT_INVALID", "components.json uses an unsupported schema or component count.");
}
const ids = new Set<string>();
const components = input.components.map((rawComponent, componentIndex): SkinPluginComponent => {
if (!isRecord(rawComponent)) fail("SKIN_PLUGIN_COMPONENT_INVALID", `Component ${componentIndex} must be an object.`);
onlyKeys(rawComponent, new Set(["id", "name", "page", "accessibleName", "permissions", "actions", "state", "root"]), "Skin plugin component");
const id = identifier(rawComponent.id, "component.id");
if (ids.has(id)) fail("SKIN_PLUGIN_COMPONENT_DUPLICATE", "Component IDs must be unique within the skin.");
ids.add(id);
if (!(rawComponent.page === "home" || rawComponent.page === "voice" || rawComponent.page === "demo")) fail("SKIN_PLUGIN_COMPONENT_INVALID", "A plugin component must target a public page.");
const page = rawComponent.page as SkinPluginPage;
const name = boundedText(rawComponent.name, "component.name", 80);
const accessibleName = boundedText(rawComponent.accessibleName, "component.accessibleName", 120);
if (!Array.isArray(rawComponent.permissions) || rawComponent.permissions.length > 8) fail("SKIN_PLUGIN_PERMISSION_INVALID", "Each component may request at most eight explicit permissions.");
const permissions: SkinPluginPermission[] = [];
for (const permission of rawComponent.permissions) {
if (typeof permission !== "string" || !PERMISSIONS.has(permission)) fail("SKIN_PLUGIN_PERMISSION_UNKNOWN", "The component requests an unsupported permission.");
if (permissions.includes(permission as SkinPluginPermission)) fail("SKIN_PLUGIN_PERMISSION_DUPLICATE", "A component permission is duplicated.");
permissions.push(permission as SkinPluginPermission);
}
const state: Record<string, string | number | boolean> = Object.create(null) as Record<string, string | number | boolean>;
if (rawComponent.state !== undefined) {
if (!isRecord(rawComponent.state) || Object.keys(rawComponent.state).length > 32) fail("SKIN_PLUGIN_STATE_INVALID", "A component may declare up to 32 local state values.");
for (const [key, value] of Object.entries(rawComponent.state)) {
identifier(key, "state.key");
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") fail("SKIN_PLUGIN_STATE_INVALID", "Component state values must be scalar.");
if (typeof value === "string" && value.length > 512) fail("SKIN_PLUGIN_STATE_INVALID", "A component state string exceeds its limit.");
state[key] = value;
}
}
const stateKeys = new Set(Object.keys(state));
if (!isRecord(rawComponent.actions)) fail("SKIN_PLUGIN_ACTION_INVALID", "A component must declare an action map.");
const actionEntries = Object.entries(rawComponent.actions);
if (actionEntries.length > 24) fail("SKIN_PLUGIN_ACTION_INVALID", "A component declares too many actions.");
const actions: Record<string, SkinPluginActionDefinition> = Object.create(null) as Record<string, SkinPluginActionDefinition>;
for (const [actionId, rawAction] of actionEntries) {
identifier(actionId, "action.id");
if (!isRecord(rawAction)) fail("SKIN_PLUGIN_ACTION_INVALID", "An action must be an object.");
onlyKeys(rawAction, new Set(["type", "args"]), "Component action");
if (typeof rawAction.type !== "string" || !ACTIONS.has(rawAction.type)) fail("SKIN_PLUGIN_ACTION_UNKNOWN", "An action type is not supported by the host.");
if (!isRecord(rawAction.args)) fail("SKIN_PLUGIN_ACTION_INVALID", "An action must define an argument object.");
const requiredPermission = ACTION_PERMISSION[rawAction.type as SkinPluginAction];
if (requiredPermission && !permissions.includes(requiredPermission)) fail("SKIN_PLUGIN_PERMISSION_MISSING", `The ${rawAction.type} action requires ${requiredPermission}.`);
if (Object.keys(rawAction.args).length > 8) fail("SKIN_PLUGIN_ACTION_INVALID", "An action has too many arguments.");
const args: Record<string, string | number | boolean> = Object.create(null) as Record<string, string | number | boolean>;
for (const [key, value] of Object.entries(rawAction.args)) {
if (!/^[a-z][a-zA-Z0-9]{0,31}$/.test(key) || !ACTION_ARGS[rawAction.type as SkinPluginAction].has(key)) fail("SKIN_PLUGIN_ACTION_INVALID", "An action uses an unsupported argument name.");
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") fail("SKIN_PLUGIN_ACTION_INVALID", "Action arguments must be static scalars or simple data bindings.");
if (typeof value === "string") {
if (value.length > 512) fail("SKIN_PLUGIN_ACTION_INVALID", "An action argument exceeds its length limit.");
}
args[key] = value;
}
if (rawAction.type === "ui.setState" && (typeof args.key !== "string" || !/^[a-z][a-z0-9_-]{0,31}$/.test(args.key) || !stateKeys.has(args.key) || !("value" in args))) {
fail("SKIN_PLUGIN_ACTION_INVALID", "ui.setState requires a local state key and value.");
}
if (rawAction.type === "voice.joinChannel" && typeof args.channelId !== "string") fail("SKIN_PLUGIN_ACTION_INVALID", "voice.joinChannel requires a channelId binding.");
if (rawAction.type === "favorites.switch" && typeof args.favoriteId !== "string") fail("SKIN_PLUGIN_ACTION_INVALID", "favorites.switch requires a favoriteId binding.");
if (rawAction.type === "chat.sendMessage" && typeof args.text !== "string") fail("SKIN_PLUGIN_ACTION_INVALID", "chat.sendMessage requires a text binding.");
actions[actionId] = { type: rawAction.type as SkinPluginAction, args };
}
let componentNodes = 0;
const walk = (rawNode: unknown, depth: number, aliases: ReadonlyMap<string, string> = new Map()): SkinPluginNode => {
componentNodes += 1;
if (componentNodes > SKIN_PLUGIN_NODE_LIMIT) fail("SKIN_PLUGIN_COMPLEXITY_LIMIT", "A component contains too many template nodes.");
if (depth > SKIN_PLUGIN_TREE_DEPTH_LIMIT || !isRecord(rawNode)) fail("SKIN_PLUGIN_TREE_INVALID", "A component tree is too deep or contains an invalid node.");
onlyKeys(rawNode, new Set(["tag", "text", "part", "className", "attributes", "asset", "bindValue", "repeat", "when", "events", "children"]), "Component node");
if ((rawNode.tag === undefined) === (rawNode.text === undefined)) fail("SKIN_PLUGIN_TREE_INVALID", "Each component node must define either a tag or text, but not both.");
const node: SkinPluginNode = {};
if (rawNode.tag !== undefined) {
if (typeof rawNode.tag !== "string" || !ELEMENTS.has(rawNode.tag)) fail("SKIN_PLUGIN_ELEMENT_INVALID", "The component uses an unsupported HTML element.");
node.tag = rawNode.tag;
}
if (rawNode.text !== undefined) {
node.text = boundedText(rawNode.text, "node.text", 2048);
for (const permission of bindingPermissions(node.text, aliases, stateKeys)) {
if (!permissions.includes(permission)) fail("SKIN_PLUGIN_PERMISSION_MISSING", `The component data requires ${permission}.`);
}
}
if (rawNode.part !== undefined) node.part = identifier(rawNode.part, "node.part");
if (rawNode.className !== undefined) {
if (typeof rawNode.className !== "string" || rawNode.className.length > 160 || /[<>]/.test(rawNode.className)) fail("SKIN_PLUGIN_ATTRIBUTE_INVALID", "Component class names must be bounded plain text.");
node.className = rawNode.className;
}
if (rawNode.attributes !== undefined) {
if (!isRecord(rawNode.attributes) || Object.keys(rawNode.attributes).length > 24) fail("SKIN_PLUGIN_ATTRIBUTE_INVALID", "Component attributes must be a small object.");
const attrs: Record<string, string | number | boolean> = Object.create(null) as Record<string, string | number | boolean>;
for (const [key, value] of Object.entries(rawNode.attributes)) {
const lowerKey = key.toLowerCase();
if (!ATTRIBUTES.has(lowerKey) || lowerKey.startsWith("on") || key !== lowerKey) fail("SKIN_PLUGIN_ATTRIBUTE_INVALID", "The component uses an unsupported or executable attribute.");
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") fail("SKIN_PLUGIN_ATTRIBUTE_INVALID", "Component attributes must be scalar values.");
if (typeof value === "string" && ["fill", "stroke"].includes(lowerKey) && /\burl\s*\(|javascript\s*:/i.test(value)) fail("SKIN_PLUGIN_ATTRIBUTE_INVALID", "SVG paint attributes cannot load external or executable resources.");
if (typeof value === "string") {
for (const permission of bindingPermissions(value, aliases, stateKeys)) {
if (!permissions.includes(permission)) fail("SKIN_PLUGIN_PERMISSION_MISSING", `The component data requires ${permission}.`);
}
if (value.length > 512) fail("SKIN_PLUGIN_ATTRIBUTE_INVALID", "A component attribute exceeds its length limit.");
}
attrs[key] = value;
}
node.attributes = attrs;
}
if (rawNode.asset !== undefined) {
const asset = boundedText(rawNode.asset, "node.asset", 120);
if (node.tag !== "img") fail("SKIN_PLUGIN_ASSET_INVALID", "Package image assets can only be attached to img elements.");
if (!/^assets\/[a-zA-Z0-9._/-]+$/.test(asset) || asset.split("/").some((part) => !part || part === "." || part === "..")) fail("SKIN_PLUGIN_ASSET_INVALID", "Component assets must use safe package-relative paths.");
node.asset = asset;
}
if (rawNode.bindValue !== undefined) {
const key = identifier(rawNode.bindValue, "node.bindValue");
if (node.tag !== "input" && node.tag !== "select" && node.tag !== "textarea") fail("SKIN_PLUGIN_BINDING_INVALID", "Only input, select, and textarea elements can bind local state.");
if (!stateKeys.has(key)) fail("SKIN_PLUGIN_BINDING_INVALID", "A bound form control must reference a declared local state key.");
node.bindValue = key;
}
if (rawNode.repeat !== undefined) {
if (!isRecord(rawNode.repeat)) fail("SKIN_PLUGIN_REPEAT_INVALID", "A repeat declaration must be an object.");
onlyKeys(rawNode.repeat, new Set(["path", "as"]), "Repeat declaration");
const path = boundedText(rawNode.repeat.path, "repeat.path", 120);
if (!(path === "session.channels" || path === "session.members" || path === "favorites.items" || path === "chat.messages")) fail("SKIN_PLUGIN_REPEAT_INVALID", "A component may repeat only a registered public collection.");
const permission = COLLECTION_PERMISSION[path];
if (!permissions.includes(permission)) fail("SKIN_PLUGIN_PERMISSION_MISSING", `Repeating ${path} requires ${permission}.`);
const alias = identifier(rawNode.repeat.as, "repeat.as");
if (aliases.has(alias)) fail("SKIN_PLUGIN_REPEAT_INVALID", "Repeat aliases cannot shadow an existing alias.");
node.repeat = { path, as: alias };
}
if (rawNode.when !== undefined) {
if (!isRecord(rawNode.when)) fail("SKIN_PLUGIN_CONDITION_INVALID", "A condition must be an object.");
onlyKeys(rawNode.when, new Set(["path", "equals"]), "Component condition");
const path = boundedText(rawNode.when.path, "condition.path", 120);
for (const permission of bindingPermissions(`{{${path}}}`, aliases, stateKeys)) {
if (!permissions.includes(permission)) fail("SKIN_PLUGIN_PERMISSION_MISSING", `The condition data requires ${permission}.`);
}
const equals = rawNode.when.equals;
if (equals !== undefined && typeof equals !== "string" && typeof equals !== "number" && typeof equals !== "boolean") fail("SKIN_PLUGIN_CONDITION_INVALID", "A condition comparison must be scalar.");
node.when = { path, ...(equals === undefined ? {} : { equals }) };
}
if (rawNode.events !== undefined) {
if (!isRecord(rawNode.events) || Object.keys(rawNode.events).length > 6) fail("SKIN_PLUGIN_EVENT_INVALID", "Component events must be a small object.");
const events: Partial<Record<SkinPluginEvent, string>> = {};
for (const [event, actionIdValue] of Object.entries(rawNode.events)) {
if (!EVENTS.has(event) || typeof actionIdValue !== "string" || !actions[actionIdValue]) fail("SKIN_PLUGIN_EVENT_INVALID", "Each component event must refer to a declared host action.");
const action = actions[actionIdValue];
if (action.type !== "ui.setState" && (event === "input" || event === "change")) fail("SKIN_PLUGIN_EVENT_INVALID", "Host actions cannot run while a form value is being typed or changed.");
for (const argument of Object.values(action.args)) {
if (typeof argument !== "string") continue;
for (const permission of bindingPermissions(argument, aliases, stateKeys)) {
if (!permissions.includes(permission)) fail("SKIN_PLUGIN_PERMISSION_MISSING", `The event data requires ${permission}.`);
}
}
events[event as SkinPluginEvent] = actionIdValue;
}
node.events = events;
}
if (rawNode.children !== undefined) {
if (!Array.isArray(rawNode.children) || rawNode.children.length > 100) fail("SKIN_PLUGIN_TREE_INVALID", "A component node contains too many children.");
if (node.tag && VOID_ELEMENTS.has(node.tag) && rawNode.children.length) fail("SKIN_PLUGIN_TREE_INVALID", "A void element cannot contain child nodes.");
const childAliases = new Map(aliases);
if (node.repeat) childAliases.set(node.repeat.as, node.repeat.path);
node.children = rawNode.children.map((child) => walk(child, depth + 1, childAliases));
}
return node;
};
const root = walk(rawComponent.root, 1);
if (!root.tag) fail("SKIN_PLUGIN_TREE_INVALID", "A plugin component root must be an element.");
return { id, name, page, accessibleName, permissions, actions, ...(Object.keys(state).length ? { state } : {}), root };
});
return { schemaVersion: SKIN_PLUGIN_SCHEMA_VERSION, components };
}
export function parseSkinPluginJson(source: string): SkinPluginDocument {
if (!source || new TextEncoder().encode(source).byteLength > SKIN_PLUGIN_DOCUMENT_LIMIT_BYTES) fail("SKIN_PLUGIN_DOCUMENT_SIZE", "components.json exceeds the 256 KiB limit.");
let input: unknown;
try { input = JSON.parse(source); } catch { fail("SKIN_PLUGIN_DOCUMENT_INVALID", "components.json must contain valid JSON."); }
return parseSkinPluginDocument(input);
}
export function listSkinPluginAssetPaths(document: SkinPluginDocument): string[] {
const paths = new Set<string>();
const visit = (node: SkinPluginNode) => {
if (node.asset) paths.add(node.asset);
node.children?.forEach(visit);
};
document.components.forEach((component) => visit(component.root));
return [...paths];
}
export function createSkinPluginApproval(pluginId: string, permissions: readonly SkinPluginPermission[]): SkinPluginApproval {
const policyVersions: Partial<Record<SkinPluginPermission, number>> = {};
for (const permission of permissions) policyVersions[permission] = SKIN_PLUGIN_PERMISSIONS[permission].policyVersion;
return { pluginId, policyVersions };
}
export function isSkinPluginPermissionApproved(approval: SkinPluginApproval | null | undefined, pluginId: string, permission: SkinPluginPermission): boolean {
return approval?.pluginId === pluginId && approval.policyVersions[permission] === SKIN_PLUGIN_PERMISSIONS[permission].policyVersion;
}
export function skinPluginActionPermission(action: SkinPluginAction): SkinPluginPermission | null {
return ACTION_PERMISSION[action];
}