|
| 1 | +import { describe, it, expect, beforeEach } from 'vitest' |
| 2 | +import { createRoom } from './createRoom' |
| 3 | +import { |
| 4 | + loadPeerId, |
| 5 | + loadSigningPrivateKey, |
| 6 | + loadSigningPublicKey, |
| 7 | + loadOaepPrivateKey, |
| 8 | + loadOaepPublicKeyB64, |
| 9 | + loadRoomKey, |
| 10 | +} from '../crypto/storage' |
| 11 | +import { ROLES, JWT_EXPIRY_SECONDS, STORAGE_KEYS } from '../../constants' |
| 12 | + |
| 13 | +function parseFragment(): { token: string | null } { |
| 14 | + const hash = window.location.hash.slice(1) |
| 15 | + if (!hash) return { token: null } |
| 16 | + const params = new URLSearchParams(hash) |
| 17 | + return { token: params.get('token') } |
| 18 | +} |
| 19 | + |
| 20 | +function decodeJWTPayload(raw: string): Record<string, unknown> { |
| 21 | + const parts = raw.split('.') |
| 22 | + if (parts.length !== 3) throw new Error('Invalid JWT') |
| 23 | + const b64 = parts[1].replace(/-/g, '+').replace(/_/g, '/') |
| 24 | + const padded = b64.padEnd(b64.length + ((4 - (b64.length % 4)) % 4), '=') |
| 25 | + return JSON.parse(atob(padded)) as Record<string, unknown> |
| 26 | +} |
| 27 | + |
| 28 | +beforeEach(() => { |
| 29 | + localStorage.clear() |
| 30 | + window.location.hash = '' |
| 31 | +}) |
| 32 | + |
| 33 | +describe('createRoom', () => { |
| 34 | + it('sets the URL fragment to #token=<jwt> with no other params', async () => { |
| 35 | + await createRoom() |
| 36 | + const hash = window.location.hash.slice(1) |
| 37 | + const params = new URLSearchParams(hash) |
| 38 | + expect(params.get('token')).not.toBeNull() |
| 39 | + // Ensure the only key in the fragment is "token" |
| 40 | + const keys = Array.from(params.keys()) |
| 41 | + expect(keys).toEqual(['token']) |
| 42 | + }) |
| 43 | + |
| 44 | + it('JWT payload has role=owner', async () => { |
| 45 | + await createRoom() |
| 46 | + const { token } = parseFragment() |
| 47 | + expect(token).not.toBeNull() |
| 48 | + const payload = decodeJWTPayload(token!) |
| 49 | + expect(payload.role).toBe(ROLES.OWNER) |
| 50 | + }) |
| 51 | + |
| 52 | + it('JWT payload has a UUID room field', async () => { |
| 53 | + await createRoom() |
| 54 | + const { token } = parseFragment() |
| 55 | + const payload = decodeJWTPayload(token!) |
| 56 | + expect(typeof payload.room).toBe('string') |
| 57 | + expect((payload.room as string).length).toBeGreaterThan(0) |
| 58 | + // UUID format |
| 59 | + expect(payload.room).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i) |
| 60 | + }) |
| 61 | + |
| 62 | + it('JWT payload iss matches the saved peerId', async () => { |
| 63 | + await createRoom() |
| 64 | + const peerId = loadPeerId() |
| 65 | + expect(peerId).not.toBeNull() |
| 66 | + const { token } = parseFragment() |
| 67 | + const payload = decodeJWTPayload(token!) |
| 68 | + expect(payload.iss).toBe(peerId) |
| 69 | + }) |
| 70 | + |
| 71 | + it('JWT payload has a jti field', async () => { |
| 72 | + await createRoom() |
| 73 | + const { token } = parseFragment() |
| 74 | + const payload = decodeJWTPayload(token!) |
| 75 | + expect(typeof payload.jti).toBe('string') |
| 76 | + expect((payload.jti as string).length).toBeGreaterThan(0) |
| 77 | + }) |
| 78 | + |
| 79 | + it('JWT payload exp - iat equals JWT_EXPIRY_SECONDS', async () => { |
| 80 | + await createRoom() |
| 81 | + const { token } = parseFragment() |
| 82 | + const payload = decodeJWTPayload(token!) |
| 83 | + expect((payload.exp as number) - (payload.iat as number)).toBe(JWT_EXPIRY_SECONDS) |
| 84 | + }) |
| 85 | + |
| 86 | + it('saves signing keypair to localStorage under peerId', async () => { |
| 87 | + await createRoom() |
| 88 | + const peerId = loadPeerId()! |
| 89 | + const privKey = await loadSigningPrivateKey(peerId) |
| 90 | + const pubKey = await loadSigningPublicKey(peerId) |
| 91 | + expect(privKey).not.toBeNull() |
| 92 | + expect(pubKey).not.toBeNull() |
| 93 | + }) |
| 94 | + |
| 95 | + it('saves OAEP keypair to localStorage under peerId', async () => { |
| 96 | + await createRoom() |
| 97 | + const peerId = loadPeerId()! |
| 98 | + const privKey = await loadOaepPrivateKey(peerId) |
| 99 | + const pubKeyB64 = loadOaepPublicKeyB64(peerId) |
| 100 | + expect(privKey).not.toBeNull() |
| 101 | + expect(pubKeyB64).not.toBeNull() |
| 102 | + }) |
| 103 | + |
| 104 | + it('saves room key to localStorage under roomId from JWT', async () => { |
| 105 | + await createRoom() |
| 106 | + const { token } = parseFragment() |
| 107 | + const payload = decodeJWTPayload(token!) |
| 108 | + const roomId = payload.room as string |
| 109 | + const roomKey = await loadRoomKey(roomId) |
| 110 | + expect(roomKey).not.toBeNull() |
| 111 | + }) |
| 112 | + |
| 113 | + it('does not put any public key in the URL', async () => { |
| 114 | + await createRoom() |
| 115 | + const hash = window.location.hash |
| 116 | + // Public keys are base64url-encoded SPKI blobs — they are long (>200 chars) |
| 117 | + // Verify no param value is a long base64url string that could be a key |
| 118 | + const params = new URLSearchParams(hash.slice(1)) |
| 119 | + for (const [key, value] of params.entries()) { |
| 120 | + if (key !== 'token') { |
| 121 | + // Any non-token param with a long value is suspicious |
| 122 | + expect(value.length).toBeLessThan(200) |
| 123 | + } |
| 124 | + } |
| 125 | + // The token itself is the only thing in the URL — no separate pubkey param |
| 126 | + expect(params.has('signingPub')).toBe(false) |
| 127 | + expect(params.has('oaepPub')).toBe(false) |
| 128 | + expect(params.has('pubkey')).toBe(false) |
| 129 | + }) |
| 130 | + |
| 131 | + it('generates distinct roomId and ownerId on each call', async () => { |
| 132 | + await createRoom() |
| 133 | + const peerId1 = loadPeerId()! |
| 134 | + const hash1 = window.location.hash |
| 135 | + |
| 136 | + localStorage.clear() |
| 137 | + window.location.hash = '' |
| 138 | + |
| 139 | + await createRoom() |
| 140 | + const peerId2 = loadPeerId()! |
| 141 | + const hash2 = window.location.hash |
| 142 | + |
| 143 | + expect(peerId1).not.toBe(peerId2) |
| 144 | + expect(hash1).not.toBe(hash2) |
| 145 | + }) |
| 146 | + |
| 147 | + it('JWT is signed with the stored signing private key', async () => { |
| 148 | + await createRoom() |
| 149 | + const peerId = loadPeerId()! |
| 150 | + const pubKey = await loadSigningPublicKey(peerId) |
| 151 | + expect(pubKey).not.toBeNull() |
| 152 | + |
| 153 | + const { token } = parseFragment() |
| 154 | + const parts = token!.split('.') |
| 155 | + const sigB64 = parts[2].replace(/-/g, '+').replace(/_/g, '/') |
| 156 | + const padded = sigB64.padEnd(sigB64.length + ((4 - (sigB64.length % 4)) % 4), '=') |
| 157 | + const sigBuf = Uint8Array.from(atob(padded), (c) => c.charCodeAt(0)) |
| 158 | + |
| 159 | + const verified = await crypto.subtle.verify( |
| 160 | + { name: 'RSASSA-PKCS1-v1_5' }, |
| 161 | + pubKey!, |
| 162 | + sigBuf, |
| 163 | + new TextEncoder().encode(`${parts[0]}.${parts[1]}`), |
| 164 | + ) |
| 165 | + expect(verified).toBe(true) |
| 166 | + }) |
| 167 | + |
| 168 | + it('saves peerId to localStorage under the PEER_ID key', async () => { |
| 169 | + await createRoom() |
| 170 | + const peerId = localStorage.getItem(STORAGE_KEYS.PEER_ID) |
| 171 | + expect(peerId).not.toBeNull() |
| 172 | + expect(typeof peerId).toBe('string') |
| 173 | + expect(peerId!.length).toBeGreaterThan(0) |
| 174 | + }) |
| 175 | +}) |
0 commit comments