Files
LudusPublic/relay/test/verify.test.ts

270 lines
11 KiB
TypeScript

/**
* The gate.
*
* The two tests this file exists for are `rejects a submission whose signature
* does not match` and `rejects a submission for a week that is not current`.
* Everything else is the surrounding fence.
*
* REGRESSION HONESTY. Both were verified by breaking the code, not by watching
* them go green: with the `ed25519.verify` call replaced by `true`, the
* tampered-field and wrong-key cases fail; with the `weekIdField.value !==
* currentWeekId` comparison removed, the two stale-week cases fail. Recorded
* here because a test whose failure mode nobody has seen is a test nobody knows
* the meaning of (CLAUDE.md §5.3).
*/
import { describe, expect, it } from 'vitest';
import { ed25519 } from '@noble/curves/ed25519.js';
import { submissionBytes } from '@ludus/schema';
import { DEFAULT_RELAY_CONFIG, relayConfig } from '../src/config.js';
import { checkSubmission } from '../src/verify.js';
import { keyFor, signedSubmission, toBase64Url } from './helpers.js';
const WEEK = '2026-W34';
const opts = { currentWeekId: WEEK, config: DEFAULT_RELAY_CONFIG };
describe('checkSubmission — the happy day', () => {
it('accepts a correctly signed submission for the current week', () => {
const result = checkSubmission(signedSubmission(keyFor(1)), opts);
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.value.xp).toBe(1240);
expect(result.value.clamped).toBe(false);
expect(result.value.displayName).toBe('tereza');
});
});
describe('checkSubmission — the signature', () => {
it('rejects a submission whose signature does not match the fields', () => {
// The exact attack the check exists for: take a valid submission and raise
// the XP. Every other field, including the signature, is untouched and
// authentic.
const honest = signedSubmission(keyFor(1), { xp: 1240 });
const tampered = { ...honest, xp: 999_999 };
const result = checkSubmission(tampered, opts);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/bad-signature');
});
it('rejects a submission tampered in a field nobody thinks about', () => {
// `displayName` is signed too, so it cannot be swapped in flight — which is
// what stops a relay from being a way to relabel someone else's score.
const honest = signedSubmission(keyFor(1), { displayName: 'tereza' });
const result = checkSubmission({ ...honest, displayName: 'not-tereza' }, opts);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/bad-signature');
});
it('rejects a submission signed by a different key than the one it claims', () => {
const impostor = keyFor(2);
const victim = keyFor(1);
const fields = {
pubkey: victim.pubkey,
displayName: 'tereza',
leagueId: 'domus.eng',
weekId: WEEK,
xp: 4000
};
// Genuinely signed — just not by the key in `pubkey`.
const sig = toBase64Url(ed25519.sign(submissionBytes(fields), impostor.privateKey));
const result = checkSubmission({ ...fields, sig }, opts);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/bad-signature');
});
it('rejects a signature of the right shape that is simply wrong', () => {
const honest = signedSubmission(keyFor(1));
const sig = toBase64Url(new Uint8Array(64).fill(7));
const result = checkSubmission({ ...honest, sig }, opts);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/bad-signature');
});
it('rejects a signature that is not 64 bytes', () => {
const honest = signedSubmission(keyFor(1));
const result = checkSubmission({ ...honest, sig: toBase64Url(new Uint8Array(32)) }, opts);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/bad-signature');
});
it('rejects a pubkey that is not 32 bytes, before it reaches the curve', () => {
const honest = signedSubmission(keyFor(1));
const result = checkSubmission({ ...honest, pubkey: toBase64Url(new Uint8Array(16)) }, opts);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/bad-pubkey');
});
it('rejects a pubkey that is not base64url at all', () => {
// `Buffer.from(_, 'base64')` does not throw on rubbish, it skips what it
// cannot read — so without the character-class test this decodes to
// something short and is reported as the wrong problem, or worse, as none.
const honest = signedSubmission(keyFor(1));
const result = checkSubmission({ ...honest, pubkey: '!!!!not a key!!!!' }, opts);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/bad-pubkey');
});
});
describe('checkSubmission — the week', () => {
it('rejects a submission for a week that has passed', () => {
const stale = signedSubmission(keyFor(1), { weekId: '2026-W33' });
const result = checkSubmission(stale, opts);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/stale-week');
// The message has to name BOTH weeks, because the client's only repair is
// to re-derive its week — and it cannot do that without knowing the relay's.
expect(result.error.message).toContain('2026-W34');
expect(result.error.message).toContain('2026-W33');
});
it('rejects a submission for a week that has not arrived', () => {
const early = signedSubmission(keyFor(1), { weekId: '2026-W35' });
const result = checkSubmission(early, opts);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/stale-week');
});
it('rejects a stale week BEFORE spending a signature verification on it', () => {
// Ordering matters: verifying Ed25519 is the most expensive thing this
// process does, and replaying last week's genuine traffic would otherwise
// buy an attacker unlimited CPU with valid signatures.
//
// The observable proof is that a submission which is BOTH stale and
// unsigned-nonsense reports the week, not the signature.
const stale = signedSubmission(keyFor(1), { weekId: '2026-W33' });
const result = checkSubmission({ ...stale, sig: toBase64Url(new Uint8Array(64)) }, opts);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/stale-week');
});
it('rejects a weekId that is not a week id', () => {
const result = checkSubmission({ ...signedSubmission(keyFor(1)), weekId: 'lastweek' }, opts);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/bad-field');
});
});
describe('checkSubmission — the XP ceiling', () => {
it('clamps rather than rejects, and says that it clamped', () => {
const greedy = signedSubmission(keyFor(1), { xp: 8_000_000 });
const result = checkSubmission(greedy, opts);
// Accepted, because rejecting would cost an honest heavy user their week.
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.value.xp).toBe(DEFAULT_RELAY_CONFIG.xpCeiling);
expect(result.value.clamped).toBe(true);
});
it('leaves a submission exactly at the ceiling alone', () => {
const config = relayConfig({ xpCeiling: 1240 });
const result = checkSubmission(signedSubmission(keyFor(1), { xp: 1240 }), {
currentWeekId: WEEK,
config
});
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.value.xp).toBe(1240);
expect(result.value.clamped).toBe(false);
});
it('rejects XP that is negative, fractional or not a number', () => {
for (const xp of [-1, 1.5, Number.NaN, Number.POSITIVE_INFINITY, '1240' as unknown as number]) {
const result = checkSubmission({ ...signedSubmission(keyFor(1)), xp }, opts);
expect(result.ok, `xp=${String(xp)} should be rejected`).toBe(false);
}
});
});
describe('checkSubmission — the fields', () => {
it('rejects a body that is not an object', () => {
for (const body of [null, 42, 'submission', [signedSubmission(keyFor(1))]]) {
const result = checkSubmission(body, opts);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/malformed-body');
}
});
it('rejects an unknown field rather than ignoring it', () => {
// The shape is frozen at six keys (CLAUDE.md §6.3). A relay that silently
// drops a seventh is a relay that will one day store a meaning it guessed.
const result = checkSubmission(
{ ...signedSubmission(keyFor(1)), ipAddress: '203.0.113.7' },
opts
);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/unknown-field');
expect(result.error.message).toContain('ipAddress');
});
it('rejects a display name longer than the relay accepts, without truncating', () => {
const long = 'x'.repeat(DEFAULT_RELAY_CONFIG.displayNameMaxChars + 1);
const result = checkSubmission(signedSubmission(keyFor(1), { displayName: long }), opts);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/bad-field');
// Truncating would change the bytes that were signed, so the stored row
// would no longer match its own signature. Say so in the message.
expect(result.error.message).toContain('shortened');
});
it('rejects a newline in a signed string field', () => {
// `\n` separates the fields inside `submissionBytes`. A name containing one
// could be split into two by a chosen `leagueId`, so two DIFFERENT
// submissions would produce identical signed bytes — a valid signature for
// a submission nobody made.
const sneaky = signedSubmission(keyFor(1), { displayName: 'tereza\ndomus.eng' });
const result = checkSubmission(sneaky, opts);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.code).toBe('relay/bad-field');
});
it('demonstrates the collision the newline rule prevents', () => {
// Without the rule these two submissions sign the same bytes: one claims
// the name `tereza` in league `x\ndomus.eng`, the other the name
// `tereza\nx` in league `domus.eng`.
const a = submissionBytes({
pubkey: 'k',
displayName: 'tereza',
leagueId: 'x\ndomus.eng',
weekId: WEEK,
xp: 10
});
const b = submissionBytes({
pubkey: 'k',
displayName: 'tereza\nx',
leagueId: 'domus.eng',
weekId: WEEK,
xp: 10
});
expect(Buffer.from(a).equals(Buffer.from(b))).toBe(true);
});
it('rejects an empty display name or league id', () => {
for (const field of ['displayName', 'leagueId'] as const) {
const result = checkSubmission({ ...signedSubmission(keyFor(1)), [field]: '' }, opts);
expect(result.ok, `empty ${field} should be rejected`).toBe(false);
}
});
it('rejects a missing field', () => {
const { sig: _sig, ...withoutSig } = signedSubmission(keyFor(1));
const result = checkSubmission(withoutSig, opts);
expect(result.ok).toBe(false);
});
});