Skip to content

Limit B614 to torch.load deserializers - #1348

Merged
ericwb merged 2 commits into
PyCQA:mainfrom
dibussoc:bugfix/false-pos-B614torch
Jan 11, 2026
Merged

ericwb merged 2 commits into
PyCQA:mainfrom
dibussoc:bugfix/false-pos-B614torch

Conversation

@dibussoc

Copy link
Copy Markdown
Contributor

Summary

  • Fixed B614 false positives by matching only torch.load and torch.serialization.load.
  • The new check uses an explicit allowlist, as opposed to checking 'torch.*.load', which will need to be updated if PyTorch adds additional pickle data deserializers—trade-off for reducing false positives.
  • torch.serialization.load is covered by the same B614 logic as torch.load; no separate example added, but can be added if needed.
  • Update docstrings to reflect the exact APIs and CWE.
  • Add a non-triggering torch.utils.cpp_extension.load example to prevent regressions.

Testing

  • tox run -e py310
  • tox run -e pep8

Note: tox run -e cover fails locally due to missing coverage combine (pre-existing).

Resolves: #1343

@ericwb ericwb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks mostly good, just recommend removing the unnecessary conditional in the unit test.

Comment thread examples/pytorch_load.py Outdated

# Example of a torch.*.load call that should NOT trigger B614
# Only pickle deserializers should trigger B614
if False: # Static analysis only; does not execute

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line really isn't needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the correction, should be updated now.

Avoids false positives for torch.*.load helpers such as
torch.utils.cpp_extension.load while preserving checks for
torch.load and torch.serialization.load.
Updated docstrings and example to reflect expected behavior.

Resolves: PyCQA#1343
@dibussoc
dibussoc force-pushed the bugfix/false-pos-B614torch branch from 41e879b to 5bc9ce0 Compare January 11, 2026 08:44
Comment thread examples/pytorch_load.py Resolved
@ericwb
ericwb merged commit 765f00d into PyCQA:main Jan 11, 2026
@dibussoc
dibussoc deleted the bugfix/false-pos-B614torch branch January 11, 2026 22:13
DevamShah added a commit to DevamShah/bandit that referenced this pull request Sep 18, 2026
B614 treated only a literal weights_only=True as safe, so
torch.load(path, weights_only=flag) was reported at HIGH confidence even
though nothing about the value is known statically.

An earlier revision of this change suppressed the finding entirely when
weights_only was a non-ast.Constant expression, following B615. That was
wrong: unlike B615's revision pin, weights_only is a boolean whose unsafe
value is frequently a local literal, so suppressing every non-constant
also silenced demonstrably unsafe calls such as

    unsafe = False
    torch.load(path, weights_only=unsafe)

Instead, keep reporting the call but drop its confidence from HIGH to
MEDIUM when weights_only is present and not an ast.Constant. No finding
is lost, and callers who do not want statically unresolvable results can
filter them with --confidence-level high. Calls with a literal
weights_only, with **kwargs, or with no weights_only at all keep HIGH
confidence.

Also adds example coverage that torch.jit.load (TorchScript, not pickle,
and outside B614's matched set since PyCQA#1348) is not flagged, pinning the
behaviour reported in PyCQA#1293.

Refs PyCQA#1293

Signed-off-by: devamshah <devamshah91@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

B614: false positive on torch.utils.cpp_extension.load

2 participants