-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Lab] Test after deploy ENG-766 #155
Changes from all commits
f3248e7
b0ada84
4a425aa
1d3a90a
f827337
6a67766
c457624
6f8b7a8
85f1b51
f05622b
7fd60f2
772251a
8d5643d
a814a3f
253044a
9037f1a
218b184
b47abed
f1b856f
0cff134
41c57bb
8b20ccb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
routes/*.js |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,14 +2,72 @@ | |||||||||
* Copyright (c) 2014-2023 Bjoern Kimminich & the OWASP Juice Shop contributors. | ||||||||||
* SPDX-License-Identifier: MIT | ||||||||||
*/ | ||||||||||
import models = require('../models/index') | ||||||||||
import { Request, Response, NextFunction } from 'express' | ||||||||||
import { UserModel } from '../models/user' | ||||||||||
|
||||||||||
import * as utils from '../lib/utils' | ||||||||||
const challengeUtils = require('../lib/challengeUtils') | ||||||||||
const challenges = require('../data/datacache').challenges | ||||||||||
|
||||||||||
class ErrorWithParent extends Error { | ||||||||||
parent: Error | undefined | ||||||||||
} | ||||||||||
|
||||||||||
// vuln-code-snippet start unionSqlInjectionChallenge dbSchemaChallenge | ||||||||||
module.exports = function searchProducts() { | ||||||||||
return (req: Request, res: Response, next: NextFunction) => { | ||||||||||
let criteria: any = req.query.q === 'undefined' ? '' : req.query.q ?? '' | ||||||||||
criteria = (criteria.length <= 200) ? criteria : criteria.substring(0, 200) | ||||||||||
console.log(criteria) | ||||||||||
models.sequelize.query(`SELECT * FROM Products WHERE ((name LIKE '%${criteria}%' OR description LIKE '%${criteria}%') AND deletedAt IS NULL) ORDER BY name`) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Warning Amplify has been notified that this line contains a vulnerability 🕷️. Vulnerability: CWE-89 Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection') Impact: MEDIUM Code Fix: ✅ Amplify Security has prepared an automated remediation for review. Click here to review and commit the code fix.
Suggested change
The code change addresses the SQL Injection vulnerability by implementing parameterized queries, which are a best practice for safely executing SQL commands. Here's how the change improves security: Explanation of the Fix
Benefits
DocumentationFor more information on preventing SQL Injection and using parameterized queries, you can refer to the following resources: By adopting these practices, the code not only becomes more secure but also aligns with industry standards for database interactions. Note Have a question or concern about this vulnerability fix? Get an answer within seconds by asking our Concierge 🤖 with |
||||||||||
.then(([products]: any) => { | ||||||||||
const dataString = JSON.stringify(products) | ||||||||||
if (challengeUtils.notSolved(challenges.unionSqlInjectionChallenge)) { // vuln-code-snippet hide-start | ||||||||||
let solved = true | ||||||||||
UserModel.findAll().then(data => { | ||||||||||
const users = utils.queryResultToJson(data) | ||||||||||
if (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) { | ||||||||||
challengeUtils.solve(challenges.unionSqlInjectionChallenge) | ||||||||||
} | ||||||||||
} | ||||||||||
}).catch((error: Error) => { | ||||||||||
next(error) | ||||||||||
}) | ||||||||||
} | ||||||||||
if (challengeUtils.notSolved(challenges.dbSchemaChallenge)) { | ||||||||||
let solved = true | ||||||||||
models.sequelize.query('SELECT sql FROM sqlite_master').then(([data]: any) => { | ||||||||||
const tableDefinitions = utils.queryResultToJson(data) | ||||||||||
if (tableDefinitions.data?.length) { | ||||||||||
for (let i = 0; i < tableDefinitions.data.length; i++) { | ||||||||||
if (tableDefinitions.data[i].sql) { | ||||||||||
solved = solved && utils.containsOrEscaped(dataString, tableDefinitions.data[i].sql) | ||||||||||
if (!solved) { | ||||||||||
break | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
if (solved) { | ||||||||||
challengeUtils.solve(challenges.dbSchemaChallenge) | ||||||||||
} | ||||||||||
} | ||||||||||
}) | ||||||||||
} // vuln-code-snippet hide-end | ||||||||||
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: ErrorWithParent) => { | ||||||||||
next(error.parent) | ||||||||||
}) | ||||||||||
} | ||||||||||
} | ||||||||||
// vuln-code-snippet end unionSqlInjectionChallenge dbSchemaChallenge |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Warning
Amplify has been notified that this line contains a vulnerability 🕷️.
Vulnerability: CWE-89 Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
Impact: HIGH
Code Fix: ✅
Amplify Security has prepared an automated remediation for review. Click here to review and commit the code fix.
The code change addresses the SQL Injection vulnerability by implementing parameterized queries instead of directly interpolating user input into the SQL command. Here's how this change mitigates the risk:
Explanation of the Vulnerability
SQL Injection occurs when an attacker is able to manipulate a SQL query by injecting malicious SQL code through user inputs. In the original code, the
criteria
variable is directly concatenated into the SQL query string. This means that if a user inputs a specially crafted string, they could alter the intended SQL command, potentially allowing them to access or manipulate the database in unauthorized ways.How the Code Change Fixes the Vulnerability
Use of Parameterized Queries: The modified code uses a parameterized query by employing the
replacements
option. Instead of directly embedding the user input into the SQL string, it uses a placeholder (:criteria
) which is then safely replaced with the actual value. This ensures that the input is treated strictly as data, not executable code.Input Sanitization: By using the
replacements
object, the framework automatically handles the necessary escaping of special characters in the user input. This means that even if a user tries to inject SQL code through thecriteria
, it will be treated as a string literal rather than executable SQL.Limiting Input Length: The original code already had a safeguard to limit the length of the
criteria
to 200 characters. This is a good practice as it reduces the risk of overly long inputs that could be used in an attack.Conclusion
By switching to a parameterized query, the code change effectively neutralizes the risk of SQL Injection. This approach not only enhances security but also improves the maintainability of the code by separating SQL logic from data handling. For more information on preventing SQL Injection, you can refer to the OWASP SQL Injection Prevention Cheat Sheet.
Note
Have a question or concern about this vulnerability fix? Get an answer within seconds by asking our Concierge 🤖 with
@amplify-security
.i.e. @amplify-security are there known performance issues resulting from this fix?