Summary
A command injection risk exists in Ruby on Rails Active Storage when user-supplied input is passed directly into image transformation methods and parameters.
In configurations using the image_processing gem with mini_magick, unsafe transformations can bypass default protections and may allow attackers to trigger arbitrary ImageMagick commands under certain conditions.
Affected context
This vulnerability occurs in Rails applications using Active Storage together with the image_processing gem configured to use mini_magick as the underlying processor.
It becomes risky when templates or controllers accept arbitrary user input as transformation methods or arguments, such as blob.variant(params[:t] => params[:v]), without strict validation.
The issue is tied to how image processing operations are translated into ImageMagick commands, and manifests when application-level safeguards are bypassed or misused.
Exploit preconditions
- Attack surface: Web-facing endpoints that render or process images via Active Storage variants.
- Required access: Ability to supply or influence transformation method names or parameters (e.g., HTTP query parameters, form fields, JSON body).
- Trigger point: Invocation of
blob.variantor similar APIs that construct image transformations from untrusted input, which are then executed through mini_magick/ImageMagick. - Why exploitation is possible: Certain allowed transformation methods and parameter combinations can be abused to circumvent Active Storage's safety defaults and cause crafted arguments to be passed into ImageMagick, enabling command execution primitives.
Why this happens
The underlying bug class is command injection arising from unsafe composition of shell-executed commands using untrusted data.
In this case, the image processing pipeline translates Ruby-level transformation options into ImageMagick command-line invocations via mini_magick.
When developers treat user-supplied transformation methods and parameters as trusted and pass them directly into these APIs, they create a path where unvalidated input can influence command construction and execution.
This maps to CWE-77: Command Injection, and also relates to CWE-94/88 where untrusted inputs control executable behavior in an underlying tool or interpreter.
Potential impact
- Attackers may execute arbitrary ImageMagick commands with the privileges of the application process.
- Attackers could read, modify, or delete files accessible to the web application user.
- Attackers can perform server-side request forgery (SSRF) or data exfiltration via ImageMagick features, depending on configuration.
- Attackers may pivot to broader system compromise if the process has elevated permissions or weak isolation.
General mitigation guidance
Effective mitigation focuses on removing trust from user-controlled transformation input and tightening the image processing boundary.
Recommended patterns include:
- Defining a fixed, application-controlled set of allowed transformations (e.g., named presets) rather than accepting methods/parameters directly from users.
- Performing strict validation and whitelisting of any transformation options that originate from external sources, ensuring they map only to safe operations.
- Applying a strong, restrictive ImageMagick security policy that disables risky coders, delegates, and file system or network access features.
- Running image processing components with least privilege and in isolated environments (separate worker, container, or sandbox) to limit blast radius if misconfiguration occurs.
- Treating all image transformation configuration as code, not data, and subjecting changes to code review and security scrutiny.
How teams detect issues like this
Detection typically involves a combination of code-level and configuration-level review:
- Static analysis rules or custom linters that flag uses of
blob.variantor other image processing APIs where parameters are derived directly fromparams, request bodies, or other untrusted sources. - Code review checklists that require auditing views and controllers for dynamic image transformation logic and ensure all transformations come from a controlled set of options.
- Configuration audits of ImageMagick security policy files to verify that dangerous coders, delegates, or command features are disabled.
- Runtime monitoring for unusual or unexpected ImageMagick command invocations, particularly those involving external network access or shell delegation.
- Integration of automated security scanners that understand common command injection patterns in image processing libraries and flag risky usages.

