mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 12:02:50 +08:00
feat(skin): add bounded UI output bridge
This commit is contained in:
1 parent
849eb1a6d9
commit
7047d71678
12 files changed
+266
-19
No files matched your search
@@ -20,11 +20,12 @@ import {
|
||||
createSkinExtensionWasmPrefixByteImmediateProbe,
|
||||
createSkinExtensionWasmStatusProbe,
|
||||
createSkinExtensionWasmTooManyFunctionsProbe,
|
||||
createSkinExtensionWasmUiOutputProbe,
|
||||
} from "../../test/skin-extension-wasm-fixture.js";
|
||||
|
||||
test("Wasm metadata policy accepts only a single run export and one bounded host memory", () => {
|
||||
const metadata = validateSkinExtensionWasmImportsAndExports(createSkinExtensionWasmPrefixByteImmediateProbe());
|
||||
assert.deepEqual(metadata, { usesSessionStatus: false, memoryMaximumPages: SKIN_EXTENSION_WASM_MEMORY_LIMIT_PAGES });
|
||||
assert.deepEqual(metadata, { usesSessionStatus: false, usesUiOutput: false, memoryMaximumPages: SKIN_EXTENSION_WASM_MEMORY_LIMIT_PAGES });
|
||||
});
|
||||
|
||||
test("Wasm policy rejects module-defined memory or tables that bypass host limits", () => {
|
||||
@@ -83,9 +84,15 @@ test("Wasm import parsing reads the bounded permission contract without native c
|
||||
});
|
||||
assert.deepEqual(validateSkinExtensionWasmImportsAndExports(status, ["session.status.read"]), {
|
||||
usesSessionStatus: true,
|
||||
usesUiOutput: false,
|
||||
memoryMaximumPages: SKIN_EXTENSION_WASM_MEMORY_LIMIT_PAGES,
|
||||
});
|
||||
assert.equal(validateSkinExtensionWasmImportsAndExports(createSkinExtensionWasmCappedRunProbe()).usesSessionStatus, false);
|
||||
assert.deepEqual(validateSkinExtensionWasmImportsAndExports(createSkinExtensionWasmUiOutputProbe()), {
|
||||
usesSessionStatus: false,
|
||||
usesUiOutput: true,
|
||||
memoryMaximumPages: SKIN_EXTENSION_WASM_MEMORY_LIMIT_PAGES,
|
||||
});
|
||||
});
|
||||
|
||||
test("Wasm instruction scanning distinguishes extension prefixes from signed integer bytes", () => {
|
||||
@@ -93,6 +100,7 @@ test("Wasm instruction scanning distinguishes extension prefixes from signed int
|
||||
assert.doesNotThrow(() => validateSkinExtensionWasmBytes(bytes));
|
||||
assert.deepEqual(validateSkinExtensionWasmImportsAndExports(bytes), {
|
||||
usesSessionStatus: false,
|
||||
usesUiOutput: false,
|
||||
memoryMaximumPages: SKIN_EXTENSION_WASM_MEMORY_LIMIT_PAGES,
|
||||
});
|
||||
});
|
||||
@@ -3,9 +3,10 @@ export const SKIN_EXTENSION_WASM_MEMORY_LIMIT_PAGES = 64;
|
||||
export const SKIN_EXTENSION_WASM_PAGE_BYTES = 64 * 1024;
|
||||
export const SKIN_EXTENSION_WASM_STARTUP_TIMEOUT_MS = 3_000;
|
||||
export const SKIN_EXTENSION_WASM_EXECUTION_LIMIT_MS = 100;
|
||||
export const SKIN_EXTENSION_WASM_MESSAGE_LIMIT = 8 * 1024;
|
||||
export const SKIN_EXTENSION_WASM_MESSAGE_LIMIT = 32 * 1024;
|
||||
export const SKIN_EXTENSION_WASM_STATUS_READ_LIMIT = 10;
|
||||
export const SKIN_EXTENSION_WASM_STATUS_BYTES_LIMIT = 512;
|
||||
export const SKIN_EXTENSION_WASM_UI_OUTPUT_LIMIT_BYTES = 16 * 1024;
|
||||
export const SKIN_EXTENSION_WASM_FUEL_LIMIT = 10_000_000n;
|
||||
export const SKIN_EXTENSION_WASM_INTERPRETER_MEMORY_LIMIT_BYTES = 64 * 1024 * 1024;
|
||||
export const SKIN_EXTENSION_WASM_FUNCTION_LIMIT = 128;
|
||||
@@ -19,7 +20,7 @@ const WASM_HEADER = [0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00] as const;
|
||||
const SCALAR_VALUE_TYPES = new Set([0x7f, 0x7e, 0x7d, 0x7c]);
|
||||
const BOUNDED_VECTOR_COUNTS = new Map<number, number>([
|
||||
[1, 128], // types
|
||||
[2, 2], // memory and one optional capability import
|
||||
[2, 3], // memory, one optional capability import, and one optional UI output import
|
||||
[3, SKIN_EXTENSION_WASM_FUNCTION_LIMIT],
|
||||
[6, 128], // globals
|
||||
[7, 64], // exports; the runner later requires exactly run
|
||||
@@ -310,6 +311,7 @@ export function validateSkinExtensionWasmBytes(source: Uint8Array): void {
|
||||
|
||||
export interface SkinExtensionWasmModuleMetadata {
|
||||
usesSessionStatus: boolean;
|
||||
usesUiOutput: boolean;
|
||||
memoryMaximumPages: number;
|
||||
}
|
||||
|
||||
@@ -360,6 +362,7 @@ export function validateSkinExtensionWasmImportsAndExports(
|
||||
let memoryMaximumPages = SKIN_EXTENSION_WASM_MEMORY_LIMIT_PAGES;
|
||||
let hasMemory = false;
|
||||
let usesSessionStatus = false;
|
||||
let usesUiOutput = false;
|
||||
let hasExports = false;
|
||||
|
||||
while (offset < bytes.byteLength) {
|
||||
@@ -371,8 +374,8 @@ export function validateSkinExtensionWasmImportsAndExports(
|
||||
|
||||
if (sectionId === 2) {
|
||||
const count = readU32(bytes, offset, end);
|
||||
if (count.value < 1 || count.value > 2) {
|
||||
throw new SkinExtensionWasmPolicyError("SKIN_EXTENSION_WASM_IMPORT_INVALID", "The module must import one host memory and at most one approved capability.");
|
||||
if (count.value < 1 || count.value > 3) {
|
||||
throw new SkinExtensionWasmPolicyError("SKIN_EXTENSION_WASM_IMPORT_INVALID", "The module must import one host memory and may import one capability and one UI output function.");
|
||||
}
|
||||
let cursor = count.next;
|
||||
for (let index = 0; index < count.value; index += 1) {
|
||||
@@ -386,10 +389,13 @@ export function validateSkinExtensionWasmImportsAndExports(
|
||||
if (kind === 0) {
|
||||
const typeIndex = readU32(bytes, cursor, end);
|
||||
cursor = typeIndex.next;
|
||||
if (fieldName.value !== "session_status_read" || usesSessionStatus) {
|
||||
if (fieldName.value === "session_status_read" && !usesSessionStatus) {
|
||||
usesSessionStatus = true;
|
||||
} else if (fieldName.value === "ui_emit_json" && !usesUiOutput) {
|
||||
usesUiOutput = true;
|
||||
} else {
|
||||
throw new SkinExtensionWasmPolicyError("SKIN_EXTENSION_WASM_IMPORT_INVALID", "The module requested an unsupported or duplicate function import.");
|
||||
}
|
||||
usesSessionStatus = true;
|
||||
} else if (kind === 2) {
|
||||
if (fieldName.value !== "memory" || hasMemory) {
|
||||
throw new SkinExtensionWasmPolicyError("SKIN_EXTENSION_WASM_IMPORT_INVALID", "The module must import exactly one env.memory value.");
|
||||
@@ -433,5 +439,5 @@ export function validateSkinExtensionWasmImportsAndExports(
|
||||
if (usesSessionStatus && !grantedPermissions.includes("session.status.read")) {
|
||||
throw new SkinExtensionWasmPolicyError("SKIN_EXTENSION_WASM_PERMISSION_INVALID", "The module imports session status without an approved session.status.read capability.");
|
||||
}
|
||||
return { usesSessionStatus, memoryMaximumPages };
|
||||
return { usesSessionStatus, usesUiOutput, memoryMaximumPages };
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
SKIN_EXTENSION_WASM_SOURCE_LIMIT,
|
||||
SKIN_EXTENSION_WASM_STARTUP_TIMEOUT_MS,
|
||||
SKIN_EXTENSION_WASM_STATUS_READ_LIMIT,
|
||||
SKIN_EXTENSION_WASM_UI_OUTPUT_LIMIT_BYTES,
|
||||
validateSkinExtensionWasmBytes,
|
||||
} from "./skin-extension-wasm-policy.js";
|
||||
|
||||
@@ -29,6 +30,7 @@ export interface SkinExtensionWasmSandboxResult {
|
||||
result: number | string;
|
||||
linearMemoryBytes: number;
|
||||
statusReadCount: number;
|
||||
uiOutput: string | null;
|
||||
sessionSnapshot?: SkinExtensionSessionStatus;
|
||||
}
|
||||
|
||||
@@ -173,10 +175,13 @@ export function createSkinExtensionWasmSandboxPrototype(options: SkinExtensionWa
|
||||
return;
|
||||
}
|
||||
if (message.type === "complete") {
|
||||
const expectedKeys = ["type", "result", "linearMemoryBytes", "statusReadCount"];
|
||||
const expectedKeys = ["type", "result", "linearMemoryBytes", "statusReadCount", "uiOutput"];
|
||||
if (Object.keys(message).length !== expectedKeys.length
|
||||
|| Object.keys(message).some((key) => !expectedKeys.includes(key))
|
||||
|| (typeof message.result !== "number" && typeof message.result !== "string")
|
||||
|| (message.uiOutput !== null && typeof message.uiOutput !== "string")
|
||||
|| (typeof message.uiOutput === "string"
|
||||
&& new TextEncoder().encode(message.uiOutput).byteLength > SKIN_EXTENSION_WASM_UI_OUTPUT_LIMIT_BYTES)
|
||||
|| !Number.isSafeInteger(message.linearMemoryBytes) || (message.linearMemoryBytes as number) < 0
|
||||
|| (message.linearMemoryBytes as number) > SKIN_EXTENSION_WASM_MEMORY_LIMIT_PAGES * 64 * 1024
|
||||
|| !Number.isSafeInteger(message.statusReadCount) || (message.statusReadCount as number) < 0
|
||||
@@ -185,7 +190,8 @@ export function createSkinExtensionWasmSandboxPrototype(options: SkinExtensionWa
|
||||
return;
|
||||
}
|
||||
complete({ result: message.result as number | string, linearMemoryBytes: message.linearMemoryBytes as number,
|
||||
statusReadCount: message.statusReadCount as number, ...(approvedSessionStatus ? { sessionSnapshot: approvedSessionStatus } : {}) });
|
||||
statusReadCount: message.statusReadCount as number, uiOutput: message.uiOutput as string | null,
|
||||
...(approvedSessionStatus ? { sessionSnapshot: approvedSessionStatus } : {}) });
|
||||
return;
|
||||
}
|
||||
if (message.type === "error" && Object.keys(message).length === 3
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
SKIN_EXTENSION_WASM_MESSAGE_LIMIT,
|
||||
SKIN_EXTENSION_WASM_STATUS_BYTES_LIMIT,
|
||||
SKIN_EXTENSION_WASM_STATUS_READ_LIMIT,
|
||||
SKIN_EXTENSION_WASM_UI_OUTPUT_LIMIT_BYTES,
|
||||
validateSkinExtensionWasmImportsAndExports,
|
||||
} from "./skin-extension-wasm-policy.js";
|
||||
|
||||
@@ -32,6 +33,8 @@ interface WasmiInterpreterExports extends WebAssembly.Exports {
|
||||
last_error_code: () => number;
|
||||
last_status_read_count: () => number;
|
||||
last_guest_memory_bytes: () => number;
|
||||
last_ui_output_pointer: () => number;
|
||||
last_ui_output_length: () => number;
|
||||
}
|
||||
|
||||
const scope = self as unknown as {
|
||||
@@ -111,7 +114,8 @@ async function run(job: RunMessage): Promise<void> {
|
||||
: errorCode === 6 ? "SKIN_EXTENSION_WASM_MEMORY_LIMIT"
|
||||
: errorCode === 10 ? "SKIN_EXTENSION_WASM_IMPORT_INVALID"
|
||||
: errorCode === 11 ? "SKIN_EXTENSION_WASM_PERMISSION_INVALID"
|
||||
: "SKIN_EXTENSION_WASM_INTERPRETER_FAILED";
|
||||
: errorCode === 12 ? "SKIN_EXTENSION_WASM_UI_OUTPUT_INVALID"
|
||||
: "SKIN_EXTENSION_WASM_INTERPRETER_FAILED";
|
||||
sendError(code, "The extension was rejected or stopped inside the bounded Wasmi interpreter.");
|
||||
return;
|
||||
}
|
||||
@@ -119,6 +123,8 @@ async function run(job: RunMessage): Promise<void> {
|
||||
const result = Number(rawResult);
|
||||
const statusReadCount = interpreter.last_status_read_count();
|
||||
const linearMemoryBytes = interpreter.last_guest_memory_bytes();
|
||||
const uiOutputPointer = interpreter.last_ui_output_pointer();
|
||||
const uiOutputLength = interpreter.last_ui_output_length();
|
||||
if (!Number.isSafeInteger(result) || !Number.isSafeInteger(statusReadCount) || statusReadCount < 0
|
||||
|| statusReadCount > SKIN_EXTENSION_WASM_STATUS_READ_LIMIT || !Number.isSafeInteger(linearMemoryBytes)
|
||||
|| linearMemoryBytes < 0 || linearMemoryBytes > metadata.memoryMaximumPages * 64 * 1024
|
||||
@@ -126,11 +132,33 @@ async function run(job: RunMessage): Promise<void> {
|
||||
sendError("SKIN_EXTENSION_WASM_RESULT_INVALID", "The interpreter returned a value outside the allowed resource policy.");
|
||||
return;
|
||||
}
|
||||
if (!Number.isSafeInteger(uiOutputPointer) || uiOutputPointer < 0 || !Number.isSafeInteger(uiOutputLength)
|
||||
|| uiOutputLength < 0 || uiOutputLength > SKIN_EXTENSION_WASM_UI_OUTPUT_LIMIT_BYTES
|
||||
|| (uiOutputLength > 0 && !metadata.usesUiOutput)
|
||||
|| uiOutputPointer > interpreter.memory.buffer.byteLength
|
||||
|| uiOutputLength > interpreter.memory.buffer.byteLength - uiOutputPointer) {
|
||||
sendError("SKIN_EXTENSION_WASM_UI_OUTPUT_INVALID", "The interpreter returned UI output outside its bounded memory range.");
|
||||
return;
|
||||
}
|
||||
let uiOutput: string | null = null;
|
||||
if (uiOutputLength > 0) {
|
||||
try {
|
||||
const decodedOutput = new TextDecoder("utf-8", { fatal: true }).decode(
|
||||
new Uint8Array(interpreter.memory.buffer, uiOutputPointer, uiOutputLength),
|
||||
);
|
||||
JSON.parse(decodedOutput);
|
||||
uiOutput = decodedOutput;
|
||||
} catch {
|
||||
sendError("SKIN_EXTENSION_WASM_UI_OUTPUT_INVALID", "The extension returned malformed UTF-8 or JSON UI output.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
const message = {
|
||||
type: "complete",
|
||||
result,
|
||||
linearMemoryBytes,
|
||||
statusReadCount,
|
||||
uiOutput,
|
||||
};
|
||||
if (new TextEncoder().encode(JSON.stringify(message)).byteLength > SKIN_EXTENSION_WASM_MESSAGE_LIMIT) {
|
||||
sendError("SKIN_EXTENSION_WASM_MESSAGE_SIZE", "The module result exceeds its message limit.");
|
||||
@@ -160,7 +188,8 @@ async function loadWasmiInterpreter(): Promise<WasmiInterpreterExports> {
|
||||
const exports = instance.exports as unknown as WasmiInterpreterExports;
|
||||
if (!(exports.memory instanceof WebAssembly.Memory) || typeof exports.alloc !== "function"
|
||||
|| typeof exports.run !== "function" || typeof exports.last_error_code !== "function"
|
||||
|| typeof exports.last_status_read_count !== "function" || typeof exports.last_guest_memory_bytes !== "function") {
|
||||
|| typeof exports.last_status_read_count !== "function" || typeof exports.last_guest_memory_bytes !== "function"
|
||||
|| typeof exports.last_ui_output_pointer !== "function" || typeof exports.last_ui_output_length !== "function") {
|
||||
throw new Error("The bounded Wasmi interpreter exposes an incompatible runtime interface.");
|
||||
}
|
||||
return exports;
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
createSkinExtensionWasmCappedRunProbe,
|
||||
createSkinExtensionWasmInfiniteRunProbe,
|
||||
createSkinExtensionWasmPrefixByteImmediateProbe,
|
||||
createSkinExtensionWasmUiOutputProbe,
|
||||
} from "../../test/skin-extension-wasm-fixture.js";
|
||||
|
||||
interface WasmiInterpreterExports extends WebAssembly.Exports {
|
||||
@@ -14,6 +15,8 @@ interface WasmiInterpreterExports extends WebAssembly.Exports {
|
||||
statusPermissionGranted: number, memoryMaximumPages: number, fuel: bigint) => bigint;
|
||||
last_error_code: () => number;
|
||||
last_guest_memory_bytes: () => number;
|
||||
last_ui_output_pointer: () => number;
|
||||
last_ui_output_length: () => number;
|
||||
}
|
||||
|
||||
test("the fixed Wasmi artifact caps its own memory and stops guest execution at fuel", async () => {
|
||||
@@ -21,7 +24,7 @@ test("the fixed Wasmi artifact caps its own memory and stops guest execution at
|
||||
const { instance } = await WebAssembly.instantiate(artifact);
|
||||
const runtime = instance.exports as unknown as WasmiInterpreterExports;
|
||||
|
||||
function run(module: Uint8Array): { result: number; error: number; guestMemoryBytes: number } {
|
||||
function run(module: Uint8Array): { result: number; error: number; guestMemoryBytes: number; uiOutput: string | null } {
|
||||
const pointer = runtime.alloc(module.byteLength);
|
||||
assert.notEqual(pointer, 0, "the bounded interpreter should accept the small fixture");
|
||||
new Uint8Array(runtime.memory.buffer, pointer, module.byteLength).set(module);
|
||||
@@ -30,6 +33,13 @@ test("the fixed Wasmi artifact caps its own memory and stops guest execution at
|
||||
result: Number(result),
|
||||
error: runtime.last_error_code(),
|
||||
guestMemoryBytes: runtime.last_guest_memory_bytes(),
|
||||
uiOutput: runtime.last_ui_output_length() === 0
|
||||
? null
|
||||
: new TextDecoder("utf-8", { fatal: true }).decode(new Uint8Array(
|
||||
runtime.memory.buffer,
|
||||
runtime.last_ui_output_pointer(),
|
||||
runtime.last_ui_output_length(),
|
||||
)),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -37,14 +47,27 @@ test("the fixed Wasmi artifact caps its own memory and stops guest execution at
|
||||
result: 123,
|
||||
error: 0,
|
||||
guestMemoryBytes: 64 * 1024,
|
||||
uiOutput: null,
|
||||
});
|
||||
assert.equal(run(createSkinExtensionWasmInfiniteRunProbe()).error, 5, "the fuel budget must trap a non-returning module");
|
||||
assert.deepEqual(run(createSkinExtensionWasmCappedRunProbe()), {
|
||||
result: -1,
|
||||
error: 0,
|
||||
guestMemoryBytes: 4 * 1024 * 1024,
|
||||
uiOutput: null,
|
||||
});
|
||||
|
||||
assert.deepEqual(run(createSkinExtensionWasmUiOutputProbe()), {
|
||||
result: 7,
|
||||
error: 0,
|
||||
guestMemoryBytes: 64 * 1024,
|
||||
uiOutput: '{"type":"root"}',
|
||||
});
|
||||
assert.equal(run(createSkinExtensionWasmUiOutputProbe("x".repeat(16 * 1024 + 1))).error, 12,
|
||||
"the interpreter must reject a UI payload above its byte cap");
|
||||
assert.equal(run(createSkinExtensionWasmUiOutputProbe('{"type":"root"}', true)).error, 12,
|
||||
"the interpreter must reject more than one UI payload per execution");
|
||||
|
||||
const memory = runtime.memory;
|
||||
const currentPages = memory.buffer.byteLength / (64 * 1024);
|
||||
memory.grow(1024 - currentPages);
|
||||
|
||||
Reference in new issue
Block a user