|
| 1 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 2 | +import { createGitHubAuthHeaders, isGitHubUrl } from './git-auth-helpers'; |
| 3 | +import { oAuthState } from './state'; |
| 4 | + |
| 5 | +describe('isGitHubUrl', () => { |
| 6 | + describe('direct GitHub URLs', () => { |
| 7 | + it('returns true for github.com URLs', () => { |
| 8 | + expect(isGitHubUrl('https://github.com/user/repo')).toBe(true); |
| 9 | + }); |
| 10 | + |
| 11 | + it('returns true for api.github.com URLs', () => { |
| 12 | + expect(isGitHubUrl('https://api.github.com/repos')).toBe(true); |
| 13 | + }); |
| 14 | + |
| 15 | + it('returns false for non-GitHub URLs', () => { |
| 16 | + expect(isGitHubUrl('https://gitlab.com/user/repo')).toBe(false); |
| 17 | + expect(isGitHubUrl('https://bitbucket.org/user/repo')).toBe(false); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('security: rejects URLs with github.com in wrong places', () => { |
| 22 | + it('returns false when github.com is in the path', () => { |
| 23 | + expect(isGitHubUrl('https://evil.com/github.com/fake')).toBe(false); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns false when github.com is in a query parameter', () => { |
| 27 | + expect(isGitHubUrl('https://evil.com?redirect=github.com')).toBe( |
| 28 | + false |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + it('returns false for look-alike domains', () => { |
| 33 | + expect(isGitHubUrl('https://github.com.evil.com')).toBe(false); |
| 34 | + expect(isGitHubUrl('https://mygithub.com')).toBe(false); |
| 35 | + expect(isGitHubUrl('https://fakegithub.com')).toBe(false); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('CORS proxy URLs', () => { |
| 40 | + it('returns true for GitHub URLs through CORS proxy', () => { |
| 41 | + expect( |
| 42 | + isGitHubUrl( |
| 43 | + 'https://playground.wordpress.net/cors-proxy.php?https://github.com/user/repo' |
| 44 | + ) |
| 45 | + ).toBe(true); |
| 46 | + expect( |
| 47 | + isGitHubUrl( |
| 48 | + 'http://127.0.0.1:5263/cors-proxy.php?https://github.com/user/repo' |
| 49 | + ) |
| 50 | + ).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it('returns false for non-GitHub URLs through CORS proxy', () => { |
| 54 | + expect( |
| 55 | + isGitHubUrl( |
| 56 | + 'https://playground.wordpress.net/cors-proxy.php?https://gitlab.com/user/repo' |
| 57 | + ) |
| 58 | + ).toBe(false); |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns false for malicious URLs through CORS proxy', () => { |
| 62 | + expect( |
| 63 | + isGitHubUrl( |
| 64 | + 'https://playground.wordpress.net/cors-proxy.php?https://evil.com/github.com/fake' |
| 65 | + ) |
| 66 | + ).toBe(false); |
| 67 | + expect( |
| 68 | + isGitHubUrl( |
| 69 | + 'http://127.0.0.1:5263/cors-proxy.php?https://github.com.evil.com' |
| 70 | + ) |
| 71 | + ).toBe(false); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('edge cases', () => { |
| 76 | + it('returns false for invalid URLs', () => { |
| 77 | + expect(isGitHubUrl('not-a-url')).toBe(false); |
| 78 | + expect(isGitHubUrl('')).toBe(false); |
| 79 | + expect(isGitHubUrl('github.com')).toBe(false); |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe('createGitHubAuthHeaders integration', () => { |
| 85 | + beforeEach(() => { |
| 86 | + oAuthState.value = { token: '', isAuthorizing: false }; |
| 87 | + }); |
| 88 | + |
| 89 | + describe('with GitHub token present', () => { |
| 90 | + beforeEach(() => { |
| 91 | + oAuthState.value = { |
| 92 | + token: 'gho_TestToken123', |
| 93 | + isAuthorizing: false, |
| 94 | + }; |
| 95 | + }); |
| 96 | + |
| 97 | + it('includes Authorization header for github.com URLs', () => { |
| 98 | + const getHeaders = createGitHubAuthHeaders(); |
| 99 | + const headers = getHeaders('https://github.com/user/repo'); |
| 100 | + |
| 101 | + expect(headers).toHaveProperty('Authorization'); |
| 102 | + expect(headers.Authorization).toMatch(/^Basic /); |
| 103 | + expect(headers).toHaveProperty( |
| 104 | + 'X-Cors-Proxy-Allowed-Request-Headers', |
| 105 | + 'Authorization' |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it('includes Authorization header for api.github.com URLs', () => { |
| 110 | + const getHeaders = createGitHubAuthHeaders(); |
| 111 | + const headers = getHeaders('https://api.github.com/repos'); |
| 112 | + |
| 113 | + expect(headers).toHaveProperty('Authorization'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('includes Authorization header for GitHub URLs through CORS proxy', () => { |
| 117 | + const getHeaders = createGitHubAuthHeaders(); |
| 118 | + const headers = getHeaders( |
| 119 | + 'https://playground.wordpress.net/cors-proxy.php?https://github.com/user/repo' |
| 120 | + ); |
| 121 | + |
| 122 | + expect(headers).toHaveProperty('Authorization'); |
| 123 | + expect(headers).toHaveProperty( |
| 124 | + 'X-Cors-Proxy-Allowed-Request-Headers' |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + it('does NOT include Authorization header for non-GitHub URLs', () => { |
| 129 | + const getHeaders = createGitHubAuthHeaders(); |
| 130 | + |
| 131 | + expect(getHeaders('https://gitlab.com/user/repo')).toEqual({}); |
| 132 | + expect(getHeaders('https://bitbucket.org/user/repo')).toEqual({}); |
| 133 | + expect(getHeaders('https://evil.com/github.com/fake')).toEqual({}); |
| 134 | + }); |
| 135 | + |
| 136 | + it('does NOT include Authorization header for non-GitHub URLs through CORS proxy', () => { |
| 137 | + const getHeaders = createGitHubAuthHeaders(); |
| 138 | + const headers = getHeaders( |
| 139 | + 'https://playground.wordpress.net/cors-proxy.php?https://gitlab.com/user/repo' |
| 140 | + ); |
| 141 | + |
| 142 | + expect(headers).toEqual({}); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe('without GitHub token', () => { |
| 147 | + beforeEach(() => { |
| 148 | + oAuthState.value = { token: '', isAuthorizing: false }; |
| 149 | + }); |
| 150 | + |
| 151 | + it('returns empty headers even for GitHub URLs', () => { |
| 152 | + const getHeaders = createGitHubAuthHeaders(); |
| 153 | + |
| 154 | + expect(getHeaders('https://github.com/user/repo')).toEqual({}); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe('token encoding', () => { |
| 159 | + it('encodes token correctly as Basic auth', () => { |
| 160 | + oAuthState.value = { token: 'test-token', isAuthorizing: false }; |
| 161 | + const getHeaders = createGitHubAuthHeaders(); |
| 162 | + const headers = getHeaders('https://github.com/user/repo'); |
| 163 | + |
| 164 | + const decoded = atob(headers.Authorization.replace('Basic ', '')); |
| 165 | + expect(decoded).toBe('test-token:'); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments