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