-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
] Add S504
SslWithNoVersion
rule
#9384
Merged
Merged
Changes from 3 commits
Commits
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
16 changes: 16 additions & 0 deletions
16
crates/ruff_linter/resources/test/fixtures/flake8_bandit/S504.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,16 @@ | ||
import ssl | ||
from ssl import wrap_socket | ||
|
||
|
||
ssl.wrap_socket() # S504 | ||
wrap_socket() # S504 | ||
ssl.wrap_socket(ssl_version=ssl.PROTOCOL_TLSv1_2) # OK | ||
|
||
|
||
class Foo: | ||
def wrap_socket(self): | ||
pass | ||
|
||
|
||
f = Foo() | ||
f.wrap_socket() # 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
53 changes: 53 additions & 0 deletions
53
crates/ruff_linter/src/rules/flake8_bandit/rules/ssl_with_no_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,53 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::ExprCall; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for calls to `ssl.wrap_socket()` with no version set | ||
/// | ||
/// ## Why is this bad? | ||
/// This method is known to provide a default value that maximizes compatibility, but permits use | ||
/// of the aforementioned broken protocol versions. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// import ssl | ||
/// | ||
/// ssl.wrap_socket() | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// import ssl | ||
/// | ||
/// ssl.wrap_socket(ssl_version=ssl.PROTOCOL_TLSv1_2) | ||
/// ``` | ||
#[violation] | ||
pub struct SslWithNoVersion; | ||
|
||
impl Violation for SslWithNoVersion { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`ssl.wrap_socket` called with no `ssl_version` set`") | ||
} | ||
} | ||
|
||
/// S504 | ||
pub(crate) fn ssl_with_no_version(checker: &mut Checker, call: &ExprCall) { | ||
if !checker | ||
.semantic() | ||
.resolve_call_path(call.func.as_ref()) | ||
.is_some_and(|call_path| matches!(call_path.as_slice(), ["ssl", "wrap_socket"])) | ||
{ | ||
return; | ||
}; | ||
|
||
if call.arguments.find_keyword("ssl_version").is_none() { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(SslWithNoVersion, call.range())); | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
...rules/flake8_bandit/snapshots/ruff_linter__rules__flake8_bandit__tests__S504_S504.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,20 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs | ||
--- | ||
S504.py:5:1: S504 `ssl.wrap_socket` called with no `ssl_version` set` | ||
| | ||
5 | ssl.wrap_socket() # S504 | ||
| ^^^^^^^^^^^^^^^^^ S504 | ||
6 | wrap_socket() # S504 | ||
7 | ssl.wrap_socket(ssl_version=ssl.PROTOCOL_TLSv1_2) # OK | ||
| | ||
|
||
S504.py:6:1: S504 `ssl.wrap_socket` called with no `ssl_version` set` | ||
| | ||
5 | ssl.wrap_socket() # S504 | ||
6 | wrap_socket() # S504 | ||
| ^^^^^^^^^^^^^ S504 | ||
7 | ssl.wrap_socket(ssl_version=ssl.PROTOCOL_TLSv1_2) # 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.
Nice work by me!