Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990import { describe, expect, test } from 'bun:test';import { mkdtemp, writeFile } from 'node:fs/promises';import { tmpdir } from 'node:os';import { join } from 'node:path';import { constantTimeEqual, hashCredential, verifyCredential, validateHost, validateOrigin, EnrollmentManager, SecurityStore, SetupTokenManager, WsTicketManager, issueSessionCookie, parseSessionCookie } from '@/security/index.js';
const clock = (value = 0) => ({ now: () => value });
describe('security primitives', () => { test('exact host/origin checks reject rebinding, suffixes, and CR/LF', () => { expect(validateHost('127.0.0.1:8080', '127.0.0.1:8080')).toBe(true); expect(validateHost('localhost:8080', '127.0.0.1:8080')).toBe(false); expect(validateHost('127.0.0.1:8080\r\nX-Evil: yes', '127.0.0.1:8080\r\nX-Evil: yes')).toBe(false); expect(validateOrigin('http://localhost:5173', ['http://localhost:5173'])).toBe(true); expect(validateOrigin('http://localhost:5173.evil', ['http://localhost:5173'])).toBe(false); });
test('credentials are salted and verified in constant time', () => { const hash = hashCredential('secret', (n) => new Uint8Array(n).fill(3)); expect(hash).not.toContain('secret'); expect(verifyCredential('secret', hash)).toBe(true); expect(verifyCredential('wrong', hash)).toBe(false); expect(constantTimeEqual('a', 'a')).toBe(true); });
test('cookie is strict, secure, and HttpOnly', () => { const cookie = issueSessionCookie('abc'); expect(cookie).toContain('HttpOnly'); expect(cookie).toContain('SameSite=Strict'); expect(parseSessionCookie(cookie)).toBe('abc'); });
test('enrollment is single-use, expiry-bound, and minimizes collection output', () => { let now = 0; const manager = new EnrollmentManager({ now: () => now }, (n) => new Uint8Array(n).fill(1)); const item = manager.create(); expect(manager.collect(item.id, item.code, item.claimNonce)).toBeUndefined(); expect(manager.approveCode(item.code)).toEqual({ id: item.id, expiresAt: item.expiresAt }); expect(manager.collect(item.id, item.code, item.claimNonce)).toEqual({ id: item.id, expiresAt: item.expiresAt }); expect(manager.collect(item.id, item.code, item.claimNonce)).toBeUndefined(); const expired = manager.create(); now = expired.expiresAt; expect(manager.collect(expired.id, expired.code, expired.claimNonce)).toBeUndefined(); });
test('enrollment rejects nonce mismatch and rate-limits wrong codes', () => { let now = 0; const manager = new EnrollmentManager({ now: () => now }, (n) => new Uint8Array(n).fill(1), 120_000, 2); const item = manager.create(); expect(manager.collect(item.id, 'wrong', 'bad-nonce')).toBeUndefined(); expect(manager.approveCode(item.code)).toEqual({ id: item.id, expiresAt: item.expiresAt }); expect(manager.collect(item.id, 'wrong', item.claimNonce)).toBeUndefined(); expect(manager.collect(item.id, 'wrong', item.claimNonce)).toBeUndefined(); expect(manager.collect(item.id, item.code, item.claimNonce)).toEqual({ id: item.id, expiresAt: item.expiresAt }); });
test('setup tokens are single-use', () => { const setup = new SetupTokenManager(clock(), (n) => new Uint8Array(n).fill(2)); const token = setup.issue(); expect(setup.consume(token)).toBe(true); expect(setup.consume(token)).toBe(false); });
test('WS tickets reject wrong principal, session, and Origin and remain one-use', () => { const manager = new WsTicketManager(clock(), (n) => new Uint8Array(n).fill(4)); const principal = manager.issue('p', 's', 'o'); expect(manager.consume(principal.value, 'wrong', 's', 'o')).toBe(false); expect(manager.consume(principal.value, 'p', 's', 'o')).toBe(false); const session = manager.issue('p', 's', 'o'); expect(manager.consume(session.value, 'p', 'wrong', 'o')).toBe(false); const origin = manager.issue('p', 's', 'o'); expect(manager.consume(origin.value, 'p', 's', 'wrong')).toBe(false); });
test('WS tickets expire and expired entries are swept', () => { let now = 0; const manager = new WsTicketManager({ now: () => now }, (n) => new Uint8Array(n).fill(4), 10); const ticket = manager.issue('p', 's', 'o'); expect(manager.pendingCount()).toBe(1); now = ticket.expiresAt; expect(manager.pendingCount()).toBe(0); expect(manager.consume(ticket.value, 'p', 's', 'o')).toBe(false); });
test('security store rejects oversized input before parsing', async () => { const home = await mkdtemp(join(tmpdir(), 'shard-security-')); await writeFile(join(home, 'security-v1.json'), ' '.repeat(1_048_577)); await expect(new SecurityStore(home).load()).rejects.toThrow('too large'); });});