forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsecurity.js
128 lines (112 loc) · 3.2 KB
/
insecurity.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* jslint node: true */
'use strict'
var crypto = require('crypto')
var expressJwt = require('express-jwt')
var jwt = require('jsonwebtoken')
var sanitizeHtml = require('sanitize-html')
var z85 = require('z85')
var utils = require('./utils')
var defaultSecret = 'too.short'
exports.defaultSecret = defaultSecret
exports.hash = function (data) {
return crypto.createHash('md5').update(data).digest('hex')
}
exports.cutOffPoisonNullByte = function (str) {
var nullByte = '%00'
if (utils.contains(str, nullByte)) {
return str.substring(0, str.indexOf(nullByte))
}
return str
}
exports.isAuthorized = function (role) {
return expressJwt({secret: role || defaultSecret})
}
exports.denyAll = function () {
return expressJwt({secret: '' + Math.random()})
}
exports.authorize = function (user, role) {
return jwt.sign(user || {}, role || defaultSecret, { expiresIn: 3600 * 5 })
}
exports.sanitizeHtml = function (html) {
return sanitizeHtml(html)
}
exports.authenticatedUsers = {
tokenMap: {},
idMap: {},
put: function (token, user) {
this.tokenMap[token] = user
this.idMap[user.data.id] = token
},
get: function (token) {
if (token) {
return this.tokenMap[utils.unquote(token)]
} else {
return undefined
}
},
tokenOf: function (user) {
if (user) {
return this.idMap[user.id]
} else {
return undefined
}
},
from: function (req) {
if (req.headers && req.headers.authorization) {
var parts = req.headers.authorization.split(' ')
if (parts.length === 2) {
var scheme = parts[0]
var token = parts[1]
if (/^Bearer$/i.test(scheme)) {
return this.get(token)
}
}
}
return undefined
}
}
exports.userEmailFrom = function (req) {
if (req.headers && req.headers['x-user-email']) {
return req.headers['x-user-email']
}
return undefined
}
exports.generateCoupon = function (date, discount) {
var coupon = utils.toMMMYY(date) + '-' + discount
return z85.encode(coupon)
}
exports.discountFromCoupon = function (coupon) {
if (coupon) {
var decoded = z85.decode(coupon)
if (decoded && hasValidFormat(decoded.toString())) {
var parts = decoded.toString().split('-')
var validity = parts[0]
if (utils.toMMMYY(new Date()) === validity) {
var discount = parts[1]
return parseInt(discount)
}
}
}
return undefined
}
function hasValidFormat (coupon) {
return coupon.match(/(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)[0-9]{2}-[0-9]{2}/)
}
var redirectWhitelist = [
'https://github.com/bkimminich/juice-shop',
'https://blockchain.info/address/1AbKfgvw9psQ41NbLi8kufDQTezwG8DRZm',
'https://gratipay.com/juice-shop',
'http://flattr.com/thing/3856930/bkimminichjuice-shop-on-GitHub',
'http://shop.spreadshirt.com/juiceshop',
'http://shop.spreadshirt.de/juiceshop',
'https://www.stickermule.com/user/1070702817/stickers',
'https://explorer.dash.org/address/Xr556RzuwX6hg5EGpkybbv5RanJoZN17kW'
]
exports.redirectWhitelist = redirectWhitelist
exports.isRedirectAllowed = function (url) {
var allowed = false
redirectWhitelist.forEach(function (allowedUrl) {
allowed = allowed || url.indexOf(allowedUrl) > -1
})
return allowed
}