Skip to content

Commit

Permalink
Fix CVE-2018-1000544 absolute path traversal
Browse files Browse the repository at this point in the history
Small refactor along the way to centralize destination handling when no explicit path is given and a potential malicious one from the zipfile is used
  • Loading branch information
bdewater committed Jul 1, 2018
1 parent 8887b70 commit f6e76d6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/zip/entry.rb
Expand Up @@ -147,14 +147,18 @@ def next_header_offset #:nodoc:all
end

# Extracts entry to file dest_path (defaults to @name).
def extract(dest_path = @name, &block)
block ||= proc { ::Zip.on_exists_proc }

if @name.squeeze('/') =~ /\.{2}(?:\/|\z)/
def extract(dest_path = nil, &block)
if dest_path.nil? && Pathname.new(@name).absolute?
puts "WARNING: skipped absolute path in #{@name}"
return self
elsif @name.squeeze('/') =~ /\.{2}(?:\/|\z)/
puts "WARNING: skipped \"../\" path component(s) in #{@name}"
return self
end

dest_path ||= @name
block ||= proc { ::Zip.on_exists_proc }

if directory? || file? || symlink?
__send__("create_#{@ftype}", dest_path, &block)
else
Expand Down
Binary file added test/data/absolutepath.zip
Binary file not shown.
26 changes: 26 additions & 0 deletions test/entry_test.rb
Expand Up @@ -151,4 +151,30 @@ def test_store_file_without_compression

assert_match(/mimetypeapplication\/epub\+zip/, first_100_bytes)
end

def test_entry_name_with_absolute_path_does_not_extract
path = '/tmp/file.txt'
File.delete(path) if File.exist?(path)

Zip::File.open('test/data/absolutepath.zip') do |zip_file|
zip_file.each do |entry|
entry.extract
end
end

refute File.exist?(path)
end

def test_entry_name_with_absolute_path_extract_when_given_different_path
path = '/tmp/CVE-2018-1000544'
FileUtils.rm_rf(path) if Dir.exist?(path)

Zip::File.open('test/data/absolutepath.zip') do |zip_file|
zip_file.each do |entry|
entry.extract("#{path}/#{entry.name}")
end
end

assert File.exist?("#{path}/tmp/file.txt")
end
end

0 comments on commit f6e76d6

Please sign in to comment.