forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
60 lines (58 loc) · 2.35 KB
/
search.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Copyright (c) 2014-2020 Bjoern Kimminich.
* SPDX-License-Identifier: MIT
*/
const utils = require('../lib/utils')
const models = require('../models/index')
const challenges = require('../data/datacache').challenges
module.exports = function searchProducts () {
return (req, res, next) => {
let criteria = req.query.q === 'undefined' ? '' : req.query.q || ''
criteria = (criteria.length <= 200) ? criteria : criteria.substring(0, 200)
models.sequelize.query(`SELECT * FROM Products WHERE ((name LIKE '%${criteria}%' OR description LIKE '%${criteria}%') AND deletedAt IS NULL) ORDER BY name`)
.then(([products]) => {
const dataString = JSON.stringify(products)
if (utils.notSolved(challenges.unionSqlInjectionChallenge)) {
let solved = true
models.User.findAll().then(data => {
const users = utils.queryResultToJson(data)
if (users.data && users.data.length) {
for (let i = 0; i < users.data.length; i++) {
solved = solved && utils.containsOrEscaped(dataString, users.data[i].email) && utils.contains(dataString, users.data[i].password)
if (!solved) {
break
}
}
if (solved) {
utils.solve(challenges.unionSqlInjectionChallenge)
}
}
})
}
if (utils.notSolved(challenges.dbSchemaChallenge)) {
let solved = true
models.sequelize.query('SELECT sql FROM sqlite_master').then(([data]) => {
const tableDefinitions = utils.queryResultToJson(data)
if (tableDefinitions.data && tableDefinitions.data.length) {
for (let i = 0; i < tableDefinitions.data.length; i++) {
solved = solved && utils.containsOrEscaped(dataString, tableDefinitions.data[i].sql)
if (!solved) {
break
}
}
if (solved) {
utils.solve(challenges.dbSchemaChallenge)
}
}
})
}
for (let i = 0; i < products.length; i++) {
products[i].name = req.__(products[i].name)
products[i].description = req.__(products[i].description)
}
res.json(utils.queryResultToJson(products))
}).catch(error => {
next(error)
})
}
}