-
-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve detecting SQL injections in f-strings (#917)
* Improve detecting SQL injections in f-strings This commit fixes detecting SQL injection in statements like: f"SELECT {column_name} FROM foo WHERE id = 1" f"INSERT INTO {table_name} VALUES (1)" f"UPDATE {table_name} SET id = 1" Before this change, the bandit was analyzing statements by parts, especially, in the case of: "SELECT {column_name} FROM foo WHERE id = 1" it was firstly checking "SELECT " for being an SQL statement and then " FROM foo WHERE id = 1". Neither of these parts match to defined regular expressions: r"(select\s.*from\s|" r"delete\s+from\s|" r"insert\s+into\s.*values\s|" r"update\s.*set\s)", Thus SQL injection was not detected. This commit makes bandit checking the whole SQL statement for matching the above regexps. Resolves: #916 * Add tests for multiline strings
- Loading branch information
Showing
4 changed files
with
165 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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,123 @@ | ||
import sqlalchemy | ||
|
||
# bad | ||
query = """SELECT * | ||
FROM foo WHERE id = '%s'""" % identifier | ||
query = """INSERT INTO foo | ||
VALUES ('a', 'b', '%s')""" % value | ||
query = """DELETE FROM foo | ||
WHERE id = '%s'""" % identifier | ||
query = """UPDATE foo | ||
SET value = 'b' | ||
WHERE id = '%s'""" % identifier | ||
query = """WITH cte AS (SELECT x FROM foo) | ||
SELECT x FROM cte WHERE x = '%s'""" % identifier | ||
# bad alternate forms | ||
query = """SELECT * | ||
FROM foo | ||
WHERE id = '""" + identifier + "'" | ||
query = """SELECT * | ||
FROM foo | ||
WHERE id = '{}'""".format(identifier) | ||
|
||
query = f""" | ||
SELECT * | ||
FROM foo | ||
WHERE id = {identifier} | ||
""" | ||
|
||
# bad | ||
cur.execute("""SELECT * | ||
FROM foo | ||
WHERE id = '%s'""" % identifier) | ||
cur.execute("""INSERT INTO foo | ||
VALUES ('a', 'b', '%s')""" % value) | ||
cur.execute("""DELETE FROM foo | ||
WHERE id = '%s'""" % identifier) | ||
cur.execute("""UPDATE foo | ||
SET value = 'b' | ||
WHERE id = '%s'""" % identifier) | ||
# bad alternate forms | ||
cur.execute("""SELECT * | ||
FROM foo | ||
WHERE id = '""" + identifier + "'") | ||
cur.execute("""SELECT * | ||
FROM foo | ||
WHERE id = '{}'""".format(identifier)) | ||
|
||
# bad with f-string | ||
query = f""" | ||
SELECT * | ||
FROM foo | ||
WHERE id = {identifier} | ||
""" | ||
query = f""" | ||
SELECT * | ||
FROM foo | ||
WHERE id = {identifier} | ||
""" | ||
|
||
query = f""" | ||
SELECT * | ||
FROM foo | ||
WHERE id = {identifier}""" | ||
query = f""" | ||
SELECT * | ||
FROM foo | ||
WHERE id = {identifier}""" | ||
|
||
cur.execute(f""" | ||
SELECT | ||
{column_name} | ||
FROM foo | ||
WHERE id = 1""") | ||
|
||
cur.execute(f""" | ||
SELECT | ||
{a + b} | ||
FROM foo | ||
WHERE id = 1""") | ||
|
||
cur.execute(f""" | ||
INSERT INTO | ||
{table_name} | ||
VALUES (1)""") | ||
cur.execute(f""" | ||
UPDATE {table_name} | ||
SET id = 1""") | ||
|
||
# implicit concatenation mixed with f-strings | ||
cur.execute("SELECT " | ||
f"{column_name} " | ||
"FROM foo " | ||
"WHERE id = 1" | ||
) | ||
cur.execute("INSERT INTO " | ||
f"{table_name} " | ||
"VALUES (1)") | ||
cur.execute(f"UPDATE {table_name} " | ||
"SET id = 1") | ||
|
||
# good | ||
cur.execute("""SELECT * | ||
FROM foo | ||
WHERE id = '%s'""", identifier) | ||
cur.execute("""INSERT INTO foo | ||
VALUES ('a', 'b', '%s')""", value) | ||
cur.execute("""DELETE FROM foo | ||
WHERE id = '%s'""", identifier) | ||
cur.execute("""UPDATE foo | ||
SET value = 'b' | ||
WHERE id = '%s'""", identifier) | ||
|
||
|
||
# bug: https://bugs.launchpad.net/bandit/+bug/1479625 | ||
def a(): | ||
def b(): | ||
pass | ||
|
||
return b | ||
|
||
|
||
a()("""SELECT %s | ||
FROM foo""" % val) |
This file contains 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 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