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

@ -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');