Repository navigation
fix(B614): lower confidence for unresolvable weights_only; add torch.jit.load regression coverage - #1441
fix(B614): lower confidence for unresolvable weights_only; add torch.jit.load regression coverage#1441DevamShah wants to merge 2 commits into
Conversation
9105c1e to
49b2147
Compare
|
Re-verified this against current Branch state. Head The false positive, minimal repro. import torch
def load_checkpoint(path, weights_only=True):
return torch.load(path, weights_only=weights_only)On On this branch: Precedent. This is the same defect and the same fix that #1358 ("Fix B615 false positive when revision is set via variable", Behaviour matrix I actually ran (
The suppression is scoped to an explicit non-literal Tests. On Why it shows as blocked. @ericwb — you reviewed and merged both #1348 (the B614 scoping) and #1358 (this exact fix for B615), so you have the most context here. Would you approve the workflow run and take a look? Happy to split the |
noqt
left a comment
There was a problem hiding this comment.
NOQT independently tested current head 49b2147. There's a blocking false-negative regression in the new non-literal early return.
unsafe_flag = False
torch.load("model.pt", weights_only=unsafe_flag)
def load(path, weights_only=False):
return torch.load(path, weights_only=weights_only)Unchanged base f8b9f1a reports B614 at both call sites; this head reports neither. In a four-case probe containing those two unsafe cases plus matching True controls, base reported four findings and this head reported zero. So the change doesn't just avoid uncertain false positives—it also suppresses values that are locally and demonstrably unsafe.
Please keep the useful torch.jit.load regression, but don't treat every non-ast.Constant value as safe. The existing finding could remain; if precision is the goal, lower confidence for unresolved expressions or resolve simple assignments/defaults, and add both unsafe cases as regressions.
The targeted B614 functional test passes. The full functional module is 77/79 on both head and base with the same two Windows-only fixture failures; plugin flake8 and git diff --check pass. Minor: examples/pytorch_load.py:43 also adds E302 (one blank line before a top-level function instead of two).
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>
49b2147 to
2434052
Compare
@noqt asked for both of his shapes as regressions. The shapes were right but the values weren't: a name bound to bool(models) and a parameter defaulting to True are not what he reported. Use his values -- a name bound to False, and a parameter defaulting to False. B614 does no dataflow, so the call site sees an ast.Name and an ast.Name either way and both stay at MEDIUM confidence. Findings are unchanged: five, three at HIGH and two at MEDIUM, matching the existing test expectation. Signed-off-by: devamshah <devamshah91@gmail.com>
|
You're right, and thanks for actually running it — same numbers here, four findings on base and zero on that head. The B615 parallel I leaned on doesn't hold up. A revision pin is normally something you compute or look up, so benefit of the doubt is fair there, but Rewritten to keep the finding and only drop its confidence from HIGH to MEDIUM when Rebased onto |
Summary
Improves the B614 ("unsafe PyTorch load") check on two fronts, both reducing false positives:
weights_onlyis a non-literal expression (a variable, attribute, subscript, call, etc.). When the value cannot be resolved statically, the check now gives the caller the benefit of the doubt rather than emitting a finding — mirroring the existing behaviour of B615 (huggingface_unsafe_download), which uses the sameweights_only/ non-literal handling.torch.jit.loadis not flagged (refs B614 False Positive when using torch.jit.load #1293).torch.jit.loaduses TorchScript serialization, notpickle, and is correctly outside B614's matched set (torch.load/torch.serialization.load). The example file now documents this so the behaviour reported in B614 False Positive when using torch.jit.load #1293 is locked in by a test fixture.Problem / motivation
weights_only(the substantive fix): todaytorch.load(f, weights_only=flag)is flagged because the check only treats the literalTrue/"True"as safe. Real code frequently passesweights_onlyas a computed value (config flag, function arg), and flagging those is a false positive. B615 already solved the identical problem the same way; this brings B614 into line.torch.jit.load(B614 False Positive when using torch.jit.load #1293): users reported B614 firing ontorch.jit.load. On currentmainits qualified name (torch.jit.load) is not in B614's matched set, so it is already not flagged — this PR adds explicit example coverage so that stays true.Change
bandit/plugins/pytorch_load.py: when the matched call (torch.load/torch.serialization.load) passesweights_onlyas a non-ast.Constantvalue, return without a finding. The existingweights_only == True/"True"safe-path and the genuine-unsafe path are unchanged. Uses the samecontext._context.get("call")keyword-inspection idiom as the merged B615 plugin.examples/pytorch_load.py: adds non-triggering cases —torch.jit.load(with and withoutmap_location) and a non-literalweights_only— alongside the existing genuinely-unsafe calls.Trade-off (intentional, precedented)
Suppressing on a non-literal
weights_onlymeanstorch.load(f, weights_only=flag)whereflagisFalseat runtime will not be flagged (a false negative). This matches B615's deliberate "benefit of the doubt" stance for unresolvable arguments — favouring precision over recall for low-confidence static cases. The version-aware behaviour also requested in #1293 (treatingweights_onlyas defaulting toTrueon torch ≥ 2.6) is intentionally out of scope here and left for a follow-up, since it requires version detection.Testing / validation
tests/functional/test_functional.py::FunctionalTests::test_pytorch_loadpasses with the unchanged expectation (MEDIUM: 3,HIGH: 3) — the added cases correctly yield zero new findings.examples/pytorch_load.py: B614 fires only on the three genuinely-unsafetorch.loadcalls; it does not fire ontorch.jit.loador the non-literalweights_onlycase.