PyCQA / PyCQA/bandit

B614 False Positive when using torch.jit.load

Open
#1,293 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
8.3k
Forks
836
Avg merge
5d 3h
Merged PRs (30d)
1

Description

Describe the bug

B614 checks for the unsafe use of torch.load with the weights_only parameter omitted or set to False.

model = torch.load('model_weights.pth')

A false positive is triggered when loading the equivalent TorchScript load function which has no equivalent weights_only parameter.

https://docs.pytorch.org/docs/stable/generated/torch.jit.load.html

model = torch.jit.load('model_weights.pth')

Note also that torch.load uses weights_only=True by default as of version 2.6 and so B614 should query the torch version and allow torch.load without a user-set weights_only parameter as of version 2.6.

https://docs.pytorch.org/docs/stable/notes/serialization.html#weights-only

Reproduction steps
import torch
model = torch.jit.load('model_weights.pth')
Expected behavior

The code above should not trigger B614.

Bandit version

1.8.3 (Default)

Python version

3.13 (Default)

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the B614 check and reproduce the reported false positive with torch.jit.load. Update the check so TorchScript loading is not flagged, and account for torch.load's weights_only default from version 2.6; done means the reproduction no longer triggers B614 while unsafe older behavior remains covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
security
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.