forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowProductReviews.js
43 lines (39 loc) · 1.38 KB
/
showProductReviews.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
/*
* Copyright (c) 2014-2020 Bjoern Kimminich.
* SPDX-License-Identifier: MIT
*/
const utils = require('../lib/utils')
const challenges = require('../data/datacache').challenges
const insecurity = require('../lib/insecurity')
const db = require('../data/mongodb')
// Blocking sleep function as in native MongoDB
global.sleep = time => {
// Ensure that users dont accidentally dos their servers for too long
if (time > 2000) {
time = 2000
}
const stop = new Date().getTime()
while (new Date().getTime() < stop + time) {
;
}
}
module.exports = function productReviews () {
return (req, res, next) => {
const id = utils.disableOnContainerEnv() ? Number(req.params.id) : req.params.id
// Measure how long the query takes to find out if an there was a nosql dos attack
const t0 = new Date().getTime()
db.reviews.find({ $where: 'this.product == ' + id }).then(reviews => {
const t1 = new Date().getTime()
utils.solveIf(challenges.noSqlCommandChallenge, () => { return (t1 - t0) > 2000 })
const user = insecurity.authenticatedUsers.from(req)
for (var i = 0; i < reviews.length; i++) {
if (user === undefined || reviews[i].likedBy.includes(user.data.email)) {
reviews[i].liked = true
}
}
res.json(utils.queryResultToJson(reviews))
}, () => {
res.status(400).json({ error: 'Wrong Params' })
})
}
}