|
| 1 | +import libcst as cst |
| 2 | +from libcst.codemod import CodemodContext |
| 3 | + |
| 4 | +from codemodder.codemods.base_codemod import ( |
| 5 | + Metadata, |
| 6 | + ReviewGuidance, |
| 7 | + ToolMetadata, |
| 8 | + ToolRule, |
| 9 | +) |
| 10 | +from codemodder.codemods.libcst_transformer import ( |
| 11 | + LibcstResultTransformer, |
| 12 | + LibcstTransformerPipeline, |
| 13 | +) |
| 14 | +from codemodder.codemods.utils_mixin import NameAndAncestorResolutionMixin |
| 15 | +from codemodder.codetf import Reference |
| 16 | +from codemodder.file_context import FileContext |
| 17 | +from codemodder.result import Result |
| 18 | +from core_codemods.sonar.api import SonarCodemod |
| 19 | + |
| 20 | +rules = [ |
| 21 | + ToolRule( |
| 22 | + id="python:S5332", |
| 23 | + name="Using clear-text protocols is security-sensitive", |
| 24 | + url="https://rules.sonarsource.com/python/RSPEC-5332/", |
| 25 | + ), |
| 26 | +] |
| 27 | + |
| 28 | + |
| 29 | +class SonarUseSecureProtocolsTransformer( |
| 30 | + LibcstResultTransformer, NameAndAncestorResolutionMixin |
| 31 | +): |
| 32 | + change_description = "Modified URLs or calls to use secure protocols" |
| 33 | + |
| 34 | + def __init__( |
| 35 | + self, |
| 36 | + context: CodemodContext, |
| 37 | + results: list[Result] | None, |
| 38 | + file_context: FileContext, |
| 39 | + _transformer: bool = False, |
| 40 | + ): |
| 41 | + self.nodes_memory_with_context_name: dict[cst.CSTNode, str] = {} |
| 42 | + super().__init__(context, results, file_context, _transformer) |
| 43 | + |
| 44 | + def _match_and_handle_statement( |
| 45 | + self, possible_smtp_call, original_node_statement, updated_node_statement |
| 46 | + ): |
| 47 | + maybe_name = self.find_base_name(possible_smtp_call) |
| 48 | + match possible_smtp_call: |
| 49 | + case cst.Call() if maybe_name == "smtplib.SMTP": |
| 50 | + # get the stored context_name or create a new one: |
| 51 | + if possible_smtp_call in self.nodes_memory_with_context_name: |
| 52 | + context_name = self.nodes_memory_with_context_name[ |
| 53 | + possible_smtp_call |
| 54 | + ] |
| 55 | + else: |
| 56 | + context_name = self.generate_available_name( |
| 57 | + original_node_statement, ["smtp_context"] |
| 58 | + ) |
| 59 | + |
| 60 | + new_statements = [] |
| 61 | + new_statements.append( |
| 62 | + cst.parse_statement( |
| 63 | + f"{context_name} = ssl.create_default_context()" |
| 64 | + ) |
| 65 | + ) |
| 66 | + new_statements.append( |
| 67 | + cst.parse_statement( |
| 68 | + f"{context_name}.verify_mode = ssl.CERT_REQUIRED" |
| 69 | + ) |
| 70 | + ) |
| 71 | + new_statements.append( |
| 72 | + cst.parse_statement(f"{context_name}.check_hostname = True") |
| 73 | + ) |
| 74 | + new_statements.append(updated_node_statement) |
| 75 | + # don't append this if we changed the call to SSL version |
| 76 | + if possible_smtp_call in self.nodes_memory_with_context_name: |
| 77 | + self.nodes_memory_with_context_name.pop(possible_smtp_call) |
| 78 | + else: |
| 79 | + new_statements.append( |
| 80 | + cst.parse_statement(f"smtplib.starttls(context={context_name})") |
| 81 | + ) |
| 82 | + self.add_needed_import("smtplib") |
| 83 | + self.add_needed_import("ssl") |
| 84 | + self.report_change(possible_smtp_call) |
| 85 | + return cst.FlattenSentinel(new_statements) |
| 86 | + return updated_node_statement |
| 87 | + |
| 88 | + def leave_SimpleStatementLine(self, original_node, updated_node): |
| 89 | + match original_node.body: |
| 90 | + # match the first statement that is either selected or is an assignment whose value is selected |
| 91 | + case [cst.Assign() as a, *_] if self.node_is_selected(a.value): |
| 92 | + return self._match_and_handle_statement( |
| 93 | + a.value, original_node, updated_node |
| 94 | + ) |
| 95 | + case [s, *_] if self.node_is_selected(s): |
| 96 | + return self._match_and_handle_statement(s, original_node, updated_node) |
| 97 | + return updated_node |
| 98 | + |
| 99 | + def leave_Call(self, original_node, updated_node): |
| 100 | + if self.node_is_selected(original_node): |
| 101 | + match self.find_base_name(original_node): |
| 102 | + case "ftplib.FTP": |
| 103 | + new_func = cst.parse_expression("ftplib.FTP_TLS") |
| 104 | + self.report_change(original_node) |
| 105 | + self.add_needed_import("ftplib") |
| 106 | + return updated_node.with_changes(func=new_func) |
| 107 | + # Just using ssl.create_default_context() may not be enough for older python versions |
| 108 | + # See https://stackoverflow.com/questions/33857698/sending-email-from-python-using-starttls |
| 109 | + case "smtplib.SMTP": |
| 110 | + # port is the second positional, check that |
| 111 | + maybe_port_value = ( |
| 112 | + original_node.args[1] |
| 113 | + if len(original_node.args) >= 2 |
| 114 | + and original_node.args[1].keyword is None |
| 115 | + else None |
| 116 | + ) |
| 117 | + # find port keyword, if any |
| 118 | + maybe_port_value = maybe_port_value or next( |
| 119 | + iter( |
| 120 | + [ |
| 121 | + a |
| 122 | + for a in original_node.args |
| 123 | + if a.keyword and a.keyword.value == "port" |
| 124 | + ] |
| 125 | + ), |
| 126 | + None, |
| 127 | + ) |
| 128 | + maybe_port_value = ( |
| 129 | + maybe_port_value.value if maybe_port_value else None |
| 130 | + ) |
| 131 | + match maybe_port_value: |
| 132 | + case None: |
| 133 | + return self._change_to_smtp_ssl(original_node, updated_node) |
| 134 | + case cst.Integer() if maybe_port_value.value == "0": |
| 135 | + return self._change_to_smtp_ssl(original_node, updated_node) |
| 136 | + return updated_node |
| 137 | + |
| 138 | + def _change_to_smtp_ssl(self, original_node, updated_node): |
| 139 | + # remember this node so we don't add the starttls |
| 140 | + new_func = cst.parse_expression("smtplib.SMTP_SSL") |
| 141 | + |
| 142 | + context_name = self.generate_available_name(original_node, ["smtp_context"]) |
| 143 | + self.nodes_memory_with_context_name[original_node] = context_name |
| 144 | + |
| 145 | + new_args = [ |
| 146 | + *original_node.args, |
| 147 | + cst.Arg( |
| 148 | + keyword=cst.Name("context"), |
| 149 | + value=cst.Name(context_name), |
| 150 | + ), |
| 151 | + ] |
| 152 | + return updated_node.with_changes(func=new_func, args=new_args) |
| 153 | + |
| 154 | + def leave_SimpleString( |
| 155 | + self, original_node: cst.SimpleString, updated_node: cst.SimpleString |
| 156 | + ) -> cst.BaseExpression: |
| 157 | + if self.node_is_selected(original_node): |
| 158 | + match original_node.raw_value: |
| 159 | + case original_node.raw_value as s if s.startswith("http"): |
| 160 | + self.report_change(original_node) |
| 161 | + return updated_node.with_changes( |
| 162 | + value=original_node.prefix |
| 163 | + + original_node.quote |
| 164 | + + s.replace("http", "https", 1) |
| 165 | + + original_node.quote |
| 166 | + ) |
| 167 | + case original_node.raw_value as s if s.startswith("ftp"): |
| 168 | + self.report_change(original_node) |
| 169 | + return updated_node.with_changes( |
| 170 | + value=original_node.prefix |
| 171 | + + original_node.quote |
| 172 | + + s.replace("ftp", "sftp", 1) |
| 173 | + + original_node.quote |
| 174 | + ) |
| 175 | + return updated_node |
| 176 | + |
| 177 | + |
| 178 | +SonarUseSecureProtocols = SonarCodemod( |
| 179 | + metadata=Metadata( |
| 180 | + name="use-secure-protocols", |
| 181 | + summary="Use encrypted protocols instead of clear-text", |
| 182 | + review_guidance=ReviewGuidance.MERGE_AFTER_CURSORY_REVIEW, |
| 183 | + references=[ |
| 184 | + Reference( |
| 185 | + url="https://docs.python.org/3/library/ftplib.html#ftplib.FTP_TLS" |
| 186 | + ), |
| 187 | + Reference( |
| 188 | + url="https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.starttls" |
| 189 | + ), |
| 190 | + Reference(url="https://owasp.org/Top10/A02_2021-Cryptographic_Failures/"), |
| 191 | + Reference( |
| 192 | + url="https://owasp.org/www-project-top-ten/2017/A3_2017-Sensitive_Data_Exposure" |
| 193 | + ), |
| 194 | + Reference(url="https://cwe.mitre.org/data/definitions/200"), |
| 195 | + Reference(url="https://cwe.mitre.org/data/definitions/319"), |
| 196 | + ] |
| 197 | + + [Reference(url=tr.url or "", description=tr.name) for tr in rules], |
| 198 | + tool=ToolMetadata( |
| 199 | + name="Sonar", |
| 200 | + rules=rules, |
| 201 | + ), |
| 202 | + ), |
| 203 | + transformer=LibcstTransformerPipeline(SonarUseSecureProtocolsTransformer), |
| 204 | + default_extensions=[".py"], |
| 205 | + requested_rules=[tr.id for tr in rules], |
| 206 | +) |
0 commit comments