-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
37 lines (33 loc) · 936 Bytes
/
index.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
import EventEmitter from 'events'
import arrayify from 'array-back'
class Blacklist extends EventEmitter {
description () {
return 'Forbid access to sensitive or private resources.'
}
optionDefinitions () {
return {
name: 'blacklist',
type: String,
multiple: true,
typeLabel: '{underline path} {underline ...}',
description: 'A list of routes to forbid, e.g. `--blacklist "/admin/(.*)" "(.*).php"`'
}
}
middleware (options) {
const blacklist = arrayify(options.blacklist)
if (blacklist.length) {
this.emit('verbose', 'middleware.blacklist.config', { blacklist })
return function (ctx, next) {
const pathIsBlacklisted = blacklist.some(function (re) {
return re.test(ctx.path)
})
if (pathIsBlacklisted) {
ctx.status = 403
} else {
return next()
}
}
}
}
}
export default Blacklist