-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 S508
(snmp insecure version) & `S509…
…` (snmp weak cryptography) (#1771) ref: #1646 Co-authored-by: messense <messense@icloud.com> Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
- Loading branch information
1 parent
643cedb
commit b8e3f0b
Showing
14 changed files
with
201 additions
and
0 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,6 @@ | ||
from pysnmp.hlapi import CommunityData | ||
|
||
CommunityData("public", mpModel=0) # S508 | ||
CommunityData("public", mpModel=1) # S508 | ||
|
||
CommunityData("public", mpModel=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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from pysnmp.hlapi import UsmUserData | ||
|
||
|
||
insecure = UsmUserData("securityName") # S509 | ||
auth_no_priv = UsmUserData("securityName", "authName") # S509 | ||
|
||
less_insecure = UsmUserData("securityName", "authName", "privName") # 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1493,6 +1493,8 @@ | |
"S50", | ||
"S501", | ||
"S506", | ||
"S508", | ||
"S509", | ||
"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
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,40 @@ | ||
use num_traits::{One, Zero}; | ||
use rustc_hash::{FxHashMap, FxHashSet}; | ||
use rustpython_ast::{Expr, ExprKind, Keyword}; | ||
use rustpython_parser::ast::Constant; | ||
|
||
use crate::ast::helpers::{collect_call_paths, dealias_call_path, match_call_path, SimpleCallArgs}; | ||
use crate::ast::types::Range; | ||
use crate::registry::Diagnostic; | ||
use crate::violations; | ||
|
||
/// S508 | ||
pub fn snmp_insecure_version( | ||
func: &Expr, | ||
args: &[Expr], | ||
keywords: &[Keyword], | ||
from_imports: &FxHashMap<&str, FxHashSet<&str>>, | ||
import_aliases: &FxHashMap<&str, &str>, | ||
) -> Option<Diagnostic> { | ||
let call_path = dealias_call_path(collect_call_paths(func), import_aliases); | ||
|
||
if match_call_path(&call_path, "pysnmp.hlapi", "CommunityData", from_imports) { | ||
let call_args = SimpleCallArgs::new(args, keywords); | ||
|
||
if let Some(mp_model_arg) = call_args.get_argument("mpModel", None) { | ||
if let ExprKind::Constant { | ||
value: Constant::Int(value), | ||
.. | ||
} = &mp_model_arg.node | ||
{ | ||
if value.is_zero() || value.is_one() { | ||
return Some(Diagnostic::new( | ||
violations::SnmpInsecureVersion, | ||
Range::from_located(mp_model_arg), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use rustc_hash::{FxHashMap, FxHashSet}; | ||
use rustpython_ast::{Expr, Keyword}; | ||
|
||
use crate::ast::helpers::{collect_call_paths, dealias_call_path, match_call_path, SimpleCallArgs}; | ||
use crate::ast::types::Range; | ||
use crate::registry::Diagnostic; | ||
use crate::violations; | ||
|
||
/// S509 | ||
pub fn snmp_weak_cryptography( | ||
func: &Expr, | ||
args: &[Expr], | ||
keywords: &[Keyword], | ||
from_imports: &FxHashMap<&str, FxHashSet<&str>>, | ||
import_aliases: &FxHashMap<&str, &str>, | ||
) -> Option<Diagnostic> { | ||
let call_path = dealias_call_path(collect_call_paths(func), import_aliases); | ||
|
||
if match_call_path(&call_path, "pysnmp.hlapi", "UsmUserData", from_imports) { | ||
let call_args = SimpleCallArgs::new(args, keywords); | ||
|
||
if call_args.len() < 3 { | ||
return Some(Diagnostic::new( | ||
violations::SnmpWeakCryptography, | ||
Range::from_located(func), | ||
)); | ||
} | ||
} | ||
None | ||
} |
25 changes: 25 additions & 0 deletions
25
src/flake8_bandit/snapshots/ruff__flake8_bandit__tests__S508_S508.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,25 @@ | ||
--- | ||
source: src/flake8_bandit/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
SnmpInsecureVersion: ~ | ||
location: | ||
row: 3 | ||
column: 32 | ||
end_location: | ||
row: 3 | ||
column: 33 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
SnmpInsecureVersion: ~ | ||
location: | ||
row: 4 | ||
column: 32 | ||
end_location: | ||
row: 4 | ||
column: 33 | ||
fix: ~ | ||
parent: ~ | ||
|
25 changes: 25 additions & 0 deletions
25
src/flake8_bandit/snapshots/ruff__flake8_bandit__tests__S509_S509.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,25 @@ | ||
--- | ||
source: src/flake8_bandit/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
SnmpWeakCryptography: ~ | ||
location: | ||
row: 4 | ||
column: 11 | ||
end_location: | ||
row: 4 | ||
column: 22 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
SnmpWeakCryptography: ~ | ||
location: | ||
row: 5 | ||
column: 15 | ||
end_location: | ||
row: 5 | ||
column: 26 | ||
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