Fix review findings: proto smuggling, trade-leg recall, escrow cap, static headers, rate limits, rankings DoS

Fixed: validator rejects __proto__/constructor keys at any depth (transport capacity bypass); trade and site shipments
cannot be recalled; trade escrow refund capped by Rack hall; CSP/nosniff on static files and connect-src 'self';
IPv6 /64 limiter keys + LRU eviction + per-username login bucket + scrypt concurrency cap; 5 s footprint cache for
rankings; rename uses text.cleanText; 429s no longer pinned in the idempotency store; 30-day notification prune;
units_offline notification on first offline. Accepted items listed in docs/DECISIONS.md R1.
Tested: npm test 182/182 incl. test/review-fixes.test.js (regression tests). Not re-reviewed after the fixes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
This commit is contained in:
jvanvik 2026-10-11 19:06:51 +02:00
parent b18645dca1
commit d84b0c9d12
11 changed files with 190 additions and 17 deletions

View file

@ -210,3 +210,23 @@ cannot undo exploit-kit damage; a building knocked to 0 comes back at 1. The not
## G2 — Allied incoming is a separate list
`GET /movements` keeps `incoming` as "heading for YOUR POPs" and adds `alliedIncoming` for partners' POPs (own alliance + confederations),
each entry with `ally: true` and `toOperator`. Mixing them would make clients count allies' attacks as their own.
## R1 — Review round 1 (time model + security): what was fixed and what was accepted
Fixed (each with a regression test in `test/review-fixes.test.js`): `__proto__`/`constructor` keys smuggling cargo past the transport
capacity check (validator now rejects them at any depth); trade legs recallable so one side kept both halves of a deal (trade/site legs
are no longer recallable); trade escrow refunds lifting stock above the Rack hall cap; static files served without CSP/nosniff/
no-referrer and a `connect-src` that allowed any `ws:`/`wss:` host (now `'self'`); IPv6 clients minting unlimited rate-limit buckets
(one bucket per /64), the limiter forgetting throttled keys when full (LRU eviction), no per-username login throttle (added), scrypt CPU DoS
(max 4 concurrent hashes, username checked before hashing); rankings/alliance reads scanning the whole world per request (5 s footprint
cache per world); weaker town-name sanitiser (now `text.cleanText`); rejected 429s pinned into the idempotency store (no longer);
notifications never pruned (30 days); `units_offline` warning missing when units went offline through an arrival.
Accepted for now, written down so nobody is surprised:
- Escrowed trade stock is not lootable, so an unattractive offer is a hiding place for up to 250-500 of one resource per link (48 h
offers, cancellable). Revisit if bots or players abuse it.
- Leaving/being kicked from an alliance drops pact protection at once (pacts bind alliances, not people). Revisit: track `left_at`.
- A battery above a lowered Battery bank cap is clamped immediately, while stock above a lowered Rack hall cap is kept. Intentional:
power is a live balance, stock is a physical pile.
- If an `arrive`/`return` handler ever throws, the event is marked failed and the movement stays `out` (units lost). No trigger found by
the reviewer or ~70 fuzz seeds; a recovery sweeper is a stage-7 item.
- HTTP connections per IP are not capped (only WebSockets are); behind nginx Proxy Manager the per-IP request limiter applies.
- Tokens do not expire and there is no change-password route yet (revoke from Settings works).

View file

@ -14,6 +14,8 @@ module.exports = function recallMovement(ctx0, { userId, movementId }) {
const op = mv && db.get('SELECT id FROM operators WHERE id = ? AND user_id = ?', mv.ownerId, userId);
if (!mv || !op) throw new GameError('not_found', { what: 'movement' });
if (mv.status !== 'out' || mv.type === 'return') throw new GameError('conflict', { what: 'movement cannot be recalled', status: mv.status, type: mv.type });
// Trade legs and backbone shipments are commitments: recalling one would let a side keep both halves of the deal.
if (mv.extra && (mv.extra.tradeId || mv.extra.siteId)) throw new GameError('conflict', { what: 'this shipment cannot be recalled' });
if (now - mv.departAt > D.MILITARY.recallWindowMs) throw new GameError('recall_expired', { departAt: mv.departAt, windowMs: D.MILITARY.recallWindowMs });
const origin = mv.fromTownId ? db.get('SELECT * FROM towns WHERE id = ?', mv.fromTownId) : null;
const dest = U.homeTownFor(db, mv.ownerId, origin && origin.owner_id === mv.ownerId ? origin.id : null);

View file

@ -2,12 +2,12 @@
const { GameError } = require('../errors');
const { ownedTown } = require('./build');
const STRIP = new RegExp('[\\u0000-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2066-\\u2069\\ufeff]', 'g');
const { cleanText } = require('../text');
// Display-name hygiene: no control or bidi characters, collapsed whitespace, 2..24 characters.
// Display-name hygiene (same rules as every other player-typed text): no control/bidi/invisible characters,
// collapsed whitespace, at most 24 characters.
function cleanName(v) {
const s = Array.from(String(v).replace(STRIP, '').replace(/\s+/g, ' ').trim()).slice(0, 24).join('').trim();
return s;
return Array.from(cleanText(v)).slice(0, 24).join('').trim();
}
module.exports = function renameTown(ctx, { userId, townId, name }) {

View file

@ -20,19 +20,32 @@ const MAX_BODY = 32 * 1024;
const INT_PARAMS = new Set(['id', 'townId', 'eventId', 'tokenId', 'inviteId', 'noteId', 'proposalId', 'otherId', 'operatorId', 'reportId']);
// Per-key token buckets (per IP, per token). Idle full buckets are pruned so the map cannot grow without bound.
// Rate-limit key for an address: IPv6 clients get a whole /64 each (they can mint 2^64 addresses), IPv4 is used as is.
function limitKey(ip) {
if (typeof ip !== 'string' || !ip.includes(':')) return ip;
const [head, tail = ''] = ip.split('::');
const h = head ? head.split(':') : [], t = tail ? tail.split(':') : [];
const full = ip.includes('::') ? h.concat(new Array(Math.max(0, 8 - h.length - t.length)).fill('0'), t) : h;
return full.slice(0, 4).map((x) => x.toLowerCase().replace(/^0+(?=.)/, '')).join(':') + '::/64';
}
class Limiter {
constructor(ratePerSec, burst, maxKeys) { this.rate = ratePerSec; this.burst = burst; this.maxKeys = maxKeys || 20000; this.m = new Map(); }
take(key) {
let b = this.m.get(key);
if (!b) {
if (b) this.m.delete(key); // re-insert below: Map order = least recently used first
else {
if (this.m.size >= this.maxKeys) this.prune();
b = new TokenBucket(this.rate, this.burst); this.m.set(key, b);
b = new TokenBucket(this.rate, this.burst);
}
this.m.set(key, b);
return b.take();
}
// Drop idle (full) buckets first; if everything is busy evict the least recently used. Never forget a throttled key
// just because many other keys showed up.
prune() {
for (const [k, b] of this.m) if (b.tokens >= b.burst - 0.5) this.m.delete(k);
if (this.m.size >= this.maxKeys) this.m.clear(); // under attack from >maxKeys distinct keys: forget, do not grow
for (const k of this.m.keys()) { if (this.m.size < this.maxKeys * 0.9) break; this.m.delete(k); }
}
}
@ -120,7 +133,7 @@ function createApi({ db, clock, log, opts, auth }) {
try {
const u = new URL(req.url, 'http://x');
pathname = u.pathname;
if (!ipLimit.take(ip)) { log('refused rate_limited(ip) ip=' + ip + ' path=' + pathname); throw new GameError('rate_limited', { retryAfter: 1 }); }
if (!ipLimit.take(limitKey(ip))) { log('refused rate_limited(ip) ip=' + ip + ' path=' + pathname); throw new GameError('rate_limited', { retryAfter: 1 }); }
if (!pathname.startsWith('/api/v1/')) throw new GameError('not_found');
const found = router.find(req.method, pathname.slice('/api/v1'.length));
if (!found) throw new GameError('not_found');
@ -129,7 +142,7 @@ function createApi({ db, clock, log, opts, auth }) {
routeKey = route.method + ' ' + route.path;
const meta = route.meta;
if (meta.limit === 'auth' && !authLimit.take(ip)) { log('refused rate_limited(auth) ip=' + ip + ' route=' + routeKey); throw new GameError('rate_limited', { retryAfter: 10 }); }
if (meta.limit === 'auth' && !authLimit.take(limitKey(ip))) { log('refused rate_limited(auth) ip=' + ip + ' route=' + routeKey); throw new GameError('rate_limited', { retryAfter: 10 }); }
let user = null, tokenId = null;
if (meta.auth !== false) {
@ -191,7 +204,7 @@ function createApi({ db, clock, log, opts, auth }) {
if (out instanceof Reply) { status = out.status; out = out.body; }
out = Object.assign({ serverTime: clock.now() }, out);
} catch (err) {
if (ctx.idem && err instanceof GameError && err.status < 500) {
if (ctx.idem && err instanceof GameError && err.status < 500 && err.status !== 429 && err.code !== 'request_in_progress') {
// Replaying a rejected request must give the same rejection, not run it again.
const eb = { serverTime: clock.now(), error: { code: err.code, message: err.message, details: err.details } };
storeIdem(user.id, ctx.idem, err.status, eb);
@ -219,9 +232,10 @@ function createApi({ db, clock, log, opts, auth }) {
function pruneIdempotency() {
db.run('DELETE FROM idempotency WHERE created_at < ?', clock.now() - 24 * 3600 * 1000);
db.run('DELETE FROM notifications WHERE created_at < ?', clock.now() - 30 * 24 * 3600 * 1000);
}
return { router, handle, pruneIdempotency, CODES };
}
module.exports = { createApi, Router, Reply, reply, Limiter };
module.exports = { createApi, Router, Reply, reply, Limiter, limitKey };

View file

@ -46,6 +46,8 @@ function reschedule(ctx, townId, now) {
if (!row || row.offline_since === null) {
db.run('INSERT INTO town_power (town_id, offline_since) VALUES (?, ?) ON CONFLICT (town_id) DO UPDATE SET offline_since = excluded.offline_since', townId, now);
row = { offline_since: now };
const uid = U.userOfOperator(db, town.ownerId);
if (uid) ctx.notify(uid, town.worldId, 'units_offline', { townId, since: now });
}
ctx.enqueue({ worldId: town.worldId, dueAt: Math.max(now, row.offline_since + R.decommissionMs(town.speed)), type: 'powerCheck', townId, payload: { townId } });
}

View file

@ -1,9 +1,24 @@
'use strict';
const { GameError } = require('../errors');
const { reply } = require('../http');
const { sha256, newToken, hashPassword, verifyPassword } = require('../auth');
const crypto = require('crypto');
const { sha256, newToken, hashPassword: hashRaw, verifyPassword: verifyRaw } = require('../auth');
const { Limiter } = require('../http');
// scrypt is CPU heavy: only a few run at once, the rest are told to back off instead of queueing up.
let busy = 0;
const MAX_SCRYPT = 4;
async function limited(fn) {
if (busy >= MAX_SCRYPT) throw new GameError('rate_limited', { retryAfter: 2 });
busy++;
try { return await fn(); } finally { busy--; }
}
const hashPassword = (p) => limited(() => hashRaw(p));
const verifyPassword = (p, h) => limited(() => verifyRaw(p, h));
const safeEq = (a, b) => { const x = Buffer.from(String(a)), y = Buffer.from(String(b)); return x.length === y.length && crypto.timingSafeEqual(x, y); };
function register(router, { db, clock, commands, queries, auth, opts }) {
const userLimit = new Limiter(0.02, 6, 5000); // per username: ~6 tries, then one per 50 s, whoever the IP is
const creds = {
username: { type: 'string', minLength: 3, maxLength: 20, pattern: '^[A-Za-z0-9_-]+$' },
password: { type: 'string', minLength: 8, maxLength: 128 },
@ -15,7 +30,8 @@ function register(router, { db, clock, commands, queries, auth, opts }) {
body: { type: 'object', required: ['username', 'password'], properties: Object.assign({ invite: { type: 'string', maxLength: 64 }, worldId: { type: 'integer', minimum: 1 } }, creds) },
response: { $ref: 'AuthResult' }, status: 201, errors: ['validation_failed', 'username_taken', 'signup_closed', 'world_full', 'rate_limited'],
}, async ({ body }) => {
if (opts.signupCode && body.invite !== opts.signupCode) throw new GameError('signup_closed');
if (opts.signupCode && !safeEq(body.invite || '', opts.signupCode)) throw new GameError('signup_closed');
if (db.get('SELECT 1 AS x FROM users WHERE username = ?', body.username)) throw new GameError('username_taken');
const passHash = await hashPassword(body.password);
const token = newToken();
const r = commands.run('signup', { username: body.username, passHash, tokenHash: sha256(token), device: body.device, worldId: body.worldId });
@ -28,6 +44,7 @@ function register(router, { db, clock, commands, queries, auth, opts }) {
body: { type: 'object', required: ['username', 'password'], properties: creds },
response: { $ref: 'AuthResult' }, errors: ['validation_failed', 'invalid_credentials', 'forbidden', 'rate_limited'],
}, async ({ body }) => {
if (!userLimit.take(body.username.toLowerCase())) throw new GameError('rate_limited', { retryAfter: 30 });
const u = db.get('SELECT id, username, pass_hash, banned FROM users WHERE username = ?', body.username);
const ok = await verifyPassword(body.password, u && u.pass_hash);
if (!u || !ok) throw new GameError('invalid_credentials');

View file

@ -21,7 +21,15 @@ function createSocialQueries({ db, clock, scheduler }) {
// ---------------------------------------------------------------- footprint (rankings and alliance sums)
// operatorId -> { footprint, towns } for a whole world, from two queries (no per-town round trips).
const fpCache = new Map(); // worldId -> { at, map }: a whole-world scan is O(towns), so reads share one for 5 s
function footprints(worldId, now) {
const hit = fpCache.get(worldId);
if (hit && now >= hit.at && now - hit.at < 5000) return hit.map;
const map = footprintsScan(worldId, now);
fpCache.set(worldId, { at: now, map });
return map;
}
function footprintsScan(worldId, now) {
const world = loadWorld(db, worldId);
const towns = new Map();
for (const t of db.all('SELECT id, owner_id, footprint, res_at FROM towns WHERE world_id = ?', worldId)) {

View file

@ -33,7 +33,7 @@ const MIME = {
'.txt': 'text/plain; charset=utf-8',
};
const CSP = "default-src 'self'; connect-src 'self' ws: wss:; img-src 'self' data:; " +
const CSP = "default-src 'self'; connect-src 'self'; img-src 'self' data:; " +
"style-src 'self' 'unsafe-inline'; media-src 'self' data:; base-uri 'none'; frame-ancestors 'none'";
// Headers every response carries (static files, /healthz, errors).
@ -124,6 +124,7 @@ function createStaticHandler(publicDir) {
}
return async function handle(req, res) {
applySecurityHeaders(res);
if (req.method !== 'GET' && req.method !== 'HEAD') {
return sendText(req, res, 405, 'Method Not Allowed', { Allow: 'GET, HEAD' });
}

View file

@ -20,12 +20,15 @@ const M = require('./military/movements');
const COL = { fiber: 'fiber', hardware: 'hardware', capital: 'capital' };
// Credit `amount` of `res` to a POP (settled first, no storage cap: it is a refund of what left the POP).
// Credit `amount` of `res` to a POP (settled first, capped by the Rack hall: escrow cannot be used to store past it).
function refund(db, townId, res, amount, at) {
const town = loadTown(db, townId);
if (!town) return;
settle(db, town, at);
db.run('UPDATE towns SET ' + COL[res] + ' = ' + COL[res] + ' + ? WHERE id = ?', amount, townId);
// Capped like cancelBuild: stock already above the Rack hall cap stays, but a refund never lifts it further.
const cap = Math.max(R.storageCap(town), town.res[res]);
const next = Math.min(cap, town.res[res] + amount);
db.run('UPDATE towns SET ' + COL[res] + ' = ? WHERE id = ?', next, townId);
}
const getOffer = (db, id) => db.get('SELECT * FROM trade_offers WHERE id = ?', id) || null;

View file

@ -6,6 +6,15 @@
* is true. With coerce: true, strings are turned into numbers/booleans first (query strings).
*/
const hasOwn = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
// JSON.parse makes "__proto__" an OWN key; copying it with plain assignment would swap the prototype of the result.
// Reject these keys everywhere, including inside pass-through (additionalProperties) values.
const BAD_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
function hasBadKey(v, depth) {
if (v === null || typeof v !== 'object') return false;
if (depth > 8) return true;
for (const k of Object.keys(v)) if (BAD_KEYS.has(k) || hasBadKey(v[k], (depth || 0) + 1)) return true;
return false;
}
function check(schema, v, path, errors, coerce) {
if (coerce && typeof v === 'string') {
@ -42,8 +51,9 @@ function check(schema, v, path, errors, coerce) {
const props = schema.properties || {};
for (const k of schema.required || []) if (!hasOwn(v, k) || v[k] === undefined) errors.push({ path: path ? path + '.' + k : k, message: 'is required' });
for (const k of Object.keys(v)) {
if (BAD_KEYS.has(k)) { errors.push({ path: path ? path + '.' + k : k, message: 'is not a known field' }); continue; }
if (hasOwn(props, k)) out[k] = check(props[k], v[k], path ? path + '.' + k : k, errors, coerce);
else if (schema.additionalProperties) out[k] = v[k];
else if (schema.additionalProperties) { if (hasBadKey(v[k], 0)) errors.push({ path: path ? path + '.' + k : k, message: 'contains a forbidden key' }); else out[k] = v[k]; }
else errors.push({ path: path ? path + '.' + k : k, message: 'is not a known field' });
}
return out;

96
test/review-fixes.test.js Normal file
View file

@ -0,0 +1,96 @@
'use strict';
/* Regression tests for the adversarial review findings (each failed before its fix). */
const test = require('node:test');
const assert = require('node:assert');
const { createTestServer } = require('./helpers');
const { validate } = require('../server/validate');
const { limitKey, Limiter } = require('../server/http');
test('validate: __proto__ / constructor keys are rejected, also inside pass-through objects', () => {
const s = { type: 'object', properties: { cargo: { type: 'object', additionalProperties: true } }, additionalProperties: false };
assert.ok(!validate(s, JSON.parse('{"__proto__": {"x": 1}}')).ok);
assert.ok(!validate(s, JSON.parse('{"cargo": {"fiber": 1, "__proto__": {"hardware": 50000}}}')).ok);
assert.ok(!validate(s, JSON.parse('{"cargo": {"a": {"constructor": 1}}}')).ok);
assert.ok(validate(s, { cargo: { fiber: 1 } }).ok);
});
test('transport: a prototype-smuggled cargo cannot dodge the capacity limit over HTTP', async () => {
const t = await createTestServer();
try {
const a = await t.signup('alice'), b = await t.signup('bobby');
const raw = '{"fromTownId":' + a.townId + ',"toTownId":' + b.townId + ',"type":"transport","cargo":{"fiber":1,"__proto__":{"hardware":50000,"capital":50000}}}';
const r = await fetch('http://127.0.0.1:' + t.srv.port + '/api/v1/movements', { method: 'POST', headers: { 'content-type': 'application/json', authorization: 'Bearer ' + a.token }, body: raw });
assert.ok(r.status === 422 || r.status === 400, 'got ' + r.status);
} finally { await t.close(); }
});
test('trade legs cannot be recalled (either side could otherwise keep both halves of the deal)', async () => {
const t = await createTestServer();
try {
const a = await t.signup('alice'), b = await t.signup('bobby');
for (const u of [a, b]) t.setBuilding(u.townId, 'internet_exchange', 3);
const made = await t.api('/trade/offers', { method: 'POST', body: { townId: a.townId, giveRes: 'fiber', giveAmount: 400, wantRes: 'hardware', wantAmount: 300, maxDistance: 60 } }, a.token);
assert.equal(made.status, 201, JSON.stringify(made.body));
const acc = await t.api('/trade/offers/' + made.body.offer.id + '/accept', { method: 'POST', body: { townId: b.townId } }, b.token);
assert.equal(acc.status, 201, JSON.stringify(acc.body));
const legs = t.srv.db.all("SELECT id, owner_id FROM movements WHERE type = 'transport' AND status = 'out'");
assert.equal(legs.length, 2);
for (const leg of legs) {
const token = leg.owner_id === a.operatorId ? a.token : b.token;
const r = await t.api('/movements/' + leg.id + '/recall', { method: 'POST' }, token);
assert.equal(r.status, 409, 'leg ' + leg.id + ' -> ' + JSON.stringify(r.body));
}
} finally { await t.close(); }
});
test('limiter: IPv6 clients share one bucket per /64; a throttled key is not forgotten when many others appear', () => {
assert.equal(limitKey('2001:db8:1:2:aaaa:bbbb:cccc:dddd'), limitKey('2001:db8:1:2::1'));
assert.notEqual(limitKey('2001:db8:1:3::1'), limitKey('2001:db8:1:2::1'));
assert.equal(limitKey('203.0.113.9'), '203.0.113.9');
const l = new Limiter(0.0001, 2, 50);
assert.ok(l.take('bad')); assert.ok(l.take('bad')); assert.ok(!l.take('bad'));
for (let i = 0; i < 200; i++) { l.take('k' + i); l.take('bad'); } // 'bad' keeps knocking: stays recent, stays throttled
assert.ok(!l.take('bad'));
});
test('static files carry CSP, nosniff and no-referrer; connect-src is self only', async () => {
const t = await createTestServer();
try {
for (const p of ['/', '/js/main.js', '/css/style.css', '/nope']) {
const r = await fetch('http://127.0.0.1:' + t.srv.port + p);
assert.ok(r.headers.get('content-security-policy'), p + ' has a CSP');
assert.equal(r.headers.get('x-content-type-options'), 'nosniff');
assert.equal(r.headers.get('referrer-policy'), 'no-referrer');
assert.ok(!/ws:|wss:/.test(r.headers.get('content-security-policy')));
}
} finally { await t.close(); }
});
test('rename: invisible and bidi characters are stripped like every other player text', async () => {
const t = await createTestServer();
try {
const a = await t.signup('alice');
const r = await t.api('/towns/' + a.townId + '/rename', { method: 'POST', body: { name: 'A‮BC​D' } }, a.token);
assert.equal(r.status, 200);
assert.equal(r.body.name, 'ABCD');
const blank = await t.api('/towns/' + a.townId + '/rename', { method: 'POST', body: { name: '​​​' } }, a.token);
assert.equal(blank.status, 422);
} finally { await t.close(); }
});
test('trade escrow refund never lifts stock above the Rack hall cap', async () => {
const t = await createTestServer();
try {
const a = await t.signup('alice');
t.setBuilding(a.townId, 'internet_exchange', 3);
const R = require('../public/js/rules.js');
const mk = () => t.api('/trade/offers', { method: 'POST', body: { townId: a.townId, giveRes: 'fiber', giveAmount: 350, wantRes: 'hardware', wantAmount: 300, maxDistance: 40 } }, a.token);
const o1 = await mk(), o2 = await mk();
assert.equal(o1.status, 201); assert.equal(o2.status, 201, JSON.stringify(o2.body));
t.clock.advance(100 * 3600e3); // production refills the stock to the cap
for (const o of [o1, o2]) await t.api('/trade/offers/' + o.body.offer.id, { method: 'DELETE' }, a.token);
const town = require('../server/state').loadTown(t.srv.db, a.townId);
const cap = R.storageCap(town);
assert.ok(R.resourcesAt(town, t.clock.now()).amounts.fiber <= cap + 1e-6, 'fiber stays <= cap ' + cap);
} finally { await t.close(); }
});