-
Notifications
You must be signed in to change notification settings - Fork 472
Javascript Drizzle ORM Raw SQL #3628
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
Open
daweifeng-replit
wants to merge
2
commits into
semgrep:develop
Choose a base branch
from
daweifeng-replit:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
180 changes: 180 additions & 0 deletions
180
javascript/drizzle-orm/security/audit/ban-drizzle-sql-raw.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
/** | ||
* CommonJS import | ||
*/ | ||
|
||
const { sql } = require("drizzle-orm"); | ||
async function rawGetUser(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
return sql.raw(`select * from users where id = ${id}`); | ||
} | ||
|
||
async function sqlEscapedGetUser(id) { | ||
// ok: ban-drizzle-sql-raw | ||
return sql.execute(sql`select * from users where id = ${id}`); | ||
} | ||
|
||
|
||
async function rawGetUserInTemplateLiteralsVariable(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
const query = `select * from users where id = ${id}`; | ||
return sql.raw(query); | ||
} | ||
|
||
async function rawGetUserWithPlusOperator(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
const query = "select * from users where id = " + id; | ||
return sql.raw(query); | ||
} | ||
|
||
async function rawGetUserWithPlusOperatorVariable(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
return sql.raw("select * from users where id = " + id); | ||
} | ||
|
||
async function rawGetUserWithoutInjection() { | ||
// ok: ban-drizzle-sql-raw | ||
return sql.raw("select * from users where id = 1"); | ||
} | ||
|
||
/** | ||
* ESM import | ||
*/ | ||
|
||
import { sql } from "drizzle-orm"; | ||
async function rawGetUser(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
return sql.raw(`select * from users where id = ${id}`); | ||
} | ||
|
||
async function sqlEscapedGetUser(id) { | ||
// ok: ban-drizzle-sql-raw | ||
return sql.execute(sql`select * from users where id = ${id}`); | ||
} | ||
|
||
async function rawGetUserInTemplateLiteralsVariable(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
const query = `select * from users where id = ${id}`; | ||
return sql.raw(query); | ||
} | ||
|
||
async function rawGetUserWithPlusOperator(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
const query = "select * from users where id = " + id; | ||
return sql.raw(query); | ||
} | ||
|
||
async function rawGetUserWithPlusOperatorVariable(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
return sql.raw("select * from users where id = " + id); | ||
} | ||
|
||
async function rawGetUserWithoutInjection() { | ||
// ok: ban-drizzle-sql-raw | ||
return sql.raw("select * from users where id = 1"); | ||
} | ||
|
||
/** | ||
* CommonJS import sql as SQL | ||
*/ | ||
|
||
const { sql: SQL } = require("drizzle-orm"); | ||
|
||
async function rawGetUser(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
return SQL.raw(`select * from users where id = ${id}`); | ||
} | ||
|
||
async function sqlEscapedGetUser(id) { | ||
// ok: ban-drizzle-sql-raw | ||
return SQL.execute(sql`select * from users where id = ${id}`); | ||
} | ||
|
||
/** | ||
* ESM import sql as SQL | ||
*/ | ||
|
||
import { sql as SQL } from "drizzle-orm"; | ||
async function rawGetUser(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
return SQL.raw(`select * from users where id = ${id}`); | ||
} | ||
|
||
async function sqlEscapedGetUser(id) { | ||
// ok: ban-drizzle-sql-raw | ||
return SQL.execute(sql`select * from users where id = ${id}`); | ||
} | ||
|
||
async function rawGetUserInTemplateLiteralsVariable(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
const query = `select * from users where id = ${id}`; | ||
return SQL.raw(query); | ||
} | ||
|
||
async function rawGetUserWithPlusOperator(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
const query = "select * from users where id = " + id; | ||
return SQL.raw(query); | ||
} | ||
|
||
async function rawGetUserWithPlusOperatorVariable(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
return SQL.raw("select * from users where id = " + id); | ||
} | ||
|
||
async function rawGetUserWithoutInjection() { | ||
// ok: ban-drizzle-sql-raw | ||
return SQL.raw("select * from users where id = 1"); | ||
} | ||
|
||
/** | ||
* ESM import Drizzle | ||
*/ | ||
|
||
import Drizzle from "drizzle-orm"; | ||
async function rawGetUser(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
return Drizzle.sql.raw(`select * from users where id = ${id}`); | ||
} | ||
|
||
async function sqlEscapedGetUser(id) { | ||
// ok: ban-drizzle-sql-raw | ||
return Drizzle.sql.execute(sql`select * from users where id = ${id}`); | ||
} | ||
|
||
/** | ||
* CommonJS import Drizzle | ||
*/ | ||
|
||
const Drizzle = require("drizzle-orm"); | ||
async function rawGetUser(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
return Drizzle.sql.raw(`select * from users where id = ${id}`); | ||
} | ||
|
||
async function sqlEscapedGetUser(id) { | ||
// ok: ban-drizzle-sql-raw | ||
return Drizzle.sql.execute(sql`select * from users where id = ${id}`); | ||
} | ||
|
||
async function rawGetUserInTemplateLiteralsVariable(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
const query = `select * from users where id = ${id}`; | ||
return Drizzle.sql.raw(query); | ||
} | ||
|
||
async function rawGetUserWithPlusOperator(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
const query = "select * from users where id = " + id; | ||
return Drizzle.sql.raw(query); | ||
} | ||
|
||
async function rawGetUserWithPlusOperatorVariable(id) { | ||
// ruleid: ban-drizzle-sql-raw | ||
return Drizzle.sql.raw("select * from users where id = " + id); | ||
} | ||
|
||
async function rawGetUserWithoutInjection() { | ||
// ok: ban-drizzle-sql-raw | ||
return Drizzle.sql.raw("select * from users where id = 1"); | ||
} | ||
|
91 changes: 91 additions & 0 deletions
91
javascript/drizzle-orm/security/audit/ban-drizzle-sql-raw.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
rules: | ||
- id: ban-drizzle-sql-raw | ||
message: | ||
Use of sql.raw() with SQL string concatenation is not allowed as it may lead to SQL injection. | ||
Use parameterized queries with Drizzle's query builders instead. | ||
fix: "/* Use sql`...` template literals or parameterized queries instead */" | ||
metadata: | ||
category: security | ||
references: | ||
- https://orm.drizzle.team/docs/sql#sqlraw | ||
technology: | ||
- javascript | ||
- typescript | ||
- drizzle-orm | ||
cwe: | ||
- "CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')" | ||
subcategory: | ||
- audit | ||
likelihood: LOW | ||
impact: HIGH | ||
confidence: LOW | ||
languages: | ||
- javascript | ||
- typescript | ||
severity: ERROR | ||
pattern-either: | ||
- patterns: | ||
- pattern-either: | ||
- pattern-inside: | | ||
const { sql } = require('drizzle-orm') | ||
... | ||
- pattern-inside: | | ||
import { sql } from 'drizzle-orm' | ||
... | ||
- pattern-either: | ||
- pattern: | | ||
sql.raw(`...${...}...`) | ||
- pattern: | | ||
$QUERY = `...${...}...` | ||
... | ||
sql.raw($QUERY) | ||
- pattern: | | ||
$QUERY = $SQL + $VALUE | ||
... | ||
sql.raw($QUERY) | ||
- pattern: | | ||
sql.raw($SQL + $VALUE) | ||
|
||
- patterns: | ||
- pattern-either: | ||
- pattern-inside: | | ||
import { sql as $ALIAS } from 'drizzle-orm' | ||
... | ||
- pattern-inside: | | ||
const { sql: $ALIAS } = require('drizzle-orm') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
... | ||
- pattern-either: | ||
- pattern: | | ||
$ALIAS.raw(`...${...}...`) | ||
- pattern: | | ||
$QUERY = `...${...}...` | ||
... | ||
$ALIAS.raw($QUERY) | ||
- pattern: | | ||
$QUERY = $SQL + $VALUE | ||
... | ||
$ALIAS.raw($QUERY) | ||
- pattern: | | ||
$ALIAS.raw($SQL + $VALUE) | ||
|
||
- patterns: | ||
- pattern-either: | ||
- pattern-inside: | | ||
const $DRIZZLE = require('drizzle-orm') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
... | ||
- pattern-inside: | | ||
import $DRIZZLE from 'drizzle-orm' | ||
... | ||
- pattern-either: | ||
- pattern: | | ||
$DRIZZLE.sql.raw(`...${...}...`) | ||
- pattern: | | ||
$QUERY = `...${...}...` | ||
... | ||
$DRIZZLE.sql.raw($QUERY) | ||
- pattern: | | ||
$QUERY = $SQL + $VALUE | ||
... | ||
$DRIZZLE.sql.raw($QUERY) | ||
- pattern: | | ||
$DRIZZLE.sql.raw($SQL + $VALUE) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explicit
const/var/let
here necessitates coverage of the other two, unless you are certain that this will only ever be aconst
import. Not making this change will cause CI to throw linter errors on this rule.