index.js
1.09 KB
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
var isString = require('lodash.isstring')
function isObject (value) {
return !!value && typeof value === 'object'
}
module.exports = function frameguard (action, domain) {
var directive
// This converts String objects.
if (isString(action)) {
action = action.valueOf()
}
if (isObject(action)) {
domain = action.domain
action = action.action
}
if (action === undefined) {
directive = 'SAMEORIGIN'
} else if (isString(action)) {
directive = action.toUpperCase()
}
if (directive === 'ALLOWFROM') {
directive = 'ALLOW-FROM'
} else if (directive === 'SAME-ORIGIN') {
directive = 'SAMEORIGIN'
}
if (['DENY', 'ALLOW-FROM', 'SAMEORIGIN'].indexOf(directive) === -1) {
throw new Error('X-Frame must be undefined, "DENY", "ALLOW-FROM", or "SAMEORIGIN"')
}
if (directive === 'ALLOW-FROM') {
if (!isString(domain)) {
throw new Error('X-Frame: ALLOW-FROM requires a second parameter')
}
directive = 'ALLOW-FROM ' + domain
}
return function frameguard (req, res, next) {
res.setHeader('X-Frame-Options', directive)
next()
}
}