<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Navid Khan (Ali)</title>
    <description>The latest articles on DEV Community by Navid Khan (Ali) (@sunwukhan).</description>
    <link>https://dev.to/sunwukhan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4259628%2F43b6bebd-0315-490d-b1f2-9574d84f21a4.png</url>
      <title>DEV Community: Navid Khan (Ali)</title>
      <link>https://dev.to/sunwukhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sunwukhan"/>
    <language>en</language>
    <item>
      <title>Pickle is code, not data: scanning your Python and ML models for unsafe deserialization</title>
      <dc:creator>Navid Khan (Ali)</dc:creator>
      <pubDate>Mon, 23 Nov 2026 03:47:59 +0000</pubDate>
      <link>https://dev.to/sunwukhan/pickle-is-code-not-data-scanning-your-python-and-ml-models-for-unsafe-deserialization-1aah</link>
      <guid>https://dev.to/sunwukhan/pickle-is-code-not-data-scanning-your-python-and-ml-models-for-unsafe-deserialization-1aah</guid>
      <description>&lt;h2&gt;
  
  
  The problem in six lines
&lt;/h2&gt;

&lt;p&gt;This is code execution, even though it does not look like it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pickle&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"model.pkl"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"rb"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pickle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# runs whatever the file's __reduce__ says&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A pickle file is a program for a small stack-based virtual machine. When you unpickle a file you did not create yourself, you run whatever opcodes the author put in it, including a &lt;code&gt;REDUCE&lt;/code&gt; to &lt;code&gt;os.system&lt;/code&gt;. This is documented behaviour, not a bug in pickle, which is exactly why it keeps biting people: the risk sits in every shared checkpoint, dataset, and joblib file, and in the ordinary-looking loader calls scattered through a codebase.&lt;/p&gt;

&lt;p&gt;It is not theoretical. CVE-2026-57845 (published 23 Oct 2026, CVSS 8.8): Hugging Face &lt;code&gt;datasets&lt;/code&gt; through 5.1.0, the latest release, routes &lt;code&gt;load_dataset("pandas", data_files=...)&lt;/code&gt; to &lt;code&gt;pandas.read_pickle()&lt;/code&gt;. A crafted pickle runs arbitrary Python with the victim's privileges the moment the dataset loads. The upstream fix (huggingface/datasets#8879) disables the pandas builder, but no patched release exists yet.&lt;/p&gt;

&lt;p&gt;I built &lt;a href="https://github.com/Sun-Wukhan/pickle-sentry" rel="noopener noreferrer"&gt;pickle-sentry&lt;/a&gt; to find both sides of this: the unsafe loaders in your code, and the payloads in the files you are about to load.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it catches
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;In code&lt;/strong&gt; (an AST scan; your code is parsed, never imported):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;pickle.load&lt;/code&gt; / &lt;code&gt;loads&lt;/code&gt;, &lt;code&gt;dill&lt;/code&gt;, &lt;code&gt;cloudpickle&lt;/code&gt;, &lt;code&gt;marshal&lt;/code&gt;, &lt;code&gt;shelve&lt;/code&gt;, &lt;code&gt;joblib.load&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pandas.read_pickle(...)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;torch.load(...)&lt;/code&gt; with &lt;code&gt;weights_only=False&lt;/code&gt;, or with no explicit &lt;code&gt;weights_only&lt;/code&gt; at all&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;yaml.load(...)&lt;/code&gt; without a SafeLoader&lt;/li&gt;
&lt;li&gt;&lt;code&gt;numpy.load(..., allow_pickle=True)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;load_dataset("pandas", ...)&lt;/code&gt;, flagged critical as CVE-2026-57845&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;In data files&lt;/strong&gt;, the distinction that matters is &lt;em&gt;invoked&lt;/em&gt; versus &lt;em&gt;referenced&lt;/em&gt;. A pickle that mentions &lt;code&gt;os.system&lt;/code&gt; in a global is suspicious; a pickle whose &lt;code&gt;REDUCE&lt;/code&gt; opcode actually calls it is a live payload. pickle-sentry runs an abstract interpreter over the &lt;code&gt;pickletools&lt;/code&gt; disassembly, tracking a symbolic stack through &lt;code&gt;REDUCE&lt;/code&gt;, &lt;code&gt;NEWOBJ&lt;/code&gt;, the memo, and nested pickle streams embedded in bytes constants. That is how it separates the two:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;PICKLE-DANGEROUS-REDUCE&lt;/code&gt; (critical): the stream actually invokes &lt;code&gt;os.system&lt;/code&gt;, &lt;code&gt;subprocess.*&lt;/code&gt;, &lt;code&gt;eval&lt;/code&gt;, &lt;code&gt;exec&lt;/code&gt;, and similar.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PICKLE-DANGEROUS-GLOBAL&lt;/code&gt; (high/medium): a dangerous global is referenced but never invoked.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PICKLE-CONTAINER-OPAQUE&lt;/code&gt; (high): a 7-Zip archive, which the standard library cannot inspect. Compressed wrappers (gzip, bzip2, xz, zlib) and zip/tar members are peeled and scanned first.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It also covers &lt;code&gt;.npy&lt;/code&gt; object-array payloads and &lt;code&gt;.npz&lt;/code&gt; members, &lt;code&gt;.keras&lt;/code&gt; Lambda layers (marshalled bytecode that runs at load time), and header checks for safetensors and GGUF. And it can scan a Hugging Face Hub repo remotely before you download anything for real:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pickle-sentry hub user/repo --repo-type model
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One property I was not willing to compromise on: &lt;strong&gt;the scanner never executes the files it inspects.&lt;/strong&gt; The test suite builds a real malicious pickle (&lt;code&gt;__reduce__&lt;/code&gt; to &lt;code&gt;os.system&lt;/code&gt;) and asserts two things: it is flagged critical, and the payload's canary file never appears on disk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip install pickle-sentry
pickle-sentry .                    &lt;span class="c"&gt;# scan code + pickle files in a project&lt;/span&gt;
pickle-sentry . --fail-on high     &lt;span class="c"&gt;# CI gate: exit 1 on high or above&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Zero dependencies, Python 3.9+, 60 tests. Output formats are text, JSON, and SARIF for GitHub code scanning.&lt;/p&gt;

&lt;p&gt;As a GitHub Action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Sun-Wukhan/pickle-sentry@v0.3.1&lt;/span&gt;
  &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;
    &lt;span class="na"&gt;fail-on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;high&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is also listed on the &lt;a href="https://github.com/marketplace/actions/pickle-sentry" rel="noopener noreferrer"&gt;GitHub Marketplace&lt;/a&gt;. As a pre-commit hook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;repo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;https://github.com/Sun-Wukhan/pickle-sentry&lt;/span&gt;
  &lt;span class="na"&gt;rev&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;v0.3.1&lt;/span&gt;
  &lt;span class="na"&gt;hooks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pickle-sentry&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Where it fits
&lt;/h2&gt;

&lt;p&gt;Use pickle-sentry alongside a model-file scanner, not instead of one. Fickling decompiles pickles back to readable Python; pickle-sentry does not. What it adds is the loader and supply-chain surface: your code, the packages you are about to install, and the Hub repo you are about to load.&lt;/p&gt;

&lt;p&gt;Two real runs are written up in the repo docs: scanning huggingface/datasets produced exactly one production finding, the CVE sink itself at &lt;code&gt;packaged_modules/pandas/pandas.py:56&lt;/code&gt;; scanning scikit-learn found its findings concentrated in dataset loaders that unpickle cached copies of remotely fetched datasets. That second pattern is the one I see everywhere, and it is invisible if you only scan model files.&lt;/p&gt;

&lt;p&gt;The longer-term fix is migration: Parquet, CSV, JSON, or Arrow for datasets, &lt;code&gt;weights_only=True&lt;/code&gt; for torch, safetensors where you can. Until a codebase gets there, a scanner that fails CI on a new unsafe loader is a reasonable seatbelt.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Repo: &lt;a href="https://github.com/Sun-Wukhan/pickle-sentry" rel="noopener noreferrer"&gt;https://github.com/Sun-Wukhan/pickle-sentry&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;PyPI: &lt;a href="https://pypi.org/project/pickle-sentry/" rel="noopener noreferrer"&gt;https://pypi.org/project/pickle-sentry/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Marketplace: &lt;a href="https://github.com/marketplace/actions/pickle-sentry" rel="noopener noreferrer"&gt;https://github.com/marketplace/actions/pickle-sentry&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;False positives and missed loaders are the feedback I most want. Issues are open.&lt;/p&gt;

</description>
      <category>python</category>
      <category>security</category>
      <category>machinelearning</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
