Skip to content
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-756 #153

Closed
wants to merge 22 commits into from
Closed

[Lab] Test after deploy ENG-756 #153

wants to merge 22 commits into from

Conversation

mwillfox
Copy link

Description

A clear and concise summary of the change and which issue (if any) it fixes. Should also include relevant motivation and context.

Resolved or fixed issue:

Affirmation

Copy link

amplify-lab bot commented Jul 23, 2024

✨ Amplify has finished checking this pull request

⚠️ 2 issues detected in 📄 3 files and ❇️ 63 lines of code.

Security Pipeline

Tool Configured Result
Semgrep ⚠️

Vulnerabilities Detected

Vulnerability Path Fingerprint
CWE-89 routes/search.ts:22 [eec4f3b0...]
CWE-89 routes/search.ts:22 [4b4e1c02...]

Note

To ignore a finding, append @amplify-ignore in a comment to the end of the vulnerable line like // @amplify-ignore or # @amplify-ignore. For more details, visit Amplify Security.


// 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`)
Copy link

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.

Suggested change
models.sequelize.query(`SELECT * FROM Products WHERE ((name LIKE '%${criteria}%' OR description LIKE '%${criteria}%') AND deletedAt IS NULL) ORDER BY name`)
models.sequelize.query(`SELECT * FROM Products WHERE ((name LIKE :criteria OR description LIKE :criteria) AND deletedAt IS NULL) ORDER BY name`, {
replacements: { criteria: `%${criteria}%` }
})

The code change addresses the SQL Injection vulnerability by implementing parameterized queries, which are a more secure way to handle user input in SQL commands. Here's how this change improves security:

  1. Parameterized Queries: The original code directly interpolated user input (criteria) into the SQL query string. This practice is highly susceptible to SQL Injection attacks, where an attacker could manipulate the input to execute arbitrary SQL commands. By using parameterized queries, the input is treated as a parameter rather than part of the SQL command itself.

  2. Input Sanitization: In the modified code, the criteria variable is passed as a replacement parameter (:criteria) in the SQL query. This means that the database engine will handle the input safely, ensuring that any special characters or SQL syntax within the user input do not alter the intended query structure.

  3. Reduced Risk of Injection: Since the user input is not directly concatenated into the SQL string, the risk of an attacker injecting malicious SQL code is significantly reduced. The database will only execute the commands that are explicitly defined in the query, ignoring any additional SQL that might be included in the user input.

  4. Maintainability and Readability: Using parameterized queries can also improve the readability and maintainability of the code. It separates the SQL logic from the data, making it clearer what the query is doing and how user input is being handled.

For further reading on SQL Injection and how to prevent it, you can refer to the OWASP SQL Injection Prevention Cheat Sheet. This resource provides comprehensive guidelines on securing applications against SQL Injection vulnerabilities.

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?


// 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`)
Copy link

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: MEDIUM

Code Fix: ✅

Amplify Security has prepared an automated remediation for review. Click here to review and commit the code fix.

Suggested change
models.sequelize.query(`SELECT * FROM Products WHERE ((name LIKE '%${criteria}%' OR description LIKE '%${criteria}%') AND deletedAt IS NULL) ORDER BY name`)
models.sequelize.query(`SELECT * FROM Products WHERE ((name LIKE :criteria OR description LIKE :criteria) AND deletedAt IS NULL) ORDER BY name`, {
replacements: { criteria: `%${criteria}%` }
})

The code change addresses the SQL Injection vulnerability by using parameterized queries instead of directly embedding 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 an SQL query by injecting malicious input. In the original code, the user input (criteria) is directly concatenated into the SQL string. This means that if a user inputs a specially crafted string, they could alter the intended SQL command, potentially allowing them to execute arbitrary SQL code.

How the Code Change Fixes the Vulnerability

  1. Parameterized Queries: The updated code uses a parameterized query with placeholders (in this case, :criteria). This means that the SQL command is defined separately from the data being passed in. The database engine treats the input as data rather than executable code.

  2. Replacements Object: The replacements option is used to safely bind the user input to the query. The input is passed as a parameter, which prevents any special characters in the input from being interpreted as SQL commands. This effectively neutralizes any potential SQL injection attempts.

  3. Input Validation: The code also includes a length check for the criteria variable, limiting it to 200 characters. While this is not a complete defense against SQL injection, it adds an additional layer of validation to ensure that excessively long inputs are truncated.

Conclusion

By switching to a parameterized query and using the replacements feature, the code change significantly reduces the risk of SQL Injection vulnerabilities. This approach ensures that user input is handled safely, preventing attackers from manipulating the SQL command structure.

For more information on preventing SQL Injection and the benefits of parameterized queries, you can refer to the following documentation:

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?

@mwillfox mwillfox closed this Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant