|
| 1 | +import { expect, test, describe } from 'vitest'; |
| 2 | +import { UNPERMITTED_SCHEMES, isPermittedRedirectUrl } from './constants'; |
| 3 | + |
| 4 | +describe('UNPERMITTED_SCHEMES', () => { |
| 5 | + // Dangerous schemes that must be blocked |
| 6 | + test.each([ |
| 7 | + 'javascript:', |
| 8 | + 'JavaScript:', |
| 9 | + 'JAVASCRIPT:', |
| 10 | + 'data:', |
| 11 | + 'Data:', |
| 12 | + 'DATA:', |
| 13 | + 'vbscript:', |
| 14 | + 'VBScript:', |
| 15 | + 'VBSCRIPT:', |
| 16 | + ])('blocks %s', (scheme) => { |
| 17 | + expect(UNPERMITTED_SCHEMES.test(scheme)).toBe(true); |
| 18 | + }); |
| 19 | + |
| 20 | + // Full URL strings (used in /oauth/complete page) |
| 21 | + test.each([ |
| 22 | + 'javascript:alert(1)', |
| 23 | + 'data:text/html,<script>alert(1)</script>', |
| 24 | + 'vbscript:MsgBox("xss")', |
| 25 | + ])('blocks full URL string: %s', (url) => { |
| 26 | + expect(UNPERMITTED_SCHEMES.test(url)).toBe(true); |
| 27 | + }); |
| 28 | + |
| 29 | + // Permitted schemes that must not be blocked |
| 30 | + test.each([ |
| 31 | + 'http:', |
| 32 | + 'https:', |
| 33 | + 'vscode:', |
| 34 | + 'cursor:', |
| 35 | + 'claude:', |
| 36 | + 'vscode://callback', |
| 37 | + 'cursor://callback?code=abc', |
| 38 | + 'http://localhost:8080/callback', |
| 39 | + 'https://example.com/callback', |
| 40 | + ])('permits %s', (url) => { |
| 41 | + expect(UNPERMITTED_SCHEMES.test(url)).toBe(false); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +describe('isPermittedRedirectUrl', () => { |
| 46 | + // --- Permitted URLs --- |
| 47 | + |
| 48 | + test('permits https URLs', () => { |
| 49 | + expect(isPermittedRedirectUrl('https://example.com/callback')).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + test('permits http URLs', () => { |
| 53 | + expect(isPermittedRedirectUrl('http://localhost:8080/callback')).toBe(true); |
| 54 | + }); |
| 55 | + |
| 56 | + test('permits /oauth/complete relative paths', () => { |
| 57 | + expect(isPermittedRedirectUrl('/oauth/complete?url=vscode%3A%2F%2Fcallback')).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + test('permits /oauth/complete with encoded custom scheme', () => { |
| 61 | + expect(isPermittedRedirectUrl('/oauth/complete?url=cursor%3A%2F%2Fcallback%3Fcode%3Dabc')).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + test('permits https URL with query params and fragment', () => { |
| 65 | + expect(isPermittedRedirectUrl('https://example.com/callback?code=abc&state=xyz#fragment')).toBe(true); |
| 66 | + }); |
| 67 | + |
| 68 | + // --- Blocked URLs --- |
| 69 | + |
| 70 | + test('blocks javascript: URLs', () => { |
| 71 | + expect(isPermittedRedirectUrl('javascript:alert(1)')).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + test('blocks data: URLs', () => { |
| 75 | + expect(isPermittedRedirectUrl('data:text/html,<script>alert(1)</script>')).toBe(false); |
| 76 | + }); |
| 77 | + |
| 78 | + test('blocks vbscript: URLs', () => { |
| 79 | + expect(isPermittedRedirectUrl('vbscript:MsgBox("xss")')).toBe(false); |
| 80 | + }); |
| 81 | + |
| 82 | + test('blocks javascript: with mixed case', () => { |
| 83 | + expect(isPermittedRedirectUrl('JavaScript:alert(1)')).toBe(false); |
| 84 | + }); |
| 85 | + |
| 86 | + test('blocks custom scheme URLs not wrapped in /oauth/complete', () => { |
| 87 | + expect(isPermittedRedirectUrl('vscode://callback?code=abc')).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + test('blocks malformed URLs', () => { |
| 91 | + expect(isPermittedRedirectUrl('not a url at all')).toBe(false); |
| 92 | + }); |
| 93 | + |
| 94 | + test('blocks empty string', () => { |
| 95 | + expect(isPermittedRedirectUrl('')).toBe(false); |
| 96 | + }); |
| 97 | + |
| 98 | + test('blocks relative paths that do not start with /oauth/complete', () => { |
| 99 | + expect(isPermittedRedirectUrl('/some/other/path')).toBe(false); |
| 100 | + }); |
| 101 | +}); |
0 commit comments