mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 03:52: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
@@ -0,0 +1,93 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import {
|
||||
listSkinRuntimePluginFilePaths,
|
||||
parseSkinRuntimePluginDocument,
|
||||
parseSkinRuntimePluginJson,
|
||||
SkinRuntimePluginValidationError,
|
||||
SKIN_RUNTIME_PLUGIN_DOCUMENT_LIMIT_BYTES,
|
||||
} from "./skin-runtime-plugins.js";
|
||||
|
||||
const plugin = {
|
||||
id: "voice-toolbar",
|
||||
name: "Voice toolbar",
|
||||
version: "1.2.3",
|
||||
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"],
|
||||
};
|
||||
|
||||
function document(plugins: unknown[] = [plugin]): unknown {
|
||||
return { schemaVersion: 1, plugins };
|
||||
}
|
||||
|
||||
test("v4 plugin descriptors accept bounded local files and freeze their parsed data", () => {
|
||||
const parsed = parseSkinRuntimePluginDocument(document() as never);
|
||||
assert.deepEqual(listSkinRuntimePluginFilePaths(parsed), [
|
||||
"plugins/voice-toolbar/index.js",
|
||||
"plugins/voice-toolbar/style.css",
|
||||
"plugins/voice-toolbar/assets/icon.png",
|
||||
]);
|
||||
assert.equal(parsed.plugins[0].permissions[0], "session.channels.read");
|
||||
assert.ok(Object.isFrozen(parsed) && Object.isFrozen(parsed.plugins) && Object.isFrozen(parsed.plugins[0]));
|
||||
assert.ok(Object.isFrozen(parsed.plugins[0].assets) && Object.isFrozen(parsed.plugins[0].permissions));
|
||||
});
|
||||
|
||||
test("surface replacement is an explicit page-scoped plugin capability", () => {
|
||||
const surface = { ...plugin, mode: "surface", permissions: ["ui.surface.replace"] };
|
||||
const parsed = parseSkinRuntimePluginDocument(document([surface]) as never);
|
||||
assert.equal(parsed.plugins[0].mode, "surface");
|
||||
assert.throws(
|
||||
() => parseSkinRuntimePluginDocument(document([{ ...surface, permissions: [] }]) as never),
|
||||
(error: unknown) => error instanceof SkinRuntimePluginValidationError && error.code === "SKIN_RUNTIME_PLUGIN_PERMISSION_MISSING",
|
||||
);
|
||||
assert.throws(
|
||||
() => parseSkinRuntimePluginDocument(document([surface, { ...surface, id: "other-surface", entry: "plugins/other-surface/index.js", style: "plugins/other-surface/style.css", assets: ["plugins/other-surface/assets/icon.png"] }]) as never),
|
||||
(error: unknown) => error instanceof SkinRuntimePluginValidationError && error.code === "SKIN_RUNTIME_PLUGIN_SURFACE_DUPLICATE",
|
||||
);
|
||||
});
|
||||
|
||||
test("v4 plugin descriptors reject unknown fields, path escape, wrong ownership, and executable assets", () => {
|
||||
const cases: Array<[unknown, string]> = [
|
||||
[{ ...plugin, arbitraryCodeHook: "run" }, "SKIN_RUNTIME_PLUGIN_FIELD_INVALID"],
|
||||
[{ ...plugin, entry: "plugins/voice-toolbar/../other.js" }, "SKIN_RUNTIME_PLUGIN_PATH_INVALID"],
|
||||
[{ ...plugin, entry: "plugins/other/index.js" }, "SKIN_RUNTIME_PLUGIN_PATH_INVALID"],
|
||||
[{ ...plugin, assets: ["plugins/voice-toolbar/assets/plugin.js"] }, "SKIN_RUNTIME_PLUGIN_ASSET_INVALID"],
|
||||
[{ ...plugin, assets: ["plugins/another/assets/icon.png"] }, "SKIN_RUNTIME_PLUGIN_ASSET_INVALID"],
|
||||
[{ ...plugin, permissions: ["network.fetch"] }, "SKIN_RUNTIME_PLUGIN_PERMISSION_INVALID"],
|
||||
[{ ...plugin, assets: [plugin.assets[0], plugin.assets[0]] }, "SKIN_RUNTIME_PLUGIN_ASSET_INVALID"],
|
||||
];
|
||||
for (const [value, code] of cases) {
|
||||
assert.throws(
|
||||
() => parseSkinRuntimePluginDocument(document([value]) as never),
|
||||
(error: unknown) => error instanceof SkinRuntimePluginValidationError && error.code === code,
|
||||
);
|
||||
}
|
||||
assert.throws(
|
||||
() => parseSkinRuntimePluginDocument(document([plugin, { ...plugin }]) as never),
|
||||
(error: unknown) => error instanceof SkinRuntimePluginValidationError && error.code === "SKIN_RUNTIME_PLUGIN_ID_INVALID",
|
||||
);
|
||||
});
|
||||
|
||||
test("v4 plugin documents enforce schema, counts, and UTF-8 byte limits", () => {
|
||||
assert.throws(
|
||||
() => parseSkinRuntimePluginDocument({ schemaVersion: 2, plugins: [plugin] } as never),
|
||||
(error: unknown) => error instanceof SkinRuntimePluginValidationError && error.code === "SKIN_RUNTIME_PLUGIN_DOCUMENT_INVALID",
|
||||
);
|
||||
assert.throws(
|
||||
() => parseSkinRuntimePluginDocument({ schemaVersion: 1, plugins: [] } as never),
|
||||
(error: unknown) => error instanceof SkinRuntimePluginValidationError && error.code === "SKIN_RUNTIME_PLUGIN_DOCUMENT_INVALID",
|
||||
);
|
||||
assert.throws(
|
||||
() => parseSkinRuntimePluginJson(" ".repeat(SKIN_RUNTIME_PLUGIN_DOCUMENT_LIMIT_BYTES + 1)),
|
||||
(error: unknown) => error instanceof SkinRuntimePluginValidationError && error.code === "SKIN_RUNTIME_PLUGIN_DOCUMENT_SIZE",
|
||||
);
|
||||
assert.throws(
|
||||
() => parseSkinRuntimePluginJson("{"),
|
||||
(error: unknown) => error instanceof SkinRuntimePluginValidationError && error.code === "SKIN_RUNTIME_PLUGIN_DOCUMENT_INVALID",
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user