
    >/iZ
                         S r SSKrSSKJr  SSKJr  \R                  " S5      \R                  " S5      S 5       5       rg)a4  
==================================
B614: Test for unsafe PyTorch load
==================================

This plugin checks for unsafe use of `torch.load`. Using `torch.load` with
untrusted data can lead to arbitrary code execution. There are two safe
alternatives:

1. Use `torch.load` with `weights_only=True` where only tensor data is
   extracted, and no arbitrary Python objects are deserialized
2. Use the `safetensors` library from huggingface, which provides a safe
   deserialization mechanism

With `weights_only=True`, PyTorch enforces a strict type check, ensuring
that only torch.Tensor objects are loaded.

:Example:

.. code-block:: none

        >> Issue: Use of unsafe PyTorch load
        Severity: Medium   Confidence: High
        CWE: CWE-94 (https://cwe.mitre.org/data/definitions/94.html)
        Location: examples/pytorch_load_save.py:8
        7    loaded_model.load_state_dict(torch.load('model_weights.pth'))
        8    another_model.load_state_dict(torch.load('model_weights.pth',
                map_location='cpu'))
        9
        10   print("Model loaded successfully!")

.. seealso::

     - https://cwe.mitre.org/data/definitions/94.html
     - https://pytorch.org/docs/stable/generated/torch.load.html#torch.load
     - https://github.com/huggingface/safetensors

.. versionadded:: 1.7.10

    N)issue)test_propertiesCallB614c           	         U R                  S5      nU R                  nU(       d  [        U[        5      (       a  gUR	                  S5      nUS   n[        SU;   US:H  /5      (       ax  U R                  S5      nUS:X  d  USL a  g[        R                  " [        R                  [        R                  S	[        R                  R                  U R                  S5      S
9$ g)z
This plugin checks for unsafe use of `torch.load`. Using `torch.load`
with untrusted data can lead to arbitrary code execution. The safe
alternative is to use `weights_only=True` or the safetensors library.
torchN.loadweights_onlyTrueTzUse of unsafe PyTorch load)severity
confidencetextcwelineno)is_module_imported_exactcall_function_name_qual
isinstancestrsplitallget_call_arg_valuebanditIssueMEDIUMHIGHr   Cwe!DESERIALIZATION_OF_UNTRUSTED_DATAget_lineno_for_call_arg)contextimportedqualnamequalname_listfuncr   s         U/home/james-whalen/.local/lib/python3.13/site-packages/bandit/plugins/pytorch_load.pypytorch_loadr'   1   s     //8H..H
8S11NN3'MD
}$FN	
  11.A6!\T%9||]]{{-		;;226:
 	
    )	__doc__r   bandit.corer   r   testcheckstest_idr'    r(   r&   <module>r/      sD   'P   / Vf
  
r(   