fix(skin-editor): hide dev controls from visitors

This commit is contained in:
Corrupted ILLUSIA committed 2026-10-11 08:48:36 +08:00
1 parent 8cde1eae5a
commit 16df12e4b2
12 files changed
+1652 -2253

No files matched your search

+48
View File
@@ -0,0 +1,48 @@
import assert from "node:assert/strict";
import test from "node:test";
import { createSkinEditorModeController } from "./skin-editor-mode.js";
function sessionResponse(authenticated: boolean, mustChangePassword = false): Response {
return new Response(JSON.stringify({
authenticated,
mustChangePassword,
...(authenticated ? { csrfToken: "test-csrf" } : {}),
}), { status: 200, headers: { "content-type": "application/json" } });
}
test("skin editor mode is available only to an authenticated admin", async () => {
const controller = createSkinEditorModeController(async (input, init) => {
assert.equal(input, "/api/admin/session");
assert.equal(init?.credentials, "same-origin");
return sessionResponse(true);
});
assert.equal(await controller.setEnabled(true), true);
assert.equal(controller.isAdmin.value, true);
assert.equal(controller.isEnabled.value, true);
assert.equal(await controller.setEnabled(false), true);
assert.equal(controller.isEnabled.value, false);
});
test("unauthenticated, forced-password-change, and failed sessions keep editor mode hidden", async () => {
for (const fetcher of [
async () => sessionResponse(false),
async () => sessionResponse(true, true),
async () => new Response("unauthorized", { status: 401 }),
async () => { throw new Error("offline"); },
]) {
const controller = createSkinEditorModeController(fetcher as typeof fetch);
assert.equal(await controller.setEnabled(true), false);
assert.equal(controller.isAdmin.value, false);
assert.equal(controller.isEnabled.value, false);
}
});
test("an expired admin session disables an already-open editor mode", async () => {
let authenticated = true;
const controller = createSkinEditorModeController(async () => sessionResponse(authenticated));
assert.equal(await controller.setEnabled(true), true);
authenticated = false;
assert.equal(await controller.refreshAdminAccess(), false);
assert.equal(controller.isEnabled.value, false);
});
+58
View File
@@ -0,0 +1,58 @@
import { readonly, ref } from "vue";
import { adminResponses } from "../../../src/shared/admin-responses.js";
export function createSkinEditorModeController(fetcher: typeof fetch = (...args) => globalThis.fetch(...args)) {
const isAdmin = ref(false);
const isEnabled = ref(false);
let pendingCheck: Promise<boolean> | null = null;
async function refreshAdminAccess(): Promise<boolean> {
if (pendingCheck) return pendingCheck;
pendingCheck = (async () => {
try {
const response = await fetcher("/api/admin/session", {
method: "GET",
headers: { accept: "application/json" },
credentials: "same-origin",
cache: "no-store",
});
if (!response.ok) throw new Error("Admin session unavailable");
const session = adminResponses.session(await response.json());
const authenticated = session.authenticated && session.mustChangePassword === false;
isAdmin.value = authenticated;
if (!authenticated) isEnabled.value = false;
return authenticated;
} catch {
isAdmin.value = false;
isEnabled.value = false;
return false;
} finally {
pendingCheck = null;
}
})();
return pendingCheck;
}
async function setEnabled(enabled: boolean): Promise<boolean> {
if (!enabled) {
isEnabled.value = false;
return true;
}
if (!await refreshAdminAccess()) return false;
isEnabled.value = true;
return true;
}
return {
isAdmin: readonly(isAdmin),
isEnabled: readonly(isEnabled),
refreshAdminAccess,
setEnabled,
};
}
const skinEditorMode = createSkinEditorModeController();
export function useSkinEditorMode() {
return skinEditorMode;
}
@@ -0,0 +1,2 @@
/** Temporary development policy. Re-enable consent before public skin distribution. */
export const PROMPT_FOR_SKIN_PERMISSIONS = false;
@@ -0,0 +1,33 @@
import assert from "node:assert/strict";
import test from "node:test";
import { parseSkinPluginDocument } from "../../../src/shared/skin-plugin.js";
import { approveSkinPluginComponents, getMissingSkinPluginApprovals, isSkinPluginComponentApproved, revokeSkinPluginApprovals } from "./skin-plugin-approval.js";
const document = parseSkinPluginDocument({
schemaVersion: 3,
components: [{
id: "admin-editor-test",
name: "Admin editor test",
page: "voice",
mode: "widget",
accessibleName: "Admin editor permission test",
actions: {},
permissions: ["session.status.read"],
root: { tag: "div", children: [] },
}],
});
test("skin component permissions can be approved for the current session without persistent storage", () => {
const skinId = "community.editor-mode-test";
const component = document.components[0];
const subset = { ...document, components: [component] };
revokeSkinPluginApprovals(skinId);
assert.deepEqual(getMissingSkinPluginApprovals(skinId, "1.0.0", subset), [component]);
approveSkinPluginComponents(skinId, "1.0.0", [component]);
assert.deepEqual(getMissingSkinPluginApprovals(skinId, "1.0.0", subset), []);
assert.equal(isSkinPluginComponentApproved(skinId, "1.0.0", component), true);
revokeSkinPluginApprovals(skinId);
assert.equal(isSkinPluginComponentApproved(skinId, "1.0.0", component), false);
});
+52 -41
View File
@@ -1,59 +1,70 @@
import {
createSkinPluginApproval,
isSkinPluginPermissionApproved,
type SkinPluginApproval,
type SkinPluginComponent,
type SkinPluginDocument,
} from "../../../src/shared/skin-plugin.js";
import {
createSkinPluginApproval,
isSkinPluginPermissionApproved,
type SkinPluginApproval,
type SkinPluginComponent,
type SkinPluginDocument,
} from "../../../src/shared/skin-plugin.js";
const STORAGE_KEY = "webspeak:skin-plugin-approvals";
interface ApprovalStore { schemaVersion: 1; grants: Record<string, SkinPluginApproval> }
const inMemoryGrants = new Set<string>();
function approvalKey(skinId: string, skinVersion: string, component: SkinPluginComponent): string {
return `${skinId}@${skinVersion}/${component.page}/${component.id}`;
}
function readStore(): ApprovalStore {
try {
const raw: unknown = JSON.parse(localStorage.getItem(STORAGE_KEY) ?? "null");
if (!raw || typeof raw !== "object" || (raw as Record<string, unknown>).schemaVersion !== 1
|| !(raw as Record<string, unknown>).grants || typeof (raw as Record<string, unknown>).grants !== "object") {
return { schemaVersion: 1, grants: {} };
}
return raw as ApprovalStore;
} catch {
return { schemaVersion: 1, grants: {} };
}
function permissionKey(componentKey: string, permission: string): string {
return `${componentKey}\u0000${permission}`;
}
function writeStore(store: ApprovalStore): void {
try { localStorage.setItem(STORAGE_KEY, JSON.stringify(store)); } catch { /* Approval persistence is optional. */ }
}
function readStore(): ApprovalStore {
try {
const raw: unknown = JSON.parse(localStorage.getItem(STORAGE_KEY) ?? "null");
if (!raw || typeof raw !== "object" || (raw as Record<string, unknown>).schemaVersion !== 1
|| !(raw as Record<string, unknown>).grants || typeof (raw as Record<string, unknown>).grants !== "object") {
return { schemaVersion: 1, grants: {} };
}
return raw as ApprovalStore;
} catch {
return { schemaVersion: 1, grants: {} };
}
}
function writeStore(store: ApprovalStore): void {
try { localStorage.setItem(STORAGE_KEY, JSON.stringify(store)); } catch { /* Approval persistence is optional. */ }
}
export function getMissingSkinPluginApprovals(skinId: string, skinVersion: string, document: SkinPluginDocument): SkinPluginComponent[] {
const grants = readStore().grants;
return document.components.filter((component) => component.permissions.some((permission) =>
!isSkinPluginPermissionApproved(grants[approvalKey(skinId, skinVersion, component)], approvalKey(skinId, skinVersion, component), permission)));
return document.components.filter((component) => {
const key = approvalKey(skinId, skinVersion, component);
return component.permissions.some((permission) => !inMemoryGrants.has(permissionKey(key, permission))
&& !isSkinPluginPermissionApproved(grants[key], key, permission));
});
}
export function isSkinPluginComponentApproved(skinId: string, skinVersion: string, component: SkinPluginComponent): boolean {
if (!component.permissions.length) return true;
export function isSkinPluginComponentApproved(skinId: string, skinVersion: string, component: SkinPluginComponent): boolean {
if (!component.permissions.length) return true;
const pluginId = approvalKey(skinId, skinVersion, component);
const grant = readStore().grants[pluginId];
return component.permissions.every((permission) => isSkinPluginPermissionApproved(grant, pluginId, permission));
}
export function approveSkinPluginComponents(skinId: string, skinVersion: string, components: readonly SkinPluginComponent[]): void {
const store = readStore();
for (const component of components) {
return component.permissions.every((permission) => inMemoryGrants.has(permissionKey(pluginId, permission))
|| isSkinPluginPermissionApproved(grant, pluginId, permission));
}
export function approveSkinPluginComponents(skinId: string, skinVersion: string, components: readonly SkinPluginComponent[]): void {
const store = readStore();
for (const component of components) {
const pluginId = approvalKey(skinId, skinVersion, component);
store.grants[pluginId] = createSkinPluginApproval(pluginId, component.permissions);
}
writeStore(store);
}
export function revokeSkinPluginApprovals(skinId: string): void {
component.permissions.forEach((permission) => inMemoryGrants.add(permissionKey(pluginId, permission)));
}
writeStore(store);
}
export function revokeSkinPluginApprovals(skinId: string): void {
const store = readStore();
for (const key of Object.keys(store.grants)) if (key.startsWith(`${skinId}@`)) delete store.grants[key];
for (const key of inMemoryGrants) if (key.startsWith(`${skinId}@`)) inMemoryGrants.delete(key);
writeStore(store);
}