-
Notifications
You must be signed in to change notification settings - Fork 1
/
dusraLafda.ts
326 lines (243 loc) · 12 KB
/
dusraLafda.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright (c) 2014-2023 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/
import sinon = require('sinon')
import config from 'config'
import type { Product } from '../../data/types'
const chai = require('chai')
const sinonChai = require('sinon-chai')
const expect = chai.expect
chai.use(sinonChai)
const cache = require('../../data/datacache')
const security = require('../../lib/insecurity')
const utils = require('../../lib/utils')
describe('verify', () => {
const verify = require('../../routes/verify')
const challenges = require('../../data/datacache').challenges
let req: any
let res: any
let next: any
let save: any
let err: any
beforeEach(() => {
req = { body: {}, headers: {} }
res = { json: sinon.spy() }
next = sinon.spy()
save = () => ({
then () { }
})
})
describe('"forgedFeedbackChallenge"', () => {
beforeEach(() => {
security.authenticatedUsers.put('token12345', {
data: {
id: 42,
email: 'test@juice-sh.op'
}
})
challenges.forgedFeedbackChallenge = { solved: false, save }
})
it('is not solved when an authenticated user passes his own ID when writing feedback', () => {
req.body.UserId = 42
req.headers = { authorization: 'Bearer token12345' }
verify.forgedFeedbackChallenge()(req, res, next)
expect(challenges.forgedFeedbackChallenge.solved).to.equal(false)
})
it('is not solved when an authenticated user passes no ID when writing feedback', () => {
req.body.UserId = undefined
req.headers = { authorization: 'Bearer token12345' }
verify.forgedFeedbackChallenge()(req, res, next)
expect(challenges.forgedFeedbackChallenge.solved).to.equal(false)
})
it('is solved when an authenticated user passes someone elses ID when writing feedback', () => {
req.body.UserId = 1
req.headers = { authorization: 'Bearer token12345' }
verify.forgedFeedbackChallenge()(req, res, next)
expect(challenges.forgedFeedbackChallenge.solved).to.equal(true)
})
it('is solved when an unauthenticated user passes someones ID when writing feedback', () => {
req.body.UserId = 1
req.headers = {}
verify.forgedFeedbackChallenge()(req, res, next)
expect(challenges.forgedFeedbackChallenge.solved).to.equal(true)
})
})
describe('accessControlChallenges', () => {
it('"scoreBoardChallenge" is solved when the 1px.png transpixel is requested', () => {
challenges.scoreBoardChallenge = { solved: false, save }
req.url = 'http://juice-sh.op/public/images/padding/1px.png'
verify.accessControlChallenges()(req, res, next)
expect(challenges.scoreBoardChallenge.solved).to.equal(true)
})
it('"adminSectionChallenge" is solved when the 19px.png transpixel is requested', () => {
challenges.adminSectionChallenge = { solved: false, save }
req.url = 'http://juice-sh.op/public/images/padding/19px.png'
verify.accessControlChallenges()(req, res, next)
expect(challenges.adminSectionChallenge.solved).to.equal(true)
})
it('"tokenSaleChallenge" is solved when the 56px.png transpixel is requested', () => {
challenges.tokenSaleChallenge = { solved: false, save }
req.url = 'http://juice-sh.op/public/images/padding/56px.png'
verify.accessControlChallenges()(req, res, next)
expect(challenges.tokenSaleChallenge.solved).to.equal(true)
})
it('"extraLanguageChallenge" is solved when the Klingon translation file is requested', () => {
challenges.extraLanguageChallenge = { solved: false, save }
req.url = 'http://juice-sh.op/public/i18n/tlh_AA.json'
verify.accessControlChallenges()(req, res, next)
expect(challenges.extraLanguageChallenge.solved).to.equal(true)
})
it('"retrieveBlueprintChallenge" is solved when the blueprint file is requested', () => {
challenges.retrieveBlueprintChallenge = { solved: false, save }
cache.retrieveBlueprintChallengeFile = 'test.dxf'
req.url = 'http://juice-sh.op/public/images/products/test.dxf'
verify.accessControlChallenges()(req, res, next)
expect(challenges.retrieveBlueprintChallenge.solved).to.equal(true)
})
it('"missingEncodingChallenge" is solved when the crazy cat photo is requested', () => {
challenges.missingEncodingChallenge = { solved: false, save }
req.url = 'http://juice-sh.op/public/images/uploads/%F0%9F%98%BC-%23zatschi-%23whoneedsfourlegs-1572600969477.jpg'
verify.accessControlChallenges()(req, res, next)
expect(challenges.missingEncodingChallenge.solved).to.equal(true)
})
it('"accessLogDisclosureChallenge" is solved when any server access log file is requested', () => {
challenges.accessLogDisclosureChallenge = { solved: false, save }
req.url = 'http://juice-sh.op/support/logs/access.log.2019-01-15'
verify.accessControlChallenges()(req, res, next)
expect(challenges.accessLogDisclosureChallenge.solved).to.equal(true)
})
})
describe('"errorHandlingChallenge"', () => {
beforeEach(() => {
challenges.errorHandlingChallenge = { solved: false, save }
})
it('is solved when an error occurs on a response with OK 200 status code', () => {
res.statusCode = 200
err = new Error()
verify.errorHandlingChallenge()(err, req, res, next)
expect(challenges.errorHandlingChallenge.solved).to.equal(true)
})
describe('is solved when an error occurs on a response with error', () => {
const httpStatus = [402, 403, 404, 500]
httpStatus.forEach(statusCode => {
it(`${statusCode} status code`, () => {
res.statusCode = statusCode
err = new Error()
verify.errorHandlingChallenge()(err, req, res, next)
expect(challenges.errorHandlingChallenge.solved).to.equal(true)
})
})
})
it('is not solved when no error occurs on a response with OK 200 status code', () => {
res.statusCode = 200
err = undefined
verify.errorHandlingChallenge()(err, req, res, next)
expect(challenges.errorHandlingChallenge.solved).to.equal(false)
})
describe('is not solved when no error occurs on a response with error', () => {
const httpStatus = [401, 402, 404, 500]
httpStatus.forEach(statusCode => {
it(`${statusCode} status code`, () => {
res.statusCode = statusCode
err = undefined
verify.errorHandlingChallenge()(err, req, res, next)
expect(challenges.errorHandlingChallenge.solved).to.equal(false)
})
})
})
it('should pass occured error on to next route', () => {
res.statusCode = 500
err = new Error()
verify.errorHandlingChallenge()(err, req, res, next)
expect(next).to.have.been.calledWith(err)
})
})
describe('databaseRelatedChallenges', () => {
describe('"changeProductChallenge"', () => {
const products = require('../../data/datacache').products
beforeEach(() => {
challenges.changeProductChallenge = { solved: false, save }
products.osaft = { reload () { return { then (cb: any) { cb() } } } }
})
it(`is solved when the link in the O-Saft product goes to ${config.get('challenges.overwriteUrlForProductTamperingChallenge')}`, () => {
products.osaft.description = `O-Saft, yeah! <a href="${config.get('challenges.overwriteUrlForProductTamperingChallenge')}" target="_blank">More...</a>`
verify.databaseRelatedChallenges()(req, res, next)
expect(challenges.changeProductChallenge.solved).to.equal(true)
})
it('is not solved when the link in the O-Saft product is changed to an arbitrary URL', () => {
products.osaft.description = 'O-Saft, nooo! <a href="http://arbitrary.url" target="_blank">More...</a>'
verify.databaseRelatedChallenges()(req, res, next)
expect(challenges.changeProductChallenge.solved).to.equal(false)
})
it('is not solved when the link in the O-Saft product remained unchanged', () => {
let urlForProductTamperingChallenge = null
for (const product of config.get<Product[]>('products')) {
if (product.urlForProductTamperingChallenge !== undefined) {
urlForProductTamperingChallenge = product.urlForProductTamperingChallenge
break
}
}
products.osaft.description = `Vanilla O-Saft! <a href="${urlForProductTamperingChallenge}" target="_blank">More...</a>`
verify.databaseRelatedChallenges()(req, res, next)
expect(challenges.changeProductChallenge.solved).to.equal(false)
})
})
})
describe('jwtChallenges', () => {
beforeEach(() => {
challenges.jwtUnsignedChallenge = { solved: false, save }
challenges.jwtForgedChallenge = { solved: false, save }
})
it('"jwtUnsignedChallenge" is solved when forged unsigned token has email jwtn3d@juice-sh.op in the payload', () => {
/*
Header: { "alg": "none", "typ": "JWT" }
Payload: { "data": { "email": "jwtn3d@juice-sh.op" }, "iat": 1508639612, "exp": 9999999999 }
*/
req.headers = { authorization: 'Bearer eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJkYXRhIjp7ImVtYWlsIjoiand0bjNkQGp1aWNlLXNoLm9wIn0sImlhdCI6MTUwODYzOTYxMiwiZXhwIjo5OTk5OTk5OTk5fQ.' }
verify.jwtChallenges()(req, res, next)
expect(challenges.jwtUnsignedChallenge.solved).to.equal(true)
})
it('"jwtUnsignedChallenge" is solved when forged unsigned token has string "jwtn3d@" in the payload', () => {
/*
Header: { "alg": "none", "typ": "JWT" }
Payload: { "data": { "email": "jwtn3d@" }, "iat": 1508639612, "exp": 9999999999 }
*/
req.headers = { authorization: 'Bearer eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJkYXRhIjp7ImVtYWlsIjoiand0bjNkQCJ9LCJpYXQiOjE1MDg2Mzk2MTIsImV4cCI6OTk5OTk5OTk5OX0.' }
verify.jwtChallenges()(req, res, next)
expect(challenges.jwtUnsignedChallenge.solved).to.equal(true)
})
it('"jwtUnsignedChallenge" is not solved via regularly signed token even with email jwtn3d@juice-sh.op in the payload', () => {
const token = security.authorize({ data: { email: 'jwtn3d@juice-sh.op' } })
req.headers = { authorization: `Bearer ${token}` }
verify.jwtChallenges()(req, res, next)
expect(challenges.jwtForgedChallenge.solved).to.equal(false)
})
if (!utils.disableOnWindowsEnv()) {
it('"jwtForgedChallenge" is solved when forged token HMAC-signed with public RSA-key has email rsa_lord@juice-sh.op in the payload', () => {
/*
Header: { "alg": "HS256", "typ": "JWT" }
Payload: { "data": { "email": "rsa_lord@juice-sh.op" }, "iat": 1508639612, "exp": 9999999999 }
*/
req.headers = { authorization: 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7ImVtYWlsIjoicnNhX2xvcmRAanVpY2Utc2gub3AifSwiaWF0IjoxNTgyMjIxNTc1fQ.ycFwtqh4ht4Pq9K5rhiPPY256F9YCTIecd4FHFuSEAg' }
verify.jwtChallenges()(req, res, next)
expect(challenges.jwtForgedChallenge.solved).to.equal(true)
})
it('"jwtForgedChallenge" is solved when forged token HMAC-signed with public RSA-key has string "rsa_lord@" in the payload', () => {
/*
Header: { "alg": "HS256", "typ": "JWT" }
Payload: { "data": { "email": "rsa_lord@" }, "iat": 1508639612, "exp": 9999999999 }
*/
req.headers = { authorization: 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7ImVtYWlsIjoicnNhX2xvcmRAIn0sImlhdCI6MTU4MjIyMTY3NX0.50f6VAIQk2Uzpf3sgH-1JVrrTuwudonm2DKn2ec7Tg8' }
verify.jwtChallenges()(req, res, next)
expect(challenges.jwtForgedChallenge.solved).to.equal(true)
})
it('"jwtForgedChallenge" is not solved when token regularly signed with private RSA-key has email rsa_lord@juice-sh.op in the payload', () => {
const token = security.authorize({ data: { email: 'rsa_lord@juice-sh.op' } })
req.headers = { authorization: `Bearer ${token}` }
verify.jwtChallenges()(req, res, next)
expect(challenges.jwtForgedChallenge.solved).to.equal(false)
})
}
})
})