BioLock / source · biolock.dev / test / server.test.js
| 1 | // Runs a software passkey (ES256 and Ed25519) against server.js. `node --test` |
| 2 | import test from 'node:test'; |
| 3 | import assert from 'node:assert/strict'; |
| 4 | import * as S from '../src/server.js'; |
| 5 | |
| 6 | const enc = new TextEncoder(); |
| 7 | const RP = 'moontrace.ai', ORIGIN = 'https://moontrace.ai'; |
| 8 | |
| 9 | // --- tiny CBOR writer for the test authenticator --- |
| 10 | function head(major, n) { |
| 11 | if (n < 24) return [major << 5 | n]; |
| 12 | if (n < 256) return [major << 5 | 24, n]; |
| 13 | return [major << 5 | 25, n >> 8, n & 255]; |
| 14 | } |
| 15 | function cbor(v) { |
| 16 | if (v instanceof Uint8Array) return [...head(2, v.length), ...v]; |
| 17 | if (typeof v === 'string') { const b = enc.encode(v); return [...head(3, b.length), ...b]; } |
| 18 | if (typeof v === 'number') return v >= 0 ? head(0, v) : head(1, -1 - v); |
| 19 | if (v instanceof Map) { const o = head(5, v.size); for (const [k, x] of v) o.push(...cbor(k), ...cbor(x)); return o; } |
| 20 | throw new Error('cbor'); |
| 21 | } |
| 22 | const u8 = (a) => new Uint8Array(a); |
| 23 | async function sha(b) { return u8(await crypto.subtle.digest('SHA-256', b)); } |
| 24 | function rawToDer(raw) { |
| 25 | 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]; }; |
| 26 | const body = [...int(raw.slice(0, 32)), ...int(raw.slice(32))]; |
| 27 | return u8([0x30, body.length, ...body]); |
| 28 | } |
| 29 | |
| 30 | async function softKey(kind) { |
| 31 | const p = kind === 'ed' ? await crypto.subtle.generateKey({ name: 'Ed25519' }, true, ['sign', 'verify']) |
| 32 | : await crypto.subtle.generateKey({ name: 'ECDSA', namedCurve: 'P-256' }, true, ['sign', 'verify']); |
| 33 | const jwk = await crypto.subtle.exportKey('jwk', p.publicKey); |
| 34 | const cose = kind === 'ed' ? new Map([[1, 1], [3, -8], [-1, 6], [-2, S.fromB64u(jwk.x)]]) |
| 35 | : new Map([[1, 2], [3, -7], [-1, 1], [-2, S.fromB64u(jwk.x)], [-3, S.fromB64u(jwk.y)]]); |
| 36 | const id = crypto.getRandomValues(new Uint8Array(16)); |
| 37 | let counter = 0; |
| 38 | const sign = async (data) => kind === 'ed' ? u8(await crypto.subtle.sign('Ed25519', p.privateKey, data)) |
| 39 | : rawToDer(u8(await crypto.subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, p.privateKey, data))); |
| 40 | const authData = async (flags, withKey, rp = RP) => { |
| 41 | const ctr = [counter >>> 24, (counter >> 16) & 255, (counter >> 8) & 255, counter & 255]; |
| 42 | const parts = [...(await sha(enc.encode(rp))), flags, ...ctr]; |
| 43 | if (withKey) parts.push(...new Uint8Array(16), 0, id.length, ...id, ...cbor(cose)); |
| 44 | return u8(parts); |
| 45 | }; |
| 46 | return { |
| 47 | id: S.toB64u(id), setCounter(n) { counter = n; }, |
| 48 | async create(opts, { origin = ORIGIN, flags = 0x45, rp = RP } = {}) { |
| 49 | const cdj = enc.encode(JSON.stringify({ type: 'webauthn.create', challenge: opts.challenge, origin })); |
| 50 | const ao = u8(cbor(new Map([['fmt', 'none'], ['attStmt', new Map()], ['authData', await authData(flags, true, rp)]]))); |
| 51 | return { id: S.toB64u(id), type: 'public-key', response: { clientDataJSON: S.toB64u(cdj), attestationObject: S.toB64u(ao), transports: ['internal'] } }; |
| 52 | }, |
| 53 | async get(opts, { origin = ORIGIN, flags = 0x05 } = {}) { |
| 54 | const cdj = enc.encode(JSON.stringify({ type: 'webauthn.get', challenge: opts.challenge, origin })); |
| 55 | const ad = await authData(flags, false); |
| 56 | const sig = await sign(u8([...ad, ...(await sha(cdj))])); |
| 57 | return { id: S.toB64u(id), type: 'public-key', response: { clientDataJSON: S.toB64u(cdj), authenticatorData: S.toB64u(ad), signature: S.toB64u(sig), userHandle: null } }; |
| 58 | }, |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | for (const kind of ['ec', 'ed']) { |
| 63 | test(`${kind}: enroll a device, then sign in with its fingerprint`, async () => { |
| 64 | const k = await softKey(kind); |
| 65 | const ro = S.registrationOptions({ rpId: RP, userName: 'member' }); |
| 66 | assert.equal(ro.authenticatorSelection.authenticatorAttachment, 'platform'); |
| 67 | assert.deepEqual(ro.hints, ['client-device']); |
| 68 | const stored = await S.verifyRegistration({ credential: await k.create(ro), expectedChallenge: ro.challenge, expectedOrigin: ORIGIN, rpId: RP }); |
| 69 | assert.equal(stored.id, k.id); |
| 70 | const so = S.signInOptions({ rpId: RP, deviceCredentialIds: [stored.id] }); |
| 71 | assert.deepEqual(so.allowCredentials[0].transports, ['internal']); |
| 72 | const ok = await S.verifySignIn({ credential: await k.get(so), stored, expectedChallenge: so.challenge, expectedOrigin: ORIGIN, rpId: RP }); |
| 73 | assert.equal(ok.counter, 0); |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | test('refuses a sign-in from the wrong website', async () => { |
| 78 | const k = await softKey('ec'); |
| 79 | const ro = S.registrationOptions({ rpId: RP, userName: 'm' }); |
| 80 | await assert.rejects(S.verifyRegistration({ credential: await k.create(ro, { origin: 'https://evil.example' }), expectedChallenge: ro.challenge, expectedOrigin: ORIGIN, rpId: RP }), /wrong website/); |
| 81 | await assert.rejects(S.verifyRegistration({ credential: await k.create(ro, { rp: 'evil.example' }), expectedChallenge: ro.challenge, expectedOrigin: ORIGIN, rpId: RP }), /different site/); |
| 82 | }); |
| 83 | |
| 84 | test('refuses a reused challenge, a missing fingerprint, a bad signature and a cloned key', async () => { |
| 85 | const k = await softKey('ec'); |
| 86 | const ro = S.registrationOptions({ rpId: RP, userName: 'm' }); |
| 87 | const stored = await S.verifyRegistration({ credential: await k.create(ro), expectedChallenge: ro.challenge, expectedOrigin: ORIGIN, rpId: RP }); |
| 88 | const so = S.signInOptions({ rpId: RP }); |
| 89 | await assert.rejects(S.verifySignIn({ credential: await k.get(so), stored, expectedChallenge: 'other', expectedOrigin: ORIGIN, rpId: RP }), /challenge/); |
| 90 | await assert.rejects(S.verifySignIn({ credential: await k.get(so, { flags: 0x01 }), stored, expectedChallenge: so.challenge, expectedOrigin: ORIGIN, rpId: RP }), /biolock/); |
| 91 | const bad = await k.get(so); const sig = S.fromB64u(bad.response.signature); sig[10] ^= 1; bad.response.signature = S.toB64u(sig); |
| 92 | await assert.rejects(S.verifySignIn({ credential: bad, stored, expectedChallenge: so.challenge, expectedOrigin: ORIGIN, rpId: RP }), /signature/); |
| 93 | k.setCounter(5); |
| 94 | const r = await S.verifySignIn({ credential: await k.get(so), stored, expectedChallenge: so.challenge, expectedOrigin: ORIGIN, rpId: RP }); |
| 95 | stored.counter = r.counter; |
| 96 | k.setCounter(3); |
| 97 | await assert.rejects(S.verifySignIn({ credential: await k.get(so), stored, expectedChallenge: so.challenge, expectedOrigin: ORIGIN, rpId: RP }), /cloned/); |
| 98 | }); |
| 99 | |
| 100 | test('refuses security keys and QR (phone-to-computer) sign-ins', async () => { |
| 101 | const k = await softKey('ec'); |
| 102 | const ro = S.registrationOptions({ rpId: RP, userName: 'm' }); |
| 103 | for (const t of ['usb', 'nfc', 'hybrid']) { |
| 104 | const c = await k.create(ro); c.response.transports = [t]; |
| 105 | await assert.rejects(S.verifyRegistration({ credential: c, expectedChallenge: ro.challenge, expectedOrigin: ORIGIN, rpId: RP }), /own fingerprint/); |
| 106 | } |
| 107 | const c = await k.create(ro); c.authenticatorAttachment = 'cross-platform'; |
| 108 | await assert.rejects(S.verifyRegistration({ credential: c, expectedChallenge: ro.challenge, expectedOrigin: ORIGIN, rpId: RP }), /own fingerprint/); |
| 109 | const stored = await S.verifyRegistration({ credential: await k.create(ro), expectedChallenge: ro.challenge, expectedOrigin: ORIGIN, rpId: RP }); |
| 110 | const so = S.signInOptions({ rpId: RP, deviceCredentialIds: [stored.id] }); |
| 111 | const g = await k.get(so); g.authenticatorAttachment = 'cross-platform'; |
| 112 | await assert.rejects(S.verifySignIn({ credential: g, stored, expectedChallenge: so.challenge, expectedOrigin: ORIGIN, rpId: RP }), /own fingerprint/); |
| 113 | }); |
| 114 | |
| 115 | test('add-a-device invite links: valid, tampered, expired', async () => { |
| 116 | const t = await S.makeDeviceInvite({ secret: 's3cret', accountId: 'acct1' }); |
| 117 | assert.equal((await S.readDeviceInvite({ secret: 's3cret', token: t })).accountId, 'acct1'); |
| 118 | await assert.rejects(S.readDeviceInvite({ secret: 'wrong', token: t }), /not valid/); |
| 119 | const old = await S.makeDeviceInvite({ secret: 's3cret', accountId: 'a', minutes: -1 }); |
| 120 | await assert.rejects(S.readDeviceInvite({ secret: 's3cret', token: old }), /expired/); |
| 121 | }); |