|
| 1 | +/** |
| 2 | + * The WebAuthn user handle must not be derived from anything a person types. |
| 3 | + * |
| 4 | + * An authenticator keeps one discoverable credential per (rp.id, user.id) and |
| 5 | + * replaces the previous one when both match — no prompt, no undo. While the |
| 6 | + * handle was `TextEncoder().encode(userId)`, two people registering under the |
| 7 | + * same name on one device destroyed each other's passkey, and with it the DID |
| 8 | + * and every entry signed under it (#45). |
| 9 | + * |
| 10 | + * The first test is the load-bearing one: it asserts what the library *sends*, |
| 11 | + * so it holds regardless of how faithfully the mock models an authenticator. |
| 12 | + * The rest describe the consequence. |
| 13 | + */ |
| 14 | +import { test, expect } from '@playwright/test'; |
| 15 | +import { |
| 16 | + createMockAuthenticator, |
| 17 | + installMockAuthenticator, |
| 18 | +} from './helpers/mock-authenticator.js'; |
| 19 | +import { silenceWebAuthnDebugLogging } from './helpers/two-peer.js'; |
| 20 | + |
| 21 | +const NAME = 'anna'; |
| 22 | + |
| 23 | +test.describe('WebAuthn user handle', () => { |
| 24 | + let restoreAuthenticator; |
| 25 | + let restoreLogging; |
| 26 | + let authenticator; |
| 27 | + let WebAuthnDIDProvider; |
| 28 | + |
| 29 | + test.beforeEach(async () => { |
| 30 | + restoreLogging = silenceWebAuthnDebugLogging(); |
| 31 | + authenticator = await createMockAuthenticator(); |
| 32 | + restoreAuthenticator = installMockAuthenticator(authenticator); |
| 33 | + ({ WebAuthnDIDProvider } = await import('../src/webauthn/provider.js')); |
| 34 | + }); |
| 35 | + |
| 36 | + test.afterEach(() => { |
| 37 | + restoreAuthenticator?.(); |
| 38 | + restoreLogging?.(); |
| 39 | + }); |
| 40 | + |
| 41 | + test('is 64 random bytes, and carries nothing the user typed', async () => { |
| 42 | + await WebAuthnDIDProvider.createCredential({ |
| 43 | + userId: NAME, |
| 44 | + displayName: 'Anna', |
| 45 | + }); |
| 46 | + await WebAuthnDIDProvider.createCredential({ |
| 47 | + userId: NAME, |
| 48 | + displayName: 'Anna', |
| 49 | + }); |
| 50 | + |
| 51 | + const [first, second] = authenticator.state.createdUsers; |
| 52 | + const firstHandle = new Uint8Array(first.id); |
| 53 | + const secondHandle = new Uint8Array(second.id); |
| 54 | + |
| 55 | + expect(firstHandle.length).toBe(64); |
| 56 | + expect(secondHandle.length).toBe(64); |
| 57 | + |
| 58 | + // Same input, different handle — the property the whole fix rests on. |
| 59 | + expect(Buffer.from(firstHandle).equals(Buffer.from(secondHandle))).toBe( |
| 60 | + false |
| 61 | + ); |
| 62 | + |
| 63 | + // And nothing recognisable in it: not the typed bytes, not the name as text. |
| 64 | + const typed = new TextEncoder().encode(NAME); |
| 65 | + expect( |
| 66 | + Buffer.from(firstHandle.subarray(0, typed.length)).equals( |
| 67 | + Buffer.from(typed) |
| 68 | + ) |
| 69 | + ).toBe(false); |
| 70 | + expect(new TextDecoder().decode(firstHandle)).not.toContain(NAME); |
| 71 | + }); |
| 72 | + |
| 73 | + test('keeps the typed value as the label the picker shows', async () => { |
| 74 | + await WebAuthnDIDProvider.createCredential({ |
| 75 | + userId: NAME, |
| 76 | + displayName: 'Anna at the front desk', |
| 77 | + }); |
| 78 | + |
| 79 | + const [user] = authenticator.state.createdUsers; |
| 80 | + expect(user.name).toBe(NAME); |
| 81 | + expect(user.displayName).toBe('Anna at the front desk'); |
| 82 | + }); |
| 83 | + |
| 84 | + test('two people with the same name no longer overwrite each other', async () => { |
| 85 | + const first = await WebAuthnDIDProvider.createCredential({ |
| 86 | + userId: NAME, |
| 87 | + displayName: 'Anna', |
| 88 | + }); |
| 89 | + const second = await WebAuthnDIDProvider.createCredential({ |
| 90 | + userId: NAME, |
| 91 | + displayName: 'Anna', |
| 92 | + }); |
| 93 | + |
| 94 | + expect(second.credentialId).not.toBe(first.credentialId); |
| 95 | + |
| 96 | + // Both survive. Under a name-derived handle the second registration would |
| 97 | + // have landed in the first one's slot and left a single credential here. |
| 98 | + const resident = authenticator.residentCredentials(); |
| 99 | + expect(resident.length).toBe(2); |
| 100 | + expect(resident.map((entry) => entry.credentialId).sort()).toEqual( |
| 101 | + [first.credentialId, second.credentialId].sort() |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + test('returns the handle, base64url of 64 bytes', async () => { |
| 106 | + const credential = await WebAuthnDIDProvider.createCredential({ |
| 107 | + userId: NAME, |
| 108 | + displayName: 'Anna', |
| 109 | + }); |
| 110 | + |
| 111 | + expect(credential.userHandle).toBeTruthy(); |
| 112 | + expect(credential.userHandle).toMatch(/^[A-Za-z0-9_-]+$/); |
| 113 | + |
| 114 | + const decoded = new Uint8Array( |
| 115 | + WebAuthnDIDProvider.base64urlToArrayBuffer(credential.userHandle) |
| 116 | + ); |
| 117 | + expect(decoded.length).toBe(64); |
| 118 | + expect( |
| 119 | + Buffer.from(decoded).equals( |
| 120 | + Buffer.from(new Uint8Array(authenticator.state.createdUsers[0].id)) |
| 121 | + ) |
| 122 | + ).toBe(true); |
| 123 | + }); |
| 124 | +}); |
0 commit comments