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] Tested with new Amplify workflow #156

Closed
wants to merge 23 commits into from
Closed

Conversation

mwillfox
Copy link

@mwillfox mwillfox commented Aug 7, 2024

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 Aug 7, 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 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 input. In the original code, the user input (criteria) is directly concatenated into the SQL string. This means that if an attacker provides specially crafted input, 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 revised code uses a parameterized query with named parameters (:criteria). This means that the SQL command structure is defined separately from the data being inserted into it. The database engine treats the parameter as a value rather than executable code.

  2. Replacements Object: The replacements object is used to safely pass the user input (criteria) into the SQL query. This ensures that the input is properly escaped and sanitized, preventing any special characters from being interpreted as part of the SQL command.

  3. Input Length Limitation: The code still includes a check to limit the length of the criteria input to 200 characters. While this is a good practice for performance and security, the main protection against SQL Injection comes from the use of parameterized queries.

Benefits of the Change

  • Security: By using parameterized queries, the risk of SQL Injection is significantly reduced, as user input cannot alter the structure of the SQL command.
  • Maintainability: Parameterized queries are generally easier to read and maintain, as they separate the SQL logic from the data being processed.
  • Performance: Many database systems can optimize parameterized queries better than dynamically constructed queries, potentially improving performance.

Further Reading

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

By implementing these changes, the code is now more secure against SQL Injection attacks, ensuring that user input is handled safely.

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 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 input. 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 gaining unauthorized access to data or executing harmful operations on the database.

How the Code Change Fixes the Vulnerability

  1. Use of Parameterized Queries: The modified code uses a parameterized query with named placeholders (in this case, :criteria). This means that the SQL command is defined separately from the data being inserted into it. The database driver treats the input as a literal value rather than executable code.

  2. Replacements Object: The replacements object is used to safely bind the user input to the SQL command. By doing this, the input is sanitized, and any special characters that could be used for SQL injection are neutralized. The database engine ensures that the input is treated as data, not as part of the SQL command.

  3. Length Limitation: The original code already included a length check on the criteria input, limiting it to 200 characters. This is a good practice, but it is not sufficient on its own to prevent SQL injection. The change to parameterized queries is a more robust solution.

Conclusion

By switching to parameterized queries, the code change effectively prevents SQL injection attacks by ensuring that user input cannot alter the structure of the SQL command. This approach is widely recommended in secure coding practices and is documented in various resources, including:

These resources provide further insights into secure coding practices and the importance of using parameterized queries to protect 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?

@mwillfox mwillfox closed this Aug 7, 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