Severity:

HIGH RISK

(7.8)

Remote Code Execution via Unsafe Model Configuration Deserialization (CWE-502) in HuggingFace Transformers

CVE-2026-4372

Analyzed

Vulnerability type

Category:

Unsafe Deserialization and Remote Code Execution

CWE:

CWE-1066, CWE-502

Impact:

Remote Code Execution (RCE) on hosts loading malicious or compromised models

Severity:

HIGH RISK

(7.8)

CVSS version:

3.0

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

All pre-5.3.0 versions of the HuggingFace transformers library contain a remote code execution flaw where a malicious config.json can direct the library to download and execute attacker-controlled Python code from the HuggingFace Hub.

When a user loads such a model with AutoModelForCausalLM.from_pretrained(), the _attn_implementation_internal field can be abused to run arbitrary code with the victim's full operating system privileges, even if trust_remote_code is disabled.

Affected context

This vulnerability affects environments that:

  • Use HuggingFace transformers versions prior to 5.3.0
  • Load models via standard APIs such as AutoModelForCausalLM.from_pretrained()
  • Consume model repositories (own, third-party, or forks) from the HuggingFace Hub or other sources that include a config.json
  • Treat model configurations as trusted without independent verification of their contents or origin

The issue is specifically triggered when config.json includes the internal _attn_implementation_internal field pointing to an attacker-controlled Hub repository that provides custom code.

Exploit preconditions

  • Attack surface: Any application, script, notebook, or service that loads transformer models using the standard from_pretrained() workflow with vulnerable transformers versions.
  • Required access: The attacker must be able to:
  • Publish or control a model repository with a malicious config.json, or
  • Tamper with an existing model's config.json (e.g., supply-chain compromise, man-in-the-middle on model artifacts, or compromised storage).
  • Trigger point: A victim calls AutoModelForCausalLM.from_pretrained() (or similar auto-model APIs) on the malicious or compromised model repository.
  • Why exploitation is possible:
  • Configuration attributes from config.json are deserialized and used without sufficient filtering or validation.
  • The internal _attn_implementation_internal field is honored as a directive to fetch and execute additional code.
  • Downloaded attention kernels are executed in-process without sandboxing.
  • The mechanism bypasses trust_remote_code, making the attack invisible under normal and ostensibly "safe" usage patterns.

Why this happens

The root cause lies in treating model configuration data as implicitly trusted executable metadata rather than untrusted input.

In vulnerable versions, the transformers library:

  • Deserializes config.json into Python objects and internal structures without strictly constraining which fields are allowed or how they may be used.
  • Interprets the _attn_implementation_internal field as an instruction to dynamically fetch and execute attention kernels from a referenced repository.
  • Executes the resulting Python code directly in the application's process, without sandboxing, privilege separation, or explicit user consent.

This behavior maps to:

  • CWE-502: Unsafe Deserialization, where untrusted configuration content controls object behavior and code execution paths.
  • CWE-1066: Missing Serialization Restrictions on Configuration Settings, as configuration fields that should be opaque and controlled by the library are instead exposed to end-user or repository-supplied data, allowing attacker-controlled values to influence critical execution logic.

Potential impact

  • Execution of arbitrary Python code with the victim's OS-level privileges.
  • Theft or exfiltration of API keys, authentication tokens, and other sensitive secrets accessible to the process.
  • Modification, exfiltration, or destruction of local or network-accessible data (e.g., training data, model weights, logs).
  • Lateral movement within the victim's infrastructure by pivoting from the compromised environment.
  • Supply-chain compromise, where downstream applications or pipelines that reuse the model inherit the malicious behavior.
  • Covert persistence or installation of backdoors on developer machines, servers, or notebook environments.

General mitigation guidance

Effective mitigation focuses on treating model configurations as untrusted input and constraining how configuration can influence code execution:

  • Constrain configuration-driven behavior
  • Restrict which configuration fields are honored, especially internal or "hidden" fields, and enforce allowlists for fields that can influence execution.
  • Decouple configuration parsing from any direct code execution or dynamic module loading.
  • Harden deserialization paths
  • Avoid generic or permissive deserialization flows that automatically map arbitrary configuration keys to internal attributes.
  • Introduce schema-based validation for config.json, rejecting or ignoring unknown or internal-only fields.
  • Reduce trust in remote model repositories
  • Treat all externally sourced models (including "trusted" public repositories) as untrusted until validated.
  • Prefer curated, internally mirrored model registries with controlled ingestion and security review.
  • Limit blast radius of model execution
  • Run model loading and inference in constrained environments (e.g., containers, VMs, or restricted user accounts) with least privilege and minimal access to secrets and sensitive file systems.
  • Separate workloads so that untrusted models are not loaded in highly privileged runtime contexts.
  • Adopt secure-by-default usage patterns
  • Favor APIs and configurations that minimize implicit remote code execution or dynamic imports.
  • Make any code execution from model artifacts explicit and user-controlled rather than automatic.

How teams detect issues like this

Organizations can detect and manage this class of issue by combining code-level, artifact-level, and operational controls:

  • Code and dependency analysis
  • Use software composition analysis (SCA) tools to flag vulnerable transformers versions and enforce dependency policies that disallow them.
  • Apply static analysis rules or linters to identify dynamic imports and remote code execution patterns in model loading code paths.
  • Configuration and artifact scanning
  • Scan config.json files in internal and third-party model repositories for suspicious or internal-only fields such as _attn_implementation_internal.
  • Maintain allowlists of expected configuration keys for different model types and alert on deviations.
  • Review and governance of model sources
  • Implement a review process for adding or updating external models in production pipelines, including code and configuration review.
  • Require provenance and integrity checks (e.g., checksums, signatures) for model artifacts fetched from remote sources.
  • Runtime monitoring
  • Monitor model-serving and training environments for unexpected outbound network connections, module downloads, or filesystem access during model loading.
  • Log and analyze model loading operations, including the exact repositories and configuration files involved, and integrate alerts for unusual patterns.

Sources

SourceLink
github.comgithub.com/huggingface/transformers/commit/a7f8e7ff37d87d1a1a0c8cf607971c607741452f
huntr.comhuntr.com/bounties/1f693a6e-6836-4b8b-a0bd-ca036fba8884

Frequently asked

questions

Yes, in many scenarios. If an application or service automatically loads models from remote repositories (such as the HuggingFace Hub) using vulnerable transformers versions, an attacker who controls or compromises a model repository can achieve remote code execution when the model is loaded.

The attacker needs the ability to supply or modify the config.json associated with a model that victims will load. This can be done by publishing a malicious repository, compromising an existing repository, tampering with artifacts in transit or storage, or convincing users to load a crafted model.

The safest pattern is to strictly separate configuration from executable behavior. Concretely, enforce schema-based validation of configuration fields, ignore or reject internal-only keys from user-controlled artifacts, and eliminate any automatic code execution driven by configuration. Additionally, constrain model loading to vetted sources and run it in low-privilege, isolated environments.

Future designs should treat all model artifacts—including configuration files—as untrusted input. Libraries should restrict deserialization to well-defined schemas, avoid dynamic imports or code execution based solely on configuration values, and require explicit, opt-in user consent for any remote code execution features. Incorporating security review, threat modeling of model-loading paths, and automated checks into development and CI pipelines helps catch such patterns early.

Find out what's already
exploitable in your codebase.