Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/docker/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,25 @@ def docker_context(directory)
end

def create_relative_dir_tar(directory, output)
root = File.realpath(directory)

Gem::Package::TarWriter.new(output) do |tar|
files = docker_context(directory)

files.each do |prefixed_file_name|
stat = File.stat(prefixed_file_name)
stat = File.lstat(prefixed_file_name)
unprefixed_file_name = prefixed_file_name[directory.length..-1]

if stat.symlink?
tar.add_symlink(
unprefixed_file_name, File.readlink(prefixed_file_name), stat.mode
)
next
end
next unless stat.file?

unprefixed_file_name = prefixed_file_name[directory.length..-1]
next unless contained?(prefixed_file_name, root)

add_file_to_tar(
tar, unprefixed_file_name, stat.mode, stat.size, stat.mtime
) do |tar_file|
Expand All @@ -190,6 +201,14 @@ def create_relative_dir_tar(directory, output)
end
end

# True when path resolves, with every symlink and '..' expanded, to
# something underneath root.
def contained?(path, root)
File.realpath(path).start_with?(root + File::SEPARATOR)
rescue SystemCallError
false
end

def add_file_to_tar(tar, name, mode, size, mtime)
tar.check_closed

Expand Down
46 changes: 46 additions & 0 deletions spec/docker/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,52 @@ def expect_tar_entries(*entries)
expect(files_in_tar(tar)).to eq ['.dockerignore', 'zig']
end
end

describe 'build context containment' do
attr_accessor :outside

def entries_in_tar(tar)
Gem::Package::TarReader.new(tar) do |content|
return content.map { |e| [e.full_name, e.header.typeflag, e.read.to_s] }
end
end

around do |example|
Dir.mktmpdir do |dir|
self.outside = dir
File.write("#{dir}/secret", 'OUTSIDE')
example.call
end
end

it 'archives a symlink as a symlink rather than following it' do
File.symlink("#{outside}/secret", "#{tmpdir}/innocent.txt")

name, typeflag, body = entries_in_tar(tar).first
expect(name).to eq 'innocent.txt'
expect(typeflag).to eq '2'
expect(body).to_not include 'OUTSIDE'
end

it 'does not raise on a dangling symlink' do
File.symlink("#{tmpdir}/no_such_target", "#{tmpdir}/dangling")

expect { files_in_tar(tar) }.to_not raise_error
end

it 'does not pack a file a negation pattern reaches outside the build directory' do
File.write("#{tmpdir}/.dockerignore", "!#{'../' * 12}#{outside[1..]}/secret")

expect(files_in_tar(tar)).to eq ['.dockerignore']
end

it 'does not pack a file reached through a symlinked directory' do
File.symlink(outside, "#{tmpdir}/a_link")
File.write("#{tmpdir}/.dockerignore", '!a_link/secret')

expect(files_in_tar(tar)).to eq ['.dockerignore', 'a_link']
end
end
end

describe '.build_auth_header' do
Expand Down
Loading