Open History to see the last commit
1import { promises as fsp } from 'node:fs'
2import { describe, expect, it, vi } from 'vitest'
3import { prerender } from '../src/prerender'
4β
5vi.mock('../src/utils', async () => {
6 const actual = await vi.importActual<any>('../src/utils')
7 return {
8 ...actual,
9 createLogger: () => ({ info: () => {}, warn: () => {}, error: () => {} }),
10 }
11})
12β
13vi.mock('node:fs', async () => {
14 const actual = await vi.importActual<any>('node:fs')
15 return {
16 ...actual,
17 promises: {
18 ...actual.promises,
19 mkdir: vi.fn().mockResolvedValue(undefined),
20 writeFile: vi.fn().mockResolvedValue(undefined),
21 },
22 }
23})
24β
25function makeStartConfig(pagePath: string) {
26 return {
27 prerender: {
28 enabled: true,
29 autoStaticPathsDiscovery: false,
30 concurrency: 1,
31 },
32 pages: [{ path: pagePath }],
33 router: { basepath: '' },
34 spa: {
35 enabled: false,
36 prerender: {
37 outputPath: '/_shell',
38 crawlLinks: false,
39 retryCount: 0,
40 enabled: true,
41 },
42 },
43 } as any
44}
45β
46function htmlResponse() {
47 return new Response('<html></html>', {
48 status: 200,
49 headers: { 'content-type': 'text/html' },
50 })
51}
52β
53describe('prerender public sinks', () => {
54 it('allows relative paths', async () => {
55 const request = vi.fn(async () => htmlResponse())
56 await expect(
57 prerender({
58 startConfig: makeStartConfig('/about'),
59 handler: { getClientOutputDirectory: () => '/client', request },
60 }),
61 ).resolves.not.toThrow()
62 })
63β
64 it.each([
65 'https://attacker.test/leak',
66 '//attacker.test/leak',
67 '/%2fattacker.test/leak',
68 '/%5cattacker.test/leak',
69 ])('rejects page target %j before requesting it', async (pagePath) => {
70 const request = vi.fn(async () => htmlResponse())
71β
72 await expect(
73 prerender({
74 startConfig: makeStartConfig(pagePath),
75 handler: { getClientOutputDirectory: () => '/client', request },
76 }),
77 ).rejects.toThrow(/prerender page path must be relative/i)
78 expect(request).not.toHaveBeenCalled()
79 })
80β
81 it('requests relative pages without automatic redirect following', async () => {
82 const request = vi.fn(async () => htmlResponse())
83β
84 await prerender({
85 startConfig: makeStartConfig('/about'),
86 handler: { getClientOutputDirectory: () => '/client', request },
87 })
88β
89 expect(request).toHaveBeenCalledWith(
90 '/about/',
91 expect.objectContaining({ redirect: 'manual' }),
92 )
93 })
94β
95 it.each([
96 { outputPath: '../escape', expected: undefined },
97 { outputPath: '../client-leak', expected: undefined },
98 { outputPath: 'nested', expected: '/client/nested/index.html' },
99 ])(
100 'keeps output $outputPath inside the client directory',
101 async ({ outputPath, expected }) => {
102 vi.mocked(fsp.writeFile).mockClear()
103 vi.mocked(fsp.mkdir).mockClear()
104 const startConfig = makeStartConfig('/about')
105 startConfig.prerender.failOnError = false
106 startConfig.pages[0].prerender = { outputPath }
107 const request = vi.fn(async () => htmlResponse())
108β
109 await prerender({
110 startConfig,
111 handler: {
112 getClientOutputDirectory: () => '/client',
113 request,
114 },
115 })
116β
117 expect(request).toHaveBeenCalledOnce()
118 if (expected) {
119 expect(fsp.writeFile).toHaveBeenCalledExactlyOnceWith(
120 expected,
121 '<html></html>',
122 )
123 } else {
124 expect(fsp.writeFile).not.toHaveBeenCalled()
125 expect(fsp.mkdir).not.toHaveBeenCalled()
126 }
127 },
128 )
129β
130 it.each([
131 'https://attacker.test/leak',
132 '//attacker.test/leak',
133 '/\\attacker.test/leak',
134 ])('does not request raw redirect target %j', async (location) => {
135 const request = vi.fn(
136 async () =>
137 new Response(null, {
138 status: 307,
139 headers: { location },
140 }),
141 )
142 const startConfig = makeStartConfig('/about')
143 startConfig.prerender.failOnError = false
144β
145 await prerender({
146 startConfig,
147 handler: { getClientOutputDirectory: () => '/client', request },
148 })
149β
150 expect(request).toHaveBeenCalledTimes(1)
151 })
152β
153 it('keeps a canonical double-slash redirect on the preview origin', async () => {
154 const requestedUrls: Array<string> = []
155 const request = vi.fn(async (requestPath: string) => {
156 requestedUrls.push(new URL(requestPath, 'http://localhost').href)
157 if (requestedUrls.length === 1) {
158 return new Response(null, {
159 status: 307,
160 headers: {
161 location: 'http://localhost/a/..//attacker.test/leak',
162 },
163 })
164 }
165 return htmlResponse()
166 })
167β
168 await prerender({
169 startConfig: makeStartConfig('/about'),
170 handler: { getClientOutputDirectory: () => '/client', request },
171 })
172β
173 expect(requestedUrls).toEqual([
174 'http://localhost/about/',
175 'http://localhost//attacker.test/leak',
176 ])
177 })
178β
179 it.each(['/outside', '/application', '/app-private', '/app/../outside'])(
180 'does not request a redirect outside the router basepath: %s',
181 async (location) => {
182 const request = vi.fn(
183 async () =>
184 new Response(null, {
185 status: 307,
186 headers: { location },
187 }),
188 )
189 const startConfig = makeStartConfig('/page')
190 startConfig.router.basepath = '/app'
191 startConfig.prerender.failOnError = false
192β
193 await prerender({
194 startConfig,
195 handler: { getClientOutputDirectory: () => '/client', request },
196 })
197β
198 expect(request).toHaveBeenCalledOnce()
199 expect(request).toHaveBeenCalledWith(
200 '/app/page/',
201 expect.objectContaining({ redirect: 'manual' }),
202 )
203 },
204 )
205β
206 it('follows a raw redirect on the preview origin and basepath', async () => {
207 const request = vi.fn(async (requestPath: string) => {
208 if (requestPath === '/app/page/') {
209 return new Response(null, {
210 status: 307,
211 headers: { location: 'http://127.0.0.1:4173/app/next/' },
212 })
213 }
214 return htmlResponse()
215 })
216 const startConfig = makeStartConfig('/page')
217 startConfig.router.basepath = '/app'
218β
219 await prerender({
220 startConfig,
221 handler: {
222 getClientOutputDirectory: () => '/client',
223 getOrigin: () => 'http://127.0.0.1:4173',
224 request,
225 },
226 })
227β
228 expect(request.mock.calls.map(([requestPath]) => requestPath)).toEqual([
229 '/app/page/',
230 '/app/next/',
231 ])
232 })
233β
234 it('skips requests when the public prerender filter excludes every page', async () => {
235 const request = vi.fn(async () => htmlResponse())
236 const startConfig = makeStartConfig('/about')
237 startConfig.prerender.filter = () => false
238β
239 await prerender({
240 startConfig,
241 handler: { getClientOutputDirectory: () => '/client', request },
242 })
243β
244 expect(request).not.toHaveBeenCalled()
245 })
246})