// Runs a software passkey (ES256 and Ed25519) against server.js. `node --test` import test from 'node:test'; import assert from 'node:assert/strict'; import * as S from '../src/server.js'; const enc = new TextEncoder(); const RP = 'moontrace.ai', ORIGIN = 'https://moontrace.ai'; // --- tiny CBOR writer for the test authenticator --- function head(major, n) { if (n < 24) return [major << 5 | n]; if (n < 256) return [major << 5 | 24, n]; return [major << 5 | 25, n >> 8, n & 255]; } function cbor(v) { if (v instanceof Uint8Array) return [...head(2, v.length), ...v]; if (typeof v === 'string') { const b = enc.encode(v); return [...head(3, b.length), ...b]; } if (typeof v === 'number') return v >= 0 ? head(0, v) : head(1, -1 - v); if (v instanceof Map) { const o = head(5, v.size); for (const [k, x] of v) o.push(...cbor(k), ...cbor(x)); return o; } throw new Error('cbor'); } const u8 = (a) => new Uint8Array(a); async function sha(b) { return u8(await crypto.subtle.digest('SHA-256', b)); } function rawToDer(raw) { const int = (b) => { let i = 0; while (i < b.length - 1 && b[i] === 0) i++; b = b.slice(i); if (b[0] & 0x80) b = u8([0, ...b]); return [2, b.length, ...b]; }; const body = [...int(raw.slice(0, 32)), ...int(raw.slice(32))]; return u8([0x30, body.length, ...body]); } async function softKey(kind) { const p = kind === 'ed' ? await crypto.subtle.generateKey({ name: 'Ed25519' }, true, ['sign', 'verify']) : await crypto.subtle.generateKey({ name: 'ECDSA', namedCurve: 'P-256' }, true, ['sign', 'verify']); const jwk = await crypto.subtle.exportKey('jwk', p.publicKey); const cose = kind === 'ed' ? new Map([[1, 1], [3, -8], [-1, 6], [-2, S.fromB64u(jwk.x)]]) : new Map([[1, 2], [3, -7], [-1, 1], [-2, S.fromB64u(jwk.x)], [-3, S.fromB64u(jwk.y)]]); const id = crypto.getRandomValues(new Uint8Array(16)); let counter = 0; const sign = async (data) => kind === 'ed' ? u8(await crypto.subtle.sign('Ed25519', p.privateKey, data)) : rawToDer(u8(await crypto.subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, p.privateKey, data))); const authData = async (flags, withKey, rp = RP) => { const ctr = [counter >>> 24, (counter >> 16) & 255, (counter >> 8) & 255, counter & 255]; const parts = [...(await sha(enc.encode(rp))), flags, ...ctr]; if (withKey) parts.push(...new Uint8Array(16), 0, id.length, ...id, ...cbor(cose)); return u8(parts); }; return { id: S.toB64u(id), setCounter(n) { counter = n; }, async create(opts, { origin = ORIGIN, flags = 0x45, rp = RP } = {}) { const cdj = enc.encode(JSON.stringify({ type: 'webauthn.create', challenge: opts.challenge, origin })); const ao = u8(cbor(new Map([['fmt', 'none'], ['attStmt', new Map()], ['authData', await authData(flags, true, rp)]]))); return { id: S.toB64u(id), type: 'public-key', response: { clientDataJSON: S.toB64u(cdj), attestationObject: S.toB64u(ao), transports: ['internal'] } }; }, async get(opts, { origin = ORIGIN, flags = 0x05 } = {}) { const cdj = enc.encode(JSON.stringify({ type: 'webauthn.get', challenge: opts.challenge, origin })); const ad = await authData(flags, false); const sig = await sign(u8([...ad, ...(await sha(cdj))])); return { id: S.toB64u(id), type: 'public-key', response: { clientDataJSON: S.toB64u(cdj), authenticatorData: S.toB64u(ad), signature: S.toB64u(sig), userHandle: null } }; }, }; } for (const kind of ['ec', 'ed']) { test(`${kind}: enroll a device, then sign in with its fingerprint`, async () => { const k = await softKey(kind); const ro = S.registrationOptions({ rpId: RP, userName: 'member' }); assert.equal(ro.authenticatorSelection.authenticatorAttachment, 'platform'); assert.deepEqual(ro.hints, ['client-device']); const stored = await S.verifyRegistration({ credential: await k.create(ro), expectedChallenge: ro.challenge, expectedOrigin: ORIGIN, rpId: RP }); assert.equal(stored.id, k.id); const so = S.signInOptions({ rpId: RP, deviceCredentialIds: [stored.id] }); assert.deepEqual(so.allowCredentials[0].transports, ['internal']); const ok = await S.verifySignIn({ credential: await k.get(so), stored, expectedChallenge: so.challenge, expectedOrigin: ORIGIN, rpId: RP }); assert.equal(ok.counter, 0); }); } test('refuses a sign-in from the wrong website', async () => { const k = await softKey('ec'); const ro = S.registrationOptions({ rpId: RP, userName: 'm' }); await assert.rejects(S.verifyRegistration({ credential: await k.create(ro, { origin: 'https://evil.example' }), expectedChallenge: ro.challenge, expectedOrigin: ORIGIN, rpId: RP }), /wrong website/); await assert.rejects(S.verifyRegistration({ credential: await k.create(ro, { rp: 'evil.example' }), expectedChallenge: ro.challenge, expectedOrigin: ORIGIN, rpId: RP }), /different site/); }); test('refuses a reused challenge, a missing fingerprint, a bad signature and a cloned key', async () => { const k = await softKey('ec'); const ro = S.registrationOptions({ rpId: RP, userName: 'm' }); const stored = await S.verifyRegistration({ credential: await k.create(ro), expectedChallenge: ro.challenge, expectedOrigin: ORIGIN, rpId: RP }); const so = S.signInOptions({ rpId: RP }); await assert.rejects(S.verifySignIn({ credential: await k.get(so), stored, expectedChallenge: 'other', expectedOrigin: ORIGIN, rpId: RP }), /challenge/); await assert.rejects(S.verifySignIn({ credential: await k.get(so, { flags: 0x01 }), stored, expectedChallenge: so.challenge, expectedOrigin: ORIGIN, rpId: RP }), /biolock/); const bad = await k.get(so); const sig = S.fromB64u(bad.response.signature); sig[10] ^= 1; bad.response.signature = S.toB64u(sig); await assert.rejects(S.verifySignIn({ credential: bad, stored, expectedChallenge: so.challenge, expectedOrigin: ORIGIN, rpId: RP }), /signature/); k.setCounter(5); const r = await S.verifySignIn({ credential: await k.get(so), stored, expectedChallenge: so.challenge, expectedOrigin: ORIGIN, rpId: RP }); stored.counter = r.counter; k.setCounter(3); await assert.rejects(S.verifySignIn({ credential: await k.get(so), stored, expectedChallenge: so.challenge, expectedOrigin: ORIGIN, rpId: RP }), /cloned/); }); test('refuses security keys and QR (phone-to-computer) sign-ins', async () => { const k = await softKey('ec'); const ro = S.registrationOptions({ rpId: RP, userName: 'm' }); for (const t of ['usb', 'nfc', 'hybrid']) { const c = await k.create(ro); c.response.transports = [t]; await assert.rejects(S.verifyRegistration({ credential: c, expectedChallenge: ro.challenge, expectedOrigin: ORIGIN, rpId: RP }), /own fingerprint/); } const c = await k.create(ro); c.authenticatorAttachment = 'cross-platform'; await assert.rejects(S.verifyRegistration({ credential: c, expectedChallenge: ro.challenge, expectedOrigin: ORIGIN, rpId: RP }), /own fingerprint/); const stored = await S.verifyRegistration({ credential: await k.create(ro), expectedChallenge: ro.challenge, expectedOrigin: ORIGIN, rpId: RP }); const so = S.signInOptions({ rpId: RP, deviceCredentialIds: [stored.id] }); const g = await k.get(so); g.authenticatorAttachment = 'cross-platform'; await assert.rejects(S.verifySignIn({ credential: g, stored, expectedChallenge: so.challenge, expectedOrigin: ORIGIN, rpId: RP }), /own fingerprint/); }); test('add-a-device invite links: valid, tampered, expired', async () => { const t = await S.makeDeviceInvite({ secret: 's3cret', accountId: 'acct1' }); assert.equal((await S.readDeviceInvite({ secret: 's3cret', token: t })).accountId, 'acct1'); await assert.rejects(S.readDeviceInvite({ secret: 'wrong', token: t }), /not valid/); const old = await S.makeDeviceInvite({ secret: 's3cret', accountId: 'a', minutes: -1 }); await assert.rejects(S.readDeviceInvite({ secret: 's3cret', token: old }), /expired/); });