Skip to content

feat(javascript): implement Go-based SQL injection checker#139

Merged
sourya-deepsource merged 1 commit intoDeepSourceCorp:masterfrom
hrideshmg:node_injection
Mar 6, 2025
Merged

feat(javascript): implement Go-based SQL injection checker#139
sourya-deepsource merged 1 commit intoDeepSourceCorp:masterfrom
hrideshmg:node_injection

Conversation

@hrideshmg
Copy link
Contributor

@hrideshmg hrideshmg commented Feb 26, 2025

Summary

This pull request introduces a Go-based static analysis checker designed to detect potential SQL injection vulnerabilities in JavaScript code.

Injection is the 3rd most critical web security risk in the OWASP Top 10. Among 94% of tested applications, it had a maximum incidence rate of 19% and an average of 3%. SQL injection remains a key pentesting target, making proper query sanitization essential to prevent unauthorized access and data loss.

Detection Logic

The scanner works in two phases, during the first phase, it goes through and creates a map of all known variables in a file. This allows it to track where each variable is assigned a value, this is needed later on for detecting SQL injection vulnerabilities.

During the second phase, the scanner finds all function calls that are used to execute raw SQL queries. A variable named vulnerableFunctions is maintained for this purpose and it includes calls from Supported ORM's. After this, it follows the below flow:

  1. It inspects the first argument of the SQL-executing function.
  2. If the argument is a variable, it looks up its definition in the map created during the first phase.
  3. If the argument (or variable) involves string concatenation (with a variable) or template literals with interpolation, it is flagged as vulnerable.
  4. A recursive call is used to track chained concatenations (+) in order to detect indirect vulnerabilities.

Supported ORMs

The checker currently covers five widely used ORMs in the JavaScript ecosystem:

  1. Sequelize -> .query()
  2. TypeORM -> .query()
  3. Prisma -> $queryRawUnsafe() and $executeRawUnsafe()
  4. Knex.js -> .raw()
  5. Objection.js -> .raw()
  6. node-postgres -> .query() [Not an ORM but a popular interface nonetheless]

These ORMs are extensively used in projects, ensuring broad vulnerability detection.

Note

I'm still new to Go and the initial version of the checker was written by Claude. However, I did personally go through and verify each line and make modifications to improve the overall flow and reduce redundancies. I believe the logic is sound, but please let me know if any changes are required.

I have also tested the checker using the new Go-based rules introduced in #123. Though i believe there is a bug with it (which is why the action fails)

@vercel
Copy link

vercel bot commented Feb 26, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
globstar ⬜️ Ignored (Inspect) Visit Preview Mar 6, 2025 7:07pm

@sanket-deepsource
Copy link
Contributor

Hi @hrideshmg — thanks for your contribution! We've made some changes to our Go interface for writing checkers, so this PR will need to be updated. Please see the reference here and this guide.

@hrideshmg hrideshmg closed this Mar 3, 2025
@hrideshmg hrideshmg deleted the node_injection branch March 3, 2025 16:38
@hrideshmg hrideshmg restored the node_injection branch March 3, 2025 16:39
@hrideshmg hrideshmg reopened this Mar 3, 2025
@hrideshmg
Copy link
Contributor Author

Hi @hrideshmg — thanks for your contribution! We've made some changes to our Go interface for writing checkers, so this PR will need to be updated. Please see the reference here and this guide.

I've adapted the PR to use the new interface, please take a look. Thanks!

@sanket-deepsource
Copy link
Contributor

Hi @hrideshmg — thanks for your contribution! We've made some changes to our Go interface for writing checkers, so this PR will need to be updated. Please see the reference here and this guide.

I've adapted the PR to use the new interface, please take a look. Thanks!

Thanks! Tagging @sourya-deepsource to help here.

@hrideshmg
Copy link
Contributor Author

Apologies for the ping but just dropping in to ask if there's any more changes to be made to this PR.

@sourya-deepsource
Copy link
Contributor

Apologies for the ping but just dropping in to ask if there's any more changes to be made to this PR.

The PR looks good. However, merging requires the commits to be signed. Can you take care of that?

@hrideshmg
Copy link
Contributor Author

hrideshmg commented Mar 6, 2025

The PR looks good. However, merging requires the commits to be signed. Can you take care of that?

Oops, my bad! I've signed them now

@sourya-deepsource sourya-deepsource merged commit 2eb3f3c into DeepSourceCorp:master Mar 6, 2025
3 checks passed
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.

3 participants