seriesoftubes/tests/act-args-builtins.test.js
jvanvik 77f4035853 Six actions look up their city among the network's own POPs only
launchBB, addSite, addRouter, upRouter, addGen and myIxUp read S.pops[city]
(S.myIx[city]) without checking the key, so a city named '__proto__' found
Object.prototype and wrote to it (launchBB left Object.prototype.bb behind,
and every for…in after it saw that), and 'constructor' spent cash on
statics of Object. Only an MCP agent or a request to a server-run network
can send such a city; the page's own buttons never do. Each now takes the
POP only when it is an own key (own(), as sanitize() does).

tests/act-args-builtins.test.js sends every action an agent may send with
'__proto__', 'constructor' and 'prototype' in each usual arg and checks
that the page's built-ins stay as they were; without this change it finds
exactly these six. No state, strings or saves change.

Performance on the big network against 61e31c5 (npm run perf, headless,
median of 15 runs, ms):

| What | Base | This | Change | p |
|---|--:|--:|--:|--:|
| Routing: compute() | 25.4 | 28.1 | +11% | 0.90 |
| One game day: dayTick() | 27.8 | 28.7 | +3% | 0.77 |
| An action: after() | 1.82 | 1.77 | -3% | 0.77 |
| Side panel and left list | 58.8 | 57.1 | -3% | 0.013 |
| NOC dashboard tabs | 95.9 | 95.9 | +0% | 1.00 |
| Map at three zoom levels | 3.49 | 3.30 | -6% | 0.53 |
| The NOC intern's shift: opsDay() | 3.45 | 3.52 | +2% | 0.80 |

Routing results are identical to the base; no slowdowns beyond noise.
2026-11-09 22:18:56 +01:00

37 lines
2.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// A click's args name things the game looks up by key: S.pops[city], S.myIx[city]. A city that is '__proto__' or
// 'constructor' (an MCP agent's, or a request to a server-run network) must find nothing there, never a prototype or a
// built-in's statics: every action that takes one, with each such name in each usual arg, leaves the page's built-ins
// as they were and spends nothing on them.
const test = require('node:test');
const assert = require('node:assert/strict');
const { load } = require('./harness');
const BAD = ['__proto__', 'constructor', 'prototype'];
const ARGS = ['city', 'id', 'net', 'k', 'c', 'v', 'a', 'b', 'to', 'from', 'link', 's'];
test('no action reaches a built-in through an arg that names one', (t) => {
// The development page: API_CMD is every action an agent may send (the MCP facade), the sent ones of a server's page.
const { ev, errors, dom } = load({ agent: true });
t.after(() => dom.window.close());
ev("ACT.start({});if(UI.pendGame)ACT.pitchSkip();S.cash=1e12;S.lvl=6;for(const g of GOALS)S.done[g.id]=0;");
for (const c of ['fra', 'ams', 'lon', 'par']) ev(`ACT.buildPop({city:'${c}'})`);
ev("ACT.launchBB({city:'fra'});closeModal();UI.confirm=null;compute();");
// What the built-ins hold now: anything an action adds to them is found by name.
ev(`window.__fp=()=>{const o={};for(const [n,x] of [['Object',Object],['Object.prototype',Object.prototype],['Array',Array],['Array.prototype',Array.prototype],['Function.prototype',Function.prototype],['String.prototype',String.prototype]])o[n]=Object.getOwnPropertyNames(x).sort().join(',');
const e=[];for(const k in {})e.push(k);for(const k in [])e.push('[]'+k);o.enum=e.join(',');return o;};window.__fp0=__fp();
window.__clean=()=>{for(const [n,x] of [['Object',Object],['Object.prototype',Object.prototype],['Array',Array],['Array.prototype',Array.prototype],['Function.prototype',Function.prototype],['String.prototype',String.prototype]]){const had=new Set(__fp0[n].split(','));for(const k of Object.getOwnPropertyNames(x))if(!had.has(k))delete x[k];}};`);
const acts = JSON.parse(ev('JSON.stringify(Object.keys(API_CMD).filter(k=>typeof ACT[k]==="function"&&!/^pz[A-Z]/.test(k)))'));
const hits = [];
for (const act of acts) {
for (const name of ARGS) {
for (const v of BAD) {
const r = JSON.parse(ev(`(()=>{const c0=S.cash;let err=null;try{ACT[${JSON.stringify(act)}]({[${JSON.stringify(name)}]:${JSON.stringify(v)}});}catch(e){err=String(e&&e.message||e);}
try{closeModal();}catch(e){}UI.confirm=null;const f=__fp(),d=Object.keys(f).filter(k=>f[k]!==__fp0[k]);if(d.length)__clean();return JSON.stringify({d,spent:c0-S.cash,err});})()`));
if (r.d.length) hits.push(`${act}({${name}:'${v}'}) changed ${r.d.join(', ')}${r.spent > 0 ? ` and spent ${r.spent}` : ''}`);
}
}
}
t.diagnostic(`${acts.length} actions × ${ARGS.length} args × ${BAD.length} names`);
assert.deepEqual(hits, []);
assert.deepEqual(errors, []);
});