"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 = `