-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathowasp-juice-shop.ts
271 lines (245 loc) · 8.75 KB
/
owasp-juice-shop.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// https://github.com/juice-shop/juice-shop/blob/master/test/api/userApiSpec.ts
/*
* Copyright (c) 2014-2021 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/
import frisby = require('frisby')
const Joi = frisby.Joi
const utils = require('../../lib/utils')
const security = require('../../lib/insecurity')
const API_URL = 'http://localhost:3000/api'
const REST_URL = 'http://localhost:3000/rest'
const authHeader = { Authorization: `Bearer ${security.authorize()}`, 'content-type': 'application/json' }
const jsonHeader = { 'content-type': 'application/json' }
describe('/api/Users', () => {
it('GET all users is forbidden via public API', () => {
return frisby.get(`${API_URL}/Users`)
.expect('status', 401)
})
it('GET all users', () => {
return frisby.get(`${API_URL}/Users`, { headers: authHeader })
.expect('status', 200)
})
it('GET all users doesnt include passwords', () => {
return frisby.get(`${API_URL}/Users`, { headers: authHeader })
.expect('status', 200)
.expect('jsonTypes', 'data.*', {
password: Joi.any().forbidden()
})
})
it('POST new user', () => {
return frisby.post(`${API_URL}/Users`, {
headers: jsonHeader,
body: {
email: '[email protected]',
password: 'hooooorst'
}
})
.expect('status', 201)
.expect('header', 'content-type', /application\/json/)
.expect('jsonTypes', 'data', {
id: Joi.number(),
createdAt: Joi.string(),
updatedAt: Joi.string(),
password: Joi.any().forbidden()
})
})
it('POST new admin', () => {
return frisby.post(`${API_URL}/Users`, {
headers: jsonHeader,
body: {
email: '[email protected]',
password: 'hooooorst',
role: 'admin'
}
})
.expect('status', 201)
.expect('header', 'content-type', /application\/json/)
.expect('jsonTypes', 'data', {
id: Joi.number(),
createdAt: Joi.string(),
updatedAt: Joi.string(),
password: Joi.any().forbidden()
})
.expect('json', 'data', {
role: 'admin'
})
})
it('POST new deluxe user', () => {
return frisby.post(`${API_URL}/Users`, {
headers: jsonHeader,
body: {
email: '[email protected]',
password: 'hooooorst',
role: 'deluxe'
}
})
.expect('status', 201)
.expect('header', 'content-type', /application\/json/)
.expect('jsonTypes', 'data', {
id: Joi.number(),
createdAt: Joi.string(),
updatedAt: Joi.string(),
password: Joi.any().forbidden()
})
.expect('json', 'data', {
role: 'deluxe'
})
})
it('POST new accounting user', () => {
return frisby.post(`${API_URL}/Users`, {
headers: jsonHeader,
body: {
email: '[email protected]',
password: 'hooooorst',
role: 'accounting'
}
})
.expect('status', 201)
.expect('header', 'content-type', /application\/json/)
.expect('jsonTypes', 'data', {
id: Joi.number(),
createdAt: Joi.string(),
updatedAt: Joi.string(),
password: Joi.any().forbidden()
})
.expect('json', 'data', {
role: 'accounting'
})
})
it('POST user not belonging to customer, deluxe, accounting, admin is forbidden', () => {
return frisby.post(`${API_URL}/Users`, {
headers: jsonHeader,
body: {
email: '[email protected]',
password: 'hooooorst',
role: 'accountinguser'
}
})
.expect('status', 400)
.expect('header', 'content-type', /application\/json/)
.then(({ json }) => {
expect(json.message).toBe('Validation error: Validation isIn on role failed')
expect(json.errors[0].field).toBe('role')
expect(json.errors[0].message).toBe('Validation isIn on role failed')
})
})
if (!utils.disableOnContainerEnv()) {
it('POST new user with XSS attack in email address', () => {
return frisby.post(`${API_URL}/Users`, {
headers: jsonHeader,
body: {
email: '<iframe src="javascript:alert(`xss`)">',
password: 'does.not.matter'
}
})
.expect('status', 201)
.expect('header', 'content-type', /application\/json/)
.expect('json', 'data', { email: '<iframe src="javascript:alert(`xss`)">' })
})
}
})
describe('/api/Users/:id', () => {
it('GET existing user by id is forbidden via public API', () => {
return frisby.get(`${API_URL}/Users/1`)
.expect('status', 401)
})
it('PUT update existing user is forbidden via public API', () => {
return frisby.put(`${API_URL}/Users/1`, {
header: jsonHeader,
body: { email: '[email protected]' }
})
.expect('status', 401)
})
it('DELETE existing user is forbidden via public API', () => {
return frisby.del(`${API_URL}/Users/1`)
.expect('status', 401)
})
it('GET existing user by id', () => {
return frisby.get(`${API_URL}/Users/1`, { headers: authHeader })
.expect('status', 200)
})
it('PUT update existing user is forbidden via API even when authenticated', () => {
return frisby.put(`${API_URL}/Users/1`, {
headers: authHeader,
body: { email: '[email protected]' }
})
.expect('status', 401)
})
it('DELETE existing user is forbidden via API even when authenticated', () => {
return frisby.del(`${API_URL}/Users/1`, { headers: authHeader })
.expect('status', 401)
})
})
describe('/rest/user/authentication-details', () => {
it('GET all users decorated with attribute for authentication token', () => {
return frisby.get(`${REST_URL}/user/authentication-details`, { headers: authHeader })
.expect('status', 200)
.expect('jsonTypes', 'data.?', {
token: Joi.string()
})
})
it('GET all users with password replaced by asterisks', () => {
return frisby.get(`${REST_URL}/user/authentication-details`, { headers: authHeader })
.expect('status', 200)
.expect('json', 'data.?', {
password: '********************************'
})
})
})
describe('/rest/user/whoami', () => {
it('GET own user id and email on who-am-i request', () => {
return frisby.post(`${REST_URL}/user/login`, {
headers: jsonHeader,
body: {
email: '[email protected]',
password: 'bW9jLmxpYW1nQGhjaW5pbW1pay5ucmVvamI='
}
})
.expect('status', 200)
.then(({ json }) => {
return frisby.get(`${REST_URL}/user/whoami`, { headers: { Cookie: `token=${json.authentication.token}` } })
.expect('status', 200)
.expect('header', 'content-type', /application\/json/)
.expect('jsonTypes', 'user', {
id: Joi.number(),
email: Joi.string()
})
.expect('json', 'user', {
email: '[email protected]'
})
})
})
it('GET who-am-i request returns nothing on missing auth token', () => {
return frisby.get(`${REST_URL}/user/whoami`)
.expect('status', 200)
.expect('header', 'content-type', /application\/json/)
.expect('json', {
user: {}
})
})
it('GET who-am-i request returns nothing on invalid auth token', () => {
return frisby.get(`${REST_URL}/user/whoami`, { headers: { Authorization: 'Bearer InvalidAuthToken' } })
.expect('status', 200)
.expect('header', 'content-type', /application\/json/)
.expect('json', {
user: {}
})
})
it('GET who-am-i request returns nothing on broken auth token', () => {
return frisby.get(`${REST_URL}/user/whoami`, { headers: { Authorization: 'BoarBeatsBear' } })
.expect('status', 200)
.expect('header', 'content-type', /application\/json/)
.expect('json', {
user: {}
})
})
it('GET who-am-i request returns nothing on expired auth token', () => {
return frisby.get(`${REST_URL}/user/whoami`, { headers: { Authorization: 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdGF0dXMiOiJzdWNjZXNzIiwiZGF0YSI6eyJpZCI6MSwidXNlcm5hbWUiOiIiLCJlbWFpbCI6ImFkbWluQGp1aWNlLXNoLm9wIiwicGFzc3dvcmQiOiIwMTkyMDIzYTdiYmQ3MzI1MDUxNmYwNjlkZjE4YjUwMCIsInJvbGUiOiJhZG1pbiIsImxhc3RMb2dpbklwIjoiMC4wLjAuMCIsInByb2ZpbGVJbWFnZSI6ImRlZmF1bHQuc3ZnIiwidG90cFNlY3JldCI6IiIsImlzQWN0aXZlIjp0cnVlLCJjcmVhdGVkQXQiOiIyMDE5LTA4LTE5IDE1OjU2OjE1LjYyOSArMDA6MDAiLCJ1cGRhdGVkQXQiOiIyMDE5LTA4LTE5IDE1OjU2OjE1LjYyOSArMDA6MDAiLCJkZWxldGVkQXQiOm51bGx9LCJpYXQiOjE1NjYyMzAyMjQsImV4cCI6MTU2NjI0ODIyNH0.FL0kkcInY5sDMGKeLHfEOYDTQd3BjR6_mK7Tcm_RH6iCLotTSRRoRxHpLkbtIQKqBFIt14J4BpLapkzG7ppRWcEley5nego-4iFOmXQvCBz5ISS3HdtM0saJnOe0agyVUen3huFp4F2UCth_y2ScjMn_4AgW66cz8NSFPRVpC8g' } })
.expect('status', 200)
.expect('header', 'content-type', /application\/json/)
.expect('json', {
user: {}
})
})
})