Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[GHA] Enable bandit scans (#34779)

jszczepadb49357122e5
diff --git a/.github/requirements_bandit.txt b/.github/requirements_bandit.txt
new file mode 100644
index 00000000000..837ec6eea35
--- /dev/null
+++ b/.github/requirements_bandit.txt
@@ -0,0 +1,7 @@
+bandit==1.9.2
+markdown-it-py==4.2.0
+mdurl==0.1.2
+Pygments==2.21.0
+PyYAML==6.0.3
+rich==15.0.0
+stevedore==5.9.1
diff --git a/.github/workflows/sdl.yml b/.github/workflows/sdl.yml
new file mode 100644
index 00000000000..5d19822f54e
--- /dev/null
+++ b/.github/workflows/sdl.yml
@@ -0,0 +1,41 @@
+name: sdl
+permissions: read-all
+
+on:
+ workflow_dispatch:
+ push:
+ branches:
+ - master
+ - 'releases/**'
+ pull_request:
+ types:
+ - opened
+ - synchronize
+ - reopened
+ - ready_for_review
+ merge_group:
+concurrency:
+ group: ${{ github.workflow }}-${{ github.event_name == 'push' && github.run_id || github.ref }}
+ cancel-in-progress: true
+
+ if: github.event.pull_request.draft == false || github.run_attempt > 1
+ timeout-minutes: 10
+
+jobs:
+ bandit:
+ name: Bandit
+ runs-on: ubuntu-latest
+ if: github.event.pull_request.draft == false || github.run_attempt > 1
+ timeout-minutes: 10
+ defaults:
+ run:
+ shell: bash
+ steps:
+ - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+ - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
+ with:
+ python-version: 3.12
+ - name: Install bandit
+ run: python3 -m venv venv && . venv/bin/activate && pip install -r .github/requirements_bandit.txt
+ - name: Run bandit
+ run: . venv/bin/activate && bandit -c pyproject.toml -r .
diff --git a/pyproject.toml b/pyproject.toml
index c3ee68656d0..e0c01cd0c53 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -66,3 +66,40 @@ reportFunctionMemberAccess = false
reportAttributeAccessIssue = false
reportMissingImports = false
reportIncompatibleMethodOverride = false
+
+[tool.bandit]
+exclude_dirs = ["docs", "tests", "thirdparty", "samples", ".github"]
+skips = [
+ 'B101',
+ 'B102',
+ 'B103',
+ 'B104',
+ 'B105',
+ 'B106',
+ 'B107',
+ 'B108',
+ 'B110',
+ 'B112',
+ 'B201',
+ 'B501',
+ 'B502',
+ 'B503',
+ 'B504',
+ 'B505',
+ 'B506',
+ 'B507',
+ 'B601',
+ 'B602',
+ 'B603',
+ 'B604',
+ 'B605',
+ 'B606',
+ 'B607',
+ 'B608',
+ 'B609',
+ 'B610',
+ 'B611',
+ 'B701',
+ 'B702',
+ 'B703']
+
diff --git a/tools/ovc/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py b/tools/ovc/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py
index 19e541d5e68..7159a3e47c2 100644
--- a/tools/ovc/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py
+++ b/tools/ovc/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py
@@ -253,10 +253,19 @@ def get_pytorch_decoder_for_model_on_disk(argv, args):
return False
inputs = prepare_torch_inputs(example_inputs)
+ # SECURITY: torch.jit.load / torch.export.load unpickle, so a malicious
+ # archive can execute arbitrary code. Mitigations:
+ # 1. _is_pytorch_zip() above rejects anything that isn't a ZIP with
+ # PyTorch-specific entries, shrinking the attack surface.
+ # 2. input_model comes from the OVC CLI / Python API; the trust
+ # boundary is the caller, who already has code-exec on this host.
+ # 3. neither torch.jit.load nor torch.export.load supports a
+ # weights_only / safe mode for TorchScript / older PT2 archives.
+ log.warning("Loading PyTorch model from '%s'. PyTorch archives are deserialized via pickle; only load model files you trust.", input_model)
# attempt to load scripted model
try:
- model = torch.jit.load(input_model)
+ model = torch.jit.load(input_model) # nosec B614 - see note above
model.eval()
decoder = TorchScriptPythonDecoder(
model,
@@ -271,7 +280,7 @@ def get_pytorch_decoder_for_model_on_disk(argv, args):
# attempt to load exported model
try:
- exported_program = torch.export.load(input_model)
+ exported_program = torch.export.load(input_model) # nosec B614 - see note above
if isinstance(exported_program, torch.export.ExportedProgram):
argv.input_model = TorchFXPythonDecoder.from_exported_program(exported_program)
argv.framework = 'pytorch'