|
| 1 | +// vuln-code-snippet start unionSqlInjectionChallenge dbSchemaChallenge |
| 2 | +module.exports = function searchProducts () { |
| 3 | + return (req: Request, res: Response, next: NextFunction) => { |
| 4 | + let criteria: any = req.query.q === 'undefined' ? '' : req.query.q ?? '' |
| 5 | + criteria = (criteria.length <= 200) ? criteria : criteria.substring(0, 200) |
| 6 | + models.sequelize.query(`SELECT * FROM Products WHERE ((name LIKE '%${criteria}%' OR description LIKE '%${criteria}%') AND deletedAt IS NULL) ORDER BY name`) // vuln-code-snippet vuln-line unionSqlInjectionChallenge dbSchemaChallenge |
| 7 | + .then(([products]: any) => { |
| 8 | + const dataString = JSON.stringify(products) |
| 9 | + if (challengeUtils.notSolved(challenges.unionSqlInjectionChallenge)) { // vuln-code-snippet hide-start |
| 10 | + let solved = true |
| 11 | + UserModel.findAll().then(data => { |
| 12 | + const users = utils.queryResultToJson(data) |
| 13 | + if (users.data?.length) { |
| 14 | + for (let i = 0; i < users.data.length; i++) { |
| 15 | + solved = solved && utils.containsOrEscaped(dataString, users.data[i].email) && utils.contains(dataString, users.data[i].password) |
| 16 | + if (!solved) { |
| 17 | + break |
| 18 | + } |
| 19 | + } |
| 20 | + if (solved) { |
| 21 | + challengeUtils.solve(challenges.unionSqlInjectionChallenge) |
| 22 | + } |
| 23 | + } |
| 24 | + }).catch((error: Error) => { |
| 25 | + next(error) |
| 26 | + }) |
| 27 | + } |
| 28 | + if (challengeUtils.notSolved(challenges.dbSchemaChallenge)) { |
| 29 | + let solved = true |
| 30 | + models.sequelize.query('SELECT sql FROM sqlite_master').then(([data]: any) => { |
| 31 | + const tableDefinitions = utils.queryResultToJson(data) |
| 32 | + if (tableDefinitions.data?.length) { |
| 33 | + for (let i = 0; i < tableDefinitions.data.length; i++) { |
| 34 | + if (tableDefinitions.data[i].sql) { |
| 35 | + solved = solved && utils.containsOrEscaped(dataString, tableDefinitions.data[i].sql) |
| 36 | + if (!solved) { |
| 37 | + break |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + if (solved) { |
| 42 | + challengeUtils.solve(challenges.dbSchemaChallenge) |
| 43 | + } |
| 44 | + } |
| 45 | + }) |
| 46 | + } // vuln-code-snippet hide-end |
| 47 | + for (let i = 0; i < products.length; i++) { |
| 48 | + products[i].name = req.__(products[i].name) |
| 49 | + products[i].description = req.__(products[i].description) |
| 50 | + } |
| 51 | + res.json(utils.queryResultToJson(products)) |
| 52 | + }).catch((error: ErrorWithParent) => { |
| 53 | + next(error.parent) |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
0 commit comments