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.jsonare deserialized and used without sufficient filtering or validation. - The internal
_attn_implementation_internalfield 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.jsoninto Python objects and internal structures without strictly constraining which fields are allowed or how they may be used. - Interprets the
_attn_implementation_internalfield 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.jsonfiles 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.

