Summary
BentoML's Dockerfile templating pipeline interpolates the docker.base_image field from bento.yaml directly into a Dockerfile template without escaping, newline filtering, or validation.
An attacker who can supply or modify a malicious bento.yaml can inject arbitrary Dockerfile directives and cause docker build to execute attacker-controlled commands on the host where bentoml containerize runs.
Affected context
This issue arises in environments where BentoML is used to containerize models or AI services and relies on user- or pipeline-provided bento.yaml files to define container settings.
The vulnerability specifically affects the Dockerfile generation logic in base_v2.j2, which interpolates docker.base_image into the Dockerfile without sanitization.
It becomes risky when bentoml containerize is run against bentos whose configuration can be influenced by untrusted users, CI inputs, or external model repositories, because the resulting Dockerfile is then built on the developer, CI, or build server host.
Exploit preconditions
- Attack surface: The Bento packaging and containerization workflow, especially
bentoml containerize. - Required access: Ability to create, modify, or supply a
bento.yamlthat will be consumed by BentoML (e.g., via a shared repo, CI pipeline, or multi-tenant ML platform). - Trigger point: Execution of
bentoml containerizewhich renders the Dockerfile template and invokesdocker buildon the generated Dockerfile. - Why exploitation is possible: The
docker.base_imagefield is interpolated into a Jinja2 Dockerfile template as raw text, with no escaping, newline stripping, or format validation. A multi-line value can break out of the expectedFROMdirective and inject arbitrary Dockerfile instructions (includingRUN), which are executed during the Docker build process on the host.
Why this happens
The root cause is direct, unsanitized interpolation of untrusted configuration data into a command-executing build specification, mapping to CWE-78: Improper Neutralization of Special Elements used in an OS Command.
In this case, the Dockerfile acts as an indirect command execution medium: user-controlled fields from bento.yaml are written into the Dockerfile template without constraints, allowing attackers to introduce additional Dockerfile instructions.
When docker build processes these instructions, any injected RUN directives execute shell commands on the build host.
Conceptually, this is a form of templating injection where a configuration field, assumed to be a simple identifier (base image), is instead treated as trusted script content that the underlying build system executes.
Potential impact
- Execution of arbitrary OS commands on the host that runs
bentoml containerizeanddocker build. - Compromise of CI/CD agents, developer workstations, or build servers.
- Theft or modification of sensitive data accessible from the build environment (secrets, tokens, SSH keys, environment variables, cached artifacts).
- Insertion of backdoors or malware into produced container images, enabling downstream supply-chain attacks.
- Lateral movement within the network from the compromised build host.
General mitigation guidance
Effective mitigation focuses on constraining how configuration values influence executable build steps and treating Dockerfiles as code, not as raw text sinks:
- Treat fields like
docker.base_imageas data, not script: enforce strict patterns (e.g., registry/name:tag with no whitespace or control characters) and reject multi-line or otherwise malformed values. - Apply robust escaping and sanitization when interpolating configuration into templates, ensuring special characters and newlines cannot alter the structure of generated Dockerfile instructions.
- Use whitelisting/allow-listing approaches for base images (e.g., selecting from a predefined set) instead of allowing arbitrary user-supplied values.
- Isolate build processes by running
docker buildin locked-down, least-privilege environments (e.g., separate build nodes, limited filesystem access, reduced credentials) to minimize the blast radius of potential injection. - Separate trust levels: do not run containerization of untrusted or user-submitted bentos on the same infrastructure that stores sensitive secrets or production signing keys.
How teams detect issues like this
Detection typically involves both code-centric and pipeline-centric practices:
- Static analysis and code review: Flag any templating where user- or config-controlled values are inserted into Dockerfiles, shell scripts, or command strings without validation or escaping. Look specifically for raw Jinja2 interpolations of configuration fields directly in Dockerfile templates.
- Configuration validation: Implement schema validation for
bento.yamlthat rejects unexpected characters, multi-line values, or patterns inconsistent with legitimate base image references. - CI/CD policies: Enforce policies that scrutinize changes to
bento.yamland Dockerfile templates, requiring review by maintainers, especially for fields impacting base images or build instructions. - Runtime/build monitoring: Monitor build logs and generated Dockerfiles for unexpected
RUNinstructions or unusual network/file operations during builds, possibly via automated checks that diff against known-good templates.

