[Bug]: `COPY`/`ADD` of a directory silently copies zero files when the build context has a symlinked ancestor path (e.g. under /tmp)
I have done the following
- [x] I have searched the existing issues
- [x] If possible, I've reproduced the issue using the 'main' branch of this project
Steps to reproduce
mkdir -p /tmp/c05-repro/srcdir
echo "hello" > /tmp/c05-repro/srcdir/file.txt
cat > /tmp/c05-repro/Dockerfile <<'EOF'
FROM alpine:3.22
COPY srcdir /srcdir
RUN ls -la /srcdir
EOF
cd /tmp/c05-repro
container build -t c05-repro .
On macOS, /tmp is a symlink to /private/tmp, so the build context path has a
symlinked ancestor.
For comparison, running the exact same Dockerfile/context from a path with no
symlinked ancestor (e.g. ~/c05-repro instead of /tmp/c05-repro) copies the
file correctly.
Problem description
Expected: RUN ls -la /srcdir should show file.txt.
Actual: /srcdir is empty — COPY srcdir /srcdir silently copies zero files.
No error or warning is printed; the build reports success.
Root cause (found while reviewing the source): BuildFSSync.walk()'s
containment check (BuildFSSync.swift, around line 212, contextDir.parentOf(url))
is a pure lexical string-prefix comparison with no symlink resolution. Its
sibling methods read()/info() in the same file do resolve symlinks
before doing the equivalent check. When the enumerated directory URLs aren't
canonically rooted at the resolved context directory — which happens whenever
the context path has a symlinked ancestor like /tmp → /private/tmp —
every entry silently fails the "is this a child of the context?" check and
gets skipped, with no error surfaced.
This only ever makes the check stricter than intended (files get dropped, never included when they shouldn't be), so it fails closed — this is a correctness/reliability bug, not a security issue.
Suggested fix: resolve url with resolvingSymlinksInPath() immediately
before the parentOf check in walk(), mirroring the existing pattern
already used in read()/info().
Environment
- OS: macOS 26.4.1 (25E253)
- Xcode: N/A (not applicable to this issue)
- Container: Container CLI version 1.4.1 (build: release, commit: 9a8917c)
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
I don't work on container, but I'm running in to this as well. Attached a minimal repro. If you run the repro in /tmp it fails. If you run the repro from your home directory it succeeds.
Seems like a dup of a bunch of other issues, e.g. https://github.com/apple/container/issues/1899
I reproduced this against main at 4e7dce6 on macOS 26.6.2. A regression test using a build context under /tmp fails on the original BuildFSSync.swift: JSON Walk omits srcdir/file.txt, and the tar archive contains only srcdir/.
I opened PR #2291 with a fix that canonicalizes the context root and makes Walk and archive path comparisons use consistent paths. The focused ContainerBuildTests suite passes all 57 tests, including the new regression, on macOS 27.0 (26A428).
I also rebuilt the CLI from that commit and ran an uncached build from a fresh /tmp context with COPY srcdir /srcdir followed by RUN ls -la /srcdir && cat /srcdir/file.txt. The relevant output was:
#4 transferring context: 76B done
#6 [linux/arm64 1/3] COPY srcdir /srcdir
#7 [linux/arm64 2/3] RUN ls -la /srcdir && cat /srcdir/file.txt
#7 0.045 -rw-r--r-- 1 root root 6 Sep 21 11:50 file.txt
#7 0.046 hello
#8 DONE 0.1s
issue-2285-proof:macos27
The same patched CLI reproduction also passed on macOS 26.6.2.
Thanks @bnham for independently confirming the issue, and thanks @danielsyauqi for the fix, regression test, and cross-version verification. Appreciate you both helping move this forward.
I think this is the intended fix, which has been merged in to trunk: https://github.com/apple/container/pull/2252
Verified fixed on current main (09d8e6ae, merged via #2252). Re-ran the exact reproduction from a fresh source build:
container build -t c05-repro --no-cache .
COPY srcdir /srcdir now correctly includes file.txt (content hello), confirmed via RUN ls -la /srcdir and by running the built image and cat-ing the file directly. Also re-ran the near-duplicate repro in #2253 with the same result, and confirmed swift test --filter ContainerBuildTests passes (57 tests) including the regression test #2252 added.
#2252 fixed this in Globber.children(of:), which turns out to be the actual source of the top-level matched entries feeding both the JSON walk and tar-archive paths in BuildFSSync.walk() — so the fix covers both output modes even though it doesn't touch BuildFSSync.swift directly. Closing.
Hi @Johnny-Kao @bnham! Thanks for the opportunity, though my PR didn't make it because of #2252 already covering it, glad that my regression and cross-version test could validate the fix to be implemented.
Will take a look for another issue to contribute!