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
5
scripts/lib/js-literal.js
Normal file
5
scripts/lib/js-literal.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
// A value as a JavaScript literal, safe to paste into code that is eval'd or into an inline <script>: JSON, with the
|
||||
// characters that could end a script tag or break a string (<, >, /, U+2028, U+2029) written as \u escapes.
|
||||
const ESC = /[<>\/\u2028\u2029]/g;
|
||||
const lit = (v) => { const s = JSON.stringify(v); return s === undefined ? 'undefined' : s.replace(ESC, (c) => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0')); };
|
||||
module.exports = { lit };
|
||||
|
|
@ -3,6 +3,7 @@
|
|||
// an exchange port at home on day 0, a NOC intern on standing orders for transit and routers, then the generator's
|
||||
// plan one step at a time whenever the bank allows it, upgrading any wave that runs hot. It never reads the goal, so the
|
||||
// same seed and plan always play the same game, and the generator can calibrate a goal from what it reached.
|
||||
const { lit } = require('./js-literal');
|
||||
const BOT = String.raw`(()=>{
|
||||
const B=window.__wsBot={};
|
||||
const cheapNet=(c,avoid)=>NETS.filter(n=>n.price&&sellsAt(n,c)&&!avoid.includes(n.id)&&!S.transit.some(t=>t.city===c&&t.net===n.id)).sort((a,b)=>a.price*netMult(a,c)-b.price*netMult(b,c))[0];
|
||||
|
|
@ -41,9 +42,9 @@ const install = (ev) => { if (!ev('!!window.__wsBot')) ev(BOT); };
|
|||
// run out. Returns the day the goal was first met (or null), what the goal measure read each day, and the end state.
|
||||
function play(ev, file, plan, { days, stop = true, measure = 'wsMeasure()' } = {}) {
|
||||
install(ev);
|
||||
ev(`localStorage.removeItem('sot-wkscn');wsBegin(${JSON.stringify(file)},'Ref');if(UI.pendGame)ACT.pitchSkip();UI.speed=0;closeModal()`);
|
||||
ev(`localStorage.removeItem('sot-wkscn');wsBegin(${lit(file)},'Ref');if(UI.pendGame)ACT.pitchSkip();UI.speed=0;closeModal()`);
|
||||
if (ev('!!(S.scn&&S.scn.wk)') !== true) throw new Error('the game would not start this scenario');
|
||||
ev(`__wsBot.start(${JSON.stringify(plan)})`);
|
||||
ev(`__wsBot.start(${lit(plan)})`);
|
||||
const won0 = ev('wsOk()'), n = days || ev('S.scn.days');
|
||||
// Each day: the bot moves, the day ticks (scnTick records a win), and we note the measure.
|
||||
const out = JSON.parse(ev(`(()=>{const m=[];let won=null;for(let i=0;i<${n};i++){__wsBot.day();dayTick();closeModal();m.push(+(${measure}).toFixed(3));if(won===null&&S.scn.done)won=S.scn.done;if(won!==null&&${stop})break;if(S.lost)break;}return JSON.stringify({won,m,lost:!!S.lost,day:S.day,cash:Math.round(S.cash)});})()`));
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
// ['scrub', c, tier] a scrubbing center ['pnis'] a PNI with every unreached network that takes one
|
||||
// ['pni', c, net, tier] one PNI with that network there ['last', c] transit there only as a last resort
|
||||
// ['loans'] take every loan the banks offer, from now on (the cheese plans)
|
||||
const { lit } = require('./js-literal');
|
||||
const REF = String.raw`(()=>{
|
||||
const Q=window.__ref={log:[]};
|
||||
const quiet=f=>{const t=window.toast;window.toast=()=>{};try{return f();}finally{window.toast=t;}};
|
||||
|
|
@ -66,7 +67,7 @@ Q.start=(steps,o={})=>quiet(()=>{Q.steps=steps;Q.i=0;Q.avoid=o.avoid||[];Q.step(
|
|||
function play(ev, id, plan, { seed, days, avoid } = {}) {
|
||||
ev(`localStorage.removeItem('sot-scn');beginGame('Ref',SCN.find(x=>x.id==='${id}').city,{scn:SCN.find(x=>x.id==='${id}')${seed ? ',seed:' + seed : ''}});if(UI.pendGame)ACT.pitchSkip();UI.speed=0;closeModal()`);
|
||||
if (ev('typeof __ref') === 'undefined') ev(REF);
|
||||
ev(`__ref.start(${JSON.stringify(plan)},${JSON.stringify({ avoid })})`);
|
||||
ev(`__ref.start(${lit(plan)},${lit({ avoid })})`);
|
||||
const n = days || ev('S.scn.days');
|
||||
return JSON.parse(ev(`(()=>{let won=null;for(let i=0;i<${n};i++){__ref.day();dayTick();closeModal();if(S.scn.done){won=S.scn.done;break;}if(S.lost||S.scn.failed)break;}
|
||||
return JSON.stringify({won,lost:!!S.lost,day:S.day,limit:S.scn.days,cash:Math.round(S.cash),served:Math.round(R.served)});})()`));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue