Skip to content

Commit a9702d8

Browse files
committed
[flake8-bandit] Stabilize string literal binding resolution (S310) (#26944)
Preview feature: #21469 Rule documentation: https://docs.astral.sh/ruff/rules/suspicious-url-open-usage/
1 parent 40e43bc commit a9702d8

4 files changed

Lines changed: 7 additions & 206 deletions

File tree

‎crates/ruff_linter/src/preview.rs‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,6 @@ pub(crate) const fn is_enumerate_for_loop_int_index_enabled(settings: &LinterSet
250250
settings.preview.is_enabled()
251251
}
252252

253-
// https://github.com/astral-sh/ruff/pull/21469
254-
pub(crate) const fn is_s310_resolve_string_literal_bindings_enabled(
255-
settings: &LinterSettings,
256-
) -> bool {
257-
settings.preview.is_enabled()
258-
}
259-
260253
// https://github.com/astral-sh/ruff/pull/22419
261254
pub(crate) const fn is_py315_support_enabled(settings: &LinterSettings) -> bool {
262255
settings.preview.is_enabled()

‎crates/ruff_linter/src/rules/flake8_bandit/rules/suspicious_function_call.rs‎

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ use ruff_text_size::{Ranged, TextRange};
1010

1111
use crate::Violation;
1212
use crate::checkers::ast::Checker;
13-
use crate::preview::{
14-
is_s310_resolve_string_literal_bindings_enabled, is_suspicious_function_reference_enabled,
15-
};
16-
use crate::settings::LinterSettings;
13+
use crate::preview::is_suspicious_function_reference_enabled;
1714

1815
/// ## What it does
1916
/// Checks for calls to `pickle` functions or modules that wrap them.
@@ -1021,13 +1018,8 @@ fn suspicious_function(
10211018
}
10221019

10231020
/// Resolves `expr` to its binding and checks if the resolved expression starts with an HTTP or HTTPS prefix.
1024-
fn expression_starts_with_http_prefix(
1025-
expr: &Expr,
1026-
semantic: &SemanticModel,
1027-
settings: &LinterSettings,
1028-
) -> bool {
1029-
let resolved_expression = if is_s310_resolve_string_literal_bindings_enabled(settings)
1030-
&& let Some(name_expr) = expr.as_name_expr()
1021+
fn expression_starts_with_http_prefix(expr: &Expr, semantic: &SemanticModel) -> bool {
1022+
let resolved_expression = if let Some(name_expr) = expr.as_name_expr()
10311023
&& let Some(binding_id) = semantic.only_binding(name_expr)
10321024
&& let Some(value) = find_binding_value(semantic.binding(binding_id), semantic)
10331025
{
@@ -1170,11 +1162,7 @@ fn suspicious_function(
11701162
.all(|keyword| keyword.arg.is_some())
11711163
{
11721164
if let Some(url_expr) = arguments.find_argument_value("url", 0)
1173-
&& expression_starts_with_http_prefix(
1174-
url_expr,
1175-
checker.semantic(),
1176-
checker.settings(),
1177-
)
1165+
&& expression_starts_with_http_prefix(url_expr, checker.semantic())
11781166
{
11791167
return;
11801168
}
@@ -1211,23 +1199,15 @@ fn suspicious_function(
12111199
}) =>
12121200
{
12131201
if let Some(url_expr) = arguments.find_argument_value("url", 0)
1214-
&& expression_starts_with_http_prefix(
1215-
url_expr,
1216-
checker.semantic(),
1217-
checker.settings(),
1218-
)
1202+
&& expression_starts_with_http_prefix(url_expr, checker.semantic())
12191203
{
12201204
return;
12211205
}
12221206
}
12231207

12241208
// If the `url` argument is a string literal (including resolved bindings), allow `http` and `https` schemes.
12251209
Some(expr)
1226-
if expression_starts_with_http_prefix(
1227-
expr,
1228-
checker.semantic(),
1229-
checker.settings(),
1230-
) =>
1210+
if expression_starts_with_http_prefix(expr, checker.semantic()) =>
12311211
{
12321212
return;
12331213
}

‎crates/ruff_linter/src/rules/flake8_bandit/snapshots/ruff_linter__rules__flake8_bandit__tests__S310_S310.py.snap‎

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -254,84 +254,3 @@ S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom sch
254254
42 | urllib.request.urlopen(urllib.request.Request(url))
255255
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
256256
|
257-
258-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
259-
--> S310.py:51:1
260-
|
261-
49 | # https://github.com/astral-sh/ruff/issues/21462
262-
50 | path = "https://example.com/data.csv"
263-
51 | urllib.request.urlretrieve(path, "data.csv")
264-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
265-
52 | url = "https://example.com/api"
266-
53 | urllib.request.Request(url)
267-
|
268-
269-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
270-
--> S310.py:53:1
271-
|
272-
51 | urllib.request.urlretrieve(path, "data.csv")
273-
52 | url = "https://example.com/api"
274-
53 | urllib.request.Request(url)
275-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
276-
54 |
277-
55 | # Test resolved f-strings and concatenated string literals
278-
|
279-
280-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
281-
--> S310.py:57:1
282-
|
283-
55 | # Test resolved f-strings and concatenated string literals
284-
56 | fstring_url = f"https://example.com/data.csv"
285-
57 | urllib.request.urlopen(fstring_url)
286-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
287-
58 | urllib.request.Request(fstring_url)
288-
|
289-
290-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
291-
--> S310.py:58:1
292-
|
293-
56 | fstring_url = f"https://example.com/data.csv"
294-
57 | urllib.request.urlopen(fstring_url)
295-
58 | urllib.request.Request(fstring_url)
296-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
297-
59 |
298-
60 | concatenated_url = "https://" + "example.com/data.csv"
299-
|
300-
301-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
302-
--> S310.py:61:1
303-
|
304-
60 | concatenated_url = "https://" + "example.com/data.csv"
305-
61 | urllib.request.urlopen(concatenated_url)
306-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
307-
62 | urllib.request.Request(concatenated_url)
308-
|
309-
310-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
311-
--> S310.py:62:1
312-
|
313-
60 | concatenated_url = "https://" + "example.com/data.csv"
314-
61 | urllib.request.urlopen(concatenated_url)
315-
62 | urllib.request.Request(concatenated_url)
316-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
317-
63 |
318-
64 | nested_concatenated = "http://" + "example.com" + "/data.csv"
319-
|
320-
321-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
322-
--> S310.py:65:1
323-
|
324-
64 | nested_concatenated = "http://" + "example.com" + "/data.csv"
325-
65 | urllib.request.urlopen(nested_concatenated)
326-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
327-
66 | urllib.request.Request(nested_concatenated)
328-
|
329-
330-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
331-
--> S310.py:66:1
332-
|
333-
64 | nested_concatenated = "http://" + "example.com" + "/data.csv"
334-
65 | urllib.request.urlopen(nested_concatenated)
335-
66 | urllib.request.Request(nested_concatenated)
336-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
337-
|

‎crates/ruff_linter/src/rules/flake8_bandit/snapshots/ruff_linter__rules__flake8_bandit__tests__preview__S310_S310.py.snap‎

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -6,100 +6,9 @@ source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
66
+linter.preview = enabled
77

88
--- Summary ---
9-
Removed: 8
9+
Removed: 0
1010
Added: 2
1111

12-
--- Removed ---
13-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
14-
--> S310.py:51:1
15-
|
16-
49 | # https://github.com/astral-sh/ruff/issues/21462
17-
50 | path = "https://example.com/data.csv"
18-
51 | urllib.request.urlretrieve(path, "data.csv")
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20-
52 | url = "https://example.com/api"
21-
53 | urllib.request.Request(url)
22-
|
23-
24-
25-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
26-
--> S310.py:53:1
27-
|
28-
51 | urllib.request.urlretrieve(path, "data.csv")
29-
52 | url = "https://example.com/api"
30-
53 | urllib.request.Request(url)
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
32-
54 |
33-
55 | # Test resolved f-strings and concatenated string literals
34-
|
35-
36-
37-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
38-
--> S310.py:57:1
39-
|
40-
55 | # Test resolved f-strings and concatenated string literals
41-
56 | fstring_url = f"https://example.com/data.csv"
42-
57 | urllib.request.urlopen(fstring_url)
43-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44-
58 | urllib.request.Request(fstring_url)
45-
|
46-
47-
48-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
49-
--> S310.py:58:1
50-
|
51-
56 | fstring_url = f"https://example.com/data.csv"
52-
57 | urllib.request.urlopen(fstring_url)
53-
58 | urllib.request.Request(fstring_url)
54-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
55-
59 |
56-
60 | concatenated_url = "https://" + "example.com/data.csv"
57-
|
58-
59-
60-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
61-
--> S310.py:61:1
62-
|
63-
60 | concatenated_url = "https://" + "example.com/data.csv"
64-
61 | urllib.request.urlopen(concatenated_url)
65-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66-
62 | urllib.request.Request(concatenated_url)
67-
|
68-
69-
70-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
71-
--> S310.py:62:1
72-
|
73-
60 | concatenated_url = "https://" + "example.com/data.csv"
74-
61 | urllib.request.urlopen(concatenated_url)
75-
62 | urllib.request.Request(concatenated_url)
76-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
77-
63 |
78-
64 | nested_concatenated = "http://" + "example.com" + "/data.csv"
79-
|
80-
81-
82-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
83-
--> S310.py:65:1
84-
|
85-
64 | nested_concatenated = "http://" + "example.com" + "/data.csv"
86-
65 | urllib.request.urlopen(nested_concatenated)
87-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
88-
66 | urllib.request.Request(nested_concatenated)
89-
|
90-
91-
92-
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
93-
--> S310.py:66:1
94-
|
95-
64 | nested_concatenated = "http://" + "example.com" + "/data.csv"
96-
65 | urllib.request.urlopen(nested_concatenated)
97-
66 | urllib.request.Request(nested_concatenated)
98-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
99-
|
100-
101-
102-
10312
--- Added ---
10413
S310 Audit URL open for permitted schemes. Allowing use of `file:` or custom schemes is often unexpected.
10514
--> S310.py:46:5

0 commit comments

Comments
 (0)