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
96 lines
5.6 KiB
JavaScript
96 lines
5.6 KiB
JavaScript
'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: 'ABCD' } }, 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(); }
|
||
});
|