feat(skins): complete visual component CRUD authoring

This commit is contained in:
illusia committed 2026-10-10 15:53:51 +08:00
1 parent 3fd8e76a98
commit f2e5695bb6
30 files changed
+1858 -511

No files matched your search

+56 -4
View File
@@ -26,7 +26,7 @@ test("skin registry validates, lists, replaces, serves, and removes packages", a
assert.equal(await registry.readArchive("sample-skin"), null);
const firstArchive = createZip([
["manifest.json", Buffer.from(JSON.stringify(manifest))],
["skin.css", Buffer.from(".voice-card { background: url(assets/background.png); }")],
["skin.css", Buffer.from('[data-ws-part="voice.member"] { background: url(assets/background.png); }')],
["content.json", Buffer.from(JSON.stringify({ defaultLocale: "en", locales: { en: { home: { title: "Hello" } } } }))],
["assets/background.png", Buffer.from([1, 2, 3, 4])],
["assets/preview.png", Buffer.from([5, 6, 7, 8])],
@@ -45,7 +45,7 @@ test("skin registry validates, lists, replaces, serves, and removes packages", a
const replacement = { ...manifest, version: "2.0.0", content: undefined, preview: undefined };
const secondArchive = createZip([
["manifest.json", Buffer.from(JSON.stringify(replacement))],
["skin.css", Buffer.from(".voice-card { border-radius: 0; }")],
["skin.css", Buffer.from('[data-ws-part="voice.member"] { border-radius: 0; }')],
]);
const second = await registry.save(secondArchive, "sample-skin");
assert.equal(second.version, "2.0.0");
@@ -128,7 +128,7 @@ test("server accepts validated declarative v2 layouts and rejects permissions or
const unknownComponentArchive = createZip([
["manifest.json", Buffer.from(JSON.stringify(manifestV2))],
["skin.css", Buffer.from(".voice-card { color: teal; }")],
["skin.css", Buffer.from('[data-ws-part="voice.member"] { color: teal; }')],
["layout.json", Buffer.from(JSON.stringify({ schemaVersion: 1, pages: { home: { desktop: { "home.unknown": { x: 1 } } } } }))],
]);
await assert.rejects(registry.save(unknownComponentArchive, "sample-skin"), (error: unknown) => error instanceof SkinRegistryError && error.code === "SKIN_LAYOUT_INVALID");
@@ -190,6 +190,58 @@ test("server accepts a v3 open visual skin without a components document", async
assert.deepEqual(await registry.readArchive("sample-skin"), archive);
});
test("server applies the browser CSS scope and package-only resource policy to every stylesheet", async (context) => {
const directory = await mkdtemp(path.join(os.tmpdir(), "webspeak-skin-css-policy-"));
context.after(() => rm(directory, { recursive: true, force: true }));
const registry = new SkinRegistry(directory);
const unsafeLegacySelector = createZip([
["manifest.json", Buffer.from(JSON.stringify({ ...manifest, content: undefined, preview: undefined }))],
["skin.css", Buffer.from(".private-class { color: teal; }")],
]);
await assert.rejects(registry.save(unsafeLegacySelector, "sample-skin"), (error: unknown) =>
error instanceof SkinRegistryError && error.code === "SKIN_CSS_SELECTOR");
const remoteSkinCss = createZip([
["manifest.json", Buffer.from(JSON.stringify({ ...manifest, content: undefined, preview: undefined }))],
["skin.css", Buffer.from('[data-ws-part="app"] { background-image: url("https://example.invalid/track.png"); }')],
]);
await assert.rejects(registry.save(remoteSkinCss, "sample-skin"), (error: unknown) =>
error instanceof SkinRegistryError && error.code === "SKIN_EXTERNAL_RESOURCE");
const manifestV4 = {
...manifest,
schemaVersion: 4,
packageType: "open-skin",
content: undefined,
preview: undefined,
plugins: "plugins.json",
};
const plugins = {
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: [],
permissions: [],
}],
};
const remotePluginCss = createZip([
["manifest.json", Buffer.from(JSON.stringify(manifestV4))],
["skin.css", Buffer.from('[data-ws-part="app"] { color: teal; }')],
["plugins.json", Buffer.from(JSON.stringify(plugins))],
["plugins/voice-toolbar/index.js", Buffer.from("export {}; ")],
["plugins/voice-toolbar/style.css", Buffer.from('.toolbar { background-image: u\\72l("https://example.invalid/track.png"); }')],
]);
await assert.rejects(registry.save(remotePluginCss, "sample-skin"), (error: unknown) =>
error instanceof SkinRegistryError && error.code === "SKIN_EXTERNAL_RESOURCE");
});
test("server validates v4 plugin manifests and package files without executing author code", async (context) => {
const directory = await mkdtemp(path.join(os.tmpdir(), "webspeak-skin-v4-"));
context.after(() => rm(directory, { recursive: true, force: true }));
@@ -308,7 +360,7 @@ test("skin registry rejects path traversal and mismatched local ZIP headers", as
const registry = new SkinRegistry(directory);
const goodFiles: Array<[string, Buffer]> = [
["manifest.json", Buffer.from(JSON.stringify({ ...manifest, content: undefined, preview: undefined }))],
["skin.css", Buffer.from(".voice-card { color: teal; }")],
["skin.css", Buffer.from('[data-ws-part="voice.member"] { color: teal; }')],
];
await assert.rejects(registry.save(createZip([["../escape.png", Buffer.from([1])], ...goodFiles]), "sample-skin"), (error: unknown) => error instanceof SkinRegistryError && error.code === "SKIN_PATH_INVALID");
+35 -1
View File
@@ -1,4 +1,5 @@
import type { SkinCatalogEntry as SharedSkinCatalogEntry } from "../shared/skin-catalog.js";
import { compileSkinCss, SkinCssValidationError, type SkinCssSchemaVersion } from "../shared/skin-css.js";
import { parseSkinLayoutJson } from "../shared/skin-layout.js";
import { listSkinPluginAssetPaths, parseSkinPluginJson, type SkinPluginDocument } from "../shared/skin-plugin.js";
import {
@@ -232,7 +233,7 @@ function validateSkinArchive(bytes: Buffer, expectedId?: string): { manifest: Pa
if (expectedId && manifest.id !== expectedId) throw new SkinRegistryError("The skin ID does not match the upload target.", "SKIN_ID_MISMATCH");
const cssBytes = fileBytes.get(manifest.entry);
if (!cssBytes || !cssBytes.byteLength || cssBytes.byteLength > MAX_CSS_BYTES) throw new SkinRegistryError("The CSS entry is missing or exceeds 512 KiB.", "SKIN_CSS_SIZE");
decodeUtf8(cssBytes, "The CSS entry is not valid UTF-8.");
const cssSource = decodeUtf8(cssBytes, "The CSS entry is not valid UTF-8.");
let runtimePlugins: SkinRuntimePluginDocument | undefined;
if (manifest.plugins) {
@@ -280,6 +281,23 @@ function validateSkinArchive(bytes: Buffer, expectedId?: string): { manifest: Pa
if (mimeType.startsWith("image/") && data.byteLength > MAX_IMAGE_BYTES) throw new SkinRegistryError("Each image must be smaller than 16 MiB.", "SKIN_IMAGE_SIZE");
if (mimeType.startsWith("font/") && data.byteLength > MAX_FONT_BYTES) throw new SkinRegistryError("Fonts must be smaller than 4 MiB.", "SKIN_FONT_SIZE");
}
const cssAssets = new Set([...fileBytes.keys()].filter((packagePath) =>
imageMime(packagePath) !== null || packagePath.toLowerCase().endsWith(".woff2")));
validateSkinCss(cssSource, manifest.id, cssAssets, manifest.schemaVersion);
for (const plugin of runtimePlugins?.plugins ?? []) {
if (!plugin.style) continue;
const styleBytes = fileBytes.get(plugin.style);
if (!styleBytes) throw new SkinRegistryError(`A declared plugin style is missing: ${plugin.style}`, "SKIN_RUNTIME_PLUGIN_FILE_MISSING");
const pluginCssAssets = new Set([...cssAssets].filter((packagePath) =>
packagePath.startsWith("assets/") || packagePath.startsWith(`plugins/${plugin.id}/assets/`)));
validateSkinCss(
decodeUtf8(styleBytes, "Plugin style files must contain valid UTF-8."),
manifest.id,
pluginCssAssets,
manifest.schemaVersion,
plugin.id,
);
}
if (manifest.content) {
const content = fileBytes.get(manifest.content);
if (!content || content.byteLength > MAX_CONTENT_BYTES) throw new SkinRegistryError("content.json is missing or exceeds 256 KiB.", "SKIN_CONTENT_SIZE");
@@ -311,6 +329,22 @@ function validateSkinArchive(bytes: Buffer, expectedId?: string): { manifest: Pa
return { manifest, ...(previewBytes ? { previewBytes, previewMime } : {}) };
}
function validateSkinCss(
source: string,
skinId: string,
assets: Set<string>,
schemaVersion: SkinCssSchemaVersion,
pluginId?: string,
): void {
try { compileSkinCss(source, skinId, assets, schemaVersion, pluginId); }
catch (error) {
const cssError = error instanceof SkinCssValidationError
? error
: new SkinCssValidationError(error instanceof Error ? error.message : "The CSS file is invalid.", "SKIN_CSS_INVALID");
throw new SkinRegistryError(cssError.message, cssError.code);
}
}
function inspectArchive(bytes: Buffer): SkinArchiveEntry[] {
if (bytes.byteLength < 22 || bytes.byteLength > MAX_ARCHIVE_BYTES) throw new SkinRegistryError("A skin package must be smaller than 20 MiB.", "SKIN_ARCHIVE_SIZE");
const minOffset = Math.max(0, bytes.byteLength - 22 - 0xffff);