Skip to content

Commit 3b1b690

Browse files
authored
measure invalid paths handled (github#26734)
1 parent a8926db commit 3b1b690

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

middleware/handle-invalid-paths.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import patterns from '../lib/patterns.js'
2+
import statsd from '../lib/statsd.js'
3+
4+
const STATSD_KEY = 'middleware.handle_invalid_paths'
25

36
export default function handleInvalidPaths(req, res, next) {
47
// prevent open redirect vulnerability
58
if (req.path.match(patterns.multipleSlashes)) {
9+
statsd.increment(STATSD_KEY, 1, ['check:multiple-slashes'])
610
return next(404)
711
}
812

@@ -14,7 +18,7 @@ export default function handleInvalidPaths(req, res, next) {
1418
if (process.env.NODE_ENV !== 'test') {
1519
console.error('unable to decode path', req.path, err)
1620
}
17-
21+
statsd.increment(STATSD_KEY, 1, ['check:decodeURIComponent'])
1822
return res.sendStatus(400)
1923
}
2024

@@ -35,21 +39,25 @@ export default function handleInvalidPaths(req, res, next) {
3539
console.error('unable to normalize path', req.path, err)
3640
}
3741

42+
statsd.increment(STATSD_KEY, 1, ['check:ERR_INVALID_URL'])
3843
return res.sendStatus(400)
3944
}
4045

4146
// Prevent some script tag injection attacks
4247
if (req.path.match(/<script/i)) {
48+
statsd.increment(STATSD_KEY, 1, ['check:script-tag-injection'])
4349
return res.sendStatus(400)
4450
}
4551

4652
// Prevent some injection attacks targeting Fastly
4753
if (req.path.match(/<esi:include/i)) {
54+
statsd.increment(STATSD_KEY, 1, ['check:esi-injection-attack'])
4855
return res.sendStatus(400)
4956
}
5057

5158
// Prevent various malicious injection attacks targeting Next.js
5259
if (req.path.match(/^\/_next[^/]/) || req.path === '/_next/data' || req.path === '/_next/data/') {
60+
statsd.increment(STATSD_KEY, 1, ['check:nextjs-injection-attack'])
5361
return next(404)
5462
}
5563

0 commit comments

Comments
 (0)