mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 12:02:50 +08:00
feat(skin): validate v4 plugin packages
This commit is contained in:
1 parent
17e4d78e52
commit
81c35356b4
11 files changed
+595
-39
No files matched your search
@@ -177,6 +177,77 @@ test("server accepts v3 declarative components and rejects unapproved capability
|
||||
await assert.rejects(registry.save(unsafeArchive, "sample-skin"), (error: unknown) => error instanceof SkinRegistryError && error.code === "SKIN_PLUGIN_INVALID");
|
||||
});
|
||||
|
||||
test("server accepts a v3 open visual skin without a components document", async (context) => {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), "webspeak-skin-v3-visual-only-"));
|
||||
context.after(() => rm(directory, { recursive: true, force: true }));
|
||||
const registry = new SkinRegistry(directory);
|
||||
const archive = createZip([
|
||||
["manifest.json", Buffer.from(JSON.stringify({ ...manifest, schemaVersion: 3, packageType: "open-skin", content: undefined, preview: undefined }))],
|
||||
["skin.css", Buffer.from('[data-ws-part="app"] { position: fixed; }')],
|
||||
]);
|
||||
const saved = await registry.save(archive, "sample-skin");
|
||||
assert.equal(saved.id, "sample-skin");
|
||||
assert.deepEqual(await registry.readArchive("sample-skin"), archive);
|
||||
});
|
||||
|
||||
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 }));
|
||||
const registry = new SkinRegistry(directory);
|
||||
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: ["plugins/voice-toolbar/assets/icon.png"],
|
||||
permissions: ["session.channels.read"],
|
||||
}],
|
||||
};
|
||||
const archive = createZip([
|
||||
["manifest.json", Buffer.from(JSON.stringify(manifestV4))],
|
||||
["skin.css", Buffer.from('[data-ws-part="app"] { position: fixed; }')],
|
||||
["plugins.json", Buffer.from(JSON.stringify(plugins))],
|
||||
["plugins/voice-toolbar/index.js", Buffer.from("export const render = () => document.createElement('button');")],
|
||||
["plugins/voice-toolbar/style.css", Buffer.from(".toolbar { color: teal; }")],
|
||||
["plugins/voice-toolbar/assets/icon.png", Buffer.from([1, 2, 3, 4])],
|
||||
]);
|
||||
const saved = await registry.save(archive, "sample-skin");
|
||||
assert.equal(saved.id, "sample-skin");
|
||||
assert.deepEqual(await registry.readArchive("sample-skin"), archive);
|
||||
|
||||
const missingEntry = 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))],
|
||||
]);
|
||||
await assert.rejects(registry.save(missingEntry, "sample-skin"), (error: unknown) => error instanceof SkinRegistryError && error.code === "SKIN_RUNTIME_PLUGIN_FILE_MISSING");
|
||||
|
||||
const unlistedScript = 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 { color: teal; }")],
|
||||
["plugins/voice-toolbar/assets/icon.png", Buffer.from([1, 2, 3, 4])],
|
||||
["plugins/other/rogue.js", Buffer.from("export {};")],
|
||||
]);
|
||||
await assert.rejects(registry.save(unlistedScript, "sample-skin"), (error: unknown) => error instanceof SkinRegistryError && error.code === "SKIN_FILE_UNSUPPORTED");
|
||||
});
|
||||
|
||||
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 }));
|
||||
|
||||
+57
-10
@@ -1,6 +1,14 @@
|
||||
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 {
|
||||
listSkinRuntimePluginFilePaths,
|
||||
parseSkinRuntimePluginJson,
|
||||
SKIN_RUNTIME_PLUGIN_DOCUMENT_LIMIT_BYTES,
|
||||
SKIN_RUNTIME_PLUGIN_SOURCE_LIMIT_BYTES,
|
||||
SKIN_RUNTIME_PLUGIN_STYLE_LIMIT_BYTES,
|
||||
type SkinRuntimePluginDocument,
|
||||
} from "../shared/skin-runtime-plugins.js";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { mkdir, readFile, readdir, rename, stat, unlink, writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
@@ -42,12 +50,13 @@ interface SkinArchiveEntry {
|
||||
}
|
||||
|
||||
type ParsedSkinManifest = Omit<SkinCatalogEntry, "installedAt" | "previewUrl" | "previewMimeType" | "enabled"> & {
|
||||
schemaVersion: 1 | 2 | 3;
|
||||
schemaVersion: 1 | 2 | 3 | 4;
|
||||
packageType?: "layout-skin" | "open-skin";
|
||||
entry: string;
|
||||
layout?: string;
|
||||
content?: string;
|
||||
components?: string;
|
||||
plugins?: string;
|
||||
preview?: string;
|
||||
};
|
||||
|
||||
@@ -225,10 +234,45 @@ function validateSkinArchive(bytes: Buffer, expectedId?: string): { manifest: Pa
|
||||
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.");
|
||||
|
||||
let runtimePlugins: SkinRuntimePluginDocument | undefined;
|
||||
if (manifest.plugins) {
|
||||
const pluginsBytes = fileBytes.get(manifest.plugins);
|
||||
if (!pluginsBytes || pluginsBytes.byteLength > SKIN_RUNTIME_PLUGIN_DOCUMENT_LIMIT_BYTES) {
|
||||
throw new SkinRegistryError("plugins.json is missing or exceeds 256 KiB.", "SKIN_RUNTIME_PLUGIN_SIZE");
|
||||
}
|
||||
try { runtimePlugins = parseSkinRuntimePluginJson(decodeUtf8(pluginsBytes, "plugins.json is not valid UTF-8.")); }
|
||||
catch (error) { throw new SkinRegistryError("plugins.json is invalid: " + (error instanceof Error ? error.message : "unknown plugin error"), "SKIN_RUNTIME_PLUGIN_INVALID"); }
|
||||
}
|
||||
const runtimePluginPaths = runtimePlugins ? listSkinRuntimePluginFilePaths(runtimePlugins) : [];
|
||||
const runtimePluginPathSet = new Set(runtimePluginPaths.map((packagePath) => packagePath.toLowerCase()));
|
||||
const manifestPathSet = ["manifest.json", manifest.entry, manifest.layout, manifest.content, manifest.components, manifest.plugins, manifest.preview]
|
||||
.filter((packagePath): packagePath is string => Boolean(packagePath)).map((packagePath) => packagePath.toLowerCase());
|
||||
if (runtimePluginPaths.some((packagePath) => manifestPathSet.includes(packagePath))) {
|
||||
throw new SkinRegistryError("Plugin files cannot reuse skin manifest, CSS, content, component, or preview paths.", "SKIN_RUNTIME_PLUGIN_FILE_DUPLICATE");
|
||||
}
|
||||
for (const pluginPath of runtimePluginPaths) {
|
||||
const data = fileBytes.get(pluginPath);
|
||||
if (!data) throw new SkinRegistryError(`A declared plugin file is missing: ${pluginPath}`, "SKIN_RUNTIME_PLUGIN_FILE_MISSING");
|
||||
const lowerPath = pluginPath.toLowerCase();
|
||||
if (lowerPath.endsWith(".js")) {
|
||||
if (data.byteLength > SKIN_RUNTIME_PLUGIN_SOURCE_LIMIT_BYTES) throw new SkinRegistryError("Plugin entry files must be smaller than 256 KiB.", "SKIN_RUNTIME_PLUGIN_SOURCE_SIZE");
|
||||
decodeUtf8(data, "Plugin entry files must contain valid UTF-8.");
|
||||
} else if (lowerPath.endsWith(".css")) {
|
||||
if (data.byteLength > SKIN_RUNTIME_PLUGIN_STYLE_LIMIT_BYTES) throw new SkinRegistryError("Plugin style files must be smaller than 512 KiB.", "SKIN_RUNTIME_PLUGIN_STYLE_SIZE");
|
||||
decodeUtf8(data, "Plugin style files must contain valid UTF-8.");
|
||||
} else {
|
||||
const mimeType = imageMime(pluginPath) ?? (lowerPath.endsWith(".woff2") ? "font/woff2" : null);
|
||||
if (!mimeType) throw new SkinRegistryError(`Unsupported plugin asset: ${pluginPath}`, "SKIN_RUNTIME_PLUGIN_ASSET_INVALID");
|
||||
if (mimeType.startsWith("image/") && data.byteLength > MAX_IMAGE_BYTES) throw new SkinRegistryError("Plugin images must be smaller than 16 MiB.", "SKIN_IMAGE_SIZE");
|
||||
if (mimeType.startsWith("font/") && data.byteLength > MAX_FONT_BYTES) throw new SkinRegistryError("Plugin fonts must be smaller than 4 MiB.", "SKIN_FONT_SIZE");
|
||||
}
|
||||
}
|
||||
|
||||
let previewBytes: Buffer | undefined;
|
||||
let previewMime: string | undefined;
|
||||
for (const [packagePath, data] of fileBytes) {
|
||||
if (["manifest.json", manifest.entry, manifest.layout, manifest.content, manifest.components, manifest.preview].includes(packagePath)) continue;
|
||||
if (["manifest.json", manifest.entry, manifest.layout, manifest.content, manifest.components, manifest.plugins, manifest.preview].includes(packagePath)
|
||||
|| runtimePluginPathSet.has(packagePath.toLowerCase())) 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");
|
||||
@@ -389,10 +433,11 @@ 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 && schemaVersion !== 3) throw new SkinRegistryError("The skin schema version or ID is unsupported.", "SKIN_SCHEMA_VERSION");
|
||||
if (schemaVersion !== 1 && schemaVersion !== 2 && schemaVersion !== 3 && schemaVersion !== 4) 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"); }
|
||||
if (schemaVersion >= 3) { fields.delete("permissions"); fields.add("components"); }
|
||||
if (schemaVersion === 4) fields.add("plugins");
|
||||
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");
|
||||
}
|
||||
@@ -407,7 +452,8 @@ function parseManifest(bytes: Buffer): ParsedSkinManifest {
|
||||
throw new SkinRegistryError("Layout skin packages must declare the layout-skin type and no executable permissions.", "SKIN_PERMISSION_INVALID");
|
||||
}
|
||||
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)) {
|
||||
if (schemaVersion === 4 && value.packageType !== "open-skin") throw new SkinRegistryError("Version 4 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 || value.plugins !== undefined)) {
|
||||
throw new SkinRegistryError("Version 1 skin packages cannot declare layout or plugin fields.", "SKIN_SCHEMA_VERSION");
|
||||
}
|
||||
const version = read("version", 32);
|
||||
@@ -417,14 +463,15 @@ function parseManifest(bytes: Buffer): ParsedSkinManifest {
|
||||
const readOptional = (key: string, max: number): string | undefined => value[key] == null ? undefined : read(key, max);
|
||||
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 components = schemaVersion >= 3 && value.components != null ? read("components", 120) : undefined;
|
||||
const plugins = schemaVersion === 4 ? read("plugins", 120) : undefined;
|
||||
const preview = readOptional("preview", 120);
|
||||
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());
|
||||
for (const candidate of [entry, layout, content, components, plugins, 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")) || (plugins && !plugins.toLowerCase().endsWith(".json")) || (preview && !imageMime(preview))) throw new SkinRegistryError("The CSS entry, layout, content, plugin, or preview path is invalid.", "SKIN_MANIFEST_INVALID");
|
||||
const declaredFiles = ["manifest.json", entry, ...(layout ? [layout] : []), ...(content ? [content] : []), ...(components ? [components] : []), ...(plugins ? [plugins] : []), ...(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 } : 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 } : {}) };
|
||||
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 } : {}), ...(plugins ? { plugins } : {}), ...(preview ? { preview } : {}), ...(description ? { description } : {}) };
|
||||
}
|
||||
|
||||
function singleRootPrefix(paths: string[]): string {
|
||||
|
||||
Reference in new issue
Block a user