Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ def _():
def _():
from builtin import exec
exec('') # No error

# https://github.com/astral-sh/ruff/issues/28011
list(map(exec, ["hi"])) # Preview: Error
foo = exec # Preview: Error

def _():
def exec(): ...

list(map(exec, [])) # Shadowed -- No error
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) {
Rule::SuspiciousInsecureCipherModeUsage,
Rule::SuspiciousMktempUsage,
Rule::SuspiciousEvalUsage,
Rule::ExecBuiltin,
Rule::SuspiciousMarkSafeUsage,
Rule::SuspiciousURLOpenUsage,
Rule::SuspiciousNonCryptographicRandomUsage,
Expand Down Expand Up @@ -410,6 +411,7 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) {
Rule::SuspiciousInsecureCipherModeUsage,
Rule::SuspiciousMktempUsage,
Rule::SuspiciousEvalUsage,
Rule::ExecBuiltin,
Rule::SuspiciousMarkSafeUsage,
Rule::SuspiciousURLOpenUsage,
Rule::SuspiciousNonCryptographicRandomUsage,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/flake8_bandit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ mod tests {
}

#[test_case(Rule::BadFilePermissions, Path::new("S103.py"))]
#[test_case(Rule::ExecBuiltin, Path::new("S102.py"))]
#[test_case(Rule::SuspiciousPickleUsage, Path::new("S301.py"))]
#[test_case(Rule::SuspiciousEvalUsage, Path::new("S307.py"))]
#[test_case(Rule::SuspiciousMarkSafeUsage, Path::new("S308.py"))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use ruff_text_size::{Ranged, TextRange};
use crate::Violation;
use crate::checkers::ast::Checker;
use crate::preview::is_suspicious_function_reference_enabled;
use crate::rules::flake8_bandit::rules::ExecBuiltin;

/// ## What it does
/// Checks for calls to `pickle` functions or modules that wrap them.
Expand Down Expand Up @@ -1161,6 +1162,14 @@ fn suspicious_function(
checker.report_diagnostic_if_enabled(SuspiciousEvalUsage, range)
}

// Exec
// Calls to `exec` are reported by [`exec_used`]; here we only flag
// non-call references (see `suspicious_function_reference`) to avoid
// duplicate diagnostics.
["" | "builtins", "exec"] if arguments.is_none() => {
checker.report_diagnostic_if_enabled(ExecBuiltin, range)
}

// MarkSafe
["django", "utils", "safestring" | "html", "mark_safe"] => {
if let Some(arguments) = arguments {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
assertion_line: 116
---
--- Linter settings ---
-linter.preview = disabled
+linter.preview = enabled

--- Summary ---
Removed: 0
Added: 2

--- Added ---
S102 Use of `exec` detected
--> S102.py:18:10
|
17 | # https://github.com/astral-sh/ruff/issues/28011
18 | list(map(exec, ["hi"])) # Preview: Error
| ^^^^
19 | foo = exec # Preview: Error
|


S102 Use of `exec` detected
--> S102.py:19:7
|
17 | # https://github.com/astral-sh/ruff/issues/28011
18 | list(map(exec, ["hi"])) # Preview: Error
19 | foo = exec # Preview: Error
| ^^^^
20 |
21 | def _():
|
Loading