|
1 | | -import patterns from '../lib/patterns.js' |
2 | 1 | import statsd from '../lib/statsd.js' |
3 | 2 |
|
4 | 3 | const STATSD_KEY = 'middleware.handle_invalid_paths' |
5 | 4 |
|
6 | 5 | 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 | | - |
58 | 6 | // Prevent various malicious injection attacks targeting Next.js |
59 | 7 | if (req.path.match(/^\/_next[^/]/) || req.path === '/_next/data' || req.path === '/_next/data/') { |
60 | 8 | statsd.increment(STATSD_KEY, 1, ['check:nextjs-injection-attack']) |
|
0 commit comments