-
-
Notifications
You must be signed in to change notification settings - Fork 667
Detect misuse of psycopg2.sql.SQL
composable
#608
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
wtkm11
wants to merge
12
commits into
PyCQA:main
Choose a base branch
from
wtkm11:psycopg2-sql-misuse
base: main
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 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
934dfeb
Detect misuse of `psycopg2.sql.SQL` composable
wtkm11 2b18f60
Add functional test for psycopg2 sql injection plugin test
wtkm11 24eef31
Merge branch 'master' into psycopg2-sql-misuse
ericwb 0700453
Apply suggestions from code review
ericwb 9b627c9
Merge branch 'main' into psycopg2-sql-misuse
ericwb a73cb45
Apply suggestions from code review
ericwb 54ce5d1
Apply suggestions from code review
ericwb e674fa2
Update bandit/plugins/psycopg2_sql_injection.py
ericwb 17ddf47
Update bandit/plugins/psycopg2_sql_injection.py
ericwb 84227c7
Update bandit/plugins/psycopg2_sql_injection.py
ericwb 9bbff0a
Apply suggestions from code review
ericwb 55a1aac
Merge branch 'main' into psycopg2-sql-misuse
ericwb 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
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
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,40 @@ | ||
# -*- coding:utf-8 -*- | ||
# | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import ast | ||
|
||
import bandit | ||
from bandit.core import test_properties as test | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
@test.checks('Call') | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@test.test_id('B612') | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def psycopg2_sql_injection(context): | ||
"""**B612: Potential SQL injection on psycopg2 raw SQL composable object ** | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The `psycopg2.sql.SQL` composable object should not be used to represent | ||
variable identifiers or values that may be controlled by an attacker since | ||
the argument that is passed to the `SQL` constructor is not escaped when | ||
the SQL statement is composed. Instead, `SQL` should only be used to | ||
represent constant strings. | ||
|
||
.. seealso:: | ||
|
||
- https://www.psycopg.org/docs/sql.html | ||
|
||
.. versionadded:: 1.6.2 | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
if context.is_module_imported_like('psycopg2.sql'): | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if context.call_function_name == 'SQL': | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
argument = context.node.args[0] | ||
if not isinstance(argument, ast.Str): | ||
return bandit.Issue( | ||
severity=bandit.MEDIUM, | ||
confidence=bandit.MEDIUM, | ||
text=( | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Possible SQL injection vector through instantiation " | ||
"of psycopg2.sql.SQL composable object on an argument " | ||
"other than a string literal." | ||
) | ||
ericwb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) |
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,4 @@ | ||
from psycopg2 import sql | ||
|
||
table = 'users; drop table users; --' | ||
sql.SQL('select * from {}').format(sql.SQL(table)) |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.