Skip to content

Do not treat symlinks as files - #530

Closed
achamo wants to merge 2 commits into
upserve:masterfrom
achamo:master
Closed

achamo wants to merge 2 commits into
upserve:masterfrom
achamo:master

Conversation

@achamo

@achamo achamo commented Jul 24, 2018

Copy link
Copy Markdown
  • File.stat returns the stat of the target
  • Symlink are ignored and replaced with files
  • If the target is absent File.stat will raise and we can't build the image
  • Using lstat and add_symlink adds symlinks to the tar archive

Change-Id: If36368071a4373b12836dd00210d12e988cae819

achamo added 2 commits July 24, 2018 16:55
* File.stat returns the stat of the target
* Symlink are ignored and replaced with files
* If the target is absent File.stat will raise and we can't build the image
* Using lstat and add_symlink adds symlinks to the tar archive

Change-Id: If36368071a4373b12836dd00210d12e988cae819
Change-Id: I1be5aa7b47f9c117944851e3bd1a83e054e2ea5e
ls-todd-lunter pushed a commit that referenced this pull request Oct 24, 2026
Docker::Util could pull files from outside the build directory into the
build context in two ways.

create_relative_dir_tar stats and opens each path with File.stat and
File.open, both of which follow symlinks, so a link sitting in the build
directory was packed with its target's bytes under the link's own name.
Separately, docker_context joins each .dockerignore negation pattern onto
the build directory with File.join and hands the result to Dir.glob with
nothing checking that it stays underneath, so a pattern containing ../
resolved outside. Either way the file landed in the tar sent to the
daemon and could be read from inside the build.

docker build does not work this way: moby matches .dockerignore against
paths it gets from walking the context root, so a pattern cannot name
anything outside it, and a COPY that leaves the context is refused.

Archive symlinks as symlinks instead of following them, as PR #530 has
proposed since 2018, and check that regular files really resolve under
the build directory. Both are needed: lstat does not stop a ../ pattern,
and validating the pattern string does not stop a symlink or a file
reached through a symlinked directory.

The check sits in the tar loop rather than in docker_context so that
file_hash_from_paths is covered too, which reaches the same code through
Container#archive_in and Image#insert_local. Using lstat also clears up
the Errno::ENOENT that a dangling symlink previously raised out of
build_from_dir.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@ls-todd-lunter

Copy link
Copy Markdown
Contributor

Thanks for this. The same approach (lstat + add_symlink) landed in #611, which also adds containment checks for the build context and shipped in v2.5.0. Closing as superseded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants