-
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
Implement flake8-bandit rule S108
#1644
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7705ba2
Implement flake8-bandit rule `S108`
edgarrmondragon 86b702b
Update lib_wasm.rs
edgarrmondragon 9db9e7b
Merge branch 'main' into bandit-s108
charliermarsh 0607199
Use a single `hardcoded_tmp_directory` in bandit internal settings
edgarrmondragon f8023ff
Fix `hardcoded_tmp_directory_extend` example
edgarrmondragon 92cfb48
Merge branch 'bandit-s108' of github.com:edgarrmondragon/ruff into ba…
charliermarsh d0cbb9f
Remove old snapshots
edgarrmondragon c10b60c
Tweak docs
charliermarsh de0f4cd
Merge branch 'bandit-s108' of github.com:edgarrmondragon/ruff into ba…
charliermarsh a01ca93
Rename method to match option
charliermarsh 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# ok | ||
with open("/abc/tmp", "w") as f: | ||
f.write("def") | ||
|
||
with open("/tmp/abc", "w") as f: | ||
f.write("def") | ||
|
||
with open("/var/tmp/123", "w") as f: | ||
f.write("def") | ||
|
||
with open("/dev/shm/unit/test", "w") as f: | ||
f.write("def") | ||
|
||
# not ok by config | ||
with open("/foo/bar", "w") as f: | ||
f.write("def") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use rustpython_ast::Expr; | ||
|
||
use crate::ast::types::Range; | ||
use crate::registry::{Check, CheckKind}; | ||
|
||
/// S108 | ||
pub fn hardcoded_tmp_directory(expr: &Expr, value: &str, prefixes: &[String]) -> Option<Check> { | ||
if prefixes.iter().any(|prefix| value.starts_with(prefix)) { | ||
Some(Check::new( | ||
CheckKind::HardcodedTempFile(value.to_string()), | ||
Range::from_located(expr), | ||
)) | ||
} else { | ||
None | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//! Settings for the `flake8-bandit` plugin. | ||
|
||
use ruff_macros::ConfigurationOptions; | ||
use schemars::JsonSchema; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
fn default_tmp_dirs() -> Vec<String> { | ||
["/tmp", "/var/tmp", "/dev/shm"] | ||
.map(std::string::ToString::to_string) | ||
.to_vec() | ||
} | ||
|
||
#[derive( | ||
Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, JsonSchema, | ||
)] | ||
#[serde( | ||
deny_unknown_fields, | ||
rename_all = "kebab-case", | ||
rename = "Flake8BanditOptions" | ||
)] | ||
pub struct Options { | ||
#[option( | ||
default = "[\"/tmp\", \"/var/tmp\", \"/dev/shm\"]", | ||
value_type = "Vec<String>", | ||
example = "hardcoded-tmp-directory = [\"/foo/bar\"]" | ||
)] | ||
/// A list of directories to consider temporary. | ||
pub hardcoded_tmp_directory: Option<Vec<String>>, | ||
#[option( | ||
default = "[]", | ||
value_type = "Vec<String>", | ||
example = "extend-hardcoded-tmp-directory = [\"/foo/bar\"]" | ||
)] | ||
/// A list of directories to consider temporary, in addition to those | ||
/// specified by `hardcoded-tmp-directory`. | ||
pub hardcoded_tmp_directory_extend: Option<Vec<String>>, | ||
} | ||
|
||
#[derive(Debug, Hash)] | ||
pub struct Settings { | ||
pub hardcoded_tmp_directory: Vec<String>, | ||
} | ||
|
||
impl From<Options> for Settings { | ||
fn from(options: Options) -> Self { | ||
Self { | ||
hardcoded_tmp_directory: options | ||
.hardcoded_tmp_directory | ||
.unwrap_or_else(default_tmp_dirs) | ||
.into_iter() | ||
.chain( | ||
options | ||
.hardcoded_tmp_directory_extend | ||
.unwrap_or_default() | ||
.into_iter(), | ||
) | ||
.collect(), | ||
} | ||
} | ||
} | ||
|
||
impl From<Settings> for Options { | ||
fn from(settings: Settings) -> Self { | ||
Self { | ||
hardcoded_tmp_directory: Some(settings.hardcoded_tmp_directory), | ||
hardcoded_tmp_directory_extend: None, | ||
} | ||
} | ||
} | ||
|
||
impl Default for Settings { | ||
fn default() -> Self { | ||
Self { | ||
hardcoded_tmp_directory: default_tmp_dirs(), | ||
} | ||
} | ||
} |
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.
You could consider just having
hardcoded_tmp_directory
(removinghardcoded_tmp_directory_extend
fromSettings
), and then concatenating the two inFrom<Options>
.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.
Yeah, that makes sense.