Skip to content

Commit 17b9fa1

Browse files
authored
generalize sonar urls (#600)
* generalize sonar urls * use sonar rule for testing
1 parent d8c0054 commit 17b9fa1

24 files changed

+36
-34
lines changed

src/core_codemods/sonar/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from codemodder.context import CodemodExecutionContext
88
from codemodder.result import ResultSet
99
from core_codemods.api.core_codemod import CoreCodemod, SASTCodemod
10-
from core_codemods.sonar.results import SonarResultSet
10+
from core_codemods.sonar.results import SonarResultSet, sonar_url_from_id
1111

1212

1313
class SonarCodemod(SASTCodemod):
@@ -22,9 +22,9 @@ def from_core_codemod(
2222
other: CoreCodemod,
2323
rule_id: str,
2424
rule_name: str,
25-
rule_url: str,
2625
transformer: BaseTransformerPipeline | None = None,
2726
):
27+
rule_url = sonar_url_from_id(rule_id)
2828
return SonarCodemod(
2929
metadata=Metadata(
3030
name=name,

src/core_codemods/sonar/results.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
from codemodder.result import LineInfo, Location, ResultSet, SASTResult
1212

1313

14+
def sonar_url_from_id(rule_id: str) -> str:
15+
# convert "python:SXXX" or "pythonsecurity:SXXX" to XXX
16+
try:
17+
rule_id = rule_id.split(":")[1][1:]
18+
except IndexError:
19+
logger.debug("Invalid sonar rule id: %s", rule_id)
20+
raise
21+
22+
return f"https://rules.sonarsource.com/python/RSPEC-{rule_id}/"
23+
24+
1425
class SonarLocation(Location):
1526
@classmethod
1627
def from_json_location(cls, json_location) -> Self:
@@ -48,7 +59,7 @@ def from_result(cls, result: dict) -> Self:
4859
rule=Rule(
4960
id=rule_id,
5061
name=rule_id,
51-
url=None,
62+
url=sonar_url_from_id(rule_id),
5263
),
5364
),
5465
)

src/core_codemods/sonar/sonar_break_or_continue_out_of_loop.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66
other=BreakOrContinueOutOfLoop,
77
rule_id="python:S1716",
88
rule_name='"break" and "continue" should not be used outside a loop',
9-
rule_url="https://rules.sonarsource.com/python/RSPEC-1716/",
109
)

src/core_codemods/sonar/sonar_disable_graphql_introspection.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66
other=DisableGraphQLIntrospection,
77
rule_id="python:S6786",
88
rule_name="GraphQL introspection should be disabled in production",
9-
rule_url="https://rules.sonarsource.com/python/RSPEC-6786/",
109
)

src/core_codemods/sonar/sonar_django_json_response_type.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66
other=DjangoJsonResponseType,
77
rule_id="pythonsecurity:S5131",
88
rule_name="Endpoints should not be vulnerable to reflected XSS attacks (Django)",
9-
rule_url="https://rules.sonarsource.com/python/type/Bug/RSPEC-5131/",
109
)

src/core_codemods/sonar/sonar_django_model_without_dunder_str.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66
other=DjangoModelWithoutDunderStr,
77
rule_id="python:S6554",
88
rule_name='Django models should define a "__str__" method',
9-
rule_url="https://rules.sonarsource.com/python/RSPEC-6554/",
109
)

src/core_codemods/sonar/sonar_django_receiver_on_top.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66
other=DjangoReceiverOnTop,
77
rule_id="python:S6552",
88
rule_name="Django signal handler functions should have the `@receiver` decorator on top of all other decorators",
9-
rule_url="https://rules.sonarsource.com/python/type/Bug/RSPEC-6552/",
109
)

src/core_codemods/sonar/sonar_enable_jinja2_autoescape.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66
other=EnableJinja2Autoescape,
77
rule_id="python:S5247",
88
rule_name="Disabling auto-escaping in template engines is security-sensitive",
9-
rule_url="https://rules.sonarsource.com/python/RSPEC-5247/",
109
)

src/core_codemods/sonar/sonar_exception_without_raise.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66
other=ExceptionWithoutRaise,
77
rule_id="python:S3984",
88
rule_name="Exceptions should not be created without being raised",
9-
rule_url="https://rules.sonarsource.com/python/type/Bug/RSPEC-3984/",
109
)

src/core_codemods/sonar/sonar_fix_assert_tuple.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66
other=FixAssertTuple,
77
rule_id="python:S5905",
88
rule_name="Assert should not be called on a tuple literal",
9-
rule_url="https://rules.sonarsource.com/python/type/Bug/RSPEC-5905/",
109
)

0 commit comments

Comments
 (0)