|
| 1 | +from textwrap import dedent |
| 2 | + |
| 3 | +import libcst as cst |
| 4 | +from libcst.codemod import ContextAwareVisitor |
| 5 | + |
| 6 | +from codemodder.codemods.base_codemod import ( |
| 7 | + Metadata, |
| 8 | + ReviewGuidance, |
| 9 | + ToolMetadata, |
| 10 | + ToolRule, |
| 11 | +) |
| 12 | +from codemodder.codemods.base_visitor import UtilsMixin |
| 13 | +from codemodder.codemods.libcst_transformer import ( |
| 14 | + LibcstResultTransformer, |
| 15 | + LibcstTransformerPipeline, |
| 16 | +) |
| 17 | +from codemodder.codemods.semgrep import SemgrepSarifFileDetector |
| 18 | +from core_codemods.semgrep.api import SemgrepCodemod, semgrep_url_from_id |
| 19 | + |
| 20 | + |
| 21 | +class NanInjectionTransformer(LibcstResultTransformer): |
| 22 | + change_description = "Add validation to untrusted numerical input to disallow `nan`" |
| 23 | + |
| 24 | + def leave_SimpleStatementLine( |
| 25 | + self, |
| 26 | + original_node: cst.SimpleStatementLine, |
| 27 | + updated_node: cst.SimpleStatementLine, |
| 28 | + ): |
| 29 | + |
| 30 | + visitor = MatchNodesInLineVisitor( |
| 31 | + self.context, file_context=self.file_context, results=self.results |
| 32 | + ) |
| 33 | + original_node.body[0].visit(visitor) |
| 34 | + if visitor.matched_nodes: |
| 35 | + # For now only handle one matched Call node in a line |
| 36 | + return self.replace_with_if_else( |
| 37 | + visitor.matched_nodes[0], original_node, updated_node |
| 38 | + ) |
| 39 | + return original_node |
| 40 | + |
| 41 | + def replace_with_if_else( |
| 42 | + self, |
| 43 | + node: cst.Call, |
| 44 | + original_node: cst.SimpleStatementLine, |
| 45 | + updated_node: cst.SimpleStatementLine, |
| 46 | + ): |
| 47 | + if not (target_node := self._get_target_in_call(node)): |
| 48 | + return original_node |
| 49 | + |
| 50 | + code = dedent( |
| 51 | + f"""\ |
| 52 | + if {self.code(target_node).strip()}.lower() == "nan": |
| 53 | + raise ValueError |
| 54 | + else: |
| 55 | + {self.code(original_node).strip()} |
| 56 | + """ |
| 57 | + ) |
| 58 | + self._report_new_lines(original_node) |
| 59 | + new_statement = cst.parse_statement(code) |
| 60 | + return new_statement.with_changes(leading_lines=updated_node.leading_lines) |
| 61 | + |
| 62 | + def _get_target_in_call(self, node: cst.Call) -> cst.CSTNode: |
| 63 | + match (wrapped_node := node.args[0].value): |
| 64 | + case cst.Name(): |
| 65 | + # float(var) |
| 66 | + return wrapped_node |
| 67 | + |
| 68 | + case cst.Call( |
| 69 | + func=cst.Name("float") | cst.Name("bool") | cst.Name("complex") |
| 70 | + ): |
| 71 | + # bool(float(var)), complex(float(var)), bool(float(var)), etc |
| 72 | + return self._get_target_in_call(wrapped_node) |
| 73 | + case cst.Call() | cst.BinaryOperation(): |
| 74 | + return wrapped_node |
| 75 | + |
| 76 | + def _report_new_lines(self, original_node: cst.SimpleStatementLine): |
| 77 | + self.report_change(original_node) |
| 78 | + line_number = self.lineno_for_node(original_node) |
| 79 | + findings = self.file_context.get_findings_for_location(line_number) |
| 80 | + for lineno in range(line_number + 1, line_number + 4): |
| 81 | + self.report_change_for_line(lineno, findings=findings) |
| 82 | + |
| 83 | + |
| 84 | +class MatchNodesInLineVisitor(ContextAwareVisitor, UtilsMixin): |
| 85 | + """Visit Call nodes and match if node location matches results.""" |
| 86 | + |
| 87 | + def __init__( |
| 88 | + self, |
| 89 | + context, |
| 90 | + file_context, |
| 91 | + results, |
| 92 | + ) -> None: |
| 93 | + self.file_context = file_context |
| 94 | + ContextAwareVisitor.__init__(self, context) |
| 95 | + UtilsMixin.__init__( |
| 96 | + self, |
| 97 | + results=results, |
| 98 | + line_include=file_context.line_include, |
| 99 | + line_exclude=file_context.line_exclude, |
| 100 | + ) |
| 101 | + |
| 102 | + self.matched_nodes: list[cst.Call] = [] |
| 103 | + |
| 104 | + def visit_Call(self, node: cst.Call) -> None: |
| 105 | + if self.node_is_selected(node): |
| 106 | + self.matched_nodes.append(node) |
| 107 | + |
| 108 | + |
| 109 | +SemgrepNanInjection = SemgrepCodemod( |
| 110 | + metadata=Metadata( |
| 111 | + name="nan-injection", |
| 112 | + summary=NanInjectionTransformer.change_description.title(), |
| 113 | + description=NanInjectionTransformer.change_description.title(), |
| 114 | + review_guidance=ReviewGuidance.MERGE_AFTER_CURSORY_REVIEW, |
| 115 | + tool=ToolMetadata( |
| 116 | + name="Semgrep", |
| 117 | + rules=[ |
| 118 | + ToolRule( |
| 119 | + id=( |
| 120 | + rule_id := "python.django.security.nan-injection.nan-injection" |
| 121 | + ), |
| 122 | + name="nan-injection", |
| 123 | + url=semgrep_url_from_id(rule_id), |
| 124 | + ) |
| 125 | + ], |
| 126 | + ), |
| 127 | + references=[], |
| 128 | + ), |
| 129 | + transformer=LibcstTransformerPipeline(NanInjectionTransformer), |
| 130 | + detector=SemgrepSarifFileDetector(), |
| 131 | + requested_rules=[rule_id], |
| 132 | +) |
0 commit comments