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

chore: introduce sql injection #3

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions data/static/codefixes/dbSchemaChallenge_2_correct.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
module.exports = function searchProducts () {
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)
models.sequelize.query(
`SELECT * FROM Products WHERE ((name LIKE '%:criteria%' OR description LIKE '%:criteria%') AND deletedAt IS NULL) ORDER BY name`,
{ replacements: { criteria } }
).then(([products]: any) => {
const dataString = JSON.stringify(products)
let criteria: any = req.query.q === "undefined" ? "" : req.query.q ?? "";
criteria = criteria.length <= 200 ? criteria : criteria.substring(0, 200);
models.sequelize
.query(
"SELECT * FROM Products WHERE ((name LIKE '%:" +
criteria +
"%' OR description LIKE '%:" +
criteria +
"%') AND deletedAt IS NULL) ORDER BY name"
)
Comment on lines +5 to +12
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [Bearer] <javascript_lang_sql_injection> reported by reviewdog 🐶

SQL injection vulnerability detected.

Description

Including unsanitized data, such as user input or request data, in raw SQL queries makes your application vulnerable to SQL injection attacks.

Remediations

❌ Avoid raw queries, especially those that contain unsanitized user input

  var sqlite = new Sequelize("sqlite::memory:");
  sqlite.query("SELECT * FROM users WHERE ID = " + req.params.userId);

Instead, consider the following approaches when writing SQL queries

✅ Validate query input wherever possible

  var rawId = req.params.userId
  if !(/[0-9]+/.test(rawId)) {
    // input is unexpected; don't make the query
  }

✅ Use prepared (or parameterized) statements when querying

Sequelize example -

  var sqlite = new Sequelize("sqlite::memory:");
  sqlite.query(
    "SELECT * FROM users WHERE ID = ?",
    { replacements: [req.params.userId] },
    type: sequelize.QueryTypes.SELECT
  )

Resources

.then(([products]: any) => {
const dataString = JSON.stringify(products);
for (let i = 0; i < products.length; i++) {
products[i].name = req.__(products[i].name)
products[i].description = req.__(products[i].description)
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)
res.json(utils.queryResultToJson(products));
})
}
}
.catch((error: ErrorWithParent) => {
next(error.parent);
});
};
};