Skip to content

Commit 9bef33a

Browse files
authored
let front door handle invalid paths (github#27954)
1 parent 3b85d5f commit 9bef33a

File tree

2 files changed

+0
-63
lines changed

2 files changed

+0
-63
lines changed

middleware/handle-invalid-paths.js

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,8 @@
1-
import patterns from '../lib/patterns.js'
21
import statsd from '../lib/statsd.js'
32

43
const STATSD_KEY = 'middleware.handle_invalid_paths'
54

65
export default function handleInvalidPaths(req, res, next) {
7-
// prevent open redirect vulnerability
8-
if (req.path.match(patterns.multipleSlashes)) {
9-
statsd.increment(STATSD_KEY, 1, ['check:multiple-slashes'])
10-
return next(404)
11-
}
12-
13-
// Prevent Express from blowing up with `URIError: Failed to decode param`
14-
// for paths like /%7B%
15-
try {
16-
decodeURIComponent(req.path)
17-
} catch (err) {
18-
if (process.env.NODE_ENV !== 'test') {
19-
console.error('unable to decode path', req.path, err)
20-
}
21-
statsd.increment(STATSD_KEY, 1, ['check:decodeURIComponent'])
22-
return res.sendStatus(400)
23-
}
24-
25-
// Prevent spammy request URLs from getting through by checking how they
26-
// handle being normalized twice in a row
27-
try {
28-
const origin = 'https://docs.github.com'
29-
const normalizedPath = new URL(req.path, origin).pathname
30-
31-
// This may also throw an error with code `ERR_INVALID_URL`
32-
const reNormalizedPath = new URL(normalizedPath, origin).pathname
33-
34-
if (reNormalizedPath !== normalizedPath) {
35-
throw new Error('URI keeps changing')
36-
}
37-
} catch (err) {
38-
if (process.env.NODE_ENV !== 'test') {
39-
console.error('unable to normalize path', req.path, err)
40-
}
41-
42-
statsd.increment(STATSD_KEY, 1, ['check:ERR_INVALID_URL'])
43-
return res.sendStatus(400)
44-
}
45-
46-
// Prevent some script tag injection attacks
47-
if (req.path.match(/<script/i)) {
48-
statsd.increment(STATSD_KEY, 1, ['check:script-tag-injection'])
49-
return res.sendStatus(400)
50-
}
51-
52-
// Prevent some injection attacks targeting Fastly
53-
if (req.path.match(/<esi:include/i)) {
54-
statsd.increment(STATSD_KEY, 1, ['check:esi-injection-attack'])
55-
return res.sendStatus(400)
56-
}
57-
586
// Prevent various malicious injection attacks targeting Next.js
597
if (req.path.match(/^\/_next[^/]/) || req.path === '/_next/data' || req.path === '/_next/data/') {
608
statsd.increment(STATSD_KEY, 1, ['check:nextjs-injection-attack'])

tests/rendering/server.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,6 @@ describe('server', () => {
169169
expect($.res.statusCode).toBe(400)
170170
})
171171

172-
// see issue 12427
173-
test('renders a 404 for leading slashes', async () => {
174-
let $ = await getDOM('//foo.com/enterprise', { allow404: true })
175-
expect($('h1').text()).toBe('Ooops!')
176-
expect($.res.statusCode).toBe(404)
177-
178-
$ = await getDOM('///foo.com/enterprise', { allow404: true })
179-
expect($('h1').text()).toBe('Ooops!')
180-
expect($.res.statusCode).toBe(404)
181-
})
182-
183172
test('renders a 500 page when errors are thrown', async () => {
184173
const $ = await getDOM('/_500', { allow500s: true })
185174
expect($('h1').text()).toBe('Ooops!')

0 commit comments

Comments
 (0)