-
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.
- Loading branch information
Showing
8 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
crates/ruff_linter/resources/test/fixtures/flake8_bandit/S702.py
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,9 @@ | ||
from mako.template import Template | ||
from mako import template | ||
import mako | ||
|
||
|
||
Template("hello") | ||
|
||
mako.template.Template("hern") | ||
template.Template("hern") |
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
57 changes: 57 additions & 0 deletions
57
crates/ruff_linter/src/rules/flake8_bandit/rules/mako_templates.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,57 @@ | ||
use crate::checkers::ast::Checker; | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{self as ast}; | ||
use ruff_text_size::Ranged; | ||
|
||
/// ## What it does | ||
/// Checks for uses of the `mako` templates. | ||
/// | ||
/// ## Why is this bad? | ||
/// Mako templates allow HTML and JavaScript rendering by default, and are | ||
/// inherently open to XSS attacks. Ensure variables in all templates are | ||
/// properly sanitized via the `n`, `h` or `x` flags (depending on context). | ||
/// For example, to HTML escape the variable `data`, use `${ data |h }`. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// from mako.template import Template | ||
/// | ||
/// Template("hello") | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// from mako.template import Template | ||
/// | ||
/// Template("hello |h") | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Mako documentation](https://www.makotemplates.org/) | ||
/// - [OpenStack security: Cross site scripting XSS](https://security.openstack.org/guidelines/dg_cross-site-scripting-xss.html) | ||
/// - [Common Weakness Enumeration: CWE-80](https://cwe.mitre.org/data/definitions/80.html) | ||
#[violation] | ||
pub struct MakoTemplates; | ||
|
||
impl Violation for MakoTemplates { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!( | ||
"Mako templates allow HTML and JavaScript rendering by default and are inherently open to XSS attacks" | ||
) | ||
} | ||
} | ||
|
||
/// S702 | ||
pub(crate) fn mako_templates(checker: &mut Checker, call: &ast::ExprCall) { | ||
if checker | ||
.semantic() | ||
.resolve_call_path(&call.func) | ||
.is_some_and(|call_path| matches!(call_path.as_slice(), ["mako", "template", "Template"])) | ||
{ | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(MakoTemplates, call.func.range())); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
...rules/flake8_bandit/snapshots/ruff_linter__rules__flake8_bandit__tests__S702_S702.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,28 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs | ||
--- | ||
S702.py:6:1: S702 Mako templates allow HTML and JavaScript rendering by default and are inherently open to XSS attacks | ||
| | ||
6 | Template("hello") | ||
| ^^^^^^^^ S702 | ||
7 | | ||
8 | mako.template.Template("hern") | ||
| | ||
|
||
S702.py:8:1: S702 Mako templates allow HTML and JavaScript rendering by default and are inherently open to XSS attacks | ||
| | ||
6 | Template("hello") | ||
7 | | ||
8 | mako.template.Template("hern") | ||
| ^^^^^^^^^^^^^^^^^^^^^^ S702 | ||
9 | template.Template("hern") | ||
| | ||
|
||
S702.py:9:1: S702 Mako templates allow HTML and JavaScript rendering by default and are inherently open to XSS attacks | ||
| | ||
8 | mako.template.Template("hern") | ||
9 | template.Template("hern") | ||
| ^^^^^^^^^^^^^^^^^ S702 | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.