forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserProfile.ts
77 lines (71 loc) · 3.25 KB
/
userProfile.ts
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
/*
* Copyright (c) 2014-2023 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/
import fs = require('fs')
import { Request, Response, NextFunction } from 'express'
import { UserModel } from '../models/user'
import challengeUtils = require('../lib/challengeUtils')
const utils = require('../lib/utils')
const security = require('../lib/insecurity')
const challenges = require('../data/datacache').challenges
const pug = require('pug')
const config = require('config')
const themes = require('../views/themes/themes').themes
const Entities = require('html-entities').AllHtmlEntities
const entities = new Entities()
module.exports = function getUserProfile () {
return (req: Request, res: Response, next: NextFunction) => {
fs.readFile('views/userProfile.pug', function (err, buf) {
if (err != null) throw err
const loggedInUser = security.authenticatedUsers.get(req.cookies.token)
if (loggedInUser) {
UserModel.findByPk(loggedInUser.data.id).then((user: UserModel | null) => {
let template = buf.toString()
let username = user?.username
if (username?.match(/#{(.*)}/) !== null && !utils.disableOnContainerEnv()) {
req.app.locals.abused_ssti_bug = true
const code = username?.substring(2, username.length - 1)
try {
if (!code) {
throw new Error('Username is null')
}
username = eval(code) // eslint-disable-line no-eval
} catch (err) {
username = '\\' + username
}
} else {
username = '\\' + username
}
const theme = themes[config.get('application.theme')]
if (username) {
template = template.replace(/_username_/g, username)
}
template = template.replace(/_emailHash_/g, security.hash(user?.email))
template = template.replace(/_title_/g, entities.encode(config.get('application.name')))
template = template.replace(/_favicon_/g, favicon())
template = template.replace(/_bgColor_/g, theme.bgColor)
template = template.replace(/_textColor_/g, theme.textColor)
template = template.replace(/_navColor_/g, theme.navColor)
template = template.replace(/_primLight_/g, theme.primLight)
template = template.replace(/_primDark_/g, theme.primDark)
template = template.replace(/_logo_/g, utils.extractFilename(config.get('application.logo')))
const fn = pug.compile(template)
const CSP = `img-src 'self' ${user?.profileImage}; script-src 'self' 'unsafe-eval' https://code.getmdl.io http://ajax.googleapis.com`
challengeUtils.solveIf(challenges.usernameXssChallenge, () => { return user?.profileImage.match(/;[ ]*script-src(.)*'unsafe-inline'/g) !== null && utils.contains(username, '<script>alert(`xss`)</script>') })
res.set({
'Content-Security-Policy': CSP
})
res.send(fn(user))
}).catch((error: Error) => {
next(error)
})
} else {
next(new Error('Blocked illegal activity by ' + req.socket.remoteAddress))
}
})
}
function favicon () {
return utils.extractFilename(config.get('application.favicon'))
}
}