/
security.js
365 lines (323 loc) · 11.1 KB
/
security.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* eslint-env jest */
import webdriver from 'next-webdriver'
import { readFileSync } from 'fs'
import url from 'url'
import { join, resolve as resolvePath } from 'path'
import {
renderViaHTTP,
getBrowserBodyText,
waitFor,
fetchViaHTTP,
} from 'next-test-utils'
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
import { homedir } from 'os'
// Does the same evaluation checking for INJECTED for 5 seconds after hydration, triggering every 500ms
async function checkInjected(browser) {
const start = Date.now()
while (Date.now() - start < 5000) {
const bodyText = await getBrowserBodyText(browser)
if (/INJECTED/.test(bodyText)) {
throw new Error('Vulnerable to XSS attacks')
}
await waitFor(500)
}
}
module.exports = (context) => {
describe('With Security Related Issues', () => {
it('should only access files inside .next directory', async () => {
const buildId = readFileSync(join(__dirname, '../.next/BUILD_ID'), 'utf8')
const pathsToCheck = [
`/_next/${buildId}/page/../../../info`,
`/_next/${buildId}/page/../../../info.js`,
`/_next/${buildId}/page/../../../info.json`,
`/_next/:buildId/webpack/chunks/../../../info.json`,
`/_next/:buildId/webpack/../../../info.json`,
`/_next/../../../info.json`,
`/static/../../../info.json`,
`/static/../info.json`,
`/../../../info.json`,
`/../../info.json`,
`/../info.json`,
`/info.json`,
]
for (const path of pathsToCheck) {
const data = await renderViaHTTP(context.appPort, path)
expect(data.includes('cool-version')).toBeFalsy()
}
})
it('should not allow accessing files outside .next/static directory', async () => {
const pathsToCheck = [
`/_next/static/../server/pages-manifest.json`,
`/_next/static/../server/build-manifest.json`,
`/_next/static/../BUILD_ID`,
`/_next/static/../routes-manifest.json`,
]
for (const path of pathsToCheck) {
const res = await fetchViaHTTP(context.appPort, path)
const text = await res.text()
try {
expect(res.status).toBe(404)
expect(text).toMatch(/This page could not be found/)
} catch (err) {
throw new Error(`Path ${path} accessible from the browser`)
}
}
})
it("should not leak the user's home directory into the build", async () => {
const buildId = readFileSync(join(__dirname, '../.next/BUILD_ID'), 'utf8')
const readPath = join(__dirname, `../.next/static/${buildId}`)
const buildFiles = await recursiveReadDir(readPath, /\.js$/)
if (buildFiles.length < 1) {
throw new Error('Could not locate any build files')
}
const homeDir = homedir()
buildFiles.forEach((buildFile) => {
const content = readFileSync(join(readPath, buildFile), 'utf8')
if (content.includes(homeDir)) {
throw new Error(
`Found the user's home directory in: ${buildFile}, ${homeDir}\n\n${content}`
)
}
const checkPathProject = resolvePath(__dirname, ...Array(5).fill('..'))
if (
content.includes(checkPathProject) ||
(process.platform.match(/win/) &&
content.includes(checkPathProject.replace(/\\/g, '\\\\')))
) {
throw new Error(
`Found the project path in: ${buildFile}, ${checkPathProject}\n\n${content}`
)
}
})
})
it('should prevent URI based XSS attacks', async () => {
const browser = await webdriver(
context.appPort,
'/\',document.body.innerHTML="INJECTED",\''
)
await checkInjected(browser)
await browser.close()
})
it('should prevent URI based XSS attacks using single quotes', async () => {
const browser = await webdriver(
context.appPort,
`/'-(document.body.innerHTML='INJECTED')-'`
)
await checkInjected(browser)
await browser.close()
})
it('should prevent URI based XSS attacks using double quotes', async () => {
const browser = await webdriver(
context.appPort,
`/"-(document.body.innerHTML='INJECTED')-"`
)
await checkInjected(browser)
await browser.close()
})
it('should prevent URI based XSS attacks using semicolons and double quotes', async () => {
const browser = await webdriver(
context.appPort,
`/;"-(document.body.innerHTML='INJECTED')-"`
)
await checkInjected(browser)
await browser.close()
})
it('should prevent URI based XSS attacks using semicolons and single quotes', async () => {
const browser = await webdriver(
context.appPort,
`/;'-(document.body.innerHTML='INJECTED')-'`
)
await checkInjected(browser)
await browser.close()
})
it('should prevent URI based XSS attacks using src', async () => {
const browser = await webdriver(
context.appPort,
`/javascript:(document.body.innerHTML='INJECTED')`
)
await checkInjected(browser)
await browser.close()
})
it('should prevent URI based XSS attacks using querystring', async () => {
const browser = await webdriver(
context.appPort,
`/?javascript=(document.body.innerHTML='INJECTED')`
)
await checkInjected(browser)
await browser.close()
})
it('should prevent URI based XSS attacks using querystring and quotes', async () => {
const browser = await webdriver(
context.appPort,
`/?javascript="(document.body.innerHTML='INJECTED')"`
)
await checkInjected(browser)
await browser.close()
})
it('should handle encoded value in the pathname correctly \\', async () => {
const res = await fetchViaHTTP(
context.appPort,
'/redirect/me/to-about/' + encodeURI('\\google.com'),
undefined,
{
redirect: 'manual',
}
)
const { pathname, hostname } = url.parse(
res.headers.get('location') || ''
)
expect(res.status).toBe(307)
expect(pathname).toBe(encodeURI('/\\google.com/about'))
expect(hostname).toBe('localhost')
})
it('should handle encoded value in the pathname correctly %', async () => {
const res = await fetchViaHTTP(
context.appPort,
'/redirect/me/to-about/%25google.com',
undefined,
{
redirect: 'manual',
}
)
const { pathname, hostname } = url.parse(
res.headers.get('location') || ''
)
expect(res.status).toBe(307)
expect(pathname).toBe('/%25google.com/about')
expect(hostname).toBe('localhost')
})
it('should handle encoded value in the query correctly', async () => {
const res = await fetchViaHTTP(
context.appPort,
'/trailing-redirect/?url=https%3A%2F%2Fgoogle.com%2Fimage%3Fcrop%3Dfocalpoint%26w%3D24&w=1200&q=100',
undefined,
{
redirect: 'manual',
}
)
const { pathname, hostname, query } = url.parse(
res.headers.get('location') || ''
)
expect(res.status).toBe(308)
expect(pathname).toBe('/trailing-redirect')
expect(hostname).toBe('localhost')
expect(query).toBe(
'url=https%3A%2F%2Fgoogle.com%2Fimage%3Fcrop%3Dfocalpoint%26w%3D24&w=1200&q=100'
)
})
it('should handle encoded value in the pathname correctly /', async () => {
const res = await fetchViaHTTP(
context.appPort,
'/redirect/me/to-about/%2fgoogle.com',
undefined,
{
redirect: 'manual',
}
)
const { pathname, hostname } = url.parse(
res.headers.get('location') || ''
)
expect(res.status).toBe(307)
expect(pathname).toBe('/%2fgoogle.com/about')
expect(hostname).not.toBe('google.com')
})
it('should handle encoded value in the pathname to query correctly (/)', async () => {
const res = await fetchViaHTTP(
context.appPort,
'/redirect-query-test/%2Fgoogle.com',
undefined,
{
redirect: 'manual',
}
)
const { pathname, hostname, query } = url.parse(
res.headers.get('location') || ''
)
expect(res.status).toBe(307)
expect(pathname).toBe('/about')
expect(query).toBe('foo=%2Fgoogle.com')
expect(hostname).not.toBe('google.com')
expect(hostname).not.toMatch(/google/)
})
it('should handle encoded / value for trailing slash correctly', async () => {
const res = await fetchViaHTTP(
context.appPort,
'/%2fexample.com/',
undefined,
{ redirect: 'manual' }
)
const { pathname, hostname } = url.parse(
res.headers.get('location') || ''
)
expect(res.status).toBe(308)
expect(pathname).toBe('/%2fexample.com')
expect(hostname).not.toBe('example.com')
})
it('should handle encoded value in the pathname correctly /', async () => {
const res = await fetchViaHTTP(
context.appPort,
'/redirect/me/to-about/%2fgoogle.com',
undefined,
{
redirect: 'manual',
}
)
const { pathname, hostname } = url.parse(
res.headers.get('location') || ''
)
expect(res.status).toBe(307)
expect(pathname).toBe('/%2fgoogle.com/about')
expect(hostname).not.toBe('google.com')
})
it('should handle encoded value in the pathname to query correctly (/)', async () => {
const res = await fetchViaHTTP(
context.appPort,
'/redirect-query-test/%2Fgoogle.com',
undefined,
{
redirect: 'manual',
}
)
const { pathname, hostname, query } = url.parse(
res.headers.get('location') || ''
)
expect(res.status).toBe(307)
expect(pathname).toBe('/about')
expect(query).toBe('foo=%2Fgoogle.com')
expect(hostname).not.toBe('google.com')
expect(hostname).not.toMatch(/google/)
})
it('should handle encoded / value for trailing slash correctly', async () => {
const res = await fetchViaHTTP(
context.appPort,
'/%2fexample.com/',
undefined,
{ redirect: 'manual' }
)
const { pathname, hostname } = url.parse(
res.headers.get('location') || ''
)
expect(res.status).toBe(308)
expect(pathname).toBe('/%2fexample.com')
expect(hostname).not.toBe('example.com')
})
it('should not execute script embedded inside svg image', async () => {
let browser
try {
browser = await webdriver(context.appPort, '/svg-image')
await browser.eval(`document.getElementById("img").scrollIntoView()`)
expect(await browser.elementById('img').getAttribute('src')).toContain(
'xss.svg'
)
expect(await browser.elementById('msg').text()).toBe('safe')
browser = await webdriver(
context.appPort,
'/_next/image?url=%2Fxss.svg&w=256&q=75'
)
expect(await browser.elementById('msg').text()).toBe('safe')
} finally {
if (browser) await browser.close()
}
})
})
}