feat(skin): widen bounded wasm ui output

This commit is contained in:
illusia committed 2026-10-10 15:53:49 +08:00
1 parent ab36825a56
commit c3810a177a
6 files changed
+253 -10

No files matched your search

+10 -5
View File
@@ -1,5 +1,6 @@
import { computed, defineComponent, Fragment, h, onUnmounted, reactive, ref, Teleport, watch, type PropType, type VNodeChild } from "vue";
import {
SKIN_PLUGIN_COMPONENT_LIMIT,
SKIN_PLUGIN_PERMISSIONS,
SKIN_PLUGIN_REPEAT_LIMIT,
parseSkinPluginDocument,
@@ -54,10 +55,10 @@ export default defineComponent({
if (!props.extensionOutput || document.schemaVersion !== 3) return document;
try {
const extensionDocument = parseSkinExtensionUiOutput(props.extensionOutput);
return parseSkinPluginDocument({
schemaVersion: 3,
components: [...document.components, ...extensionDocument.components],
});
const ids = new Set(document.components.map((component) => component.id));
if (document.components.length + extensionDocument.components.length > SKIN_PLUGIN_COMPONENT_LIMIT
|| extensionDocument.components.some((component) => ids.has(component.id))) return document;
return { schemaVersion: 3 as const, components: [...document.components, ...extensionDocument.components] };
} catch {
// Dynamic output is optional; an invalid or colliding result leaves the package UI intact.
return document;
@@ -239,7 +240,11 @@ export default defineComponent({
}
if (node.tag === "form" && !node.events?.submit) attrs.onSubmit = (event: Event) => event.preventDefault();
const children = (node.children ?? []).map((child, index) => renderNode(component, child, context, state, false, `${nodePath}-${index}`));
return h(node.tag, attrs, children);
// Custom element names are inert host-rendered placeholders. Never instantiate a page-registered
// custom element from generated output, since its connectedCallback would run with page privileges.
const tag = node.tag.includes("-") ? "div" : node.tag;
if (tag !== node.tag) attrs["data-ws-generated-element"] = node.tag;
return h(tag, attrs, children);
}
function accept(): void {
@@ -26,6 +26,13 @@ function outputDocument(componentId = "runtime-ui", root?: Record<string, unknow
});
}
function generatedOutput(componentId = "runtime-ui", root: Record<string, unknown> = { tag: "main", children: [{ text: "Generated by Wasm" }] }): string {
return JSON.stringify({
schemaVersion: 4,
components: [{ id: componentId, name: "Runtime UI", page: "voice", accessibleName: "Runtime UI", root }],
});
}
function expectOutputError(source: string, code: string): void {
assert.throws(() => parseSkinExtensionUiOutput(source), (error: unknown) =>
error instanceof SkinExtensionUiOutputError && error.code === code);
@@ -56,6 +63,48 @@ test("Wasm UI output rejects malformed, oversized, legacy, and capability-bearin
expectOutputError(outputDocument("runtime-ui", { tag: "p", text: "{{session.status.channelName}}" }), "SKIN_EXTENSION_UI_OUTPUT_SCHEMA");
});
test("generated UI supports broad safe markup, ARIA/data attributes, and inert custom-element placeholders", () => {
const document = parseSkinExtensionUiOutput(generatedOutput("kook-rail", {
tag: "kook-server-rail",
attributes: { "aria-label": "Servers", "data-selected": "home", role: "navigation" },
children: [
{ tag: "a", attributes: { href: "#voice" }, children: [{ text: "Voice" }] },
{ tag: "button", attributes: { type: "button", "aria-pressed": true }, children: [{ text: "Home" }] },
],
}));
assert.equal(document.schemaVersion, 3);
assert.equal(document.components[0].root.tag, "kook-server-rail");
assert.equal(document.components[0].root.children?.[0].tag, "a");
assert.equal(document.components[0].root.children?.[1].tag, "button");
});
test("generated UI rejects executable elements, event handlers, and network/navigation sinks", () => {
expectOutputError(generatedOutput("runtime-ui", { tag: "script" }), "SKIN_EXTENSION_UI_ELEMENT_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "iframe" }), "SKIN_EXTENSION_UI_ELEMENT_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "button", attributes: { onclick: "run" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "img", attributes: { src: "https://example.invalid/a.png" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "a", attributes: { href: "https://example.invalid/" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "div", attributes: { style: "display:none" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "div", attributes: { mystery: "value" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
expectOutputError(generatedOutput("runtime-ui", { tag: "div", attributes: { "data-ws-part": "spoofed" } }), "SKIN_EXTENSION_UI_ATTRIBUTE_INVALID");
});
test("generated UI enforces component and per-node child limits", () => {
const tooManyComponents = JSON.stringify({
schemaVersion: 4,
components: Array.from({ length: 65 }, (_, index) => ({
id: `runtime-${index}`, name: "Runtime UI", page: "voice", accessibleName: "Runtime UI", root: { tag: "main" },
})),
});
const tooManyChildren = generatedOutput("runtime-ui", {
tag: "main",
children: Array.from({ length: 257 }, () => ({ text: "x" })),
});
expectOutputError(tooManyComponents, "SKIN_EXTENSION_UI_OUTPUT_SCHEMA");
expectOutputError(tooManyChildren, "SKIN_EXTENSION_UI_COMPLEXITY_LIMIT");
});
test("SkinPluginOutlet renders validated dynamic output and preserves package UI on collisions", async () => {
const document: SkinPluginDocument = {
schemaVersion: 3,
@@ -96,6 +145,33 @@ test("SkinPluginOutlet renders validated dynamic output and preserves package UI
assert.match(collisionHtml, /Package UI/);
});
test("SkinPluginOutlet renders generated custom elements as inert host nodes", async () => {
const document: SkinPluginDocument = {
schemaVersion: 3,
components: [{
id: "base-ui",
name: "Base UI",
page: "voice",
accessibleName: "Base UI",
permissions: [],
actions: {},
root: { tag: "p", children: [{ text: "Base" }] },
}],
};
const html = await renderToString(createSSRApp(SkinPluginOutlet, {
skinId: "community.test",
skinVersion: "1.0.0",
document,
extensionOutput: generatedOutput("kook-rail", { tag: "kook-server-rail", children: [{ text: "Server" }] }),
page: "voice",
data: {},
assets: {},
actions: {},
}));
assert.match(html, /<div[^>]*data-ws-generated-element="kook-server-rail"/);
assert.match(html, /Server/);
});
test("skin-authored DOM nodes expose independent layout parts and retain touch bounds for controls", async () => {
const document: SkinPluginDocument = {
schemaVersion: 3,