Files
kevinveenbirkenbachandClaude Opus 5.5 6e045bced8 fix(web-app-docs): encode line breaks in redirects, navigate by built URLs
- py/http-response-splitting (server.py:87): _redirect percent-encodes CR
  and LF in the Location header
- js/xss-through-dom (versions.js:82, languages.js:25): both switchers
  navigate to the URL they built instead of select.value

The new cases in test_server.py, versions.test.js and languages.test.js
fail without the fix and pass with it.

🎯 Fix: 92 %
💥 Break: 3 %
💸 Cost: –
⏱️ Time: –
🛡️ Security: 100 %
🏆 Score: 89
🚦 Gate: ✅ 90/5, cost and time <= 100, security 100 %

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 16:54:27 +00:00

100 lines
3.1 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const vm = require("node:vm");
const SOURCE = path.join(
__dirname,
"..", "..", "..", "..", "..", "..",
"roles", "web-app-docs", "files", "javascript", "languages.js",
);
const LANGUAGES = [
{ code: "en", native: "English" },
{ code: "de", native: "Deutsch" },
{ code: "ar", native: "العربية" },
];
function select(dataset) {
return {
dataset,
children: [],
selectedIndex: -1,
get value() {
return this.children[this.selectedIndex]?.value ?? "";
},
listeners: {},
replaceChildren(...nodes) {
this.children = nodes;
},
addEventListener(type, handler) {
this.listeners[type] = handler;
},
};
}
async function load(dataset, answer = { ok: true, body: { languages: LANGUAGES } }) {
let ready = null;
const requested = [];
const switcher = select(dataset);
const window = { location: { href: "" } };
const document = {
addEventListener: (type, handler) => {
if (type === "DOMContentLoaded") {
ready = handler;
}
},
createElement: () => ({}),
querySelectorAll: (selector) => (selector === ".docs-language-switcher" ? [switcher] : []),
};
const fetch = async (url) => {
requested.push(url);
return { ok: answer.ok, json: async () => answer.body };
};
vm.runInNewContext(fs.readFileSync(SOURCE, "utf8"), { document, window, fetch });
ready();
await new Promise((resolve) => setImmediate(resolve));
return { switcher, window, requested };
}
test("the switcher lists every built language and keeps the page", async () => {
const { switcher, requested } = await load({ version: "deployed", language: "de", page: "docs/index" });
assert.deepEqual(requested, ["/api/languages/deployed"]);
assert.deepEqual(
switcher.children.map((option) => [option.textContent, option.value, option.selected]),
[
["English", "/deployed/docs/index.html", false],
["Deutsch", "/deployed/de/docs/index.html", true],
["العربية", "/deployed/ar/docs/index.html", false],
],
);
});
test("choosing a language navigates to the same page in it", async () => {
const { switcher, window } = await load({ version: "v14.1.0", language: "en", page: "index" });
switcher.selectedIndex = 1;
switcher.listeners.change();
assert.equal(window.location.href, "/v14.1.0/de/index.html");
});
test("navigation follows the built page url, never the option value read back from the DOM", async () => {
const { switcher, window } = await load({ version: "v14.1.0", language: "en", page: "index" });
switcher.children[1].value = "javascript:alert(document.domain)";
switcher.selectedIndex = 1;
switcher.listeners.change();
assert.equal(window.location.href, "/v14.1.0/de/index.html");
});
test("a failed request leaves the rendered placeholder in place", async () => {
const { switcher } = await load({ version: "latest", language: "en", page: "index" }, { ok: false, body: null });
assert.deepEqual(switcher.children, []);
assert.equal(switcher.listeners.change, undefined);
});