Skip to content

A platform for community discussion. Free, open, simple.

FIX: ZIP symlink entries are extracted as real directories (#41168)

Gabriel Grubba8f62172de031
diff --git a/lib/compression/strategy.rb b/lib/compression/strategy.rb
index 2f5d0d1dd13..6f81c30a893 100644
--- a/lib/compression/strategy.rb
+++ b/lib/compression/strategy.rb
@@ -17,6 +17,10 @@ module Compression
available_size = calculate_available_size(max_size)
entries_of(compressed_file).each do |entry|
+ # only extract regular files and directories; skip symlinks,
+ # hardlinks, devices and any other special entry types
+ next if !is_file?(entry) && !is_directory?(entry)
+
entry_path = build_entry_path(sanitized_dest_path, entry, sanitized_compressed_file_path)
next if !is_safe_path_for_extraction?(entry_path, sanitized_dest_path)
@@ -50,6 +54,10 @@ module Compression
entry.file?
end
+ def is_directory?(entry)
+ entry.directory?
+ end
+
def chunk_size
@chunk_size ||= 1024**2 * 2 # 2MiB
end
diff --git a/spec/lib/compression/engine_spec.rb b/spec/lib/compression/engine_spec.rb
index 87cbed2488b..9f6896cb3eb 100644
--- a/spec/lib/compression/engine_spec.rb
+++ b/spec/lib/compression/engine_spec.rb
@@ -81,6 +81,35 @@ RSpec.describe Compression::Engine do
end
end
+ it "skips symlink entries when decompressing zip files" do
+ FileUtils.rm_rf(temp_folder)
+ FileUtils.mkdir(temp_folder)
+
+ source_dir = "#{temp_folder}/source"
+ FileUtils.mkdir(source_dir)
+ File.write("#{source_dir}/real-file", "real file")
+ File.symlink("#{source_dir}/real-file", "#{source_dir}/symlink-entry")
+
+ zip_file = "#{temp_folder}/theme.zip"
+ Zip::File.open(zip_file, create: true) do |zipfile|
+ zipfile.get_output_stream("child-file") { |f| f.write("child file") }
+ zipfile.add("symlink-entry", "#{source_dir}/symlink-entry")
+ end
+ FileUtils.rm_rf(source_dir)
+
+ extract_location = "#{temp_folder}/extract_location"
+ FileUtils.mkdir(extract_location)
+ engine = described_class.engine_for(zip_file)
+ engine.decompress(extract_location, zip_file, available_size)
+
+ symlink_entry_path = "#{extract_location}/symlink-entry"
+
+ expect(read_file("extract_location/child-file")).to eq("child file")
+ expect(File.exist?(symlink_entry_path)).to eq(false)
+ expect(File.symlink?(symlink_entry_path)).to eq(false)
+ expect(Dir.exist?(symlink_entry_path)).to eq(false)
+ end
+
it "decompresses into symlinked directory" do
real_location = "#{temp_folder}/extract_location"
extract_location = "#{temp_folder}/is/symlinked"