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
|
|
@ -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 };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue