import { generateHMAC, verifyHMAC } from './auth'; import crypto from 'crypto'; describe('Slidev HMAC Authentication', () => { const originalEnv = process.env.SLIDEV_HMAC_SECRET; beforeEach(() => { // Set a test secret for all tests process.env.SLIDEV_HMAC_SECRET = 'test-secret-key-for-hmac-testing-purposes'; }); afterEach(() => { // Restore original environment process.env.SLIDEV_HMAC_SECRET = originalEnv; }); describe('generateHMAC', () => { it('should generate consistent HMAC for same inputs', () => { const hmac1 = generateHMAC('a1b2-c3d4-e5f6', 'next'); const hmac2 = generateHMAC('a1b2-c3d4-e5f6', 'next'); expect(hmac1).toBe(hmac2); expect(hmac1).toHaveLength(64); // SHA256 hex = 64 characters }); it('should generate different HMAC for different deckIds', () => { const hmac1 = generateHMAC('a1b2-c3d4-e5f6', 'next'); const hmac2 = generateHMAC('b2c3-d4e5-f6a1', 'next'); expect(hmac1).not.toBe(hmac2); }); it('should generate different HMAC for different commands', () => { const hmacNext = generateHMAC('a1b2-c3d4-e5f6', 'next'); const hmacPrev = generateHMAC('a1b2-c3d4-e5f6', 'prev'); expect(hmacNext).not.toBe(hmacPrev); }); it('should generate different HMAC when slideId is provided', () => { const hmacWithout = generateHMAC('a1b2-c3d4-e5f6', 'goto'); const hmacWith = generateHMAC('a1b2-c3d4-e5f6', 'goto', '5'); expect(hmacWithout).not.toBe(hmacWith); }); it('should handle slideId correctly for goto commands', () => { const hmac = generateHMAC('a1b2-c3d4-e5f6', 'goto', '10'); expect(hmac).toBeDefined(); expect(hmac).toHaveLength(64); }); it('should generate same HMAC when slideId is undefined or empty string', () => { const hmacUndefined = generateHMAC('a1b2-c3d4-e5f6', 'next', undefined); const hmacNoParam = generateHMAC('a1b2-c3d4-e5f6', 'next'); expect(hmacUndefined).toBe(hmacNoParam); }); it('should throw error when HMAC secret is not set', () => { delete process.env.SLIDEV_HMAC_SECRET; expect(() => generateHMAC('a1b2-c3d4-e5f6', 'next')).toThrow( 'SLIDEV_HMAC_SECRET environment variable is not set' ); }); it('should generate hex string (only 0-9 and a-f)', () => { const hmac = generateHMAC('a1b2-c3d4-e5f6', 'next'); expect(hmac).toMatch(/^[0-9a-f]{64}$/); }); }); describe('verifyHMAC', () => { it('should verify valid HMAC for next command', () => { const deckId = 'a1b2-c3d4-e5f6'; const command = 'next'; const hmac = generateHMAC(deckId, command); const isValid = verifyHMAC(deckId, command, undefined, hmac); expect(isValid).toBe(true); }); it('should verify valid HMAC for prev command', () => { const deckId = 'a1b2-c3d4-e5f6'; const command = 'prev'; const hmac = generateHMAC(deckId, command); const isValid = verifyHMAC(deckId, command, undefined, hmac); expect(isValid).toBe(true); }); it('should verify valid HMAC for goto command with slideId', () => { const deckId = 'a1b2-c3d4-e5f6'; const command = 'goto'; const slideId = '5'; const hmac = generateHMAC(deckId, command, slideId); const isValid = verifyHMAC(deckId, command, slideId, hmac); expect(isValid).toBe(true); }); it('should reject invalid HMAC', () => { const deckId = 'a1b2-c3d4-e5f6'; const command = 'next'; const invalidHmac = '0000000000000000000000000000000000000000000000000000000000000000'; const isValid = verifyHMAC(deckId, command, undefined, invalidHmac); expect(isValid).toBe(false); }); it('should reject HMAC with wrong deckId', () => { const hmac = generateHMAC('a1b2-c3d4-e5f6', 'next'); const isValid = verifyHMAC('wrong-deck-id', 'next', undefined, hmac); expect(isValid).toBe(false); }); it('should reject HMAC with wrong command', () => { const hmac = generateHMAC('a1b2-c3d4-e5f6', 'next'); const isValid = verifyHMAC('a1b2-c3d4-e5f6', 'prev', undefined, hmac); expect(isValid).toBe(false); }); it('should reject HMAC with wrong slideId', () => { const hmac = generateHMAC('a1b2-c3d4-e5f6', 'goto', '5'); const isValid = verifyHMAC('a1b2-c3d4-e5f6', 'goto', '10', hmac); expect(isValid).toBe(false); }); it('should reject HMAC with different length', () => { const isValid = verifyHMAC('a1b2-c3d4-e5f6', 'next', undefined, 'short'); expect(isValid).toBe(false); }); it('should reject malformed HMAC (non-hex characters)', () => { const isValid = verifyHMAC( 'a1b2-c3d4-e5f6', 'next', undefined, 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz' ); expect(isValid).toBe(false); }); it('should use timing-safe comparison (crypto.timingSafeEqual)', () => { // Spy on crypto.timingSafeEqual to ensure it's being used const timingSafeEqualSpy = jest.spyOn(crypto, 'timingSafeEqual'); const deckId = 'a1b2-c3d4-e5f6'; const command = 'next'; const hmac = generateHMAC(deckId, command); verifyHMAC(deckId, command, undefined, hmac); expect(timingSafeEqualSpy).toHaveBeenCalled(); timingSafeEqualSpy.mockRestore(); }); it('should prevent timing attacks by using constant-time comparison', () => { // This test verifies that timing-safe comparison is used // We can't directly test timing, but we can verify the function is called const deckId = 'a1b2-c3d4-e5f6'; const command = 'next'; const correctHmac = generateHMAC(deckId, command); const incorrectHmac = '0' + correctHmac.substring(1); // Change first character const spy = jest.spyOn(crypto, 'timingSafeEqual'); verifyHMAC(deckId, command, undefined, incorrectHmac); // Verify timingSafeEqual was called, not simple string comparison expect(spy).toHaveBeenCalledWith( Buffer.from(correctHmac, 'hex'), Buffer.from(incorrectHmac, 'hex') ); spy.mockRestore(); }); it('should handle empty string HMAC gracefully', () => { const isValid = verifyHMAC('a1b2-c3d4-e5f6', 'next', undefined, ''); expect(isValid).toBe(false); }); }); describe('Command type coverage', () => { it('should support next command', () => { const hmac = generateHMAC('a1b2-c3d4-e5f6', 'next'); const isValid = verifyHMAC('a1b2-c3d4-e5f6', 'next', undefined, hmac); expect(isValid).toBe(true); }); it('should support prev command', () => { const hmac = generateHMAC('a1b2-c3d4-e5f6', 'prev'); const isValid = verifyHMAC('a1b2-c3d4-e5f6', 'prev', undefined, hmac); expect(isValid).toBe(true); }); it('should support goto command', () => { const hmac = generateHMAC('a1b2-c3d4-e5f6', 'goto', '3'); const isValid = verifyHMAC('a1b2-c3d4-e5f6', 'goto', '3', hmac); expect(isValid).toBe(true); }); }); });