|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { ALLOWED_PROTOCOLS, CLERK_BEFORE_UNLOAD_EVENT, windowNavigate } from '../windowNavigate'; |
| 4 | + |
| 5 | +describe('windowNavigate', () => { |
| 6 | + let originalLocation: Location; |
| 7 | + let hrefSetter: ReturnType<typeof vi.fn>; |
| 8 | + let warnSpy: ReturnType<typeof vi.spyOn>; |
| 9 | + let eventSpy: ReturnType<typeof vi.fn>; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + originalLocation = window.location; |
| 13 | + hrefSetter = vi.fn(); |
| 14 | + Object.defineProperty(window, 'location', { |
| 15 | + configurable: true, |
| 16 | + value: new Proxy( |
| 17 | + { href: 'https://example.com/' }, |
| 18 | + { |
| 19 | + set: (target, prop, value) => { |
| 20 | + if (prop === 'href') { |
| 21 | + hrefSetter(value); |
| 22 | + (target as any).href = value; |
| 23 | + return true; |
| 24 | + } |
| 25 | + (target as any)[prop] = value; |
| 26 | + return true; |
| 27 | + }, |
| 28 | + }, |
| 29 | + ), |
| 30 | + }); |
| 31 | + warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 32 | + eventSpy = vi.fn(); |
| 33 | + window.addEventListener(CLERK_BEFORE_UNLOAD_EVENT, eventSpy); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + window.removeEventListener(CLERK_BEFORE_UNLOAD_EVENT, eventSpy); |
| 38 | + Object.defineProperty(window, 'location', { |
| 39 | + configurable: true, |
| 40 | + value: originalLocation, |
| 41 | + }); |
| 42 | + warnSpy.mockRestore(); |
| 43 | + }); |
| 44 | + |
| 45 | + it.each([ |
| 46 | + ['absolute https URL', 'https://example.com/dashboard'], |
| 47 | + ['absolute http URL', 'http://example.com/dashboard'], |
| 48 | + ['relative path', '/sign-in'], |
| 49 | + ['wails protocol', 'wails://app/route'], |
| 50 | + ['chrome-extension protocol', 'chrome-extension://abc/route'], |
| 51 | + ])('navigates to %s', (_label, to) => { |
| 52 | + windowNavigate(to); |
| 53 | + expect(hrefSetter).toHaveBeenCalledTimes(1); |
| 54 | + expect(eventSpy).toHaveBeenCalledTimes(1); |
| 55 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it.each([ |
| 59 | + ['javascript', 'javascript:alert(1)'], |
| 60 | + ['data', 'data:text/html,<script>alert(1)</script>'], |
| 61 | + ['file', 'file:///etc/passwd'], |
| 62 | + ['vbscript', 'vbscript:msgbox(1)'], |
| 63 | + ['mixed-case JavaScript', 'JavaScript:alert(1)'], |
| 64 | + ['upper-case JAVASCRIPT', 'JAVASCRIPT:alert(1)'], |
| 65 | + ['leading-whitespace javascript', ' javascript:alert(1)'], |
| 66 | + ['leading-tab javascript', '\tjavascript:alert(1)'], |
| 67 | + ['leading-newline javascript', '\njavascript:alert(1)'], |
| 68 | + ])('blocks %s: protocol and does not navigate', (_label, to) => { |
| 69 | + windowNavigate(to); |
| 70 | + expect(hrefSetter).not.toHaveBeenCalled(); |
| 71 | + expect(eventSpy).not.toHaveBeenCalled(); |
| 72 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 73 | + }); |
| 74 | + |
| 75 | + it('blocks javascript: URLs that the URL parser normalizes via the base URL', () => { |
| 76 | + windowNavigate('javascript:alert(location.origin)//'); |
| 77 | + expect(hrefSetter).not.toHaveBeenCalled(); |
| 78 | + expect(eventSpy).not.toHaveBeenCalled(); |
| 79 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 80 | + }); |
| 81 | + |
| 82 | + it.each([ |
| 83 | + ['scheme-relative //host', '//evil.example/path'], |
| 84 | + ['scheme-relative ///host', '///evil.example/path'], |
| 85 | + ['backslash \\\\host', '\\\\evil.example\\path'], |
| 86 | + ['mixed /\\host', '/\\evil.example/path'], |
| 87 | + ['mixed \\/host', '\\/evil.example/path'], |
| 88 | + ['leading-whitespace scheme-relative', ' //evil.example/path'], |
| 89 | + ['leading-tab scheme-relative', '\t//evil.example/path'], |
| 90 | + ])('blocks %s and does not navigate', (_label, to) => { |
| 91 | + windowNavigate(to); |
| 92 | + expect(hrefSetter).not.toHaveBeenCalled(); |
| 93 | + expect(eventSpy).not.toHaveBeenCalled(); |
| 94 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 95 | + }); |
| 96 | + |
| 97 | + it('still rejects scheme-relative URLs when an extended allowlist is supplied', () => { |
| 98 | + windowNavigate('//evil.example/path', { |
| 99 | + allowedProtocols: [...ALLOWED_PROTOCOLS, 'slack:'], |
| 100 | + }); |
| 101 | + expect(hrefSetter).not.toHaveBeenCalled(); |
| 102 | + expect(eventSpy).not.toHaveBeenCalled(); |
| 103 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 104 | + }); |
| 105 | + |
| 106 | + it('honors a caller-supplied extended allowlist for custom protocols', () => { |
| 107 | + windowNavigate('slack://channel/123', { |
| 108 | + allowedProtocols: [...ALLOWED_PROTOCOLS, 'slack:'], |
| 109 | + }); |
| 110 | + expect(hrefSetter).toHaveBeenCalledTimes(1); |
| 111 | + expect(hrefSetter).toHaveBeenCalledWith('slack://channel/123'); |
| 112 | + expect(eventSpy).toHaveBeenCalledTimes(1); |
| 113 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('still rejects disallowed protocols when an extended allowlist is supplied', () => { |
| 117 | + windowNavigate('javascript:alert(1)', { |
| 118 | + allowedProtocols: [...ALLOWED_PROTOCOLS, 'slack:'], |
| 119 | + }); |
| 120 | + expect(hrefSetter).not.toHaveBeenCalled(); |
| 121 | + expect(eventSpy).not.toHaveBeenCalled(); |
| 122 | + expect(warnSpy).toHaveBeenCalledTimes(1); |
| 123 | + }); |
| 124 | +}); |
0 commit comments