Summary
BuildKit's handling of Git URLs with subdirectory components can allow builds to access files outside the checked-out repository root when subdir paths are resolved through symlinks.
In environments where untrusted Git URLs or subdir values are used, an attacker-controlled repository may cause a build to read sensitive files from the same mounted filesystem, potentially exposing secrets or internal configuration.
Affected context
The vulnerability occurs in BuildKit builds that:
- Fetch build context from Git repositories using Git URLs with a
subdir(subpath) component, and - Evaluate that subdir against a filesystem that may contain symlinks or indirect path segments.
Risk is most pronounced when build definitions (e.g., Dockerfiles) and Git URLs originate from untrusted or semi-trusted sources, and when the build worker's filesystem also hosts sensitive files on the same mount as the checkout.
Exploit preconditions
- Attack surface: BuildKit daemon or build environment that accepts Git URLs with a subdir component.
- Required access: Ability to influence or supply:
- The Git repository content, including symlink layout; and/or
- The Git URL used as build context, especially its subdir value.
- Trigger point: A build is started that:
- Uses a Git URL with a subdir component, and
- Resolves that subdir into the build context before robust path normalization and restriction to the repository root.
- Why exploitation is possible: Insufficient validation and canonicalization of the subdir path and symlink resolution allows path traversal outside the intended repository directory, while still remaining on the same mounted filesystem.
Why this happens
The core issue is incomplete enforcement of directory boundaries when resolving subdirectory components within Git-based build contexts.
BuildKit allows specifying a subdir in Git URLs to limit the build context to a specific folder; however, the logic responsible for resolving this subdir did not fully guard against symlink-based escapes or path traversal patterns that lead outside the repository root.
This maps primarily to CWE-22: Improper Limitation of a Pathname to a Restricted Directory, where user-controllable path elements are insufficiently constrained, and secondarily to CWE-59: Improper Link Resolution Before File Access, because symlink resolution is not validated against a fixed root.
Conceptually, the unsafe pattern is: taking a user-influenced path component (subdir in Git URL) and combining it with the repository checkout path, then following filesystem links without re-checking that the final resolved path still lies within the intended repository boundary.
Potential impact
- Read access to files outside the cloned Git repository but on the same filesystem mount
- Exposure of build-time secrets, credentials, or configuration files stored adjacent to the checkout root
- Disclosure of internal source code or scripts that were not meant to be part of the build context
- Increased ability to map or infer the structure of the build worker's filesystem on the affected mount
- Indirect assistance to further attacks if sensitive information is recovered (e.g., tokens, SSH keys)
General mitigation guidance
Effective mitigation focuses on constraining path resolution and reducing trust in external build inputs:
- Enforce strict directory boundary checks: after resolving any subdir path (including symlinks), confirm that the canonical path remains within the Git checkout root.
- Apply robust path normalization: normalize and canonicalize paths (resolving
.., symlinks, and redundant separators) before any access decisions. - Treat Git URLs and subdir values as untrusted input: avoid depending on repository-controlled symlinks or complex subdir layouts for security controls.
- Isolate build filesystem mounts: ensure sensitive files are not colocated on the same mount as untrusted build contexts, or run builds in minimally privileged, ephemeral workspaces.
- Limit secret exposure: store secrets in dedicated, tightly scoped locations and use secret-injection mechanisms that do not rely on the general filesystem around the build context.
- Prefer secure defaults: discourage or disable subdir usage from untrusted repositories when not strictly necessary, or require additional validation when such patterns are used.
How teams detect issues like this
Organizations can look for and detect similar issues with a combination of analysis and process controls:
- Static analysis patterns:
- Identify code paths where user- or configuration-controlled path segments (e.g.,
subdirparameters) are concatenated with base directories without subsequent canonicalization and boundary checks. - Flag logic that follows symlinks or uses generic filesystem traversal on paths derived from external inputs, especially in build- or packaging-related components.
- Code review signals:
- Review Git-context and build-context handling code for:
- Any use of subdirectory selection, path joins, or symlink resolution.
- Lack of explicit enforcement that final resolved paths remain under an expected root.
- Pay attention to areas where repository contents (e.g., symlinks) influence which files are included in build contexts.
- Policy and tooling:
- Implement policy checks that restrict or log builds using Git URLs with subdir components, especially when the source is untrusted.
- Use automated code review or SAST tools capable of modeling path manipulation and symlink resolution, and configure them with rules focused on directory traversal and link resolution weaknesses.
- Monitor build logs and telemetry for unexpected file access patterns or failures when resolving subdirectories during Git-based builds.

