-
Notifications
You must be signed in to change notification settings - Fork 19
fix: don't flag parameterized cursor.execute() as SQL injection #39
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,8 @@ | |
| mode: "search", | ||
| patterns: Some([ | ||
| "execute_raw_query(", | ||
| "*cursor.execute(*%*", | ||
| // Match the % operator after a complete query expression, not DB-API placeholders. | ||
| "regex:(?s)cursor\\.execute\\(\\s*\\(*\\s*(?:(?:[rRuUbBfF]{0,2})?(?:\"\"\".*?\"\"\"|'''.*?'''|\"(?:\\\\.|[^\"\\\\])*\"|'(?:\\\\.|[^'\\\\])*')|[A-Za-z_][A-Za-z0-9_.]*)\\s*\\)*\\s*%\\s*[^\\s,)]", | ||
|
Contributor
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. The trailing |
||
| "*cursor.execute(*.format(*", | ||
| "*cursor.execute(f\"*", | ||
| "*cursor.execute(f'*", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| def load_user(cursor, user_id): | ||
| cursor.execute("SELECT * FROM users WHERE id = " + user_id) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| def load_user(cursor, user_id): | ||
| cursor.execute("SELECT * FROM users WHERE id = {}".format(user_id)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| def load_user(cursor, user_id): | ||
| cursor.execute(f"SELECT * FROM users WHERE id = {user_id}") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| def load_firms(cursor, firm_id, firm_name): | ||
| cursor.execute( | ||
| "SELECT * FROM firms WHERE firm_id = %s", | ||
| [firm_id], | ||
| ) | ||
| cursor.execute( | ||
| "SELECT * FROM firms WHERE name = %(name)s", | ||
| {"name": firm_name}, | ||
| ) | ||
| cursor.execute( | ||
| "SELECT * FROM firms WHERE name LIKE %s", | ||
| [f"%{firm_name}%"], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| def load_user(cursor, user_id): | ||
| cursor.execute("SELECT * FROM users WHERE id = %s" % user_id) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| def load_user(cursor, user_id): | ||
|
Contributor
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. Can we add these to the same sql test file for readability and coverages? like:
Contributor
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. We are cleaning up test folder structures starting from python and PR is up here fyi #43
Contributor
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. #43 is now merged. If there's a new fixture files for test cases, can you move them under https://github.com/Corgea/Sighthound/tree/main/tests/test_files/python/fixtures ? |
||
| cursor.execute("SELECT * FROM users WHERE id = %s"%user_id) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| def load_user(cursor, query, user_id): | ||
| cursor.execute(query % user_id) |
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.
We try to avoid very complex regex like this as it may become too specific and hard to understand it.
We could have separate AST rule here instead.
As an example: