forked from vercel/next.js
/
security.js
217 lines (190 loc) · 6.67 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
/* 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).not.toBe('google.com')
})
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).not.toBe('google.com')
})
})
}