89 lines
4.3 KiB
JavaScript
89 lines
4.3 KiB
JavaScript
"use strict";
|
|
|
|
// The files-explorer error branch (loadExplorer's catch) used to interpolate
|
|
// the failure message straight into innerHTML:
|
|
//
|
|
// entriesEl.innerHTML = `...
|
|
// ${hasPerm('files_read') ? ' Failed to load: ' + (err.message || String(err)) : ' Unauthorized'}
|
|
// ...`;
|
|
//
|
|
// It was the only raw innerHTML sink in the whole files flow - every sibling
|
|
// error handler in the file (store, scheduler, planner) correctly routes the
|
|
// message through escapeHtml. Today the reachable strings are client-generated
|
|
// only, so there is no live injection, but the moment server-provided error
|
|
// detail is routed through this branch it becomes stored XSS in an admin's
|
|
// session. The fix routes the message through the file's own escapeHtml, and
|
|
// these assertions pin that (reading the sources as text, like the other audit
|
|
// tests):
|
|
//
|
|
// * the exact sink - "Failed to load: " is concatenated with
|
|
// escapeHtml(err.message || String(err)), never the bare expression;
|
|
// * the old raw shape is gone from the file entirely;
|
|
// * the escape helper itself - bot.js defines escapeHtml over [&<>"'];
|
|
// * scoping - the err.message || String(err) expression occurs exactly once
|
|
// in the whole file, inside the escapeHtml call, so the same raw
|
|
// concatenation cannot silently reappear at another sink.
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
const BOT_JS = path.join(__dirname, "..", "public", "js", "bot.js");
|
|
|
|
test("files-explorer error branch escapes the failure message", () => {
|
|
const source = fs.readFileSync(BOT_JS, "utf8");
|
|
|
|
// The fixed sink: the exact expression the branch renders, right after the
|
|
// catch block's own log line (pins the fix to THIS error path, not just
|
|
// somewhere in the file).
|
|
const catchAnchor = source.indexOf('console.error("[FileManager] Load failed:", err);');
|
|
assert.notStrictEqual(catchAnchor, -1, "the loadExplorer catch block anchor must be present");
|
|
const catchWindow = source.slice(catchAnchor, catchAnchor + 600);
|
|
assert.match(
|
|
catchWindow,
|
|
/entriesEl\.innerHTML = `<li[\s\S]*?Failed to load: ' \+ escapeHtml\(err\.message \|\| String\(err\)\)/,
|
|
"the explorer error branch must render 'Failed to load: ' + escapeHtml(err.message || String(err))"
|
|
);
|
|
|
|
// The unfixed shape must be gone: the bare (unescaped) concatenation.
|
|
assert.doesNotMatch(
|
|
source,
|
|
/Failed to load: ' \+ \(err\.message \|\| String\(err\)\)/,
|
|
"the raw unescaped err.message concatenation must not come back"
|
|
);
|
|
|
|
// The escape helper the fix relies on, defined in the same file and covering
|
|
// the full HTML-sensitive set (the error message can contain any of these
|
|
// once server text reaches this branch). Asserted on the entity values so
|
|
// the check is independent of the map's key quoting style.
|
|
const helper = source.match(/function escapeHtml\(s\) \{([\s\S]*?)\n \}/);
|
|
assert.ok(helper, "bot.js must define escapeHtml(s)");
|
|
for (const entity of ["&", "<", ">", """, "'"]) {
|
|
assert.ok(
|
|
helper[1].includes(`'${entity}'`) || helper[1].includes(`"${entity}"`),
|
|
`escapeHtml must map a character to ${entity}`
|
|
);
|
|
}
|
|
|
|
// Scoping guard: the sink expression occurs exactly once in the file and
|
|
// that occurrence is the escaped one - the raw concatenation cannot quietly
|
|
// exist at a second innerHTML sink.
|
|
const occurrences = source.split("err.message || String(err)").length - 1;
|
|
assert.strictEqual(occurrences, 1, "err.message || String(err) must appear exactly once (escaped)");
|
|
});
|
|
|
|
test("no bare err.message concatenation feeds an innerHTML assignment in the files flow", () => {
|
|
const source = fs.readFileSync(BOT_JS, "utf8");
|
|
|
|
// The other error sinks of the file go through escapeHtml first (store,
|
|
// scheduler, planner). Pin that: every "Failed to load" style innerHTML
|
|
// rendering of err.message in bot.js must be escapeHtml-wrapped. Only
|
|
// direct concatenations/interpolations are checked here - the ai-chat flows
|
|
// deliberately render markdown through renderAiMarkdown and are out of
|
|
// scope for this finding.
|
|
for (const match of source.matchAll(/Failed to load[^'`]*' \+ \(?err\.message/g)) {
|
|
assert.fail(`unescaped err.message concatenation feeding a UI string: ${match[0]}`);
|
|
}
|
|
});
|