-
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.
ref: #1646 Co-authored-by: Charlie Marsh <[email protected]>
- Loading branch information
1 parent
07134c5
commit 1a90408
Showing
10 changed files
with
183 additions
and
1 deletion.
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,29 @@ | ||
import jinja2 | ||
from jinja2 import Environment, select_autoescape | ||
templateLoader = jinja2.FileSystemLoader( searchpath="/" ) | ||
something = '' | ||
|
||
Environment(loader=templateLoader, load=templateLoader, autoescape=True) | ||
templateEnv = jinja2.Environment(autoescape=True, | ||
loader=templateLoader ) | ||
Environment(loader=templateLoader, load=templateLoader, autoescape=something) # S701 | ||
templateEnv = jinja2.Environment(autoescape=False, loader=templateLoader ) # S701 | ||
Environment(loader=templateLoader, | ||
load=templateLoader, | ||
autoescape=False) # S701 | ||
|
||
Environment(loader=templateLoader, # S701 | ||
load=templateLoader) | ||
|
||
Environment(loader=templateLoader, autoescape=select_autoescape()) | ||
|
||
Environment(loader=templateLoader, | ||
autoescape=select_autoescape(['html', 'htm', 'xml'])) | ||
|
||
Environment(loader=templateLoader, | ||
autoescape=jinja2.select_autoescape(['html', 'htm', 'xml'])) | ||
|
||
|
||
def fake_func(): | ||
return 'foobar' | ||
Environment(loader=templateLoader, autoescape=fake_func()) # S701 |
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 |
---|---|---|
|
@@ -1515,6 +1515,9 @@ | |
"S506", | ||
"S508", | ||
"S509", | ||
"S7", | ||
"S70", | ||
"S701", | ||
"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,57 @@ | ||
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; | ||
|
||
/// S701 | ||
pub fn jinja2_autoescape_false( | ||
func: &Expr, | ||
args: &[Expr], | ||
keywords: &[Keyword], | ||
from_imports: &FxHashMap<&str, FxHashSet<&str>>, | ||
import_aliases: &FxHashMap<&str, &str>, | ||
) -> Option<Diagnostic> { | ||
if match_call_path( | ||
&dealias_call_path(collect_call_paths(func), import_aliases), | ||
"jinja2", | ||
"Environment", | ||
from_imports, | ||
) { | ||
let call_args = SimpleCallArgs::new(args, keywords); | ||
|
||
if let Some(autoescape_arg) = call_args.get_argument("autoescape", None) { | ||
match &autoescape_arg.node { | ||
ExprKind::Constant { | ||
value: Constant::Bool(true), | ||
.. | ||
} => (), | ||
ExprKind::Call { func, .. } => { | ||
if let ExprKind::Name { id, .. } = &func.node { | ||
if id.as_str() != "select_autoescape" { | ||
return Some(Diagnostic::new( | ||
violations::Jinja2AutoescapeFalse(true), | ||
Range::from_located(autoescape_arg), | ||
)); | ||
} | ||
} | ||
} | ||
_ => { | ||
return Some(Diagnostic::new( | ||
violations::Jinja2AutoescapeFalse(true), | ||
Range::from_located(autoescape_arg), | ||
)) | ||
} | ||
} | ||
} else { | ||
return Some(Diagnostic::new( | ||
violations::Jinja2AutoescapeFalse(false), | ||
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
55 changes: 55 additions & 0 deletions
55
src/flake8_bandit/snapshots/ruff__flake8_bandit__tests__S701_S701.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,55 @@ | ||
--- | ||
source: src/flake8_bandit/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
Jinja2AutoescapeFalse: true | ||
location: | ||
row: 9 | ||
column: 67 | ||
end_location: | ||
row: 9 | ||
column: 76 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
Jinja2AutoescapeFalse: true | ||
location: | ||
row: 10 | ||
column: 44 | ||
end_location: | ||
row: 10 | ||
column: 49 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
Jinja2AutoescapeFalse: true | ||
location: | ||
row: 13 | ||
column: 23 | ||
end_location: | ||
row: 13 | ||
column: 28 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
Jinja2AutoescapeFalse: false | ||
location: | ||
row: 15 | ||
column: 0 | ||
end_location: | ||
row: 15 | ||
column: 11 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
Jinja2AutoescapeFalse: true | ||
location: | ||
row: 29 | ||
column: 46 | ||
end_location: | ||
row: 29 | ||
column: 57 | ||
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