-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds sonar version of SQLParameterizer codemod (#495)
* Fixed issue with multiple class with injections * Sonar sql parameterization codemod with tests
- Loading branch information
1 parent
d16ff03
commit a2a1eb0
Showing
9 changed files
with
2,311 additions
and
2,045 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
integration_tests/sonar/test_sonar_sql_parameterization.py
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,28 @@ | ||
from codemodder.codemods.test import SonarIntegrationTest | ||
from core_codemods.sonar.sonar_sql_parameterization import SonarSQLParameterization | ||
from core_codemods.sql_parameterization import SQLQueryParameterizationTransformer | ||
|
||
|
||
class TestSonarSQLParameterization(SonarIntegrationTest): | ||
codemod = SonarSQLParameterization | ||
code_path = "tests/samples/fix_sonar_sql_parameterization.py" | ||
replacement_lines = [ | ||
(11, ' sql = """SELECT user FROM users WHERE user = ?"""\n'), | ||
(14, " conn.cursor().execute(sql, ((user), ))\n"), | ||
] | ||
expected_diff = """\ | ||
--- | ||
+++ | ||
@@ -8,7 +8,7 @@ | ||
@app.route("/example") | ||
def f(): | ||
user = request.args["user"] | ||
- sql = \"\"\"SELECT user FROM users WHERE user = \\'%s\\'\"\"\" | ||
+ sql = \"\"\"SELECT user FROM users WHERE user = ?\"\"\" | ||
conn = sqlite3.connect("example") | ||
- conn.cursor().execute(sql % (user)) | ||
+ conn.cursor().execute(sql, ((user), )) | ||
""" | ||
expected_line_change = "14" | ||
change_description = SQLQueryParameterizationTransformer.change_description |
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
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,10 @@ | ||
from core_codemods.sonar.api import SonarCodemod | ||
from core_codemods.sql_parameterization import SQLQueryParameterization | ||
|
||
SonarSQLParameterization = SonarCodemod.from_core_codemod( | ||
name="sql-parameterization-S3649", | ||
other=SQLQueryParameterization, | ||
rule_id="pythonsecurity:S3649", | ||
rule_name="Database queries should not be vulnerable to injection attacks", | ||
rule_url="https://rules.sonarsource.com/python/RSPEC-3649/", | ||
) |
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,60 @@ | ||
import json | ||
|
||
from codemodder.codemods.test import BaseSASTCodemodTest | ||
from core_codemods.sonar.sonar_sql_parameterization import SonarSQLParameterization | ||
|
||
|
||
class TestSonarSQLParameterization(BaseSASTCodemodTest): | ||
codemod = SonarSQLParameterization | ||
tool = "sonar" | ||
|
||
def test_name(self): | ||
assert self.codemod.name == "sql-parameterization-S3649" | ||
|
||
def test_simple(self, tmpdir): | ||
input_code = """ | ||
import sqlite3 | ||
from flask import Flask, request | ||
app = Flask(__name__) | ||
@app.route("/example") | ||
def f(): | ||
user = request.args["user"] | ||
sql = '''SELECT user FROM users WHERE user = \'%s\' ''' | ||
conn = sqlite3.connect("example") | ||
conn.cursor().execute(sql % (user)) | ||
""" | ||
expected = """ | ||
import sqlite3 | ||
from flask import Flask, request | ||
app = Flask(__name__) | ||
@app.route("/example") | ||
def f(): | ||
user = request.args["user"] | ||
sql = '''SELECT user FROM users WHERE user = ? ''' | ||
conn = sqlite3.connect("example") | ||
conn.cursor().execute(sql, ((user), )) | ||
""" | ||
issues = { | ||
"issues": [ | ||
{ | ||
"rule": "pythonsecurity:S3649", | ||
"status": "OPEN", | ||
"component": "code.py", | ||
"textRange": { | ||
"startLine": 14, | ||
"endLine": 14, | ||
"startOffset": 4, | ||
"endOffset": 39, | ||
}, | ||
} | ||
] | ||
} | ||
self.run_and_assert(tmpdir, input_code, expected, results=json.dumps(issues)) |
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,14 @@ | ||
import sqlite3 | ||
|
||
from flask import Flask, request | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
@app.route("/example") | ||
def f(): | ||
user = request.args["user"] | ||
sql = """SELECT user FROM users WHERE user = \'%s\'""" | ||
|
||
conn = sqlite3.connect("example") | ||
conn.cursor().execute(sql % (user)) |
Oops, something went wrong.