-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flake8-bandit
] Implement S502
SslInsecureVersion
rule
#9390
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
crates/ruff_linter/resources/test/fixtures/flake8_bandit/S502.py
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import ssl | ||
from ssl import wrap_socket | ||
from OpenSSL import SSL | ||
from OpenSSL.SSL import Context | ||
|
||
wrap_socket(ssl_version=ssl.PROTOCOL_SSLv3) # S502 | ||
ssl.wrap_socket(ssl_version=ssl.PROTOCOL_TLSv1) # S502 | ||
ssl.wrap_socket(ssl_version=ssl.PROTOCOL_SSLv2) # S502 | ||
SSL.Context(method=SSL.SSLv2_METHOD) # S502 | ||
SSL.Context(method=SSL.SSLv23_METHOD) # S502 | ||
Context(method=SSL.SSLv3_METHOD) # S502 | ||
Context(method=SSL.TLSv1_METHOD) # S502 | ||
|
||
|
||
def func(): | ||
pass | ||
|
||
|
||
func(ssl_version=ssl.PROTOCOL_SSLv2) # S502 | ||
func(ssl_version=ssl.PROTOCOL_SSLv3) # S502 | ||
func(ssl_version=ssl.PROTOCOL_TLSv1) # S502 | ||
func(method=SSL.SSLv2_METHOD) # S502 | ||
func(method=SSL.SSLv23_METHOD) # S502 | ||
func(method=SSL.SSLv3_METHOD) # S502 | ||
func(method=SSL.TLSv1_METHOD) # S502 | ||
|
||
wrap_socket(ssl_version=ssl.PROTOCOL_TLS_CLIENT) # OK | ||
SSL.Context(method=SSL.TLS_SERVER_METHOD) # OK | ||
func(ssl_version=ssl.PROTOCOL_TLSv1_2) # OK | ||
|
||
|
||
|
||
|
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 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
121 changes: 121 additions & 0 deletions
121
crates/ruff_linter/src/rules/flake8_bandit/rules/ssl_insecure_version.rs
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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{self as ast, Expr, ExprCall}; | ||
use ruff_python_semantic::analyze::typing::find_assigned_value; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for calls to Python methods with parameters that indicate the used broken SSL/TLS | ||
/// protocol versions. | ||
/// | ||
/// ## Why is this bad? | ||
/// Several highly publicized exploitable flaws have been discovered in all versions of SSL and | ||
/// early versions of TLS. It is strongly recommended that use of the following known broken | ||
/// protocol versions be avoided: | ||
/// - SSL v2 | ||
/// - SSL v3 | ||
/// - TLS v1 | ||
/// - TLS v1.1 | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// import ssl | ||
/// | ||
/// ssl.wrap_socket(ssl_version=ssl.PROTOCOL_TLSv1) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// import ssl | ||
/// | ||
/// ssl.wrap_socket(ssl_version=ssl.PROTOCOL_TLSv1_2) | ||
/// ``` | ||
#[violation] | ||
pub struct SslInsecureVersion { | ||
protocol: String, | ||
} | ||
|
||
impl Violation for SslInsecureVersion { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Call made with insecure SSL protocol: {}", self.protocol) | ||
} | ||
} | ||
|
||
const INSECURE_SSL_PROTOCOLS: &[&str] = &[ | ||
"PROTOCOL_SSLv2", | ||
"PROTOCOL_SSLv3", | ||
"PROTOCOL_TLSv1", | ||
"PROTOCOL_TLSv1_1", | ||
"SSLv2_METHOD", | ||
"SSLv23_METHOD", | ||
"SSLv3_METHOD", | ||
"TLSv1_METHOD", | ||
"TLSv1_1_METHOD", | ||
]; | ||
|
||
/// S502 | ||
pub(crate) fn ssl_insecure_version(checker: &mut Checker, call: &ExprCall) { | ||
let keywords = match checker.semantic().resolve_call_path(call.func.as_ref()) { | ||
Some(call_path) => { | ||
match *call_path.as_slice() { | ||
["ssl", "wrap_socket"] => vec!["ssl_version"], | ||
["OpenSSL", "SSL", "Context"] => vec!["method"], | ||
_ => vec!["ssl_version", "method"], | ||
} | ||
}, | ||
None => vec!["ssl_version", "method"] | ||
}; | ||
|
||
for arg in keywords { | ||
let Some(keyword) = call.arguments.find_keyword(arg) else { | ||
continue; | ||
}; | ||
match &keyword.value { | ||
Expr::Name(ast::ExprName { id, .. }) => { | ||
let Some(val) = find_assigned_value(id, checker.semantic()) else { | ||
continue; | ||
}; | ||
match val { | ||
Expr::Name(ast::ExprName { id, .. }) => { | ||
if INSECURE_SSL_PROTOCOLS.contains(&id.as_str()) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
SslInsecureVersion { | ||
protocol: id.to_string(), | ||
}, | ||
keyword.range, | ||
)); | ||
} | ||
} | ||
Expr::Attribute(ast::ExprAttribute { attr, .. }) => { | ||
if INSECURE_SSL_PROTOCOLS.contains(&attr.as_str()) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
SslInsecureVersion { | ||
protocol: attr.to_string(), | ||
}, | ||
keyword.range, | ||
)); | ||
} | ||
} | ||
_ => { | ||
continue; | ||
} | ||
} | ||
} | ||
Expr::Attribute(ast::ExprAttribute { attr, .. }) => { | ||
if INSECURE_SSL_PROTOCOLS.contains(&attr.as_str()) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
SslInsecureVersion { | ||
protocol: attr.to_string(), | ||
}, | ||
keyword.range, | ||
)); | ||
} | ||
} | ||
_ => { | ||
continue; | ||
} | ||
} | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
...rules/flake8_bandit/snapshots/ruff_linter__rules__flake8_bandit__tests__S502_S502.py.snap
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 |
---|---|---|
@@ -0,0 +1,136 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs | ||
--- | ||
S502.py:6:13: S502 Call made with insecure SSL protocol: PROTOCOL_SSLv3 | ||
| | ||
4 | from OpenSSL.SSL import Context | ||
5 | | ||
6 | wrap_socket(ssl_version=ssl.PROTOCOL_SSLv3) # S502 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ S502 | ||
7 | ssl.wrap_socket(ssl_version=ssl.PROTOCOL_TLSv1) # S502 | ||
8 | ssl.wrap_socket(ssl_version=ssl.PROTOCOL_SSLv2) # S502 | ||
| | ||
|
||
S502.py:7:17: S502 Call made with insecure SSL protocol: PROTOCOL_TLSv1 | ||
| | ||
6 | wrap_socket(ssl_version=ssl.PROTOCOL_SSLv3) # S502 | ||
7 | ssl.wrap_socket(ssl_version=ssl.PROTOCOL_TLSv1) # S502 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ S502 | ||
8 | ssl.wrap_socket(ssl_version=ssl.PROTOCOL_SSLv2) # S502 | ||
9 | SSL.Context(method=SSL.SSLv2_METHOD) # S502 | ||
| | ||
|
||
S502.py:8:17: S502 Call made with insecure SSL protocol: PROTOCOL_SSLv2 | ||
| | ||
6 | wrap_socket(ssl_version=ssl.PROTOCOL_SSLv3) # S502 | ||
7 | ssl.wrap_socket(ssl_version=ssl.PROTOCOL_TLSv1) # S502 | ||
8 | ssl.wrap_socket(ssl_version=ssl.PROTOCOL_SSLv2) # S502 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ S502 | ||
9 | SSL.Context(method=SSL.SSLv2_METHOD) # S502 | ||
10 | SSL.Context(method=SSL.SSLv23_METHOD) # S502 | ||
| | ||
|
||
S502.py:9:13: S502 Call made with insecure SSL protocol: SSLv2_METHOD | ||
| | ||
7 | ssl.wrap_socket(ssl_version=ssl.PROTOCOL_TLSv1) # S502 | ||
8 | ssl.wrap_socket(ssl_version=ssl.PROTOCOL_SSLv2) # S502 | ||
9 | SSL.Context(method=SSL.SSLv2_METHOD) # S502 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ S502 | ||
10 | SSL.Context(method=SSL.SSLv23_METHOD) # S502 | ||
11 | Context(method=SSL.SSLv3_METHOD) # S502 | ||
| | ||
|
||
S502.py:10:13: S502 Call made with insecure SSL protocol: SSLv23_METHOD | ||
| | ||
8 | ssl.wrap_socket(ssl_version=ssl.PROTOCOL_SSLv2) # S502 | ||
9 | SSL.Context(method=SSL.SSLv2_METHOD) # S502 | ||
10 | SSL.Context(method=SSL.SSLv23_METHOD) # S502 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ S502 | ||
11 | Context(method=SSL.SSLv3_METHOD) # S502 | ||
12 | Context(method=SSL.TLSv1_METHOD) # S502 | ||
| | ||
|
||
S502.py:11:9: S502 Call made with insecure SSL protocol: SSLv3_METHOD | ||
| | ||
9 | SSL.Context(method=SSL.SSLv2_METHOD) # S502 | ||
10 | SSL.Context(method=SSL.SSLv23_METHOD) # S502 | ||
11 | Context(method=SSL.SSLv3_METHOD) # S502 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ S502 | ||
12 | Context(method=SSL.TLSv1_METHOD) # S502 | ||
| | ||
|
||
S502.py:12:9: S502 Call made with insecure SSL protocol: TLSv1_METHOD | ||
| | ||
10 | SSL.Context(method=SSL.SSLv23_METHOD) # S502 | ||
11 | Context(method=SSL.SSLv3_METHOD) # S502 | ||
12 | Context(method=SSL.TLSv1_METHOD) # S502 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ S502 | ||
| | ||
|
||
S502.py:19:6: S502 Call made with insecure SSL protocol: PROTOCOL_SSLv2 | ||
| | ||
19 | func(ssl_version=ssl.PROTOCOL_SSLv2) # S502 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ S502 | ||
20 | func(ssl_version=ssl.PROTOCOL_SSLv3) # S502 | ||
21 | func(ssl_version=ssl.PROTOCOL_TLSv1) # S502 | ||
| | ||
|
||
S502.py:20:6: S502 Call made with insecure SSL protocol: PROTOCOL_SSLv3 | ||
| | ||
19 | func(ssl_version=ssl.PROTOCOL_SSLv2) # S502 | ||
20 | func(ssl_version=ssl.PROTOCOL_SSLv3) # S502 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ S502 | ||
21 | func(ssl_version=ssl.PROTOCOL_TLSv1) # S502 | ||
22 | func(method=SSL.SSLv2_METHOD) # S502 | ||
| | ||
|
||
S502.py:21:6: S502 Call made with insecure SSL protocol: PROTOCOL_TLSv1 | ||
| | ||
19 | func(ssl_version=ssl.PROTOCOL_SSLv2) # S502 | ||
20 | func(ssl_version=ssl.PROTOCOL_SSLv3) # S502 | ||
21 | func(ssl_version=ssl.PROTOCOL_TLSv1) # S502 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ S502 | ||
22 | func(method=SSL.SSLv2_METHOD) # S502 | ||
23 | func(method=SSL.SSLv23_METHOD) # S502 | ||
| | ||
|
||
S502.py:22:6: S502 Call made with insecure SSL protocol: SSLv2_METHOD | ||
| | ||
20 | func(ssl_version=ssl.PROTOCOL_SSLv3) # S502 | ||
21 | func(ssl_version=ssl.PROTOCOL_TLSv1) # S502 | ||
22 | func(method=SSL.SSLv2_METHOD) # S502 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ S502 | ||
23 | func(method=SSL.SSLv23_METHOD) # S502 | ||
24 | func(method=SSL.SSLv3_METHOD) # S502 | ||
| | ||
|
||
S502.py:23:6: S502 Call made with insecure SSL protocol: SSLv23_METHOD | ||
| | ||
21 | func(ssl_version=ssl.PROTOCOL_TLSv1) # S502 | ||
22 | func(method=SSL.SSLv2_METHOD) # S502 | ||
23 | func(method=SSL.SSLv23_METHOD) # S502 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ S502 | ||
24 | func(method=SSL.SSLv3_METHOD) # S502 | ||
25 | func(method=SSL.TLSv1_METHOD) # S502 | ||
| | ||
|
||
S502.py:24:6: S502 Call made with insecure SSL protocol: SSLv3_METHOD | ||
| | ||
22 | func(method=SSL.SSLv2_METHOD) # S502 | ||
23 | func(method=SSL.SSLv23_METHOD) # S502 | ||
24 | func(method=SSL.SSLv3_METHOD) # S502 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ S502 | ||
25 | func(method=SSL.TLSv1_METHOD) # S502 | ||
| | ||
|
||
S502.py:25:6: S502 Call made with insecure SSL protocol: TLSv1_METHOD | ||
| | ||
23 | func(method=SSL.SSLv23_METHOD) # S502 | ||
24 | func(method=SSL.SSLv3_METHOD) # S502 | ||
25 | func(method=SSL.TLSv1_METHOD) # S502 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ S502 | ||
26 | | ||
27 | wrap_socket(ssl_version=ssl.PROTOCOL_TLS_CLIENT) # OK | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upstream implementations checks for all calls at MEDIUM severity and the the specific functions at HIGH severity. Unsure if we want to replicate this, as it might be a bit heavy performance wise and Ruff/flake8-bandit has no way of separating the severity levels right now. Just copied upstream implementation for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to reduce the rule scope for the same reason.