Skip to content

Commit d287a0a

Browse files
committed
Integration tests for use-secure-protocols
1 parent 448a913 commit d287a0a

File tree

5 files changed

+72
-8
lines changed

5 files changed

+72
-8
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from codemodder.codemods.test import SonarIntegrationTest
2+
from core_codemods.sonar.sonar_use_secure_protocols import (
3+
SonarUseSecureProtocols,
4+
SonarUseSecureProtocolsTransformer,
5+
)
6+
7+
8+
class TestSonarUseSecureProtocols(SonarIntegrationTest):
9+
codemod = SonarUseSecureProtocols
10+
code_path = "tests/samples/use_secure_protocols.py"
11+
replacement_lines = [
12+
(
13+
5,
14+
"""url = "https://example.com"\n""",
15+
),
16+
]
17+
# fmt: off
18+
expected_diff = (
19+
"""--- \n"""
20+
"""+++ \n"""
21+
"""@@ -2,4 +2,4 @@\n"""
22+
''' import smtplib\n'''
23+
''' import telnetlib\n'''
24+
''' \n'''
25+
'''-url = "http://example.com"\n'''
26+
'''+url = "https://example.com"\n'''
27+
)
28+
# fmt: on
29+
expected_line_change = "5"
30+
change_description = SonarUseSecureProtocolsTransformer.change_description

src/codemodder/codemods/test/integration_utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,9 @@ def _assert_run_fields(self, run, output_path):
9999
assert run[
100100
"commandLine"
101101
] == f'codemodder {self.code_dir} --output {output_path} --codemod-include={self.codemod_instance.id} --path-include={self.code_filename} --path-exclude=""' + (
102-
f" --sonar-issues-json={self.sonar_issues_json}"
103-
if self.sonar_issues_json
104-
else ""
102+
f" --sonar-json={self.sonar_issues_json}" if self.sonar_issues_json else ""
105103
) + (
106-
f" --sonar-hotspots-json={self.sonar_hotspots_json}"
104+
f" --sonar-json={self.sonar_hotspots_json}"
107105
if self.sonar_hotspots_json
108106
else ""
109107
)
@@ -142,6 +140,7 @@ def _assert_results_fields(self, results, output_path):
142140
change = [
143141
result for result in result["changeset"] if result["path"] == output_path
144142
][0]
143+
print(change["diff"])
145144
assert change["path"] == output_path
146145
assert change["diff"] == self.expected_diff
147146

@@ -197,9 +196,9 @@ def test_file_rewritten(self, codetf_schema):
197196
]
198197

199198
if self.sonar_issues_json:
200-
command.append(f"--sonar-issues-json={self.sonar_issues_json}")
199+
command.append(f"--sonar-json={self.sonar_issues_json}")
201200
if self.sonar_hotspots_json:
202-
command.append(f"--sonar-hotspots-json={self.sonar_hotspots_json}")
201+
command.append(f"--sonar-json={self.sonar_hotspots_json}")
203202

204203
self.write_original_code()
205204
self.write_original_dependencies()

src/core_codemods/sonar/sonar_use_secure_protocols.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import libcst as cst
22
from libcst.codemod import CodemodContext
33

4-
from codemodder.codemods.base_codemod import Metadata, ReviewGuidance, ToolRule
4+
from codemodder.codemods.base_codemod import (
5+
Metadata,
6+
ReviewGuidance,
7+
ToolMetadata,
8+
ToolRule,
9+
)
510
from codemodder.codemods.libcst_transformer import (
611
LibcstResultTransformer,
712
LibcstTransformerPipeline,
@@ -188,7 +193,12 @@ def leave_SimpleString(
188193
),
189194
Reference(url="https://cwe.mitre.org/data/definitions/200"),
190195
Reference(url="https://cwe.mitre.org/data/definitions/319"),
191-
],
196+
]
197+
+ [Reference(url=tr.url or "", description=tr.name) for tr in rules],
198+
tool=ToolMetadata(
199+
name="Sonar",
200+
rules=rules,
201+
),
192202
),
193203
transformer=LibcstTransformerPipeline(SonarUseSecureProtocolsTransformer),
194204
default_extensions=[".py"],

tests/samples/sonar_hotspots.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,26 @@
128128
},
129129
"flows": [],
130130
"ruleKey": "python:S5247"
131+
},
132+
{
133+
"key": "AZSN_hIp0UcGAUz9sZqH",
134+
"component": "pixee_codemodder-python:use_secure_protocols.py",
135+
"project": "pixee_codemodder-python",
136+
"securityCategory": "encrypt-data",
137+
"vulnerabilityProbability": "LOW",
138+
"status": "TO_REVIEW",
139+
"line": 5,
140+
"message": "Using http protocol is insecure. Use https instead",
141+
"creationDate": "2025-01-22T13:20:10+0100",
142+
"updateDate": "2025-01-22T13:29:45+0100",
143+
"textRange": {
144+
"startLine": 5,
145+
"endLine": 5,
146+
"startOffset": 6,
147+
"endOffset": 26
148+
},
149+
"flows": [],
150+
"ruleKey": "python:S5332"
131151
}
132152
],
133153
"components": [
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import ftplib
2+
import smtplib
3+
import telnetlib
4+
5+
url = "http://example.com"

0 commit comments

Comments
 (0)