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:
parent
b18645dca1
commit
d84b0c9d12
11 changed files with 190 additions and 17 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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 }) {
|
||||
|
|
|
|||
|
|
@ -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 };
|
||||
|
|
|
|||
|
|
@ -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 } });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -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)) {
|
||||
|
|
|
|||
|
|
@ -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' });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue