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] Demo for Selina #151

Closed
wants to merge 22 commits into from
Closed

[Lab] Demo for Selina #151

wants to merge 22 commits into from

Conversation

mwillfox
Copy link

@mwillfox mwillfox commented Jul 1, 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 Jul 1, 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 fixes the SQL Injection vulnerability by using parameterized queries instead of directly concatenating user input into the SQL query string.

In the original code, the user input criteria is directly interpolated into the SQL query string using string concatenation. This allows an attacker to manipulate the criteria value and inject malicious SQL code, potentially leading to unauthorized access or data manipulation.

The code change introduces a parameterized query by using placeholders (:criteria) in the SQL query string and providing the actual value (%${criteria}%) as a replacement using the replacements option. Parameterized queries separate the SQL code from the user input, preventing the injection of malicious code.

By using parameterized queries, the code ensures that the user input is treated as data rather than executable code, effectively mitigating the SQL Injection vulnerability.

For more information on SQL Injection and how to prevent it, you can refer to the OWASP SQL Injection Prevention Cheat Sheet: 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?

Comment on lines +22 to +23
models.sequelize.query(`SELECT * FROM Products WHERE ((name LIKE '%${criteria}%' OR description LIKE '%${criteria}%') AND deletedAt IS NULL) ORDER BY name`)
.then(([products]: any) => {
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`)
.then(([products]: any) => {
models.sequelize.query(`SELECT * FROM Products WHERE ((name LIKE ? OR description LIKE ?) AND deletedAt IS NULL) ORDER BY name`, {
replacements: [`%${criteria}%`, `%${criteria}%`],
type: models.sequelize.QueryTypes.SELECT
})
.then((products: any) => {

The code change fixes the SQL Injection vulnerability by using parameterized queries instead of directly concatenating user input into the SQL query string.

In the original code, the user input criteria is directly interpolated into the SQL query string using string concatenation. This allows an attacker to manipulate the input and inject malicious SQL code, potentially leading to unauthorized access or data manipulation.

In the fixed code, the models.sequelize.query method is used with parameterized queries. The user input criteria is replaced with placeholders (?) in the query string, and the actual values are passed as an array of replacements. This ensures that the user input is treated as a value and not as part of the SQL query structure, effectively preventing SQL Injection attacks.

By using parameterized queries, the code ensures that user input is properly sanitized and escaped, eliminating the possibility of SQL Injection vulnerabilities.

For more information on SQL Injection and how to prevent it, you can refer to the OWASP SQL Injection Prevention Cheat Sheet: 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?

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