93 lines
5.1 KiB
JavaScript
93 lines
5.1 KiB
JavaScript
"use strict";
|
|
|
|
// Regression tests for audit finding 12: the taskbar Console button's inline
|
|
// onclick was dead code.
|
|
//
|
|
// bot.ejs shipped `#dockConsole` with
|
|
// onclick="window.location.href = '/server/<bot>'"
|
|
// but the global CSP (index.js) pins `script-src-attr 'none'`, so the browser
|
|
// silently refuses to run any inline event-handler attribute. No JS listener
|
|
// bound the "open-console" action either — bot.js's data-action registry had
|
|
// handlers for open-files, open-store, ... but not open-console — so the
|
|
// designed "fresh page load back to /server/<bot>" could never happen.
|
|
// (A nonce'd inline script in bot.ejs does bind a showConsoleView() view-swap
|
|
// on the button, which is why the button looked alive while its documented
|
|
// primary behaviour never fired.)
|
|
//
|
|
// The fix removes the dead inline attribute and registers a real
|
|
// "open-console" handler in bot.js's data-action registry, navigating to
|
|
// /server/<bot> exactly like the neighbouring "go-home" handler navigates to
|
|
// "/". The inline showConsoleView() listener stays as the instant in-page
|
|
// fallback while the browser processes the navigation.
|
|
//
|
|
// These tests pin: no inline handler attributes anywhere in bot.ejs (the CSP
|
|
// makes every one of them silently dead), the registry handler exists and
|
|
// navigates to the encoded server URL, and the CSP invariant that motivates
|
|
// the design stays in place.
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
const ROOT = path.join(__dirname, "..");
|
|
const BOT_JS = path.join(ROOT, "public", "js", "bot.js");
|
|
const BOT_EJS = path.join(ROOT, "views", "bot.ejs");
|
|
const INDEX_JS = path.join(ROOT, "index.js");
|
|
|
|
const botSrc = fs.readFileSync(BOT_JS, "utf8");
|
|
const botEjs = fs.readFileSync(BOT_EJS, "utf8");
|
|
const indexSrc = fs.readFileSync(INDEX_JS, "utf8");
|
|
|
|
function sourceWindow(src, startAnchor, endAnchor, label) {
|
|
const start = src.indexOf(startAnchor);
|
|
assert.notStrictEqual(start, -1, `${label || "window"}: start anchor not found: ${startAnchor}`);
|
|
const end = src.indexOf(endAnchor, start + startAnchor.length);
|
|
assert.notStrictEqual(end, -1, `${label || "window"}: end anchor not found: ${endAnchor}`);
|
|
return src.slice(start, end);
|
|
}
|
|
|
|
test("no element in bot.ejs carries an inline event-handler attribute", () => {
|
|
// script-src-attr 'none' means the browser silently drops ALL of them:
|
|
// dead markup that looks wired but can never run. If a handler is ever
|
|
// needed again it must be bound in JS (data-action registry or a listener).
|
|
const inlineHandler = /\son(?:click|change|submit|load|error|input|keydown|keyup|mouseover|mouseout|mouseenter|mouseleave|focus|blur)\s*=/i;
|
|
const match = botEjs.match(inlineHandler);
|
|
assert.strictEqual(match, null,
|
|
`bot.ejs must not contain inline event-handler attributes (CSP script-src-attr 'none' silently blocks them); found: ${match && match[0]}`);
|
|
});
|
|
|
|
test("#dockConsole keeps its open-console action and no inline handler", () => {
|
|
const dockWindow = sourceWindow(botEjs, 'id="dockConsole"', "</button>", "dockConsole");
|
|
assert.match(dockWindow, /data-action="open-console"/,
|
|
"the Console dock button must keep its data-action so bot.js's registry binds it");
|
|
assert.ok(!/onclick\s*=/i.test(dockWindow),
|
|
"the Console dock button must not carry an inline onclick (CSP blocks it)");
|
|
});
|
|
|
|
test("bot.js's data-action registry navigates open-console to /server/<bot>", () => {
|
|
const handlersWindow = sourceWindow(botSrc, "const actionHandlers = {", "document.querySelectorAll('[data-action]')", "actionHandlers");
|
|
assert.match(handlersWindow, /"open-console":\s*\(\)\s*=>\s*\{\s*window\.location\.href\s*=\s*`\/server\/\$\{encodeURIComponent\(bot\)\}`;?\s*\}/,
|
|
'the registry must map "open-console" to a full-page navigation to the encoded /server/<bot> URL');
|
|
assert.match(handlersWindow, /"go-home"/,
|
|
"the registry must keep its other navigation handler; open-console deliberately mirrors its idiom");
|
|
});
|
|
|
|
test("the CSP stays script-src-attr 'none' — the reason inline handlers are forbidden", () => {
|
|
assert.match(indexSrc, /scriptSrcAttr:\s*\[\s*"'none'"\s*\]/,
|
|
"if the CSP ever allows inline handler attributes again, revisit the open-console binding and the bot.ejs no-inline-handler invariant");
|
|
});
|
|
|
|
test("the in-page showConsoleView fallback for #dockConsole stays bound", () => {
|
|
// The fallback listener is marked by a unique comment in the bot.ejs inline
|
|
// page system (the plain getElementById anchor also matches setActiveDock).
|
|
const marker = "// Console button -> return to default console + stats view";
|
|
const at = botEjs.indexOf(marker);
|
|
assert.notStrictEqual(at, -1, "the dockConsole fallback listener block must exist");
|
|
const fallbackWindow = botEjs.slice(at, at + 600);
|
|
assert.match(fallbackWindow, /getElementById\('dockConsole'\)/,
|
|
"the fallback must still target the dock Console button");
|
|
assert.match(fallbackWindow, /showConsoleView\(\)/,
|
|
"the nonce'd inline script must keep swapping to the console view immediately; the data-action navigation then reloads the page clean");
|
|
});
|