Zip upload: reject archives over 500 MB uncompressed

Sums the entries' declared sizes before extracting anything; rubyzip's
own validate_entry_sizes (on by default) catches an entry that inflates
past what it declared.
This commit is contained in:
randy committed 2026-11-12 11:54:49 +01:00
1 parent ca2e67599b
commit 7429e4ae33
2 files changed
+23 -2

No files matched your search

+9 -2
View File
@@ -173,12 +173,19 @@ module Maptool
"#{request.base_url}/auth/callback"
end
# Zip-bomb guard: the entries' *declared* uncompressed sizes are summed
# before anything is extracted, and rubyzip itself (validate_entry_sizes,
# on by default) raises Zip::EntrySizeError if an entry inflates past
# what it declared — so a lying header can't sneak past this either.
MAX_ZIP_UNCOMPRESSED = 500 * 1024 * 1024
def save_zip_entries(folder:, io:)
paths = []
Zip::File.open_buffer(io) do |zip|
zip.each do |entry|
next unless entry.file? && GpxStore::TRACK_EXTENSIONS.any? { |ext| entry.name.downcase.end_with?(ext) }
entries = zip.select { |entry| entry.file? && GpxStore::TRACK_EXTENSIONS.any? { |ext| entry.name.downcase.end_with?(ext) } }
halt 413, json(error: "zip contents too large") if entries.sum(&:size) > MAX_ZIP_UNCOMPRESSED
entries.each do |entry|
paths << GPX_STORE.save(folder: folder, filename: File.basename(entry.name), io: StringIO.new(entry.get_input_stream.read))
end
end