torch.load may unpickle code from the file

tx0/python:torch-load-unrestrictedrecommendedwarningsecurity, cwe-502

What it finds

torch.load(path): without weights_only=True, PyTorch reads a checkpoint with pickle, which runs any code the file names, so loading a model from someone else can take over the program (CWE-502). PyTorch 2.6 made weights_only=True the default, but code that must run on older versions, or that passes False, is open.

Calls with weights_only=True are left alone, and so are calls that pass **kwargs.

From the rule's source, torch_load_unrestricted.rs.

Example from the rule's tests
import torch  def load(path, options):    a = torch.load(path)tx0 reports: torch.load without weights_only=True may run code the file names    b = torch.load(path, weights_only=False)tx0 reports: torch.load with weights_only=False runs any code    c = torch.load(path, weights_only=True)    d = torch.load(path, **options)    return a, b, c, d
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:torch-load-unrestricted" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:torch-load-unrestricted] reason="..."

tx0 explain tx0/python:torch-load-unrestricted shows the same in the terminal. Found a false positive? Report it.