Files
Kelx/tests/brand-html-escape-sites.test.js
2026-11-30 16:32:37 +00:00

73 lines
2.8 KiB
JavaScript

"use strict";
// Automated guard for the branding/avatar HTML sink escapes in
// public/js/bot.js.
//
// Eight innerHTML template sinks interpolate values an operator or account
// controls (BRAND_NAME / BRAND_LOGO via the branding settings, userAvatarSrc
// from the account profile). They were wrapped in escapeHtml() by hand in the
// multi-fix security pass, but no automated guard existed — a future template
// edit could silently revert a wrap and re-open a stored-XSS sink rendered to
// every user of the bot page. This file pins each of the eight sites, with
// exact occurrence counts (so copying a template without its wrap cannot slip
// through), and asserts that no unescaped src="${...}" interpolation of the
// same values remains.
//
// Same source-window convention as the other bot.js tests in this directory:
// read the file with fs and assert on the source. public/js/bot.js ships
// unminified and there is no bot.min.js to keep in sync.
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_SRC = fs.readFileSync(path.join(ROOT, "public", "js", "bot.js"), "utf8");
function countOccurrences(haystack, needle) {
let count = 0;
let at = haystack.indexOf(needle);
while (at !== -1) {
count += 1;
at = haystack.indexOf(needle, at + needle.length);
}
return count;
}
function assertExactCount(needle, expected, label) {
const got = countOccurrences(BOT_SRC, needle);
assert.strictEqual(
got,
expected,
`${label}: expected ${expected} occurrence(s) of ${JSON.stringify(needle)}, found ${got}`
);
}
test("planner 'Send to <brand> Agent' tooltips escape BRAND_NAME (2 sites)", () => {
assertExactCount('title="Send to ${escapeHtml(BRAND_NAME)} Agent"', 2, "planner item + action send buttons");
// The unescaped shape must not exist in any tooltip.
assert.strictEqual(countOccurrences(BOT_SRC, 'title="Send to ${BRAND_NAME}'), 0);
});
test("AI avatar <img> sources escape BRAND_LOGO and userAvatarSrc (4 sites)", () => {
assertExactCount('src="${escapeHtml(BRAND_LOGO)}"', 3, "AI message / typing / agent avatars");
assertExactCount('src="${escapeHtml(userAvatarSrc)}"', 1, "user avatar");
assert.strictEqual(
countOccurrences(BOT_SRC, 'src="${BRAND_LOGO}'),
0,
"unescaped BRAND_LOGO <img> src must not remain"
);
assert.strictEqual(
countOccurrences(BOT_SRC, 'src="${userAvatarSrc}'),
0,
"unescaped userAvatarSrc <img> src must not remain"
);
});
test("AI agent approval/question titles escape BRAND_NAME (2 sites)", () => {
assertExactCount("${escapeHtml(BRAND_NAME)} Agent wants to:", 1, "agent approval title");
assertExactCount("${escapeHtml(BRAND_NAME)} Agent asks:", 1, "agent question title");
});