forked from helmetjs/helmet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (48 loc) · 1.52 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var connect = require('connect')
var DEFAULT_MIDDLEWARE = [
'dnsPrefetchControl',
'frameguard',
'hidePoweredBy',
'hsts',
'ieNoOpen',
'noSniff',
'xssFilter'
]
var middlewares
function helmet (options) {
options = options || {}
if (options.constructor.name === 'IncomingMessage') {
throw new Error('It appears you have done something like `app.use(helmet)`, but it should be `app.use(helmet())`.')
}
var chain = connect()
middlewares.forEach(function (middlewareName) {
var middleware = helmet[middlewareName]
var middlewareOptions = options[middlewareName]
var isDefault = DEFAULT_MIDDLEWARE.indexOf(middlewareName) !== -1
if (middlewareOptions === false) {
return
} else if (middlewareOptions === true) {
middlewareOptions = {}
}
if (middlewareOptions != null) {
chain.use(middleware(middlewareOptions))
} else if (isDefault) {
chain.use(middleware({}))
}
})
return chain
}
helmet.contentSecurityPolicy = require('helmet-csp')
helmet.dnsPrefetchControl = require('dns-prefetch-control')
helmet.expectCt = require('expect-ct')
helmet.frameguard = require('frameguard')
helmet.hidePoweredBy = require('hide-powered-by')
helmet.hpkp = require('hpkp')
helmet.hsts = require('hsts')
helmet.ieNoOpen = require('ienoopen')
helmet.noCache = require('nocache')
helmet.noSniff = require('dont-sniff-mimetype')
helmet.referrerPolicy = require('referrer-policy')
helmet.xssFilter = require('x-xss-protection')
middlewares = Object.keys(helmet)
module.exports = helmet