-
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.
See: #1646. Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
- Loading branch information
1 parent
5eb03d5
commit 2d23b1a
Showing
9 changed files
with
148 additions
and
11 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,31 @@ | ||
import json | ||
import yaml | ||
from yaml import CSafeLoader | ||
from yaml import SafeLoader | ||
from yaml import SafeLoader as NewSafeLoader | ||
|
||
|
||
def test_yaml_load(): | ||
ystr = yaml.dump({"a": 1, "b": 2, "c": 3}) | ||
y = yaml.load(ystr) | ||
yaml.dump(y) | ||
try: | ||
y = yaml.load(ystr, Loader=yaml.CSafeLoader) | ||
except AttributeError: | ||
# CSafeLoader only exists if you build yaml with LibYAML | ||
y = yaml.load(ystr, Loader=yaml.SafeLoader) | ||
|
||
|
||
def test_json_load(): | ||
# no issue should be found | ||
j = json.load("{}") | ||
|
||
|
||
yaml.load("{}", Loader=yaml.Loader) | ||
|
||
# no issue should be found | ||
yaml.load("{}", SafeLoader) | ||
yaml.load("{}", yaml.SafeLoader) | ||
yaml.load("{}", CSafeLoader) | ||
yaml.load("{}", yaml.CSafeLoader) | ||
yaml.load("{}", NewSafeLoader) |
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 |
---|---|---|
|
@@ -930,6 +930,9 @@ | |
"S3", | ||
"S32", | ||
"S324", | ||
"S5", | ||
"S50", | ||
"S506", | ||
"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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use rustc_hash::{FxHashMap, FxHashSet}; | ||
use rustpython_ast::{Expr, ExprKind, Keyword}; | ||
|
||
use crate::ast::helpers::{match_module_member, SimpleCallArgs}; | ||
use crate::ast::types::Range; | ||
use crate::registry::{Check, CheckKind}; | ||
|
||
/// S506 | ||
pub fn unsafe_yaml_load( | ||
func: &Expr, | ||
args: &[Expr], | ||
keywords: &[Keyword], | ||
from_imports: &FxHashMap<&str, FxHashSet<&str>>, | ||
import_aliases: &FxHashMap<&str, &str>, | ||
) -> Option<Check> { | ||
if match_module_member(func, "yaml", "load", from_imports, import_aliases) { | ||
let call_args = SimpleCallArgs::new(args, keywords); | ||
if let Some(loader_arg) = call_args.get_argument("Loader", Some(1)) { | ||
if !match_module_member( | ||
loader_arg, | ||
"yaml", | ||
"SafeLoader", | ||
from_imports, | ||
import_aliases, | ||
) && !match_module_member( | ||
loader_arg, | ||
"yaml", | ||
"CSafeLoader", | ||
from_imports, | ||
import_aliases, | ||
) { | ||
let loader = match &loader_arg.node { | ||
ExprKind::Attribute { attr, .. } => Some(attr.to_string()), | ||
ExprKind::Name { id, .. } => Some(id.to_string()), | ||
_ => None, | ||
}; | ||
return Some(Check::new( | ||
CheckKind::UnsafeYAMLLoad(loader), | ||
Range::from_located(loader_arg), | ||
)); | ||
} | ||
} else { | ||
return Some(Check::new( | ||
CheckKind::UnsafeYAMLLoad(None), | ||
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
25 changes: 25 additions & 0 deletions
25
src/flake8_bandit/snapshots/ruff__flake8_bandit__tests__S506_S506.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: checks | ||
--- | ||
- kind: | ||
UnsafeYAMLLoad: ~ | ||
location: | ||
row: 10 | ||
column: 8 | ||
end_location: | ||
row: 10 | ||
column: 17 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
UnsafeYAMLLoad: Loader | ||
location: | ||
row: 24 | ||
column: 23 | ||
end_location: | ||
row: 24 | ||
column: 34 | ||
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