Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a090753

Browse files
authored
chore(dev-tools): port bandit config to ruff (NVIDIA#1033)
* chore(deps): run ruff with config toml * chore(autofix): ruff formatting * chore: port bandit config to ruff * ci: port bandit scan to ruff * chore: port bandit ignore comments to ruff * chore: remove bandit from pre-commit config * chore: let config fall through * chore: unify bandit config for cuda_pathfinder * chore(autofix): imports * ci: compute codes so that we do not have to remember to keep things in sync * chore: remove unused nosec comments * chore: set up errors and use uvx * ci: pass `check` to ruff args * ci: install uv to pick up uvx * chore: fix misspelling * ci: use hash to install setup-uv action * ci: simplify munging
1 parent dbde2b4 commit a090753

File tree

11 files changed

+51
-33
lines changed

11 files changed

+51
-33
lines changed

.bandit

Lines changed: 0 additions & 5 deletions
This file was deleted.

.github/workflows/bandit.yml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,27 @@ jobs:
1919
permissions:
2020
security-events: write
2121
steps:
22-
- name: Perform Bandit Analysis
23-
# KEEP IN SYNC WITH bandit rev in .pre-commit-config.yaml
24-
# Current runner uses Python 3.8, so the action installs bandit==1.7.10
25-
# via `pip install bandit[sarif]`. If runner Python moves to >=3.9,
26-
# the action will resolve to 1.8.x and you'll need to bump pre-commit.
27-
# (Bandit >=1.8.0 dropped Python 3.8 via Requires-Python metadata.)
28-
uses: PyCQA/bandit-action@8a1b30610f61f3f792fe7556e888c9d7dffa52de # v1.0.0
22+
- name: Checkout
23+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
24+
25+
- name: Install uv
26+
uses: astral-sh/setup-uv@b75a909f75acd358c2196fb9a5f1299a9a8868a4 # v6.7.0
27+
28+
- name: Get ignore codes
29+
id: ignore-codes
30+
# This are computed so that we can run only the `S` (bandit)
31+
# checks. Passing --select to ruff overrides any config files
32+
# (ruff.toml, pyproject.toml, etc), so to avoid having keep everything
33+
# in sync we grab them from the TOML programmatically
34+
run: |
35+
set -euxo pipefail
36+
37+
echo "codes=$(uvx toml2json ./ruff.toml | jq -r '.lint.ignore | map(select(test("^S\\d+"))) | join(",")')" >> "$GITHUB_OUTPUT"
38+
- name: Perform Bandit Analysis using Ruff
39+
uses: astral-sh/ruff-action@57714a7c8a2e59f32539362ba31877a1957dded1 # v3.5.1
40+
with:
41+
args: "check --select S --ignore ${{ steps.ignore-codes.outputs.codes }} --output-format sarif --output-file results.sarif"
42+
- name: Upload SARIF file
43+
uses: github/codeql-action/upload-sarif@v3
44+
with:
45+
sarif_file: results.sarif

.pre-commit-config.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@ repos:
6464
- id: rst-directive-colons
6565
- id: rst-inline-touching-normal
6666

67-
- repo: https://github.com/PyCQA/bandit
68-
rev: "36fd65054fc8864b4037d0918904f9331512feb5" # frozen: 1.7.10 KEEP IN SYNC WITH .github/workflows/bandit.yml
69-
hooks:
70-
- id: bandit
71-
args:
72-
- --ini
73-
- .bandit
74-
7567
- repo: https://github.com/pre-commit/mirrors-mypy
7668
rev: 0f86793af5ef5f6dc63c8d04a3cabfa3ea8f9c6a # frozen: v1.16.1
7769
hooks:

cuda_bindings/tests/test_cuda.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ def test_get_error_name_and_string():
653653
@pytest.mark.skipif(not callableBinary("nvidia-smi"), reason="Binary existance needed")
654654
def test_device_get_name():
655655
# TODO: Refactor this test once we have nvml bindings to avoid the use of subprocess
656-
import subprocess # nosec B404
656+
import subprocess
657657

658658
(err,) = cuda.cuInit(0)
659659
assert err == cuda.CUresult.CUDA_SUCCESS
@@ -663,8 +663,10 @@ def test_device_get_name():
663663
assert err == cuda.CUresult.CUDA_SUCCESS
664664

665665
p = subprocess.check_output(
666-
["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"], shell=False, stderr=subprocess.PIPE
667-
) # nosec B603, B607
666+
["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"], # noqa: S607
667+
shell=False,
668+
stderr=subprocess.PIPE,
669+
)
668670

669671
delimiter = b"\r\n" if platform.system() == "Windows" else b"\n"
670672
expect = p.split(delimiter)

cuda_bindings/tests/test_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import platform
55
import random
6-
import subprocess # nosec B404
6+
import subprocess
77
import sys
88
from pathlib import Path
99

@@ -72,7 +72,7 @@ def test_ptx_utils(kernel, actual_ptx_ver, min_cuda_ver):
7272
),
7373
)
7474
def test_get_handle(target):
75-
ptr = random.randint(1, 1024)
75+
ptr = random.randint(1, 1024) # noqa: S311
7676
obj = target(ptr)
7777
handle = get_cuda_native_handle(obj)
7878
assert handle == ptr
@@ -105,6 +105,6 @@ def test_get_handle_error(target):
105105
],
106106
)
107107
def test_cyclical_imports(module):
108-
subprocess.check_call( # nosec B603
108+
subprocess.check_call( # noqa: S603
109109
[sys.executable, Path(__file__).parent / "utils" / "check_cyclical_import.py", f"cuda.bindings.{module}"],
110110
)

cuda_core/tests/example_tests/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def run_example(samples_path, filename, env=None):
2929
old_sys_path = sys.path.copy()
3030
sys.path.append(samples_path)
3131
# TODO: Refactor the examples to give them a common callable `main()` to avoid needing to use exec here?
32-
exec(script, env if env else {}) # nosec B102
32+
exec(script, env if env else {}) # noqa: S102
3333
except ImportError as e:
3434
# for samples requiring any of optional dependencies
3535
for m in ("cupy", "torch"):

cuda_core/tests/test_module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
import ctypes
5-
import pickle # nosec B403, B301
5+
import pickle
66
import warnings
77

88
import cuda.core.experimental
@@ -372,7 +372,7 @@ def test_occupancy_max_potential_cluster_size(get_saxpy_kernel):
372372

373373
def test_module_serialization_roundtrip(get_saxpy_kernel):
374374
_, objcode = get_saxpy_kernel
375-
result = pickle.loads(pickle.dumps(objcode)) # nosec B403, B301
375+
result = pickle.loads(pickle.dumps(objcode)) # noqa: S403, S301
376376

377377
assert isinstance(result, ObjectCode)
378378
assert objcode.code == result.code

cuda_pathfinder/cuda/pathfinder/_utils/platform_aware.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
def quote_for_shell(s: str) -> str:
1010
if IS_WINDOWS:
11-
# This is a relatively heavy import; keep pathfinder if possible.
12-
from subprocess import list2cmdline # nosec B404
11+
# This is a relatively heavy import; keep pathfinder lean if possible.
12+
from subprocess import list2cmdline
1313

1414
return list2cmdline([s])
1515
else:

cuda_pathfinder/pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,16 @@ select = [
7373
"RUF", # Ruff-specific rules
7474
"PT", # flake8-pytest-style
7575
"DTZ", # flake8-datetimez
76+
"S",
7677
]
7778
extend-select = ["B9"]
7879

80+
ignore = [
81+
"S101", # asserts
82+
"S311", # allow use of the random.* even though many are not cryptographically secure
83+
"S404", # allow importing the subprocess module
84+
]
85+
7986
[tool.ruff.lint.flake8-quotes]
8087
inline-quotes = "double"
8188

cuda_pathfinder/tests/spawned_process_runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __call__(self):
5353
sys.stderr = old_stderr
5454
try: # noqa: SIM105
5555
self.result_queue.put((returncode, stdout, stderr))
56-
except Exception: # nosec B110
56+
except Exception: # noqa: S110
5757
# If the queue is broken (e.g., parent gone), best effort logging
5858
pass
5959

@@ -120,7 +120,7 @@ def run_in_spawned_child_process(
120120
try:
121121
result_queue.close()
122122
result_queue.join_thread()
123-
except Exception: # nosec B110
123+
except Exception: # noqa: S110
124124
pass
125125
if process.is_alive():
126126
process.kill()

0 commit comments

Comments
 (0)