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:
parent
e7729d0a1e
commit
dd6e4f4f8a
89 changed files with 316 additions and 213 deletions
|
|
@ -3,9 +3,10 @@ const assert = require('node:assert/strict');
|
|||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const { load } = require('./harness');
|
||||
const { lit } = require('../scripts/lib/js-literal');
|
||||
|
||||
const SAVE = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json'), 'utf8');
|
||||
const big = (ev) => ev(`S=migrate(JSON.parse(${JSON.stringify(SAVE)}));UI.started=true;UI.pitched=true;UI.speed=0;closeModal();compute()`);
|
||||
const big = (ev) => ev(`S=migrate(JSON.parse(${lit(SAVE)}));UI.started=true;UI.pitched=true;UI.speed=0;closeModal();compute()`);
|
||||
// Everything a player sees from routing: totals, money, every link, port and city.
|
||||
const digest = "JSON.stringify([R.served,R.demand,R.reach,R.peered,R.revMo,R.costMo,R.cost,R.egT,R.load,R.useT,R.usePni,R.useIx,R.useCache,R.citySrv,R.cityDem])";
|
||||
|
||||
|
|
@ -16,7 +17,7 @@ test('a field routing never reads really changes nothing it gives (every key the
|
|||
const keys = JSON.parse(ev('JSON.stringify([...RK_SKIP])'));
|
||||
for (const k of keys) {
|
||||
// Change the field all through: every number moves, every flag flips, text changes, lists grow, objects gain a field.
|
||||
ev(`(()=>{const m=x=>x==null?1:typeof x==='number'?x+12345:typeof x==='string'?x+'x':typeof x==='boolean'?!x:Array.isArray(x)?x.map(m).concat(x.length?[m(x[0])]:[]):Object.assign(Object.fromEntries(Object.entries(x).map(([a,b])=>[a,m(b)])),{zz:1});const v=S[${JSON.stringify(k)}];window.__k=[${JSON.stringify(k)},v];S[${JSON.stringify(k)}]=m(JSON.parse(JSON.stringify(v===undefined?null:v)));compute();})()`);
|
||||
ev(`(()=>{const m=x=>x==null?1:typeof x==='number'?x+12345:typeof x==='string'?x+'x':typeof x==='boolean'?!x:Array.isArray(x)?x.map(m).concat(x.length?[m(x[0])]:[]):Object.assign(Object.fromEntries(Object.entries(x).map(([a,b])=>[a,m(b)])),{zz:1});const v=S[${lit(k)}];window.__k=[${lit(k)},v];S[${lit(k)}]=m(JSON.parse(JSON.stringify(v===undefined?null:v)));compute();})()`);
|
||||
assert.equal(ev(digest), base, `routing reads S.${k}: take it off RK_SKIP`);
|
||||
ev('S[__k[0]]=__k[1];compute()');
|
||||
}
|
||||
|
|
@ -41,7 +42,7 @@ test('routing never even reads a field the shortcut skips (so an empty one canno
|
|||
const { ev, errors } = load();
|
||||
big(ev);
|
||||
const read = JSON.parse(ev(`(()=>{const seen=new Set(),real=S;S=new Proxy(real,{get(t,k){if(typeof k==='string')seen.add(k);return t[k];}});try{compute();}finally{S=real;}return JSON.stringify([...seen]);})()`));
|
||||
assert.deepEqual(read.filter((k) => ev(`RK_SKIP.has(${JSON.stringify(k)})`)), [], 'routing reads these: take them off RK_SKIP');
|
||||
assert.deepEqual(read.filter((k) => ev(`RK_SKIP.has(${lit(k)})`)), [], 'routing reads these: take them off RK_SKIP');
|
||||
assert.ok(read.includes('loans'), 'loans pay into the month, so routing reads them');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue