Summary
A path traversal vulnerability in Rails Active Storage's DiskService#path_for allows filesystem paths derived from blob keys to escape the configured storage root directory.
If untrusted input is ever used as a blob key, an attacker could read, write, or delete arbitrary files on the server by crafting keys containing traversal sequences like ../.
Affected context
This issue affects Rails applications using Active Storage's Disk service where blob keys influence on-disk storage paths.
While blob keys are usually treated as trusted, any application code that derives keys from user-controlled input, external identifiers, or unvalidated parameters may unintentionally expose this vulnerability.
The risk is highest in environments where the web process has read or write access to sensitive files outside the Active Storage root, such as configuration files, credentials, logs, or source code.
Exploit preconditions
- Attack surface: HTTP endpoints or background jobs that create, fetch, or manage Active Storage blobs using keys.
- Required access: Ability to cause the application to use an attacker-controlled blob key (directly or indirectly) when resolving a disk path.
- Trigger point: Invocation of
DiskService#path_forwith a blob key containing directory traversal sequences (e.g.,../, nested traversals, or absolute path tricks). - Why exploitation is possible: The path joining logic does not enforce that the final resolved path remains within the configured storage root, so crafted keys can escape the intended directory sandbox.
Why this happens
The underlying bug is a classic directory traversal flaw, mapped to CWE-22: Improper Limitation of a Pathname to a Restricted Directory.
Active Storage's DiskService#path_for constructs filesystem paths by combining the storage root with the blob key but fails to normalize and validate the resulting path.
Without checks that the resolved path still resides under the storage root, an attacker-controlled key containing ../ or similar sequences can traverse up the directory tree and point to arbitrary locations.
Conceptually, this reflects an insecure pattern where trusted assumptions about identifier format (blob keys) are not enforced at the boundary where those identifiers are converted into filesystem paths.
Potential impact
- Reading arbitrary files that the application process can access, potentially exposing secrets, configuration, or source code.
- Overwriting or truncating existing files, including logs, configuration files, or application code.
- Deleting arbitrary files, leading to data loss or denial of service.
- Modifying files in ways that enable further compromise, such as altering scripts or configuration to gain code execution in some deployments.
- Bypassing logical access controls by directly accessing stored files that should not be exposed through normal application paths.
General mitigation guidance
Mitigation centers on constraining how paths are derived and ensuring all filesystem access remains within a controlled root.
Helpful defensive patterns include:
- Normalizing and resolving any constructed path, then verifying it is still inside the expected storage root before use (e.g., by comparing canonical paths).
- Treating blob keys and similar identifiers as untrusted unless they are generated exclusively by the server and never influenced by user input.
- Introducing a strict format or whitelist for blob keys (e.g., constrained character sets and fixed-length tokens) and rejecting values that do not conform.
- Encapsulating all file operations behind a storage abstraction that enforces directory boundaries and performs validation centrally.
- Applying least-privilege file permissions to the application process so that even if traversal is attempted, the impact on sensitive system files is limited.
How teams detect issues like this
Effective detection usually combines code-level review with automated checks:
- Static analysis: Search for filesystem operations that combine a base directory with externally influenced values (such as blob keys) without normalization and boundary checks; tools can flag uses of
File.join,Pathname#join, or similar APIs where inputs are not constrained. - Code review: Examine Active Storage customization points, factories, and any code that sets or manipulates blob keys, looking for direct use of user parameters or request data.
- Runtime monitoring: Log and monitor unexpected access patterns, such as requests that result in paths containing
../, or attempts to access files outside known storage directories. - Security testing: Include directory traversal test cases in application security tests, fuzzing blob key handling and upload/download endpoints to ensure traversal sequences are rejected.

