-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecated sslcontext with no Arg (#105)
* support empty call * handle no args and use api * remove comments
- Loading branch information
1 parent
2bdc9dc
commit 9b80e96
Showing
5 changed files
with
91 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,70 @@ | ||
import libcst as cst | ||
from libcst.codemod import CodemodContext | ||
from codemodder.codemods.base_visitor import BaseTransformer | ||
from codemodder.codemods.base_codemod import ( | ||
SemgrepCodemod, | ||
CodemodMetadata, | ||
ReviewGuidance, | ||
) | ||
from codemodder.change import Change | ||
from codemodder.file_context import FileContext | ||
from codemodder.codemods.base_codemod import ReviewGuidance | ||
from codemodder.codemods.api import SemgrepCodemod | ||
from codemodder.codemods.api.helpers import NewArg | ||
|
||
|
||
class UpgradeSSLContextTLS(SemgrepCodemod, BaseTransformer): | ||
METADATA = CodemodMetadata( | ||
DESCRIPTION="Replaces known insecure TLS/SSL protocol versions in SSLContext with secure ones.", | ||
NAME="upgrade-sslcontext-tls", | ||
REVIEW_GUIDANCE=ReviewGuidance.MERGE_AFTER_CURSORY_REVIEW, | ||
REFERENCES=[ | ||
{ | ||
"url": "https://docs.python.org/3/library/ssl.html#security-considerations", | ||
"description": "", | ||
}, | ||
{"url": "https://datatracker.ietf.org/doc/rfc8996/", "description": ""}, | ||
{ | ||
"url": "https://www.digicert.com/blog/depreciating-tls-1-0-and-1-1", | ||
"description": "", | ||
}, | ||
], | ||
) | ||
class UpgradeSSLContextTLS(SemgrepCodemod): | ||
NAME = "upgrade-sslcontext-tls" | ||
REVIEW_GUIDANCE = ReviewGuidance.MERGE_AFTER_CURSORY_REVIEW | ||
SUMMARY = "Upgrade TLS Version In SSLContext" | ||
DESCRIPTION = "Replaces known insecure TLS/SSL protocol versions in SSLContext with secure ones." | ||
CHANGE_DESCRIPTION = "Upgrade to use a safe version of TLS in SSLContext" | ||
YAML_FILES = [ | ||
"upgrade_sslcontext_tls.yaml", | ||
REFERENCES = [ | ||
{ | ||
"url": "https://docs.python.org/3/library/ssl.html#security-considerations", | ||
"description": "", | ||
}, | ||
{"url": "https://datatracker.ietf.org/doc/rfc8996/", "description": ""}, | ||
{ | ||
"url": "https://www.digicert.com/blog/depreciating-tls-1-0-and-1-1", | ||
"description": "", | ||
}, | ||
] | ||
|
||
# TODO: in the majority of cases, using PROTOCOL_TLS_CLIENT will be the | ||
# right fix. However in some cases it will be appropriate to use | ||
# PROTOCOL_TLS_SERVER instead. We currently don't have a good way to handle | ||
# this. Eventually, when the platform supports parameters, we want to | ||
# revisit this to provide PROTOCOL_TLS_SERVER as an alternative fix. | ||
SAFE_TLS_PROTOCOL_VERSION = "PROTOCOL_TLS_CLIENT" | ||
PROTOCOL_ARG_INDEX = 0 | ||
PROTOCOL_KWARG_NAME = "protocol" | ||
SAFE_TLS_PROTOCOL_VERSION = "ssl.PROTOCOL_TLS_CLIENT" | ||
|
||
def __init__(self, codemod_context: CodemodContext, file_context: FileContext): | ||
SemgrepCodemod.__init__(self, file_context) | ||
BaseTransformer.__init__(self, codemod_context, self._results) | ||
@classmethod | ||
def rule(cls): | ||
return """ | ||
rules: | ||
- patterns: | ||
- pattern-inside: | | ||
import ssl | ||
... | ||
- pattern-either: | ||
- pattern: ssl.SSLContext() | ||
- pattern: ssl.SSLContext(...,ssl.PROTOCOL_SSLv2,...) | ||
- pattern: ssl.SSLContext(...,protocol=ssl.PROTOCOL_SSLv2,...) | ||
- pattern: ssl.SSLContext(...,ssl.PROTOCOL_SSLv3,...) | ||
- pattern: ssl.SSLContext(...,protocol=ssl.PROTOCOL_SSLv3,...) | ||
- pattern: ssl.SSLContext(...,ssl.PROTOCOL_TLSv1,...) | ||
- pattern: ssl.SSLContext(...,protocol=ssl.PROTOCOL_TLSv1,...) | ||
- pattern: ssl.SSLContext(...,ssl.PROTOCOL_TLSv1_1,...) | ||
- pattern: ssl.SSLContext(...,protocol=ssl.PROTOCOL_TLSv1_1,...) | ||
- pattern: ssl.SSLContext(...,ssl.PROTOCOL_TLS,...) | ||
- pattern: ssl.SSLContext(...,protocol=ssl.PROTOCOL_TLS,...) | ||
""" | ||
|
||
# TODO: apply unused import remover | ||
def on_result_found(self, original_node, updated_node): | ||
self.remove_unused_import(original_node) | ||
self.add_needed_import("ssl") | ||
|
||
def update_arg(self, arg: cst.Arg) -> cst.Arg: | ||
new_name = cst.Name(self.SAFE_TLS_PROTOCOL_VERSION) | ||
# TODO: are there other cases to handle here? | ||
new_value = ( | ||
arg.value.with_changes(attr=new_name) | ||
if isinstance(arg.value, cst.Attribute) | ||
else new_name | ||
) | ||
return arg.with_changes(value=new_value) | ||
|
||
def leave_Call(self, original_node: cst.Call, updated_node: cst.Arg): | ||
pos_to_match = self.get_metadata(self.METADATA_DEPENDENCIES[0], original_node) | ||
if self.filter_by_result( | ||
pos_to_match | ||
) and self.filter_by_path_includes_or_excludes(pos_to_match): | ||
line_number = pos_to_match.start.line | ||
self.file_context.codemod_changes.append( | ||
Change(line_number, self.CHANGE_DESCRIPTION) | ||
) | ||
|
||
return updated_node.with_changes( | ||
args=[ | ||
self.update_arg(arg) | ||
if idx == self.PROTOCOL_ARG_INDEX | ||
or (arg.keyword and arg.keyword.value == self.PROTOCOL_KWARG_NAME) | ||
else arg | ||
for idx, arg in enumerate(original_node.args) | ||
] | ||
if len((args := original_node.args)) == 1 and args[0].keyword is None: | ||
new_args = [self.make_new_arg(self.SAFE_TLS_PROTOCOL_VERSION)] | ||
else: | ||
new_args = self.replace_args( | ||
original_node, | ||
[ | ||
NewArg( | ||
name="protocol", | ||
value=self.SAFE_TLS_PROTOCOL_VERSION, | ||
add_if_missing=True, | ||
) | ||
], | ||
) | ||
|
||
return updated_node | ||
return self.update_arg_target(updated_node, new_args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters