|
| 1 | +// @vitest-environment node |
| 2 | + |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { sealAuth, unsealAuth, parseCookieValue, AUTH_COOKIE_NAME, type AuthCookiePayload } from './auth-cookie'; |
| 5 | + |
| 6 | +const SECRET = 'test-session-secret-at-least-32-chars-long'; |
| 7 | + |
| 8 | +const VALID_PAYLOAD: AuthCookiePayload = { |
| 9 | + githubToken: 'gho_abc123def456', |
| 10 | + githubUser: { login: 'octocat', name: 'The Octocat' }, |
| 11 | + githubAuthTime: Date.now(), |
| 12 | +}; |
| 13 | + |
| 14 | +describe('auth-cookie', () => { |
| 15 | + describe('sealAuth / unsealAuth', () => { |
| 16 | + it('round-trips a valid payload', () => { |
| 17 | + const sealed = sealAuth(VALID_PAYLOAD, SECRET); |
| 18 | + const result = unsealAuth(sealed, SECRET); |
| 19 | + |
| 20 | + expect(result).toEqual(VALID_PAYLOAD); |
| 21 | + }); |
| 22 | + |
| 23 | + it('produces different ciphertexts for the same payload (random IV)', () => { |
| 24 | + const a = sealAuth(VALID_PAYLOAD, SECRET); |
| 25 | + const b = sealAuth(VALID_PAYLOAD, SECRET); |
| 26 | + |
| 27 | + expect(a).not.toBe(b); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns null for a wrong secret', () => { |
| 31 | + const sealed = sealAuth(VALID_PAYLOAD, SECRET); |
| 32 | + const result = unsealAuth(sealed, 'wrong-secret-that-is-long-enough'); |
| 33 | + |
| 34 | + expect(result).toBeNull(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('returns null for tampered ciphertext', () => { |
| 38 | + const sealed = sealAuth(VALID_PAYLOAD, SECRET); |
| 39 | + // Flip a character in the middle |
| 40 | + const tampered = sealed.slice(0, 10) + (sealed[10] === 'a' ? 'b' : 'a') + sealed.slice(11); |
| 41 | + const result = unsealAuth(tampered, SECRET); |
| 42 | + |
| 43 | + expect(result).toBeNull(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('returns null for truncated data', () => { |
| 47 | + const sealed = sealAuth(VALID_PAYLOAD, SECRET); |
| 48 | + const result = unsealAuth(sealed.slice(0, 10), SECRET); |
| 49 | + |
| 50 | + expect(result).toBeNull(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('returns null for empty string', () => { |
| 54 | + expect(unsealAuth('', SECRET)).toBeNull(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns null for non-base64 garbage', () => { |
| 58 | + expect(unsealAuth('not-valid-base64!!!', SECRET)).toBeNull(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns null when authTime exceeds maxAgeMs', () => { |
| 62 | + const oldPayload: AuthCookiePayload = { |
| 63 | + ...VALID_PAYLOAD, |
| 64 | + githubAuthTime: Date.now() - 8 * 24 * 60 * 60 * 1000, // 8 days ago |
| 65 | + }; |
| 66 | + const sealed = sealAuth(oldPayload, SECRET); |
| 67 | + const result = unsealAuth(sealed, SECRET, 7 * 24 * 60 * 60 * 1000); // 7-day max |
| 68 | + |
| 69 | + expect(result).toBeNull(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('accepts payload when authTime is within maxAgeMs', () => { |
| 73 | + const recentPayload: AuthCookiePayload = { |
| 74 | + ...VALID_PAYLOAD, |
| 75 | + githubAuthTime: Date.now() - 1000, // 1 second ago |
| 76 | + }; |
| 77 | + const sealed = sealAuth(recentPayload, SECRET); |
| 78 | + const result = unsealAuth(sealed, SECRET, 7 * 24 * 60 * 60 * 1000); |
| 79 | + |
| 80 | + expect(result).toEqual(recentPayload); |
| 81 | + }); |
| 82 | + |
| 83 | + it('accepts payload when maxAgeMs is not provided', () => { |
| 84 | + const oldPayload: AuthCookiePayload = { |
| 85 | + ...VALID_PAYLOAD, |
| 86 | + githubAuthTime: Date.now() - 30 * 24 * 60 * 60 * 1000, // 30 days ago |
| 87 | + }; |
| 88 | + const sealed = sealAuth(oldPayload, SECRET); |
| 89 | + const result = unsealAuth(sealed, SECRET); |
| 90 | + |
| 91 | + expect(result).toEqual(oldPayload); |
| 92 | + }); |
| 93 | + |
| 94 | + it('returns null for payload missing githubToken', () => { |
| 95 | + const badPayload = { githubUser: { login: 'octocat', name: 'Octo' }, githubAuthTime: Date.now() }; |
| 96 | + // Manually seal a bad payload by casting |
| 97 | + const sealed = sealAuth(badPayload as AuthCookiePayload, SECRET); |
| 98 | + const result = unsealAuth(sealed, SECRET); |
| 99 | + |
| 100 | + expect(result).toBeNull(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('returns null for payload missing githubUser.login', () => { |
| 104 | + const badPayload = { githubToken: 'tok', githubUser: { name: 'Octo' }, githubAuthTime: Date.now() }; |
| 105 | + const sealed = sealAuth(badPayload as unknown as AuthCookiePayload, SECRET); |
| 106 | + const result = unsealAuth(sealed, SECRET); |
| 107 | + |
| 108 | + expect(result).toBeNull(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('returns null for payload with non-number githubAuthTime', () => { |
| 112 | + const badPayload = { githubToken: 'tok', githubUser: { login: 'x', name: 'X' }, githubAuthTime: 'not-a-number' }; |
| 113 | + const sealed = sealAuth(badPayload as unknown as AuthCookiePayload, SECRET); |
| 114 | + const result = unsealAuth(sealed, SECRET); |
| 115 | + |
| 116 | + expect(result).toBeNull(); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('parseCookieValue', () => { |
| 121 | + it('extracts a cookie value from a header string', () => { |
| 122 | + const header = 'connect.sid=s%3Aabc; __copilot_auth=sealed123; other=val'; |
| 123 | + expect(parseCookieValue(header, '__copilot_auth')).toBe('sealed123'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('returns undefined for a missing cookie', () => { |
| 127 | + const header = 'connect.sid=s%3Aabc; other=val'; |
| 128 | + expect(parseCookieValue(header, '__copilot_auth')).toBeUndefined(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('returns undefined for undefined header', () => { |
| 132 | + expect(parseCookieValue(undefined, '__copilot_auth')).toBeUndefined(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('handles cookie value with equals signs', () => { |
| 136 | + const header = '__copilot_auth=abc=def=ghi'; |
| 137 | + expect(parseCookieValue(header, '__copilot_auth')).toBe('abc=def=ghi'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('handles whitespace around cookie pairs', () => { |
| 141 | + const header = ' __copilot_auth = value123 ; other=x'; |
| 142 | + expect(parseCookieValue(header, '__copilot_auth')).toBeUndefined(); // space before = means the prefix doesn't match |
| 143 | + }); |
| 144 | + |
| 145 | + it('returns first match for duplicate cookie names', () => { |
| 146 | + const header = '__copilot_auth=first; __copilot_auth=second'; |
| 147 | + expect(parseCookieValue(header, '__copilot_auth')).toBe('first'); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + describe('AUTH_COOKIE_NAME', () => { |
| 152 | + it('is the expected value', () => { |
| 153 | + expect(AUTH_COOKIE_NAME).toBe('__copilot_auth'); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments