Skip to content

Commit 7a10a40

Browse files
authored
[flake8-bandit] Permit suspicious imports within stub files (S4) (#15822)
## Summary Permits suspicious imports (the `S4` namespaced diagnostics) from stub files. Closes #15207. ## Test Plan Added tests and ran `cargo nextest run`. The test files are copied from the `.py` variants.
1 parent 3125332 commit 7a10a40

30 files changed

+128
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import telnetlib
2+
from telnetlib import Telnet
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import ftplib
2+
from ftplib import FTP
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import dill
2+
from dill import objects
3+
import shelve
4+
from shelve import open
5+
import cPickle
6+
from cPickle import load
7+
import pickle
8+
from pickle import load
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import subprocess
2+
from subprocess import Popen
3+
from subprocess import Popen as pop
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import xml.etree.cElementTree
2+
from xml.etree import cElementTree
3+
import xml.etree.ElementTree
4+
from xml.etree import ElementTree
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from xml import sax
2+
import xml.sax as xmls
3+
import xml.sax
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from xml.dom import expatbuilder
2+
import xml.dom.expatbuilder
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from xml.dom.minidom import parseString
2+
import xml.dom.minidom
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from xml.dom.pulldom import parseString
2+
import xml.dom.pulldom
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import lxml
2+
from lxml import etree
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import xmlrpc
2+
from xmlrpc import server
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from twisted.web.twcgi import CGIScript
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Crypto.Hash
2+
from Crypto.Hash import MD2
3+
import Crypto.PublicKey
4+
from Crypto.PublicKey import RSA
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import pyghmi
2+
from pyghmi import foo

crates/ruff_linter/src/rules/flake8_bandit/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,33 @@ mod tests {
5252
#[test_case(Rule::SuspiciousNonCryptographicRandomUsage, Path::new("S311.py"))]
5353
#[test_case(Rule::SuspiciousTelnetUsage, Path::new("S312.py"))]
5454
#[test_case(Rule::SuspiciousTelnetlibImport, Path::new("S401.py"))]
55+
#[test_case(Rule::SuspiciousTelnetlibImport, Path::new("S401.pyi"))]
5556
#[test_case(Rule::SuspiciousFtplibImport, Path::new("S402.py"))]
57+
#[test_case(Rule::SuspiciousFtplibImport, Path::new("S402.pyi"))]
5658
#[test_case(Rule::SuspiciousPickleImport, Path::new("S403.py"))]
59+
#[test_case(Rule::SuspiciousPickleImport, Path::new("S403.pyi"))]
5760
#[test_case(Rule::SuspiciousSubprocessImport, Path::new("S404.py"))]
61+
#[test_case(Rule::SuspiciousSubprocessImport, Path::new("S404.pyi"))]
5862
#[test_case(Rule::SuspiciousXmlEtreeImport, Path::new("S405.py"))]
63+
#[test_case(Rule::SuspiciousXmlEtreeImport, Path::new("S405.pyi"))]
5964
#[test_case(Rule::SuspiciousXmlSaxImport, Path::new("S406.py"))]
65+
#[test_case(Rule::SuspiciousXmlSaxImport, Path::new("S406.pyi"))]
6066
#[test_case(Rule::SuspiciousXmlExpatImport, Path::new("S407.py"))]
67+
#[test_case(Rule::SuspiciousXmlExpatImport, Path::new("S407.pyi"))]
6168
#[test_case(Rule::SuspiciousXmlMinidomImport, Path::new("S408.py"))]
69+
#[test_case(Rule::SuspiciousXmlMinidomImport, Path::new("S408.pyi"))]
6270
#[test_case(Rule::SuspiciousXmlPulldomImport, Path::new("S409.py"))]
71+
#[test_case(Rule::SuspiciousXmlPulldomImport, Path::new("S409.pyi"))]
6372
#[test_case(Rule::SuspiciousLxmlImport, Path::new("S410.py"))]
73+
#[test_case(Rule::SuspiciousLxmlImport, Path::new("S410.pyi"))]
6474
#[test_case(Rule::SuspiciousXmlrpcImport, Path::new("S411.py"))]
75+
#[test_case(Rule::SuspiciousXmlrpcImport, Path::new("S411.pyi"))]
6576
#[test_case(Rule::SuspiciousHttpoxyImport, Path::new("S412.py"))]
77+
#[test_case(Rule::SuspiciousHttpoxyImport, Path::new("S412.pyi"))]
6678
#[test_case(Rule::SuspiciousPycryptoImport, Path::new("S413.py"))]
79+
#[test_case(Rule::SuspiciousPycryptoImport, Path::new("S413.pyi"))]
6780
#[test_case(Rule::SuspiciousPyghmiImport, Path::new("S415.py"))]
81+
#[test_case(Rule::SuspiciousPyghmiImport, Path::new("S415.pyi"))]
6882
#[test_case(Rule::TryExceptContinue, Path::new("S112.py"))]
6983
#[test_case(Rule::TryExceptPass, Path::new("S110.py"))]
7084
#[test_case(Rule::UnixCommandWildcardInjection, Path::new("S609.py"))]

crates/ruff_linter/src/rules/flake8_bandit/rules/suspicious_imports.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ impl Violation for SuspiciousPyghmiImport {
352352

353353
/// S401, S402, S403, S404, S405, S406, S407, S408, S409, S410, S411, S412, S413, S415
354354
pub(crate) fn suspicious_imports(checker: &mut Checker, stmt: &Stmt) {
355+
// Skip stub files.
356+
if checker.source_type.is_stub() {
357+
return;
358+
}
359+
355360
match stmt {
356361
Stmt::Import(ast::StmtImport { names, .. }) => {
357362
for name in names {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
snapshot_kind: text
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
snapshot_kind: text
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
snapshot_kind: text
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
snapshot_kind: text
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
snapshot_kind: text
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
snapshot_kind: text
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
snapshot_kind: text
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
snapshot_kind: text
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
snapshot_kind: text
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
snapshot_kind: text
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
snapshot_kind: text
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
snapshot_kind: text
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
snapshot_kind: text
4+
---
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
3+
snapshot_kind: text
4+
---
5+

0 commit comments

Comments
 (0)