Summary
A code injection vulnerability in whichllm's run and snippet commands allows malicious HuggingFace repository owners to execute arbitrary code on a user's machine. The issue occurs when a crafted GGUF filename is inserted into generated Python source without escaping, before the model is downloaded.
Affected context
The vulnerability affects whichllm deployments before 0.5.16 when the run or snippet commands process model metadata from a HuggingFace repository. It becomes risky when repository-controlled GGUF filenames are obtained from the Hub API and interpolated into generated Python code without treating the values as untrusted data.
Exploit preconditions
- Attack surface: HuggingFace repository metadata consumed by the whichllm CLI
- Required access: Control of, or the ability to publish or modify, a repository referenced by the victim
- Trigger point: Execution of the
runorsnippetcommand against the malicious repository - Why exploitation is possible: A specially crafted
rfilenamevalue can escape the generated string literal and introduce executable Python statements before model download or subsequent validation
Why this happens
The underlying bug is direct interpolation of externally controlled filenames into Python source code. Special characters such as double quotes are not escaped or represented through a safe code-generation mechanism, allowing data to alter the structure of the generated program.
This behavior maps to CWE-94: Improper Control of Generation of Code ('Code Injection'), because repository metadata is treated as trusted source-code content rather than as a value that must remain inert.
Potential impact
- Arbitrary code execution on the user's machine
- Theft or modification of local files and credentials
- Installation of persistent malware or additional tooling
- Compromise of downstream development or model-serving environments
- Unauthorized network access from the affected host
General mitigation guidance
Mitigation generally focuses on preventing untrusted metadata from becoming executable source code.
Common defensive patterns include:
- Passing filenames and model metadata as runtime data rather than interpolating them into source text
- Using structured code-generation or serialization APIs that handle escaping automatically
- Applying strict validation and allowlisting for filenames and repository metadata
- Treating all remote repository fields as untrusted, even when they come from a reputable hosting service
- Separating metadata processing from command execution and model loading
- Running CLI operations with least-privilege accounts, restricted filesystem access, and limited network permissions
- Requiring explicit trust decisions before executing repository-provided artifacts or generated code
How teams detect issues like this
Detection can include static analysis for string interpolation, concatenation, or template construction that places external values into Python source code. Code review should flag generated-language constructs, use of exec-like execution paths, and repository metadata incorporated into scripts without escaping or structured parameterization.
CI policies can require safe code-generation patterns, prohibit dynamic execution of untrusted content, and test filenames containing quotes, backslashes, delimiters, and other syntax-significant characters. Automated security review tools may help identify these patterns, but findings should be validated against the data flow from remote metadata to code execution.

