Fix CodeQL code-scanning alerts

- Values pasted into eval'd or inline-script code go through lit() (scripts/lib/js-literal.js), which escapes <, >, /, U+2028 and U+2029 after JSON.stringify
- build test and i18n-check read script tags with a DOM parser or a plain search, not HTML regexes
- mcp.js parses the Authorization header without a backtracking regex
This commit is contained in:
Frederic Jung 2026-11-05 21:24:26 +01:00
parent e7729d0a1e
commit dd6e4f4f8a
No known key found for this signature in database
89 changed files with 316 additions and 213 deletions

View file

@ -3,18 +3,19 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { load } = require('./harness');
const { lit: js } = require('../scripts/lib/js-literal');
const BASE = "ACT.start({});S.cash=1e8;for(const g of GOALS)S.done[g.id]=0;S.lvl=4;S.nextFail=S.nextEvent=1e9;UI.speed=0;S.rules=makeRules('freeze');" +
"ACT.launchBB({city:'fra'});S.pops.fra.rgen=2;ACT.buyTransit({city:'fra',net:'cog',tier:1});ACT.buildPop({city:'ams'});S.pops.ams.rgen=2;compute();" +
"ACT.opsHire({id:'kevin'});S.ops.floor=0;closeModal()";
// A ROADMaster fiber Frankfurt–Amsterdam, dark or lit with `ch`, running at `hot` of what it carries.
const fiber = (ch, hot) => `S.links.push({id:'f',a:'fra',b:'ams',kind:'df',sys:'roadm',ch:${JSON.stringify(ch)},km:450,sub:false,ready:0,cut:0,mo:1,trench:'f'});compute();R.load.f=dfCap(S.links.at(-1))*${hot}`;
const fiber = (ch, hot) => `S.links.push({id:'f',a:'fra',b:'ams',kind:'df',sys:'roadm',ch:${js(ch)},km:450,sub:false,ready:0,cut:0,mo:1,trench:'f'});compute();R.load.f=dfCap(S.links.at(-1))*${hot}`;
function game(t, ch, hot, p, cap) {
const h = load();
t.after(() => h.dom.window.close());
h.ev(BASE);
h.ev(fiber(ch, hot));
h.ev(`S.ops.on.optics=true;S.ops.p.optics=${JSON.stringify(p)};${cap === undefined ? '' : `S.ops.cap.optics=${cap};`}opsDay(S.day)`);
h.ev(`S.ops.on.optics=true;S.ops.p.optics=${js(p)};${cap === undefined ? '' : `S.ops.cap.optics=${cap};`}opsDay(S.day)`);
return h;
}
const lit = (ev) => JSON.parse(ev("JSON.stringify(S.links.find(l=>l.id==='f').ch)"));
@ -43,7 +44,7 @@ test('#262 the biggest optic that fits the budget, and it never swaps what is al
test('#262 the setting is saved, an unknown value falls back, and it shows on the phone', (t) => {
const { ev, dom, errors } = game(t, [], 0, { pick: 'big' });
const back = (p) => ev(`(()=>{const s=JSON.parse(JSON.stringify(S));s.ops.p.optics=${JSON.stringify(p)};sanitize(s);return JSON.stringify(s.ops.p.optics);})()`);
const back = (p) => ev(`(()=>{const s=JSON.parse(JSON.stringify(S));s.ops.p.optics=${js(p)};sanitize(s);return JSON.stringify(s.ops.p.optics);})()`);
assert.deepEqual(JSON.parse(back({ pick: 'big' })), { pick: 'big' });
assert.deepEqual(JSON.parse(back({ pick: 'huge' })), {});
assert.deepEqual(JSON.parse(back(undefined)), {}, 'an old save without it');