-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.ts
144 lines (139 loc) · 4.04 KB
/
user.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (c) 2014-2023 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/
/* jslint node: true */
import config from 'config'
import {
type InferAttributes,
type InferCreationAttributes,
Model,
DataTypes,
type CreationOptional,
type Sequelize
} from 'sequelize'
import challengeUtils = require('../lib/challengeUtils')
import * as utils from '../lib/utils'
const security = require('../lib/insecurity')
const challenges = require('../data/datacache').challenges
class User extends Model<
InferAttributes<User>,
InferCreationAttributes<User>
> {
declare id: CreationOptional<number>
declare username: string | undefined
declare email: CreationOptional<string>
declare password: CreationOptional<string>
declare role: CreationOptional<string>
declare deluxeToken: CreationOptional<string>
declare lastLoginIp: CreationOptional<string>
declare profileImage: CreationOptional<string>
declare totpSecret: CreationOptional<string>
declare isActive: CreationOptional<boolean>
}
const UserModelInit = (sequelize: Sequelize) => { // vuln-code-snippet start weakPasswordChallenge
User.init(
{ // vuln-code-snippet hide-start
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
username: {
type: DataTypes.STRING,
defaultValue: '',
set (username: string) {
if (!utils.disableOnContainerEnv()) {
username = security.sanitizeLegacy(username)
} else {
username = security.sanitizeSecure(username)
}
this.setDataValue('username', username)
}
},
email: {
type: DataTypes.STRING,
unique: true,
set (email: string) {
if (!utils.disableOnContainerEnv()) {
challengeUtils.solveIf(challenges.persistedXssUserChallenge, () => {
return utils.contains(
email,
'<iframe src="javascript:alert(`xss`)">'
)
})
} else {
email = security.sanitizeSecure(email)
}
this.setDataValue('email', email)
}
}, // vuln-code-snippet hide-end
password: {
type: DataTypes.STRING,
set (clearTextPassword) {
this.setDataValue('password', security.hash(clearTextPassword)) // vuln-code-snippet vuln-line weakPasswordChallenge
}
}, // vuln-code-snippet end weakPasswordChallenge
role: {
type: DataTypes.STRING,
defaultValue: 'customer',
validate: {
isIn: [['customer', 'deluxe', 'accounting', 'admin']]
},
set (role: string) {
const profileImage = this.getDataValue('profileImage')
if (
role === security.roles.admin &&
(!profileImage ||
profileImage === '/assets/public/images/uploads/default.svg')
) {
this.setDataValue(
'profileImage',
'/assets/public/images/uploads/defaultAdmin.png'
)
}
this.setDataValue('role', role)
}
},
deluxeToken: {
type: DataTypes.STRING,
defaultValue: ''
},
lastLoginIp: {
type: DataTypes.STRING,
defaultValue: '0.0.0.0'
},
profileImage: {
type: DataTypes.STRING,
defaultValue: '/assets/public/images/uploads/default.svg'
},
totpSecret: {
type: DataTypes.STRING,
defaultValue: ''
},
isActive: {
type: DataTypes.BOOLEAN,
defaultValue: true
}
},
{
tableName: 'Users',
paranoid: true,
sequelize
}
)
User.addHook('afterValidate', async (user: User) => {
if (
user.email &&
user.email.toLowerCase() ===
`acc0unt4nt@${config.get('application.domain')}`.toLowerCase()
) {
await Promise.reject(
new Error(
'Nice try, but this is not how the "Ephemeral Accountant" challenge works!'
)
)
}
})
}
export { User as UserModel, UserModelInit }