ADD <git source> over git clone inside RUN.
| Property | Value |
|---|---|
| Severity | Warning |
| Category | Security |
| Default | Enabled |
| Auto-fix | Yes (--fix --fix-unsafe) |
Description
FlagsRUN instructions that fetch source code with git clone, recommending BuildKit git sources such as
ADD --link https://github.com/user/repo.git /src/repo.
Moving repository acquisition out of RUN makes the fetch explicit in the Dockerfile dependency graph, reduces mutable network behavior inside
shell steps, and improves hermeticity for supply-chain-sensitive builds.
Detected Patterns
The rule reports remotegit clone usage in shell-form RUN instructions, including:
- Plain clones:
RUN git clone https://github.com/NVIDIA/apex - Branch or tag selection:
RUN git clone https://github.com/aws/aws-ofi-nccl.git -b v${BRANCH_OFI} - Clone flows in chained commands:
RUN echo foo && git clone ... && cd repo && git checkout <full-commit-sha> && make - GitLab HTTP remotes that need the generic selector form:
RUN git clone https://gitlab.haskell.org/haskell-wasm/ghc-wasm-meta.git -b ${GHC_WASM_META_COMMIT}
Examples
Before (violation)
After (fixed with —fix —fix-unsafe)
Auto-fix Conditions
The rule emits a syncFixSuggestion when it can safely isolate the clone flow into:
- optional leading
RUNcommands that stay before the fetch - one
ADD <git source> <destination> - optional trailing
RUNcommands that continue after the fetch
- simple POSIX shell-form
RUNinstructions &&chains where the clone flow can be isolated cleanly- optional
-b/--branch - optional explicit destination directory
- optional
cd <repo>followed bygit checkout <full-commit-sha> - optional recursive clone flags, mapped to
submodules=true - GitLab HTTP remotes via the generic
?ref=selector form ADD --linkfor better cache reuse on extracted git-source layersADD --keep-git-dir=truewhen later commands in the rewritten flow still rungitADD --checksum=<full-commit-sha>when the selected ref is a full commit ID
Report-Only Cases
The rule still reports, but does not auto-fix, when the clone appears in a shape that currently cannot be rewritten without dropping execution context, such as:RUNinstructions with non-mount BuildKit flags like--network=...RUNinstructions using mounts- complex shell constructs outside a simple
&&chain - abbreviated hex
git checkoutvalues likeaa756ce, because BuildKit git URLs safely encode full commit IDs, not abbreviated checkout SHAs - clone flows with unsupported git flags or unresolved destination paths
Limitations
- Current auto-fix targets POSIX shell parsing; non-POSIX shells are report-only
- The generated fix uses BuildKit git-source URLs, so it requires BuildKit-enabled builds
- The fixer emits
ref=as the git-source selector. It does not guess betweenbranch=andtag=becausegit clone -b <name>can refer to either one. - When the selected ref is a full commit ID, the fixer emits
--checksum=<sha>as a verifier. - The fixer only rewrites the first clone flow in a matching
RUN; additional clone flows can be fixed on a later run