Summary
OpenMed's PII privacy-filter dispatcher can route untrusted, user-supplied model names to a Hugging Face model loader configured with trust_remote_code=True, enabling execution of arbitrary Python code embedded in model repositories.
An unauthenticated attacker who controls or references a malicious Hugging Face repository can cause OpenMed to import attacker-controlled Transformers code, leading to remote code execution under the OpenMed service account.
Affected context
This vulnerability occurs in OpenMed deployments that use the PII privacy-filter feature and dynamically load Hugging Face models based on a model_name parameter.
The dispatcher relies on broad substring matching to decide which loading path to use and, for certain matches, invokes Hugging Face model loading with trust_remote_code=True.
Environments where the model_name is influenced by client input and where OpenMed has outbound network access to fetch models from remote repositories are particularly exposed, especially if the service runs with elevated privileges.
Exploit preconditions
- Attack surface: Network-exposed OpenMed endpoints that accept or derive a
model_namefor the PII privacy-filter. - Required access: None; an attacker can be unauthenticated if the endpoint is publicly reachable.
- Trigger point: Supplying a crafted
model_namesuch asattacker/foo-privacy-filter-barthat passes the dispatcher's substring checks and routes to the Hugging Face loading path usingtrust_remote_code=True. - Why exploitation is possible: The dispatcher's routing logic over-trusts string patterns in
model_nameand does not enforce a strict allowlist of approved repositories or disable remote code execution features.
As a result, the Hugging Face loader fetches and executes custom code defined via auto_map in config.json or tokenizer_config.json from an attacker-controlled model repository.
Why this happens
The root cause is the combination of:
- Broad, non-strict substring matching for routing
model_namevalues, and - Dynamic loading of Hugging Face models with
trust_remote_code=Truefrom sources influenced by user input.
This aligns with CWE-94: Improper Control of Generation of Code (Code Injection) because untrusted configuration (the model repository and its metadata) is allowed to define Python code that will be imported and executed in the OpenMed process.
Instead of confining user input to data-only models, the implementation effectively treats it as a handle to executable logic, with no robust verification that the model repository is trusted or pre-approved.
The abstract insecure pattern is: routing untrusted identifiers into a remote code loading mechanism that is allowed to execute arbitrary Python classes and functions at load time.
Potential impact
- Execution of arbitrary Python code with the privileges of the OpenMed service process.
- Complete compromise of the OpenMed application, including access to PII and medical data it processes.
- Lateral movement within the hosting environment, such as accessing local files, environment variables, credentials, or internal network services.
- Tampering with PII filtering behavior, potentially disabling privacy protections or exfiltrating sensitive text before redaction.
- Use of the compromised service as a foothold for further attacks against the infrastructure.
General mitigation guidance
Effective mitigation focuses on removing the ability for untrusted input to influence remote code loading:
- Constrain model selection:
- Replace substring-based routing with an explicit allowlist of known-safe model identifiers.
- Map user-facing model options to internal IDs that cannot be arbitrarily controlled by clients.
- Disable remote code execution in model loaders:
- Avoid using
trust_remote_code=Truefor any model loading paths reachable from user-controlled input. - Prefer loading only standard, library-defined model and tokenizer classes that do not require custom Python code.
- Harden configuration and deployment:
- Treat model repositories as code, not data, and subject them to the same review and promotion pipeline as application code.
- Restrict network egress for the OpenMed service where practical, or require models to be pulled from an internal, curated mirror.
- Run the OpenMed service under a non-privileged account with minimal filesystem and network permissions to reduce blast radius.
- Apply defense-in-depth checks:
- Validate that
model_namevalues conform to a strict pattern and are resolved against a trusted registry. - Separate concerns so that user-driven configuration cannot directly toggle advanced execution features like remote code loading.
How teams detect issues like this
Organizations can detect and monitor this class of issue using a combination of code-level and operational techniques:
- Static analysis and code review:
- Search for use of
trust_remote_code=Trueor equivalent dynamic code loading flags, especially in paths reachable from network input. - Flag routing logic that uses substring or pattern matching on user-controlled strings to select code paths or plugin modules.
- Review any custom integration with ML model hubs to ensure repository names or URLs are not directly derived from request parameters.
- Policy and pipeline enforcement:
- Enforce a policy that all models used in production are declared in configuration and pinned to vetted repositories and revisions.
- Integrate automated checks into CI to detect the introduction of new dynamic import or remote execution patterns tied to user input.
- Runtime monitoring:
- Log and monitor all
model_namevalues and corresponding repositories being loaded in production, alerting on unknown or external namespaces. - Use application security monitoring to detect anomalous behavior such as unexpected outbound connections, file system modifications, or shell command executions triggered during model loading.

