Open History to see the last commit
1import { describe, expect, test } from 'vitest'
2import { parseHref } from '../src'
3β
4const baseUrl = new URL('https://victim.example/base')
5β
6describe('parseHref', () => {
7 test.each(['\x00', '\x01', '\x1f', '\x7f', '\t', '\n', '\r'])(
8 'preserves browser path, query, and fragment interpretation for %j',
9 (character) => {
10 const href = `/a${character}b?q=a${character}b#a${character}b`
11 expect(new URL(parseHref(href, undefined).href, baseUrl).href).toBe(
12 new URL(href, baseUrl).href,
13 )
14 },
15 )
16β
17 test('works', () => {
18 const parsed = parseHref('/foo?bar=baz#qux', {} as any)
19 expect(parsed.pathname).toEqual('/foo')
20 expect(parsed.search).toEqual('?bar=baz')
21 expect(parsed.hash).toEqual('#qux')
22 })
23β
24 test.each([
25 ['?x=1', { pathname: '', search: '?x=1', hash: '' }],
26 ['#top', { pathname: '', search: '', hash: '#top' }],
27 ['?x=1#top', { pathname: '', search: '?x=1', hash: '#top' }],
28 ['#top?x=1', { pathname: '', search: '', hash: '#top?x=1' }],
29 ])(
30 'keeps a leading query or fragment out of the pathname: %j',
31 (href, expected) => {
32 expect(parseHref(href, undefined)).toMatchObject({ href, ...expected })
33 },
34 )
35β
36 test.each([
37 { __TSR_index: 2 },
38 { __TSR_index: 2, key: 'legacy-key', __TSR_key: 'current-key' },
39 ])('preserves supplied state %j', (state) => {
40 Object.freeze(state)
41 expect(parseHref('/foo', state).state).toBe(state)
42 })
43β
44 test('creates independent initial states with matching history keys', () => {
45 const first = parseHref('/foo', undefined)
46 const second = parseHref('/bar', undefined)
47β
48 for (const location of [first, second]) {
49 expect(location.state).toEqual({
50 __TSR_index: 0,
51 key: expect.any(String),
52 __TSR_key: location.state.key,
53 })
54 }
55β
56 first.state.__TSR_index = 1
57 expect(second.state.__TSR_index).toBe(0)
58 })
59β
60 describe('open redirect prevention', () => {
61 test('strips CR characters to prevent open redirect', () => {
62 // If \r (CR) is in the href, it should be stripped
63 const parsed = parseHref('/\r/google.com/', undefined)
64 expect(parsed.href).toBe('/google.com/')
65 expect(parsed.pathname).toBe('/google.com/')
66 expect(parsed.href).not.toMatch(/^\/\//)
67 })
68β
69 test('strips LF characters to prevent open redirect', () => {
70 const parsed = parseHref('/\n/evil.com/', undefined)
71 expect(parsed.href).toBe('/evil.com/')
72 expect(parsed.pathname).toBe('/evil.com/')
73 expect(parsed.href).not.toMatch(/^\/\//)
74 })
75β
76 test('strips CRLF characters to prevent open redirect', () => {
77 const parsed = parseHref('/\r\n/evil.com/', undefined)
78 expect(parsed.href).toBe('/evil.com/')
79 expect(parsed.pathname).toBe('/evil.com/')
80 expect(parsed.href).not.toMatch(/^\/\//)
81 })
82β
83 test('collapses leading double slashes to prevent protocol-relative URLs', () => {
84 const parsed = parseHref('//evil.com/path', undefined)
85 expect(parsed.href).toBe('/evil.com/path')
86 expect(parsed.pathname).toBe('/evil.com/path')
87 })
88β
89 test('sanitized href resolves safely to same origin', () => {
90 const parsed = parseHref('/\r/evil.com/', undefined)
91 const url = new URL(parsed.href, 'http://localhost:3000')
92 expect(url.origin).toBe('http://localhost:3000')
93 expect(url.pathname).toBe('/evil.com/')
94 })
95β
96 test.each([
97 '//evil.com/path',
98 '///evil.com/path',
99 '/\\evil.com/path',
100 '/\\\\evil.com/path',
101 '/\\/evil.com/path',
102 '\\/evil.com/path',
103 '\\\\evil.com/path',
104 ' /\\evil.com/path',
105 '\x01//evil.com/path',
106 '/\t/evil.com/path',
107 ])('keeps protocol-relative input %j on the current origin', (href) => {
108 const parsed = parseHref(href, undefined)
109 const url = new URL(parsed.href, 'https://victim.example')
110β
111 expect(url.origin).toBe('https://victim.example')
112 })
113β
114 test.each([
115 'h\x00ttps://evil.example',
116 'java\x00script:alert(1)',
117 '/\x00/evil.example',
118 '\x7f//evil.example',
119 '/%5c/evil.example',
120 '/%2f/evil.example',
121 '?next=//evil.example',
122 '#//evil.example',
123 'οΌοΌevil.example',
124 '',
125 ' ',
126 '\x00',
127 '\x00\t\n',
128 ])('does not make same-origin input %j cross-origin', (href) => {
129 expect(new URL(href, baseUrl).origin).toBe(baseUrl.origin)
130β
131 const parsed = parseHref(href, undefined)
132β
133 expect(new URL(parsed.href, baseUrl).origin).toBe(baseUrl.origin)
134 })
135β
136 test('matches WHATWG parsing of protocol-relative prefixes', () => {
137 const alphabet = [
138 '/',
139 '\\',
140 '\t',
141 '\n',
142 '\r',
143 '\x00',
144 '\x0b',
145 ' ',
146 '\x7f',
147 ]
148β
149 for (let length = 0; length <= 4; length++) {
150 const count = alphabet.length ** length
151 for (let value = 0; value < count; value++) {
152 let input = ''
153 let cursor = value
154 for (let index = 0; index < length; index++) {
155 input += alphabet[cursor % alphabet.length]
156 cursor = Math.floor(cursor / alphabet.length)
157 }
158 input += 'evil.example/path'
159β
160 try {
161 new URL(input, baseUrl)
162 } catch {
163 continue
164 }
165β
166 const parsed = parseHref(input, undefined)
167β
168 expect(
169 new URL(parsed.href, baseUrl).origin,
170 JSON.stringify(input),
171 ).toBe(baseUrl.origin)
172 }
173 }
174 })
175β
176 test('normal paths remain unchanged', () => {
177 const parsed = parseHref('/users/profile?id=1#section', undefined)
178 expect(parsed.href).toBe('/users/profile?id=1#section')
179 expect(parsed.pathname).toBe('/users/profile')
180 expect(parsed.search).toBe('?id=1')
181 expect(parsed.hash).toBe('#section')
182 })
183 })
184})