mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 03:52:50 +08:00
feat: add host chrome theming and a skin-proof recovery layer
Allow skins to theme host chrome through a second scope and explicit visibleParts. Official and built-in skins stay decoration-free by default. Harden the CSS compiler so skins cannot hide the document root, push protected controls off-screen, or target the host layer with sibling selectors. Move recovery and authorization UI into a closed Shadow DOM on the top layer, so a third-party skin cannot block the built-in skin reset.
This commit is contained in:
1 parent
33ba062686
commit
f8d61d22ef
45 files changed
+3535
-558
No files matched your search
+131
@@ -0,0 +1,131 @@
|
||||
<!doctype html>
|
||||
<html lang="en" data-ws-route="webclient" data-ws-page="voice" data-ws-host-skin="community.hostile-fixture">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
||||
<title>Host chrome recovery fixture</title>
|
||||
<script>
|
||||
// Authorize third-party skins before instance-authorization.ts is evaluated.
|
||||
try { localStorage.setItem("webspeak:instance-authorization:v1", JSON.stringify({ schemaVersion: 1, allowed: true })); } catch {}
|
||||
</script>
|
||||
<style>
|
||||
html, body { margin: 0; min-height: 100%; font: 12px/1.5 system-ui, sans-serif; }
|
||||
.ws-skin-root { min-height: 100vh; }
|
||||
#dock { display: flex; gap: 8px; padding: 12px; background: #eef4f2; }
|
||||
#overlay { width: 320px; height: 180px; margin: 12px; background: #fff; border: 1px solid #ccd; }
|
||||
#app { position: absolute; left: 0; top: 0; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<!-- A minimal public-skin surface so compiled hostile rules have real targets. -->
|
||||
<div class="ws-skin-root" data-ws-skin="community.hostile-fixture" data-ws-page="voice" data-ws-part="app">
|
||||
<div id="dock" data-ws-part="voice.audio-dock">
|
||||
<button type="button" data-ws-part="voice.audio-dock.microphone">Mic</button>
|
||||
<button type="button" data-ws-part="voice.audio-dock.output">Output</button>
|
||||
</div>
|
||||
<div id="overlay" data-ws-part="host.plugins.overlay">Application library</div>
|
||||
<button type="button" id="launcher" data-ws-part="host.plugins.launcher">Plugins</button>
|
||||
</div>
|
||||
<div id="app"></div>
|
||||
<div id="auth-app"></div>
|
||||
<pre id="result" style="position:relative;z-index:5;background:#fff">pending</pre>
|
||||
<script type="module">
|
||||
import { createApp, h } from "vue";
|
||||
import HostChrome from "/src/components/HostChrome.vue";
|
||||
import InstanceAuthorization from "/src/components/InstanceAuthorization.vue";
|
||||
import { hostBuiltInSkinRequests, setHostChromeActiveSkin } from "/src/services/host-chrome.ts";
|
||||
import { hostOwnedLayers, syncRecoveryPopover } from "/src/services/host-owned-layer.ts";
|
||||
|
||||
const output = document.getElementById("result");
|
||||
const scenarios = JSON.parse(decodeURIComponent(location.hash.slice(1) || "[]"));
|
||||
const style = document.createElement("style");
|
||||
style.id = "hostile-skin";
|
||||
document.head.append(style);
|
||||
|
||||
setHostChromeActiveSkin("community.hostile-fixture");
|
||||
createApp({ render: () => h(HostChrome, { language: "en", connected: true }) }).mount("#app");
|
||||
createApp({ render: () => h(InstanceAuthorization, { language: "en" }) }).mount("#auth-app");
|
||||
|
||||
const layer = hostOwnedLayers().find((entry) => entry.name === "recovery");
|
||||
const results = [];
|
||||
|
||||
if (!layer) {
|
||||
output.textContent = JSON.stringify({ error: "recovery layer was not created" });
|
||||
} else {
|
||||
const popover = layer.container.querySelector("[data-ws-host-recovery]");
|
||||
// The component promotes the popover on the next microtask; do it here so
|
||||
// the whole fixture runs synchronously and the result is ready before load.
|
||||
if (!popover.matches(":popover-open")) popover.showPopover();
|
||||
for (const scenario of scenarios) {
|
||||
style.textContent = scenario.css;
|
||||
const button = layer.container.querySelector('[data-ws-recovery-action="built-in"]');
|
||||
// Reading geometry forces a synchronous style/layout pass, so the hostile
|
||||
// stylesheet is fully applied before the hit test.
|
||||
const rect = button.getBoundingClientRect();
|
||||
const hit = document.elementFromPoint(rect.left + rect.width / 2, rect.top + rect.height / 2);
|
||||
const computed = getComputedStyle(button);
|
||||
const dock = document.getElementById("dock");
|
||||
const before = hostBuiltInSkinRequests.value;
|
||||
if (hit === button || button.contains(hit)) {
|
||||
hit.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, composed: true }));
|
||||
} else {
|
||||
button.click();
|
||||
}
|
||||
results.push({
|
||||
name: scenario.name,
|
||||
popoverOpen: popover.matches(":popover-open"),
|
||||
// elementFromPoint retargets nodes inside a closed shadow root to its
|
||||
// host, so either the inner button or the host proves the top layer won.
|
||||
topLayerHit: hit === layer.host || hit === button || layer.container.contains(hit),
|
||||
hostileCovered: hit === dock,
|
||||
visible: computed.display !== "none" && computed.visibility !== "hidden"
|
||||
&& Number(computed.opacity) > 0.01 && computed.pointerEvents !== "none",
|
||||
clicked: hostBuiltInSkinRequests.value === before + 1,
|
||||
layerOutsideSkinRoot: layer.host.closest(".ws-skin-root") === null,
|
||||
buttonRect: { left: rect.left, top: rect.top, width: rect.width, height: rect.height },
|
||||
});
|
||||
}
|
||||
// Authorization: the real dialog is teleported into its own closed shadow
|
||||
// root. showModal() must place it above the same hostile fullscreen layer.
|
||||
style.textContent = scenarios[0]?.css ?? "";
|
||||
const authLayer = hostOwnedLayers().find((entry) => entry.name === "authorization");
|
||||
const authDialog = authLayer?.container.querySelector("dialog");
|
||||
let authorization = null;
|
||||
if (authLayer && authDialog) {
|
||||
authDialog.showModal();
|
||||
const rect = authDialog.getBoundingClientRect();
|
||||
const hit = document.elementFromPoint(window.innerWidth / 2, window.innerHeight / 2);
|
||||
authorization = {
|
||||
modalOpen: authDialog.matches(":modal"),
|
||||
topLayerHit: hit === authLayer.host || authDialog === hit || authLayer.container.contains(hit),
|
||||
hostileCovered: hit === document.getElementById("dock"),
|
||||
layerOutsideSkinRoot: authLayer.host.closest(".ws-skin-root") === null,
|
||||
sized: rect.width > 0 && rect.height > 0,
|
||||
};
|
||||
authDialog.close();
|
||||
}
|
||||
// Verify the rejected-promotion fallback really lays the capsule out. A
|
||||
// closed [popover] is `display:none !important` until the attribute is gone.
|
||||
const fallback = document.createElement("div");
|
||||
fallback.setAttribute("popover", "manual");
|
||||
fallback.textContent = "fallback";
|
||||
fallback.style.cssText = "position:fixed;left:0;top:0;width:80px;height:24px;";
|
||||
Object.defineProperty(fallback, "showPopover", {
|
||||
configurable: true,
|
||||
value: () => { throw new Error("top-layer promotion rejected"); },
|
||||
});
|
||||
document.body.append(fallback);
|
||||
syncRecoveryPopover(fallback, true);
|
||||
const fallbackStyle = getComputedStyle(fallback);
|
||||
const fallbackRect = fallback.getBoundingClientRect();
|
||||
const popoverFallback = {
|
||||
display: fallbackStyle.display,
|
||||
popoverAttribute: fallback.hasAttribute("popover"),
|
||||
visible: fallbackStyle.display !== "none" && fallbackRect.width > 0 && fallbackRect.height > 0,
|
||||
};
|
||||
fallback.remove();
|
||||
output.textContent = JSON.stringify({ results, authorization, popoverFallback });
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,201 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { spawn } from "node:child_process";
|
||||
import { existsSync } from "node:fs";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { createServer } from "vite";
|
||||
import { compileSkinCss } from "../../src/shared/skin-css.ts";
|
||||
|
||||
const read = (path) => readFile(new URL(path, import.meta.url), "utf8");
|
||||
const webRoot = fileURLToPath(new URL("../", import.meta.url));
|
||||
const CHROME = ["/usr/bin/google-chrome", "/usr/bin/google-chrome-stable"].find((candidate) => existsSync(candidate));
|
||||
|
||||
const compileHostile = (css) => compileSkinCss(css, "community.hostile-fixture", {}, 3).css;
|
||||
|
||||
/**
|
||||
* Hostile rules that the compiler still accepts (they are legitimate page /
|
||||
* themable-host styling) but that must not be able to hide, cover, or disable
|
||||
* the recovery entry. `fullscreen-audio-dock` is the case the previous review
|
||||
* reproduced: a public part promoted to a `z-index: 2147483647` fullscreen layer.
|
||||
*/
|
||||
const scenarios = [
|
||||
{
|
||||
name: "fullscreen-audio-dock",
|
||||
css: compileHostile('[data-ws-part="voice.audio-dock"] { position: fixed; inset: 0; z-index: 2147483647; }'),
|
||||
},
|
||||
{
|
||||
name: "opacity-launcher",
|
||||
css: compileHostile('[data-ws-part="host.plugins.launcher"] { opacity: 0; }'),
|
||||
},
|
||||
{
|
||||
name: "filter-launcher",
|
||||
css: compileHostile('[data-ws-part="host.plugins.launcher"] { filter: opacity(0); }'),
|
||||
},
|
||||
{
|
||||
name: "display-none-overlay",
|
||||
css: compileHostile('[data-ws-part="host.plugins.overlay"] { display: none !important; }'),
|
||||
},
|
||||
{
|
||||
name: "pointer-events-none",
|
||||
css: compileHostile('[data-ws-part="host.plugins.launcher"], [data-ws-part="host.plugins.overlay"] { pointer-events: none !important; }'),
|
||||
},
|
||||
];
|
||||
|
||||
test("host chrome renders recovery and authorization in a closed-shadow top layer", async () => {
|
||||
const [hostChrome, authorization, ownedLayer, hostChromeService, publicSkin] = await Promise.all([
|
||||
read("../src/components/HostChrome.vue"),
|
||||
read("../src/components/InstanceAuthorization.vue"),
|
||||
read("../src/services/host-owned-layer.ts"),
|
||||
read("../src/services/host-chrome.ts"),
|
||||
read("../src/composables/usePublicSkin.ts"),
|
||||
]);
|
||||
|
||||
// Recovery: closed shadow root + manual popover + no skin hook.
|
||||
assert.match(hostChrome, /createHostOwnedLayer\("recovery"/);
|
||||
assert.match(hostChrome, /popover="manual"/);
|
||||
assert.match(hostChrome, /data-ws-recovery-action="built-in"/);
|
||||
assert.match(hostChrome, /requestBuiltInSkin/);
|
||||
assert.match(hostChrome, /isThirdPartySkinActive/);
|
||||
assert.doesNotMatch(hostChrome, /data-ws-part="host\.recovery"/);
|
||||
assert.doesNotMatch(hostChrome, /ws-host-recovery-layer/);
|
||||
|
||||
// Authorization: showModal() inside the same host-owned mechanism.
|
||||
assert.match(authorization, /createHostOwnedLayer\('authorization'/);
|
||||
assert.match(authorization, /showModal\(\)/);
|
||||
assert.doesNotMatch(authorization, /data-ws-part="host\.permission"/);
|
||||
|
||||
// The layer itself: closed shadow root, no data-ws-part, fixed fallback.
|
||||
assert.match(ownedLayer, /attachShadow\(\{ mode: "closed" \}\)/);
|
||||
assert.match(ownedLayer, /data-ws-host-layer/);
|
||||
assert.doesNotMatch(ownedLayer, /setAttribute\("data-ws-part"/);
|
||||
|
||||
// The reset path is independent of the skinnable plugin overlay.
|
||||
assert.match(hostChromeService, /requestBuiltInSkin/);
|
||||
assert.match(hostChromeService, /isThirdPartySkinActive/);
|
||||
assert.match(publicSkin, /hostBuiltInSkinRequests/);
|
||||
assert.match(publicSkin, /switchToBuiltIn/);
|
||||
|
||||
// Defense in depth: there is still no compiler route to a "host.recovery"
|
||||
// selector, even if a node ever regressed to carrying that attribute.
|
||||
for (const selector of ['[data-ws-part="host.recovery"]', '[data-ws-part="host.permission"]']) {
|
||||
assert.throws(() => compileSkinCss(`${selector} { display: none; }`, "community.evil", {}, 3), /Trusted or unknown host part/);
|
||||
}
|
||||
|
||||
// A leading `:root` must not reach body-level siblings (the host layer in the
|
||||
// preview/fixture topology) through `+` / `~`.
|
||||
for (const selector of [
|
||||
":root ~ [data-ws-host-layer]",
|
||||
":root ~ *",
|
||||
":root + [data-ws-host-layer]",
|
||||
":root:hover ~ *",
|
||||
]) {
|
||||
assert.throws(
|
||||
() => compileSkinCss(`${selector} { display: none !important; }`, "community.evil", {}, 3),
|
||||
/sibling combinator after :root/,
|
||||
selector,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("the themed preview keeps recovery in its own hookless top layer", async () => {
|
||||
const [preview, bridge] = await Promise.all([
|
||||
read("../public/host-chrome-preview.html"),
|
||||
read("../src/services/host-chrome-preview.ts"),
|
||||
]);
|
||||
assert.match(bridge, /resolveHiddenHostParts/);
|
||||
assert.match(preview, /host-chrome-preview\.ts/);
|
||||
assert.match(preview, /resolveHiddenHostParts\(chromeData, \[\]\)/);
|
||||
assert.match(preview, /hiddenParts\.has\(part\)/);
|
||||
assert.match(preview, /isHostPartReachable\(/);
|
||||
assert.match(preview, /launcherReachable/);
|
||||
assert.match(preview, /recoveryLayer\.remove\(\)/);
|
||||
assert.match(preview, /popover="manual"/);
|
||||
assert.doesNotMatch(preview, /data-ws-part="host\.recovery"/);
|
||||
assert.doesNotMatch(preview, /const watermarkHidden = getComputedStyle/);
|
||||
});
|
||||
|
||||
test("the public-skin composable opts in only through chrome.json#visibleParts", async () => {
|
||||
const usePublicSkin = await read("../src/composables/usePublicSkin.ts");
|
||||
assert.match(usePublicSkin, /setSkinHostChromeParts\(skin\?\.chromeData\)/);
|
||||
// Sniffing the combined stylesheet would match the importer-generated
|
||||
// hiddenParts/icon CSS and leak decorations for hiddenParts-only packs.
|
||||
assert.doesNotMatch(usePublicSkin, /\.test\(skin\.css\)/);
|
||||
});
|
||||
|
||||
async function dumpDom(url) {
|
||||
return await new Promise((resolve, reject) => {
|
||||
const child = spawn(CHROME, [
|
||||
"--headless=new",
|
||||
"--no-sandbox",
|
||||
"--disable-gpu",
|
||||
"--disable-dev-shm-usage",
|
||||
"--hide-scrollbars",
|
||||
"--dump-dom",
|
||||
url,
|
||||
], { stdio: ["ignore", "pipe", "pipe"] });
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
child.stdout.on("data", (chunk) => { stdout += chunk; });
|
||||
child.stderr.on("data", (chunk) => { stderr += chunk; });
|
||||
const timer = setTimeout(() => {
|
||||
child.kill("SIGKILL");
|
||||
reject(new Error(`Headless Chrome timed out.\n${stderr.slice(-2000)}`));
|
||||
}, 60000);
|
||||
child.on("error", (error) => { clearTimeout(timer); reject(error); });
|
||||
child.on("exit", (code) => {
|
||||
clearTimeout(timer);
|
||||
if (!stdout.includes('id="result"')) {
|
||||
reject(new Error(`Headless Chrome exited ${code} without a result.\n${stderr.slice(-2000)}`));
|
||||
return;
|
||||
}
|
||||
resolve(stdout);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
test("recovery stays visible and clickable under hostile compiled skins", { skip: !CHROME && "google-chrome is not installed" }, async (t) => {
|
||||
const server = await createServer({
|
||||
root: webRoot,
|
||||
configFile: path.join(webRoot, "vite.config.ts"),
|
||||
logLevel: "silent",
|
||||
server: { host: "127.0.0.1", port: 0, strictPort: false, hmr: false, ws: false },
|
||||
});
|
||||
t.after(() => server.close());
|
||||
await server.listen();
|
||||
const address = server.httpServer?.address();
|
||||
const port = address && typeof address === "object" ? address.port : 0;
|
||||
assert.ok(port, "Vite did not expose a listening port");
|
||||
|
||||
const hash = encodeURIComponent(JSON.stringify(scenarios));
|
||||
const dom = await dumpDom(`http://127.0.0.1:${port}/test/fixtures/host-chrome-recovery.html#${hash}`);
|
||||
const match = dom.match(/<pre id="result"[^>]*>([\s\S]*?)<\/pre>/);
|
||||
assert.ok(match, "the fixture did not emit a result block");
|
||||
const payload = JSON.parse(match[1]);
|
||||
assert.equal(payload.error, undefined, payload.error);
|
||||
assert.equal(payload.results.length, scenarios.length);
|
||||
|
||||
assert.ok(payload.authorization, "the authorization dialog was not mounted");
|
||||
assert.equal(payload.authorization.modalOpen, true, "the authorization dialog is not modal / in the top layer");
|
||||
assert.equal(payload.authorization.topLayerHit, true, "the hostile layer covers the authorization dialog");
|
||||
assert.equal(payload.authorization.hostileCovered, false, "the hostile layer received the authorization hit test");
|
||||
assert.equal(payload.authorization.layerOutsideSkinRoot, true, "the authorization dialog was rendered inside .ws-skin-root");
|
||||
assert.equal(payload.authorization.sized, true, "the authorization dialog has no layout box");
|
||||
|
||||
// The pre-Popover / rejected-promotion fallback must actually lay the capsule
|
||||
// out: a closed [popover] is display:none !important until the attribute goes.
|
||||
assert.ok(payload.popoverFallback, "the fixture did not exercise the popover fallback");
|
||||
assert.equal(payload.popoverFallback.display, "flex", "the fallback capsule is not displayed");
|
||||
assert.equal(payload.popoverFallback.popoverAttribute, false, "the popover attribute still forces display:none");
|
||||
assert.equal(payload.popoverFallback.visible, true, "the fallback capsule has no layout box");
|
||||
|
||||
for (const result of payload.results) {
|
||||
assert.equal(result.popoverOpen, true, `${result.name}: recovery popover is not in the top layer`);
|
||||
assert.equal(result.topLayerHit, true, `${result.name}: recovery is covered by the hostile layer`);
|
||||
assert.equal(result.hostileCovered, false, `${result.name}: the hostile layer received the hit test`);
|
||||
assert.equal(result.visible, true, `${result.name}: recovery is visually hidden or non-interactive`);
|
||||
assert.equal(result.clicked, true, `${result.name}: clicking "use built-in skin" did not reach the handler`);
|
||||
assert.equal(result.layerOutsideSkinRoot, true, `${result.name}: recovery was rendered inside .ws-skin-root`);
|
||||
}
|
||||
});
|
||||
@@ -22,7 +22,11 @@ const skin = id => ({ id, name: id, version: "1.0.0", minAppVersion: "0.2.6",
|
||||
css: `[data-ws-skin="${id}"] { --skin-test: blue; }`, assets: {}, installedAt: 1 });
|
||||
const json = body => new Response(JSON.stringify(body));
|
||||
class Element {
|
||||
dataset = {}; textContent = ""; id = "";
|
||||
dataset = {}; textContent = ""; id = ""; attributes = new Map();
|
||||
setAttribute(name, value) { this.attributes.set(String(name), String(value)); }
|
||||
getAttribute(name) { return this.attributes.get(String(name)) ?? null; }
|
||||
hasAttribute(name) { return this.attributes.has(String(name)); }
|
||||
removeAttribute(name) { this.attributes.delete(String(name)); }
|
||||
append(el) { elements.set(el.id, el); }
|
||||
remove() { elements.delete(this.id); }
|
||||
matches() { return false; }
|
||||
|
||||
Reference in new issue
Block a user