[relay] Relay: The league relay from Ludus, byte for byte, with the slice of the schema it imports
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* Signer and verifier are two different Ed25519 implementations. Prove they agree.
|
||||
*
|
||||
* The player signs through `crypto.subtle` (`@ludus/identity`'s `sign`, because
|
||||
* a WebCrypto key can be non-extractable and a JavaScript one cannot). The relay
|
||||
* verifies with `@noble/curves`, because it runs in Node and has no key material
|
||||
* of its own to protect.
|
||||
*
|
||||
* Two implementations of the same algorithm are a place where a signature can
|
||||
* fail for a reason that has nothing to do with an attacker — a PKCS#8 wrapper,
|
||||
* a byte order, a hash choice — and the symptom would be indistinguishable from
|
||||
* a hostile client. So this test does the player's half with Node's own
|
||||
* WebCrypto, which is the same API `@ludus/identity` calls, and hands the result
|
||||
* to the relay's real gate.
|
||||
*
|
||||
* It does NOT import `@ludus/identity`: the relay may import `@ludus/schema` and
|
||||
* nothing else from this repository (CLAUDE.md §2). What it shares with the
|
||||
* player is the part that matters — `submissionBytes`, the one canonical
|
||||
* encoding, which now lives in `@ludus/schema` for exactly this reason.
|
||||
*/
|
||||
import { webcrypto } from 'node:crypto';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { submissionBytes } from '@ludus/schema';
|
||||
import { DEFAULT_RELAY_CONFIG } from '../src/config.js';
|
||||
import { checkSubmission } from '../src/verify.js';
|
||||
|
||||
const subtle = webcrypto.subtle;
|
||||
|
||||
function toBase64Url(bytes: Uint8Array): string {
|
||||
return Buffer.from(bytes).toString('base64url');
|
||||
}
|
||||
|
||||
/**
|
||||
* Raw 32-byte public key from a WebCrypto Ed25519 key.
|
||||
*
|
||||
* `spki` is what WebCrypto exports; the last 32 bytes of it are the key. The
|
||||
* 12-byte prefix is the fixed ASN.1 header for Ed25519 — same family of detail
|
||||
* as the PKCS#8 wrapper in `@ludus/identity`, and the same place this goes
|
||||
* wrong.
|
||||
*/
|
||||
async function rawPublicKey(key: CryptoKey): Promise<Uint8Array> {
|
||||
const spki = new Uint8Array(await subtle.exportKey('spki', key));
|
||||
return spki.slice(spki.length - 32);
|
||||
}
|
||||
|
||||
describe('WebCrypto signs, @noble verifies', () => {
|
||||
it('accepts a submission signed the way the player signs it', async () => {
|
||||
const pair = (await subtle.generateKey({ name: 'Ed25519' }, true, [
|
||||
'sign',
|
||||
'verify'
|
||||
])) as CryptoKeyPair;
|
||||
|
||||
const pubkey = toBase64Url(await rawPublicKey(pair.publicKey));
|
||||
const fields = {
|
||||
pubkey,
|
||||
displayName: 'mnovak',
|
||||
leagueId: 'domus.eng',
|
||||
weekId: '2026-W34',
|
||||
xp: 1240
|
||||
};
|
||||
|
||||
const signature = new Uint8Array(
|
||||
await subtle.sign({ name: 'Ed25519' }, pair.privateKey, submissionBytes(fields))
|
||||
);
|
||||
|
||||
const result = checkSubmission(
|
||||
{ ...fields, sig: toBase64Url(signature) },
|
||||
{ currentWeekId: '2026-W34', config: DEFAULT_RELAY_CONFIG }
|
||||
);
|
||||
|
||||
expect(result.ok, result.ok ? '' : result.error.message).toBe(true);
|
||||
});
|
||||
|
||||
it('still rejects a WebCrypto-signed submission that was altered afterwards', async () => {
|
||||
// The interop test must not become a test that accepts everything. Same
|
||||
// key, same real signature, one field changed after signing.
|
||||
const pair = (await subtle.generateKey({ name: 'Ed25519' }, true, [
|
||||
'sign',
|
||||
'verify'
|
||||
])) as CryptoKeyPair;
|
||||
|
||||
const pubkey = toBase64Url(await rawPublicKey(pair.publicKey));
|
||||
const fields = {
|
||||
pubkey,
|
||||
displayName: 'mnovak',
|
||||
leagueId: 'domus.eng',
|
||||
weekId: '2026-W34',
|
||||
xp: 1240
|
||||
};
|
||||
const signature = new Uint8Array(
|
||||
await subtle.sign({ name: 'Ed25519' }, pair.privateKey, submissionBytes(fields))
|
||||
);
|
||||
|
||||
const result = checkSubmission(
|
||||
{ ...fields, xp: 4999, sig: toBase64Url(signature) },
|
||||
{ currentWeekId: '2026-W34', config: DEFAULT_RELAY_CONFIG }
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error.code).toBe('relay/bad-signature');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user