Summary
Diffusers' DiffusionPipeline.from_pretrained method could load and execute arbitrary remote Python code even when callers explicitly set trust_remote_code=False or rely on its default.
Because the trust gate was implemented in an internal download helper instead of at the actual dynamic import sites, several code paths that bypassed download() still allowed attacker-controlled pipeline and component code to run.
Affected context
This vulnerability affects applications that use Diffusers' DiffusionPipeline.from_pretrained API with:
- custom_pipeline pointing to a remote repository, or
- local snapshots that reference custom components in model_index.json, or
- any setup where callers assume trust_remote_code=False prevents execution of untrusted code.
It is most relevant in environments that load models or pipelines supplied by third parties or user input, especially in shared or multi-tenant inference services.
Exploit preconditions
- Attack surface: The Python process running Diffusers where DiffusionPipeline.from_pretrained is called.
- Required access: Ability to influence which repository, custom_pipeline, or local snapshot path is passed to from_pretrained (e.g., via configuration, user input, or model selection features).
- Trigger point: A call to DiffusionPipeline.from_pretrained that loads:
- a custom_pipeline from attacker/repoB while trust_remote_code=False, or
- a local snapshot whose model_index.json references custom component modules.
- Why exploitation is possible: The trust_remote_code check is performed inside DiffusionPipeline.download() instead of at the dynamic module load sites. Any path that:
- evaluated the gate against the wrong repository (repoA vs. attacker/repoB), or
- skipped download() entirely when using a local snapshot,
would still proceed to import and execute arbitrary pipeline or component code.
Why this happens
The core issue is a misplaced security gate around dynamic code loading, aligning with CWE-94 (Improper Control of Code Generation / Execution).
Instead of enforcing trust_remote_code at the point where pipeline.py or custom component modules are actually imported and executed, the check lives inside DiffusionPipeline.download().
Three variants emerge:
- When using from_pretrained('repoA', custom_pipeline='attacker/repoB', trust_remote_code=False), the gate is evaluated against repoA's files, not attacker/repoB's, so pipeline.py from attacker/repoB is still loaded.
- When using from_pretrained('/local/snapshot', custom_pipeline='attacker/repoB', trust_remote_code=False), the local-path shortcut bypasses download(), so no trust check is performed before loading attacker/repoB.
- When using from_pretrained('/local/snapshot', trust_remote_code=False) with custom component files referenced by model_index.json, the local path again bypasses download(), allowing those modules to be imported.
Conceptually, this is a trust-boundary failure: a security control is attached to a helper function (downloading) rather than to the higher-risk operation (executing dynamically loaded Python modules).
Potential impact
- Arbitrary code execution within the Python process that loads the DiffusionPipeline.
- Compromise of API servers or batch workers that perform model loading or inference.
- Unauthorized access to environment secrets, credentials, or connected resources (databases, object storage, internal APIs).
- Alteration of inference behavior, data exfiltration, or injection of backdoors into downstream processing pipelines.
- Lateral movement within the hosting environment if the compromised process has sufficient privileges.
General mitigation guidance
Risk reduction centers on treating model and pipeline code as untrusted and enforcing security boundaries at the actual import sites:
- Ensure dynamic loading of pipelines and custom components is always gated by explicit trust decisions (e.g., require a positive allowlist check before importing any code from a remote repo or snapshot).
- Separate configuration data (model metadata) from executable code, and avoid loading arbitrary .py modules solely based on model_index.json references.
- Run model loading and inference in sandboxed, least-privilege environments (e.g., isolated containers, restricted OS users, minimized filesystem and network permissions).
- Treat model repositories and snapshots as potentially hostile inputs; avoid allowing arbitrary user-supplied repository names or paths without validation or review.
- Centralize security checks so that any path that can reach dynamic imports must pass the same trust and policy enforcement logic.
These patterns reduce the blast radius even if future mistakes occur in trust flag wiring.
How teams detect issues like this
Detection typically involves a mix of code-level and operational checks:
- Static analysis / code review:
- Search for dynamic imports or execution paths (e.g., importlib, exec, custom module loading) that are not consistently guarded by trust flags or policy checks.
- Review any code that passes user-controlled repository names, custom_pipeline values, or snapshot paths into from_pretrained or similar loaders.
- Flag patterns where security gates are placed on helper functions (like downloaders) instead of being enforced directly at the import/execute boundary.
- Policy enforcement:
- Enforce internal policies that only approved/allowlisted repositories or snapshots may be used in production.
- Require security review for features that allow user-driven or dynamic model selection.
- Runtime monitoring:
- Monitor for unexpected outbound network requests to model-hosting platforms from inference services.
- Log and inspect which repositories, commit hashes, and local paths are used at runtime to load pipelines and components.
- Automated review tools (SAST/IAST or CI hooks) can be configured to flag new dynamic loading patterns that lack explicit trust and isolation logic.

