Skip to content

Commit 887a47c

Browse files
authored
Avoid S108 if path is inside tempfile.* call (#6416)
1 parent a275851 commit 887a47c

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

crates/ruff/resources/test/fixtures/flake8_bandit/S108.py

+16
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,19 @@
1414
# not ok by config
1515
with open("/foo/bar", "w") as f:
1616
f.write("def")
17+
18+
# Using `tempfile` module should be ok
19+
import tempfile
20+
from tempfile import TemporaryDirectory
21+
22+
with tempfile.NamedTemporaryFile(dir="/tmp") as f:
23+
f.write(b"def")
24+
25+
with tempfile.NamedTemporaryFile(dir="/var/tmp") as f:
26+
f.write(b"def")
27+
28+
with tempfile.TemporaryDirectory(dir="/dev/shm") as d:
29+
pass
30+
31+
with TemporaryDirectory(dir="/tmp") as d:
32+
pass

crates/ruff/src/checkers/ast/analyze/expression.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1229,13 +1229,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
12291229
}
12301230
}
12311231
if checker.enabled(Rule::HardcodedTempFile) {
1232-
if let Some(diagnostic) = flake8_bandit::rules::hardcoded_tmp_directory(
1233-
expr,
1234-
value,
1235-
&checker.settings.flake8_bandit.hardcoded_tmp_directory,
1236-
) {
1237-
checker.diagnostics.push(diagnostic);
1238-
}
1232+
flake8_bandit::rules::hardcoded_tmp_directory(checker, expr, value);
12391233
}
12401234
if checker.enabled(Rule::UnicodeKindPrefix) {
12411235
pyupgrade::rules::unicode_kind_prefix(checker, expr, kind.as_deref());

crates/ruff/src/rules/flake8_bandit/rules/hardcoded_tmp_directory.rs

+31-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use ruff_python_ast::{Expr, Ranged};
1+
use ruff_python_ast::{self as ast, Expr, Ranged};
22

33
use ruff_diagnostics::{Diagnostic, Violation};
44
use ruff_macros::{derive_message_formats, violation};
55

6+
use crate::checkers::ast::Checker;
7+
68
/// ## What it does
79
/// Checks for the use of hardcoded temporary file or directory paths.
810
///
@@ -49,19 +51,33 @@ impl Violation for HardcodedTempFile {
4951
}
5052

5153
/// S108
52-
pub(crate) fn hardcoded_tmp_directory(
53-
expr: &Expr,
54-
value: &str,
55-
prefixes: &[String],
56-
) -> Option<Diagnostic> {
57-
if prefixes.iter().any(|prefix| value.starts_with(prefix)) {
58-
Some(Diagnostic::new(
59-
HardcodedTempFile {
60-
string: value.to_string(),
61-
},
62-
expr.range(),
63-
))
64-
} else {
65-
None
54+
pub(crate) fn hardcoded_tmp_directory(checker: &mut Checker, expr: &Expr, value: &str) {
55+
if !checker
56+
.settings
57+
.flake8_bandit
58+
.hardcoded_tmp_directory
59+
.iter()
60+
.any(|prefix| value.starts_with(prefix))
61+
{
62+
return;
63+
}
64+
65+
if let Some(Expr::Call(ast::ExprCall { func, .. })) =
66+
checker.semantic().current_expression_parent()
67+
{
68+
if checker
69+
.semantic()
70+
.resolve_call_path(func)
71+
.is_some_and(|call_path| matches!(call_path.as_slice(), ["tempfile", ..]))
72+
{
73+
return;
74+
}
6675
}
76+
77+
checker.diagnostics.push(Diagnostic::new(
78+
HardcodedTempFile {
79+
string: value.to_string(),
80+
},
81+
expr.range(),
82+
));
6783
}

0 commit comments

Comments
 (0)