-
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 S501
(request call with `verify=Fals…
- Loading branch information
Showing
9 changed files
with
321 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,40 @@ | ||
import httpx | ||
import requests | ||
|
||
requests.get('https://gmail.com', timeout=30, verify=True) | ||
requests.get('https://gmail.com', timeout=30, verify=False) | ||
requests.post('https://gmail.com', timeout=30, verify=True) | ||
requests.post('https://gmail.com', timeout=30, verify=False) | ||
requests.put('https://gmail.com', timeout=30, verify=True) | ||
requests.put('https://gmail.com', timeout=30, verify=False) | ||
requests.delete('https://gmail.com', timeout=30, verify=True) | ||
requests.delete('https://gmail.com', timeout=30, verify=False) | ||
requests.patch('https://gmail.com', timeout=30, verify=True) | ||
requests.patch('https://gmail.com', timeout=30, verify=False) | ||
requests.options('https://gmail.com', timeout=30, verify=True) | ||
requests.options('https://gmail.com', timeout=30, verify=False) | ||
requests.head('https://gmail.com', timeout=30, verify=True) | ||
requests.head('https://gmail.com', timeout=30, verify=False) | ||
|
||
httpx.request('GET', 'https://gmail.com', verify=True) | ||
httpx.request('GET', 'https://gmail.com', verify=False) | ||
httpx.get('https://gmail.com', verify=True) | ||
httpx.get('https://gmail.com', verify=False) | ||
httpx.options('https://gmail.com', verify=True) | ||
httpx.options('https://gmail.com', verify=False) | ||
httpx.head('https://gmail.com', verify=True) | ||
httpx.head('https://gmail.com', verify=False) | ||
httpx.post('https://gmail.com', verify=True) | ||
httpx.post('https://gmail.com', verify=False) | ||
httpx.put('https://gmail.com', verify=True) | ||
httpx.put('https://gmail.com', verify=False) | ||
httpx.patch('https://gmail.com', verify=True) | ||
httpx.patch('https://gmail.com', verify=False) | ||
httpx.delete('https://gmail.com', verify=True) | ||
httpx.delete('https://gmail.com', verify=False) | ||
httpx.stream('https://gmail.com', verify=True) | ||
httpx.stream('https://gmail.com', verify=False) | ||
httpx.Client() | ||
httpx.Client(verify=False) | ||
httpx.AsyncClient() | ||
httpx.AsyncClient(verify=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 |
---|---|---|
|
@@ -934,6 +934,7 @@ | |
"S324", | ||
"S5", | ||
"S50", | ||
"S501", | ||
"S506", | ||
"SIM", | ||
"SIM1", | ||
|
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
69 changes: 69 additions & 0 deletions
69
src/flake8_bandit/checks/request_with_no_cert_validation.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,69 @@ | ||
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::{Check, CheckKind}; | ||
|
||
const REQUESTS_HTTP_VERBS: [&str; 7] = ["get", "options", "head", "post", "put", "patch", "delete"]; | ||
const HTTPX_METHODS: [&str; 11] = [ | ||
"get", | ||
"options", | ||
"head", | ||
"post", | ||
"put", | ||
"patch", | ||
"delete", | ||
"request", | ||
"stream", | ||
"Client", | ||
"AsyncClient", | ||
]; | ||
|
||
/// S501 | ||
pub fn request_with_no_cert_validation( | ||
func: &Expr, | ||
args: &[Expr], | ||
keywords: &[Keyword], | ||
from_imports: &FxHashMap<&str, FxHashSet<&str>>, | ||
import_aliases: &FxHashMap<&str, &str>, | ||
) -> Option<Check> { | ||
let call_path = dealias_call_path(collect_call_paths(func), import_aliases); | ||
let call_args = SimpleCallArgs::new(args, keywords); | ||
|
||
for func_name in &REQUESTS_HTTP_VERBS { | ||
if match_call_path(&call_path, "requests", func_name, from_imports) { | ||
if let Some(verify_arg) = call_args.get_argument("verify", None) { | ||
if let ExprKind::Constant { | ||
value: Constant::Bool(false), | ||
.. | ||
} = &verify_arg.node | ||
{ | ||
return Some(Check::new( | ||
CheckKind::RequestWithNoCertValidation("requests".to_string()), | ||
Range::from_located(verify_arg), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
for func_name in &HTTPX_METHODS { | ||
if match_call_path(&call_path, "httpx", func_name, from_imports) { | ||
if let Some(verify_arg) = call_args.get_argument("verify", None) { | ||
if let ExprKind::Constant { | ||
value: Constant::Bool(false), | ||
.. | ||
} = &verify_arg.node | ||
{ | ||
return Some(Check::new( | ||
CheckKind::RequestWithNoCertValidation("httpx".to_string()), | ||
Range::from_located(verify_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
185 changes: 185 additions & 0 deletions
185
src/flake8_bandit/snapshots/ruff__flake8_bandit__tests__S501_S501.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,185 @@ | ||
--- | ||
source: src/flake8_bandit/mod.rs | ||
expression: checks | ||
--- | ||
- kind: | ||
RequestWithNoCertValidation: requests | ||
location: | ||
row: 5 | ||
column: 53 | ||
end_location: | ||
row: 5 | ||
column: 58 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: requests | ||
location: | ||
row: 7 | ||
column: 54 | ||
end_location: | ||
row: 7 | ||
column: 59 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: requests | ||
location: | ||
row: 9 | ||
column: 53 | ||
end_location: | ||
row: 9 | ||
column: 58 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: requests | ||
location: | ||
row: 11 | ||
column: 56 | ||
end_location: | ||
row: 11 | ||
column: 61 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: requests | ||
location: | ||
row: 13 | ||
column: 55 | ||
end_location: | ||
row: 13 | ||
column: 60 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: requests | ||
location: | ||
row: 15 | ||
column: 57 | ||
end_location: | ||
row: 15 | ||
column: 62 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: requests | ||
location: | ||
row: 17 | ||
column: 54 | ||
end_location: | ||
row: 17 | ||
column: 59 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: httpx | ||
location: | ||
row: 20 | ||
column: 49 | ||
end_location: | ||
row: 20 | ||
column: 54 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: httpx | ||
location: | ||
row: 22 | ||
column: 38 | ||
end_location: | ||
row: 22 | ||
column: 43 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: httpx | ||
location: | ||
row: 24 | ||
column: 42 | ||
end_location: | ||
row: 24 | ||
column: 47 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: httpx | ||
location: | ||
row: 26 | ||
column: 39 | ||
end_location: | ||
row: 26 | ||
column: 44 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: httpx | ||
location: | ||
row: 28 | ||
column: 39 | ||
end_location: | ||
row: 28 | ||
column: 44 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: httpx | ||
location: | ||
row: 30 | ||
column: 38 | ||
end_location: | ||
row: 30 | ||
column: 43 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: httpx | ||
location: | ||
row: 32 | ||
column: 40 | ||
end_location: | ||
row: 32 | ||
column: 45 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: httpx | ||
location: | ||
row: 34 | ||
column: 41 | ||
end_location: | ||
row: 34 | ||
column: 46 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: httpx | ||
location: | ||
row: 36 | ||
column: 41 | ||
end_location: | ||
row: 36 | ||
column: 46 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: httpx | ||
location: | ||
row: 38 | ||
column: 20 | ||
end_location: | ||
row: 38 | ||
column: 25 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RequestWithNoCertValidation: httpx | ||
location: | ||
row: 40 | ||
column: 25 | ||
end_location: | ||
row: 40 | ||
column: 30 | ||
fix: ~ | ||
parent: ~ | ||
|
Oops, something went wrong.