-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-bandit
] Add Rule for S324
(Insecure hash functions in `ha…
- Loading branch information
Showing
13 changed files
with
289 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import hashlib | ||
from hashlib import new as hashlib_new | ||
from hashlib import sha1 as hashlib_sha1 | ||
|
||
# Invalid | ||
|
||
hashlib.new('md5') | ||
|
||
hashlib.new('md4', b'test') | ||
|
||
hashlib.new(name='md5', data=b'test') | ||
|
||
hashlib.new('MD4', data=b'test') | ||
|
||
hashlib.new('sha1') | ||
|
||
hashlib.new('sha1', data=b'test') | ||
|
||
hashlib.new('sha', data=b'test') | ||
|
||
hashlib.new(name='SHA', data=b'test') | ||
|
||
hashlib.sha(data=b'test') | ||
|
||
hashlib.md5() | ||
|
||
hashlib_new('sha1') | ||
|
||
hashlib_sha1('sha1') | ||
|
||
# usedforsecurity arg only available in Python 3.9+ | ||
hashlib.new('sha1', usedforsecurity=True) | ||
|
||
# Valid | ||
|
||
hashlib.new('sha256') | ||
|
||
hashlib.new('SHA512') | ||
|
||
hashlib.sha256(data=b'test') | ||
|
||
# usedforsecurity arg only available in Python 3.9+ | ||
hashlib_new(name='sha1', usedforsecurity=False) | ||
|
||
# usedforsecurity arg only available in Python 3.9+ | ||
hashlib_sha1(name='sha1', usedforsecurity=False) | ||
|
||
# usedforsecurity arg only available in Python 3.9+ | ||
hashlib.md4(usedforsecurity=False) | ||
|
||
# usedforsecurity arg only available in Python 3.9+ | ||
hashlib.new(name='sha256', usedforsecurity=False) |
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 |
---|---|---|
|
@@ -927,6 +927,9 @@ | |
"S106", | ||
"S107", | ||
"S108", | ||
"S3", | ||
"S32", | ||
"S324", | ||
"SIM", | ||
"SIM1", | ||
"SIM10", | ||
|
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
66 changes: 66 additions & 0 deletions
66
src/flake8_bandit/checks/hashlib_insecure_hash_functions.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,66 @@ | ||
use rustc_hash::{FxHashMap, FxHashSet}; | ||
use rustpython_ast::{Constant, Expr, ExprKind, Keyword}; | ||
|
||
use crate::ast::helpers::{match_module_member, SimpleCallArgs}; | ||
use crate::ast::types::Range; | ||
use crate::flake8_bandit::helpers::string_literal; | ||
use crate::registry::{Check, CheckKind}; | ||
|
||
const WEAK_HASHES: [&str; 4] = ["md4", "md5", "sha", "sha1"]; | ||
|
||
fn is_used_for_security(call_args: &SimpleCallArgs) -> bool { | ||
match call_args.get_argument("usedforsecurity", None) { | ||
Some(expr) => !matches!( | ||
&expr.node, | ||
ExprKind::Constant { | ||
value: Constant::Bool(false), | ||
.. | ||
} | ||
), | ||
_ => true, | ||
} | ||
} | ||
|
||
/// S324 | ||
pub fn hashlib_insecure_hash_functions( | ||
func: &Expr, | ||
args: &[Expr], | ||
keywords: &[Keyword], | ||
from_imports: &FxHashMap<&str, FxHashSet<&str>>, | ||
import_aliases: &FxHashMap<&str, &str>, | ||
) -> Option<Check> { | ||
if match_module_member(func, "hashlib", "new", from_imports, import_aliases) { | ||
let call_args = SimpleCallArgs::new(args, keywords); | ||
|
||
if !is_used_for_security(&call_args) { | ||
return None; | ||
} | ||
|
||
if let Some(name_arg) = call_args.get_argument("name", Some(0)) { | ||
let hash_func_name = string_literal(name_arg)?; | ||
|
||
if WEAK_HASHES.contains(&hash_func_name.to_lowercase().as_str()) { | ||
return Some(Check::new( | ||
CheckKind::HashlibInsecureHashFunction(hash_func_name.to_string()), | ||
Range::from_located(name_arg), | ||
)); | ||
} | ||
} | ||
} else { | ||
for func_name in &WEAK_HASHES { | ||
if match_module_member(func, "hashlib", func_name, from_imports, import_aliases) { | ||
let call_args = SimpleCallArgs::new(args, keywords); | ||
|
||
if !is_used_for_security(&call_args) { | ||
return None; | ||
} | ||
|
||
return Some(Check::new( | ||
CheckKind::HashlibInsecureHashFunction((*func_name).to_string()), | ||
Range::from_located(func), | ||
)); | ||
} | ||
} | ||
} | ||
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
135 changes: 135 additions & 0 deletions
135
src/flake8_bandit/snapshots/ruff__flake8_bandit__tests__S324_S324.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,135 @@ | ||
--- | ||
source: src/flake8_bandit/mod.rs | ||
expression: checks | ||
--- | ||
- kind: | ||
HashlibInsecureHashFunction: md5 | ||
location: | ||
row: 7 | ||
column: 12 | ||
end_location: | ||
row: 7 | ||
column: 17 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
HashlibInsecureHashFunction: md4 | ||
location: | ||
row: 9 | ||
column: 12 | ||
end_location: | ||
row: 9 | ||
column: 17 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
HashlibInsecureHashFunction: md5 | ||
location: | ||
row: 11 | ||
column: 17 | ||
end_location: | ||
row: 11 | ||
column: 22 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
HashlibInsecureHashFunction: MD4 | ||
location: | ||
row: 13 | ||
column: 12 | ||
end_location: | ||
row: 13 | ||
column: 17 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
HashlibInsecureHashFunction: sha1 | ||
location: | ||
row: 15 | ||
column: 12 | ||
end_location: | ||
row: 15 | ||
column: 18 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
HashlibInsecureHashFunction: sha1 | ||
location: | ||
row: 17 | ||
column: 12 | ||
end_location: | ||
row: 17 | ||
column: 18 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
HashlibInsecureHashFunction: sha | ||
location: | ||
row: 19 | ||
column: 12 | ||
end_location: | ||
row: 19 | ||
column: 17 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
HashlibInsecureHashFunction: SHA | ||
location: | ||
row: 21 | ||
column: 17 | ||
end_location: | ||
row: 21 | ||
column: 22 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
HashlibInsecureHashFunction: sha | ||
location: | ||
row: 23 | ||
column: 0 | ||
end_location: | ||
row: 23 | ||
column: 11 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
HashlibInsecureHashFunction: md5 | ||
location: | ||
row: 25 | ||
column: 0 | ||
end_location: | ||
row: 25 | ||
column: 11 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
HashlibInsecureHashFunction: sha1 | ||
location: | ||
row: 27 | ||
column: 12 | ||
end_location: | ||
row: 27 | ||
column: 18 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
HashlibInsecureHashFunction: sha1 | ||
location: | ||
row: 29 | ||
column: 0 | ||
end_location: | ||
row: 29 | ||
column: 12 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
HashlibInsecureHashFunction: sha1 | ||
location: | ||
row: 32 | ||
column: 12 | ||
end_location: | ||
row: 32 | ||
column: 18 | ||
fix: ~ | ||
parent: ~ | ||
|
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
Oops, something went wrong.