Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make extraction safer #2

Merged
merged 1 commit into from
Jul 5, 2016
Merged
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
4 changes: 2 additions & 2 deletions lib/mixlib/archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ def initialize(archive, empty: false)
@extractor = Mixlib::Archive::Tar.new(archive)
end

def extract(destination)
def extract(destination, perms: true, ignore: [])
create_and_empty(destination)

extractor.extract(destination)
extractor.extract(destination, perms: perms, ignore: ignore)
end

private
Expand Down
21 changes: 17 additions & 4 deletions lib/mixlib/archive/tar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,41 @@ def initialize(archive, options = {})
@options = options
end

def extract(destination)
# Extracts the archive to the given +destination+
#
# === Parameters
# perms<Boolean>:: should the extracter use permissions from the archive.
# ignore[Array]:: an array of matches of file paths to ignore
def extract(destination, perms: true, ignore: [])
# (http://stackoverflow.com/a/31310593/506908)
ignore_re = Regexp.union(ignore)
reader do |tar|
dest = nil
tar.each do |entry|
if entry.full_name == TAR_LONGLINK
dest = File.join(destination, entry.read.strip)
next
end
next if entry.full_name =~ ignore_re
dest ||= File.join(destination, entry.full_name)
parent = File.dirname(dest)
FileUtils.mkdir_p(parent, mode: 0755)
FileUtils.mkdir_p(parent)

if entry.directory? || (entry.header.typeflag == "" && entry.full_name.end_with?("/"))
File.delete(dest) if File.file?(dest)
FileUtils.mkdir_p(dest, mode: entry.header.mode, verbose: false)

if perms
FileUtils.mkdir_p(dest, mode: entry.header.mode, verbose: false)
else
FileUtils.mkdir_p(dest, verbose: false)
end

elsif entry.file? || (entry.header.typeflag == "" && !entry.full_name.end_with?("/"))
FileUtils.rm_rf(dest) if File.directory?(dest)
File.open(dest, "wb") do |f|
f.print(entry.read)
end
FileUtils.chmod(entry.header.mode, dest, verbose: false)
FileUtils.chmod(entry.header.mode, dest, verbose: false) if perms
elsif entry.header.typeflag == "2"
# handle symlink
File.symlink(entry.header.linkname, dest)
Expand Down
2 changes: 1 addition & 1 deletion lib/mixlib/archive/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Mixlib
class Archive
VERSION = "0.1.0"
VERSION = "0.2.0"
end
end
7 changes: 6 additions & 1 deletion spec/mixlib/archive_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@
end

it "runs the extractor" do
expect(extractor).to receive(:extract).with(destination)
expect(extractor).to receive(:extract).with(destination, { perms: true, ignore: [] })
archive.extract(destination)
end

it "passes options to the extractor" do
expect(extractor).to receive(:extract).with(destination, { perms: false, ignore: [] })
archive.extract(destination, perms: false)
end
end

describe "#create_and_empty" do
Expand Down