xlsxrb 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +51 -0
- data/README.md +24 -2
- data/lib/xlsxrb/dsl_helpers.rb +16 -0
- data/lib/xlsxrb/elements/styles.rb +15 -6
- data/lib/xlsxrb/number_formatter.rb +26 -8
- data/lib/xlsxrb/ooxml/cfb.rb +11 -3
- data/lib/xlsxrb/ooxml/crypto/agile.rb +48 -10
- data/lib/xlsxrb/ooxml/crypto/standard.rb +5 -1
- data/lib/xlsxrb/ooxml/crypto.rb +3 -0
- data/lib/xlsxrb/ooxml/shared_strings_parser.rb +26 -8
- data/lib/xlsxrb/ooxml/workbook_writer.rb +30 -19
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +116 -12
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +43 -38
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +206 -201
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +99 -99
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +46 -46
- data/lib/xlsxrb/ooxml/writer.rb +166 -159
- data/lib/xlsxrb/ooxml/xml_builder.rb +17 -7
- data/lib/xlsxrb/ooxml/xml_parser.rb +34 -1
- data/lib/xlsxrb/ooxml/zip_reader.rb +95 -68
- data/lib/xlsxrb/ooxml/zip_writer.rb +16 -0
- data/lib/xlsxrb/stream_sheet.rb +26 -11
- data/lib/xlsxrb/stream_writer.rb +9 -6
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/worksheet_builder.rb +6 -5
- data/lib/xlsxrb.rb +68 -21
- data/sig/generated/xlsxrb/dsl_helpers.rbs +9 -0
- data/sig/generated/xlsxrb/elements/styles.rbs +5 -0
- data/sig/generated/xlsxrb/number_formatter.rbs +7 -0
- data/sig/generated/xlsxrb/ooxml/cfb.rbs +2 -0
- data/sig/generated/xlsxrb/ooxml/crypto/agile.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/shared_strings_parser.rbs +7 -0
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +8 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +40 -0
- data/sig/generated/xlsxrb/ooxml/writer.rbs +4 -0
- data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +7 -2
- data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +12 -1
- data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +8 -0
- data/sig/generated/xlsxrb/stream_sheet.rbs +9 -0
- data/sig/generated/xlsxrb/stream_writer.rbs +6 -2
- data/sig/generated/xlsxrb/worksheet_builder.rbs +3 -1
- data/sig/generated/xlsxrb.rbs +38 -8
- metadata +9 -3
|
@@ -108,13 +108,19 @@ module Xlsxrb
|
|
|
108
108
|
|
|
109
109
|
UNESCAPE_RE = /&(?:amp|lt|gt|quot|apos);/
|
|
110
110
|
|
|
111
|
-
|
|
111
|
+
INVALID_XML_CHARS_RE = /[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/
|
|
112
|
+
private_constant :INVALID_XML_CHARS_RE
|
|
113
|
+
|
|
114
|
+
# Escapes XML special characters in a string, and removes characters that XML 1.0
|
|
115
|
+
# cannot hold, such as U+0001 and U+FFFE: a part that contains one is not well-formed.
|
|
116
|
+
# Binary strings are only escaped.
|
|
112
117
|
#
|
|
113
118
|
# @param value [Object]
|
|
114
119
|
# @return [String]
|
|
115
120
|
#: (untyped value) -> String
|
|
116
121
|
def self.escape(value)
|
|
117
122
|
str = value.to_s
|
|
123
|
+
str = str.gsub(INVALID_XML_CHARS_RE, "") if !str.encoding.equal?(Encoding::BINARY) && str.match?(INVALID_XML_CHARS_RE)
|
|
118
124
|
str.match?(ESCAPE_RE) ? str.gsub(ESCAPE_RE, ESCAPE_MAP) : str
|
|
119
125
|
end
|
|
120
126
|
|
|
@@ -136,12 +142,13 @@ module Xlsxrb
|
|
|
136
142
|
value.gsub(XSTRING_ENCODE_RE) { |char| format("_x%04X_", char.ord) }
|
|
137
143
|
end
|
|
138
144
|
|
|
139
|
-
|
|
145
|
+
ENTITY_RE = /&(?:amp|lt|gt|quot|apos|#(\d+)|#x([0-9a-fA-F]+));/
|
|
140
146
|
XSTRING_RE = /_x([0-9A-Fa-f]{4})_/
|
|
141
|
-
private_constant :
|
|
147
|
+
private_constant :ENTITY_RE, :XSTRING_RE
|
|
142
148
|
|
|
143
149
|
# Unescapes predefined and numeric/hex XML entities, as well as
|
|
144
150
|
# ISO/IEC 29500-1 ST_Xstring (_xHHHH_) control character sequences.
|
|
151
|
+
# Each entity is decoded once: "&lt;" is the text "<", not "<".
|
|
145
152
|
#
|
|
146
153
|
# @param value [Object]
|
|
147
154
|
# @return [String]
|
|
@@ -150,11 +157,14 @@ module Xlsxrb
|
|
|
150
157
|
str = value.to_s
|
|
151
158
|
return str unless str.include?("&") || str.include?("_x")
|
|
152
159
|
|
|
153
|
-
|
|
154
|
-
|
|
160
|
+
# Predefined and numeric entities in one pass, so that the output of one is not read
|
|
161
|
+
# as the start of another.
|
|
162
|
+
res = str.gsub(ENTITY_RE) do |match|
|
|
155
163
|
dec = Regexp.last_match(1)
|
|
156
|
-
|
|
157
|
-
|
|
164
|
+
hex = Regexp.last_match(2)
|
|
165
|
+
next UNESCAPE_MAP.fetch(match) unless dec || hex
|
|
166
|
+
|
|
167
|
+
(dec ? Integer(dec) : Integer(hex, 16)).chr(Encoding::UTF_8)
|
|
158
168
|
rescue RangeError
|
|
159
169
|
match
|
|
160
170
|
end
|
|
@@ -75,8 +75,32 @@ module Xlsxrb
|
|
|
75
75
|
DecodingSAX2Parser.new(xml_string)
|
|
76
76
|
end
|
|
77
77
|
|
|
78
|
+
# Elements may nest this deep. SpreadsheetML, DrawingML, and VML parts stay far below
|
|
79
|
+
# it, and REXML's SAX2 parser takes time proportional to the depth for every tag, so
|
|
80
|
+
# that 20,000 nested elements (a 3 KB file) took 15 seconds.
|
|
81
|
+
MAX_ELEMENT_DEPTH = 256
|
|
82
|
+
|
|
78
83
|
# SAX2 parser that wraps each listener in an AttributeDecoder.
|
|
79
84
|
class DecodingSAX2Parser < REXML::Parsers::SAX2Parser
|
|
85
|
+
# Parses the document, reporting malformed XML and exceeded entity expansion limits
|
|
86
|
+
# as Xlsxrb::ParseError.
|
|
87
|
+
#: () -> untyped
|
|
88
|
+
def parse
|
|
89
|
+
super
|
|
90
|
+
rescue REXML::ParseException => e
|
|
91
|
+
raise Xlsxrb::ParseError, "Malformed XML: #{e.message.lines.first.to_s.strip}"
|
|
92
|
+
rescue ArgumentError => e
|
|
93
|
+
# Bytes that are not valid in the document's encoding.
|
|
94
|
+
raise unless e.message.include?("invalid byte sequence")
|
|
95
|
+
|
|
96
|
+
raise Xlsxrb::ParseError, "Malformed XML: #{e.message}"
|
|
97
|
+
rescue RuntimeError => e
|
|
98
|
+
# REXML raises a plain RuntimeError when an entity expansion limit is exceeded.
|
|
99
|
+
raise unless e.instance_of?(RuntimeError)
|
|
100
|
+
|
|
101
|
+
raise Xlsxrb::ParseError, "Malformed XML: #{e.message}"
|
|
102
|
+
end
|
|
103
|
+
|
|
80
104
|
#: (*untyped args) ?{ (*untyped) -> untyped } -> untyped
|
|
81
105
|
def listen(*args, &)
|
|
82
106
|
return super if args.size != 1 || args.first.is_a?(Symbol) || args.first.is_a?(Array) || block_given?
|
|
@@ -90,6 +114,7 @@ module Xlsxrb
|
|
|
90
114
|
#: (untyped listener) -> void
|
|
91
115
|
def initialize(listener)
|
|
92
116
|
@listener = listener
|
|
117
|
+
@depth = 0
|
|
93
118
|
end
|
|
94
119
|
|
|
95
120
|
# Plain methods rather than define_method blocks, which Ractors cannot share.
|
|
@@ -101,8 +126,13 @@ module Xlsxrb
|
|
|
101
126
|
def start_prefix_mapping(*args) = @listener.start_prefix_mapping(*args)
|
|
102
127
|
#: (*untyped args) -> untyped
|
|
103
128
|
def end_prefix_mapping(*args) = @listener.end_prefix_mapping(*args)
|
|
129
|
+
|
|
104
130
|
#: (*untyped args) -> untyped
|
|
105
|
-
def end_element(*args)
|
|
131
|
+
def end_element(*args)
|
|
132
|
+
@depth -= 1
|
|
133
|
+
@listener.end_element(*args)
|
|
134
|
+
end
|
|
135
|
+
|
|
106
136
|
#: (*untyped args) -> untyped
|
|
107
137
|
def characters(*args) = @listener.characters(*args)
|
|
108
138
|
#: (*untyped args) -> untyped
|
|
@@ -128,6 +158,9 @@ module Xlsxrb
|
|
|
128
158
|
|
|
129
159
|
#: (String? uri, String local_name, String qname, Hash[String, String] attributes) -> untyped
|
|
130
160
|
def start_element(uri, local_name, qname, attributes)
|
|
161
|
+
@depth += 1
|
|
162
|
+
raise Xlsxrb::ParseError, "Malformed XML: elements are nested more than #{MAX_ELEMENT_DEPTH} levels deep" if @depth > MAX_ELEMENT_DEPTH
|
|
163
|
+
|
|
131
164
|
if attributes.each_value.any? { |value| value.include?("&") }
|
|
132
165
|
attributes = attributes.transform_values { |value| value.include?("&") ? REXML::Text.unnormalize(value) : value }
|
|
133
166
|
end
|
|
@@ -184,7 +184,11 @@ module Xlsxrb
|
|
|
184
184
|
else
|
|
185
185
|
@bytes_read_compressed += raw.bytesize
|
|
186
186
|
inflater = @inflater
|
|
187
|
-
|
|
187
|
+
begin
|
|
188
|
+
inflated << inflater.inflate(raw) if inflater
|
|
189
|
+
rescue Zlib::Error => e
|
|
190
|
+
raise ZipError, "Corrupt ZIP entry data: #{e.message}"
|
|
191
|
+
end
|
|
188
192
|
end
|
|
189
193
|
else
|
|
190
194
|
inflater = @inflater
|
|
@@ -206,8 +210,7 @@ module Xlsxrb
|
|
|
206
210
|
|
|
207
211
|
inflated.empty? && @eof ? nil : inflated
|
|
208
212
|
else
|
|
209
|
-
|
|
210
|
-
nil
|
|
213
|
+
raise ZipError, "Unsupported ZIP compression method: #{@compression_method}"
|
|
211
214
|
end
|
|
212
215
|
end
|
|
213
216
|
|
|
@@ -224,7 +227,7 @@ module Xlsxrb
|
|
|
224
227
|
while (chunk = fetch_chunk(DEFAULT_CHUNK_SIZE))
|
|
225
228
|
rest << chunk
|
|
226
229
|
end
|
|
227
|
-
full = (peek + rest).force_encoding(Encoding::UTF_16LE).encode(Encoding::UTF_8).delete_prefix("\uFEFF")
|
|
230
|
+
full = (peek + rest).force_encoding(Encoding::UTF_16LE).encode(Encoding::UTF_8, invalid: :replace, undef: :replace).delete_prefix("\uFEFF")
|
|
228
231
|
@buffer = full.b
|
|
229
232
|
@eof = true
|
|
230
233
|
elsif raw_b.start_with?("\xFE\xFF".b)
|
|
@@ -232,7 +235,7 @@ module Xlsxrb
|
|
|
232
235
|
while (chunk = fetch_chunk(DEFAULT_CHUNK_SIZE))
|
|
233
236
|
rest << chunk
|
|
234
237
|
end
|
|
235
|
-
full = (peek + rest).force_encoding(Encoding::UTF_16BE).encode(Encoding::UTF_8).delete_prefix("\uFEFF")
|
|
238
|
+
full = (peek + rest).force_encoding(Encoding::UTF_16BE).encode(Encoding::UTF_8, invalid: :replace, undef: :replace).delete_prefix("\uFEFF")
|
|
236
239
|
@buffer = full.b
|
|
237
240
|
@eof = true
|
|
238
241
|
elsif raw_b.start_with?("\xEF\xBB\xBF".b)
|
|
@@ -277,7 +280,7 @@ module Xlsxrb
|
|
|
277
280
|
@io = nil
|
|
278
281
|
@catalog = nil
|
|
279
282
|
@ci_catalog = nil
|
|
280
|
-
@
|
|
283
|
+
@archive_size = 0
|
|
281
284
|
end
|
|
282
285
|
|
|
283
286
|
# Returns a Hash { entry_name => raw_bytes } for all entries.
|
|
@@ -290,18 +293,16 @@ module Xlsxrb
|
|
|
290
293
|
result
|
|
291
294
|
end
|
|
292
295
|
|
|
296
|
+
XML_PART_SUFFIXES = %w[.xml .rels .vml].freeze
|
|
297
|
+
private_constant :XML_PART_SUFFIXES
|
|
298
|
+
|
|
293
299
|
# Returns raw bytes for a single entry, or nil if not found.
|
|
294
300
|
#: (String name) -> String?
|
|
295
301
|
def read_entry(name)
|
|
296
|
-
|
|
297
|
-
cached = @cached_entries[norm_name] if @cached_entries&.key?(norm_name)
|
|
298
|
-
|
|
299
|
-
entry = lookup_entry(norm_name)
|
|
302
|
+
entry = lookup_entry(name)
|
|
300
303
|
return nil unless entry
|
|
301
304
|
|
|
302
|
-
|
|
303
|
-
return normalize_text_encoding(cached) if cached
|
|
304
|
-
|
|
305
|
+
verify_entry_bounds!(entry)
|
|
305
306
|
raw = if @should_close_io && @raw_io.respond_to?(:closed?) && @raw_io.closed? && @filepath
|
|
306
307
|
File.open(@filepath, "rb") do |f|
|
|
307
308
|
f.seek(entry.data_offset, IO::SEEK_SET)
|
|
@@ -311,7 +312,11 @@ module Xlsxrb
|
|
|
311
312
|
io.seek(entry.data_offset, IO::SEEK_SET)
|
|
312
313
|
io.read(entry.compressed_size)
|
|
313
314
|
end
|
|
314
|
-
decompress(raw, entry.method)
|
|
315
|
+
data = decompress(raw, entry.method)
|
|
316
|
+
# Bytes that are not UTF-8 in an XML part become U+FFFD, so that the parsers and the
|
|
317
|
+
# strings they return never hold an invalid sequence. Other parts are left as they are.
|
|
318
|
+
data = data.scrub if data.encoding == Encoding::UTF_8 && !data.valid_encoding? && entry.name.end_with?(*XML_PART_SUFFIXES)
|
|
319
|
+
data
|
|
315
320
|
end
|
|
316
321
|
|
|
317
322
|
# Yields (entry_name, data_string) for each file in the archive.
|
|
@@ -331,6 +336,7 @@ module Xlsxrb
|
|
|
331
336
|
entry = lookup_entry(entry_name)
|
|
332
337
|
raise ZipError, "Entry not found in archive: #{entry_name}" unless entry
|
|
333
338
|
|
|
339
|
+
verify_entry_bounds!(entry)
|
|
334
340
|
resolved_encoding = if encoding == false
|
|
335
341
|
nil
|
|
336
342
|
elsif encoding
|
|
@@ -360,6 +366,7 @@ module Xlsxrb
|
|
|
360
366
|
entry = lookup_entry(entry_name)
|
|
361
367
|
raise ZipError, "Entry not found in archive: #{entry_name}" unless entry
|
|
362
368
|
|
|
369
|
+
verify_entry_bounds!(entry)
|
|
363
370
|
resolved_encoding = if encoding == false
|
|
364
371
|
nil
|
|
365
372
|
elsif encoding
|
|
@@ -416,6 +423,7 @@ module Xlsxrb
|
|
|
416
423
|
entry = lookup_entry(entry_name)
|
|
417
424
|
raise ZipError, "Entry not found in archive: #{entry_name}" unless entry
|
|
418
425
|
|
|
426
|
+
verify_entry_bounds!(entry)
|
|
419
427
|
if entry.compressed_size.zero? && entry.uncompressed_size.positive?
|
|
420
428
|
data = read_entry(entry_name)
|
|
421
429
|
writer.add_binary_entry(target_path || entry_name, data || "")
|
|
@@ -473,6 +481,9 @@ module Xlsxrb
|
|
|
473
481
|
first_sig = io.read(4)
|
|
474
482
|
raise ZipError, "Invalid magic number: Expected a valid ZIP/XLSX file format (PK\\x03\\x04)" unless first_sig == LOCAL_HEADER_SIG
|
|
475
483
|
|
|
484
|
+
io.seek(0, IO::SEEK_END)
|
|
485
|
+
@archive_size = io.pos
|
|
486
|
+
|
|
476
487
|
# Try Central Directory first
|
|
477
488
|
cd_catalog = parse_catalog_from_central_directory(io)
|
|
478
489
|
return cd_catalog if cd_catalog && !cd_catalog.empty?
|
|
@@ -580,7 +591,10 @@ module Xlsxrb
|
|
|
580
591
|
name_len = header[22, 2].unpack1("v")
|
|
581
592
|
extra_len = header[24, 2].unpack1("v")
|
|
582
593
|
|
|
583
|
-
entry_name = io.read(name_len)
|
|
594
|
+
entry_name = io.read(name_len)
|
|
595
|
+
break unless entry_name && entry_name.bytesize == name_len
|
|
596
|
+
|
|
597
|
+
entry_name.force_encoding("UTF-8")
|
|
584
598
|
extra_data = extra_len.positive? ? io.read(extra_len) : nil
|
|
585
599
|
uncompressed_size, compressed_size, = parse_zip64_extra(extra_data, uncompressed_size, compressed_size, 0) if extra_data && (uncompressed_size == 0xFFFF_FFFF || compressed_size == 0xFFFF_FFFF)
|
|
586
600
|
data_offset = io.pos
|
|
@@ -592,16 +606,13 @@ module Xlsxrb
|
|
|
592
606
|
|
|
593
607
|
if has_data_descriptor && compressed_size.zero?
|
|
594
608
|
# A ZIP64 entry (version needed 4.5) carries 8-byte sizes in its data descriptor.
|
|
595
|
-
|
|
596
|
-
@cached_entries ||= {}
|
|
597
|
-
@cached_entries[norm_name] = entry_data
|
|
598
|
-
@cached_entries[entry_name] = entry_data if entry_name != norm_name
|
|
609
|
+
compressed_size, uncompressed_size, descriptor_crc = scan_data_descriptor_entry(io, method, zip64: version_needed >= 45)
|
|
599
610
|
entry = Entry.new(
|
|
600
611
|
name: norm_name,
|
|
601
612
|
method: method,
|
|
602
|
-
compressed_size:
|
|
603
|
-
uncompressed_size:
|
|
604
|
-
crc32: crc,
|
|
613
|
+
compressed_size: compressed_size,
|
|
614
|
+
uncompressed_size: uncompressed_size,
|
|
615
|
+
crc32: descriptor_crc || crc,
|
|
605
616
|
local_header_offset: header_pos,
|
|
606
617
|
data_offset: data_offset
|
|
607
618
|
)
|
|
@@ -620,66 +631,82 @@ module Xlsxrb
|
|
|
620
631
|
result[norm_name] = entry
|
|
621
632
|
result[entry_name] = entry if entry_name != norm_name
|
|
622
633
|
|
|
634
|
+
# An entry that claims to run past the end of the archive ends the scan; reading
|
|
635
|
+
# it raises ZipError.
|
|
636
|
+
break if data_offset + compressed_size > @archive_size
|
|
637
|
+
|
|
623
638
|
io.seek(compressed_size, IO::SEEK_CUR) if io.respond_to?(:seek)
|
|
624
639
|
end
|
|
625
640
|
end
|
|
626
641
|
result
|
|
627
642
|
end
|
|
628
643
|
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
begin
|
|
636
|
-
while !inflater.finished? && (chunk = io.read(chunk_size))
|
|
637
|
-
break if chunk.empty?
|
|
638
|
-
|
|
639
|
-
fed += chunk.bytesize
|
|
640
|
-
inflated = inflater.inflate(chunk)
|
|
641
|
-
limit = @max_uncompressed_size || MAX_UNCOMPRESSED_SIZE
|
|
642
|
-
raise ZipBombError, "ZIP bomb detected: Uncompressed size exceeds #{limit} bytes" if result.bytesize + inflated.bytesize > limit
|
|
643
|
-
|
|
644
|
-
result << inflated
|
|
645
|
-
end
|
|
646
|
-
rescue Zlib::BufError, Zlib::DataError
|
|
647
|
-
# Inflation ended
|
|
648
|
-
ensure
|
|
649
|
-
# Ruby's Zlib::Inflate drops input past the end of the deflate stream (avail_in
|
|
650
|
-
# is 0), so rewind by what was read beyond the compressed length (total_in).
|
|
651
|
-
remaining_bytes = fed - inflater.total_in
|
|
652
|
-
inflater.close
|
|
653
|
-
end
|
|
644
|
+
# Finds where a deflate stream of unknown length ends by inflating it, without keeping
|
|
645
|
+
# the output, and consumes the data descriptor that follows.
|
|
646
|
+
# Returns the compressed size, the uncompressed size, and the descriptor's CRC-32.
|
|
647
|
+
#: (untyped io, Integer method, ?zip64: bool) -> [Integer, Integer, Integer?]
|
|
648
|
+
def scan_data_descriptor_entry(io, method, zip64: false)
|
|
649
|
+
return [0, 0, nil] unless method == 8
|
|
654
650
|
|
|
655
|
-
|
|
651
|
+
inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
|
|
652
|
+
limit = @max_uncompressed_size || MAX_UNCOMPRESSED_SIZE
|
|
653
|
+
chunk_size = 4096
|
|
654
|
+
fed = 0
|
|
655
|
+
uncompressed_size = 0
|
|
656
|
+
begin
|
|
657
|
+
while !inflater.finished? && (chunk = io.read(chunk_size))
|
|
658
|
+
break if chunk.empty?
|
|
656
659
|
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
io.read(4 + sizes_len)
|
|
661
|
-
elsif desc_sig == [0x50, 0x4B, 0x03, 0x04].pack("C4") || desc_sig == [0x50, 0x4B, 0x01, 0x02].pack("C4")
|
|
662
|
-
io.seek(-4, IO::SEEK_CUR) if io.respond_to?(:seek)
|
|
663
|
-
elsif desc_sig
|
|
664
|
-
io.read(sizes_len)
|
|
660
|
+
fed += chunk.bytesize
|
|
661
|
+
uncompressed_size += inflater.inflate(chunk).bytesize
|
|
662
|
+
raise ZipBombError, "ZIP bomb detected: Uncompressed size exceeds #{limit} bytes" if uncompressed_size > limit
|
|
665
663
|
end
|
|
664
|
+
rescue Zlib::BufError, Zlib::DataError
|
|
665
|
+
# Inflation ended
|
|
666
|
+
ensure
|
|
667
|
+
# Ruby's Zlib::Inflate drops input past the end of the deflate stream (avail_in
|
|
668
|
+
# is 0), so rewind by what was read beyond the compressed length (total_in).
|
|
669
|
+
compressed_size = inflater.total_in
|
|
670
|
+
inflater.close
|
|
671
|
+
end
|
|
666
672
|
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
673
|
+
remaining_bytes = fed - compressed_size
|
|
674
|
+
io.seek(-remaining_bytes, IO::SEEK_CUR) if io.respond_to?(:seek) && remaining_bytes.positive?
|
|
675
|
+
|
|
676
|
+
desc_sig = io.read(4)
|
|
677
|
+
sizes_len = zip64 ? 16 : 8
|
|
678
|
+
descriptor_crc = nil
|
|
679
|
+
if desc_sig == [0x50, 0x4B, 0x07, 0x08].pack("C4")
|
|
680
|
+
descriptor_crc = io.read(4)&.unpack1("V")
|
|
681
|
+
io.read(sizes_len)
|
|
682
|
+
elsif desc_sig == [0x50, 0x4B, 0x03, 0x04].pack("C4") || desc_sig == [0x50, 0x4B, 0x01, 0x02].pack("C4")
|
|
683
|
+
io.seek(-4, IO::SEEK_CUR) if io.respond_to?(:seek)
|
|
684
|
+
elsif desc_sig
|
|
685
|
+
descriptor_crc = desc_sig.unpack1("V") if desc_sig.bytesize == 4
|
|
686
|
+
io.read(sizes_len)
|
|
670
687
|
end
|
|
688
|
+
|
|
689
|
+
[compressed_size, uncompressed_size, descriptor_crc]
|
|
671
690
|
end
|
|
672
691
|
|
|
673
692
|
def decompress(raw, method)
|
|
674
|
-
data =
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
693
|
+
data = case method
|
|
694
|
+
when 0 then raw&.dup || ""
|
|
695
|
+
when 8 then safe_inflate(raw || "", -Zlib::MAX_WBITS)
|
|
696
|
+
else raise ZipError, "Unsupported ZIP compression method: #{method}"
|
|
678
697
|
end
|
|
679
698
|
normalize_text_encoding(data)
|
|
680
|
-
rescue Zlib::
|
|
681
|
-
|
|
682
|
-
|
|
699
|
+
rescue Zlib::Error => e
|
|
700
|
+
raise ZipError, "Corrupt ZIP entry data: #{e.message}"
|
|
701
|
+
end
|
|
702
|
+
|
|
703
|
+
# Raises ZipError when the entry's data would lie outside the archive, so that a
|
|
704
|
+
# forged size is never passed to IO#read as a length to allocate.
|
|
705
|
+
#: (Entry entry) -> void
|
|
706
|
+
def verify_entry_bounds!(entry)
|
|
707
|
+
return if entry.data_offset + entry.compressed_size <= @archive_size
|
|
708
|
+
|
|
709
|
+
raise ZipError, "Malformed ZIP archive: entry #{entry.name.inspect} extends past the end of the archive"
|
|
683
710
|
end
|
|
684
711
|
|
|
685
712
|
def safe_inflate(raw, wbits)
|
|
@@ -728,9 +755,9 @@ module Xlsxrb
|
|
|
728
755
|
|
|
729
756
|
raw_b = data.b
|
|
730
757
|
if raw_b.start_with?("\xFF\xFE".b)
|
|
731
|
-
data.force_encoding(Encoding::UTF_16LE).encode(Encoding::UTF_8).delete_prefix("\uFEFF")
|
|
758
|
+
data.force_encoding(Encoding::UTF_16LE).encode(Encoding::UTF_8, invalid: :replace, undef: :replace).delete_prefix("\uFEFF")
|
|
732
759
|
elsif raw_b.start_with?("\xFE\xFF".b)
|
|
733
|
-
data.force_encoding(Encoding::UTF_16BE).encode(Encoding::UTF_8).delete_prefix("\uFEFF")
|
|
760
|
+
data.force_encoding(Encoding::UTF_16BE).encode(Encoding::UTF_8, invalid: :replace, undef: :replace).delete_prefix("\uFEFF")
|
|
734
761
|
elsif raw_b.start_with?("\xEF\xBB\xBF".b)
|
|
735
762
|
data.b.delete_prefix("\xEF\xBB\xBF".b).force_encoding(Encoding::UTF_8)
|
|
736
763
|
else
|
|
@@ -77,6 +77,7 @@ module Xlsxrb
|
|
|
77
77
|
def start_entry(path)
|
|
78
78
|
raise "ZipWriter is closed" if @closed
|
|
79
79
|
|
|
80
|
+
validate_entry_name!(path)
|
|
80
81
|
@entry_path = path
|
|
81
82
|
@entry_offset = @bytes_written
|
|
82
83
|
@entry_gp_flag = @seekable ? 0 : DATA_DESCRIPTOR_FLAG
|
|
@@ -150,6 +151,7 @@ module Xlsxrb
|
|
|
150
151
|
def copy_raw_entry(path, source_io, data_offset:, compressed_size:, uncompressed_size:, crc32:, method: 8)
|
|
151
152
|
raise "ZipWriter is closed" if @closed
|
|
152
153
|
|
|
154
|
+
validate_entry_name!(path)
|
|
153
155
|
offset = @bytes_written
|
|
154
156
|
write_local_header(path, crc32, compressed_size, uncompressed_size, gp_flag: 0, method: method)
|
|
155
157
|
|
|
@@ -183,6 +185,19 @@ module Xlsxrb
|
|
|
183
185
|
|
|
184
186
|
private
|
|
185
187
|
|
|
188
|
+
UNSAFE_ENTRY_NAME_RE = %r{\A/|\\|[\x00-\x1F\x7F]|(?:\A|/)\.\.(?:/|\z)}
|
|
189
|
+
private_constant :UNSAFE_ENTRY_NAME_RE
|
|
190
|
+
|
|
191
|
+
# Rejects names that a program extracting the archive could write outside its target
|
|
192
|
+
# directory (absolute paths, ".." segments, backslashes) and names with control
|
|
193
|
+
# characters.
|
|
194
|
+
#: (String path) -> void
|
|
195
|
+
def validate_entry_name!(path)
|
|
196
|
+
return unless path.empty? || path.b.match?(UNSAFE_ENTRY_NAME_RE)
|
|
197
|
+
|
|
198
|
+
raise ZipError, "Unsafe ZIP entry name: #{path.inspect}"
|
|
199
|
+
end
|
|
200
|
+
|
|
186
201
|
def determine_seekable
|
|
187
202
|
return true if @io.is_a?(StringIO)
|
|
188
203
|
return false unless @io.respond_to?(:seek) && @io.respond_to?(:tell)
|
|
@@ -222,6 +237,7 @@ module Xlsxrb
|
|
|
222
237
|
end
|
|
223
238
|
|
|
224
239
|
def write_raw_entry(path, content_bytes)
|
|
240
|
+
validate_entry_name!(path)
|
|
225
241
|
crc = Zlib.crc32(content_bytes) & 0xFFFFFFFF
|
|
226
242
|
compressed = deflate(content_bytes)
|
|
227
243
|
offset = @bytes_written
|
data/lib/xlsxrb/stream_sheet.rb
CHANGED
|
@@ -1239,21 +1239,36 @@ module Xlsxrb
|
|
|
1239
1239
|
end
|
|
1240
1240
|
end
|
|
1241
1241
|
|
|
1242
|
+
DIMENSION_SCAN_LIMIT = 1024 * 1024
|
|
1243
|
+
private_constant :DIMENSION_SCAN_LIMIT
|
|
1244
|
+
|
|
1245
|
+
DIMENSION_RE = /<(?:[a-zA-Z0-9_]+:)?dimension\b[^>]*\bref=["']([^"']+)["']/
|
|
1246
|
+
private_constant :DIMENSION_RE
|
|
1247
|
+
|
|
1248
|
+
# Returns the ref of the <dimension> element in +xml+, or nil. Text that is not valid
|
|
1249
|
+
# UTF-8 is searched as bytes.
|
|
1250
|
+
#: (String xml) -> String?
|
|
1251
|
+
def dimension_ref_in(xml)
|
|
1252
|
+
return xml.slice(DIMENSION_RE, 1) if xml.valid_encoding?
|
|
1253
|
+
|
|
1254
|
+
xml.b.slice(DIMENSION_RE, 1)&.force_encoding(Encoding::UTF_8)&.scrub
|
|
1255
|
+
end
|
|
1256
|
+
|
|
1242
1257
|
# Extracts dimension reference from XML source lazily without parsing the entire file.
|
|
1243
1258
|
#: () -> String?
|
|
1244
1259
|
def extract_dimension
|
|
1245
1260
|
if @sheet_source.is_a?(String)
|
|
1246
|
-
@sheet_source
|
|
1261
|
+
dimension_ref_in(@sheet_source)
|
|
1247
1262
|
elsif @zip_reader && @entry_name
|
|
1248
1263
|
buf = +""
|
|
1249
1264
|
dim = nil
|
|
1250
1265
|
@zip_reader.each_entry_chunk(@entry_name) do |chunk|
|
|
1251
1266
|
buf << chunk
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
break
|
|
1255
|
-
end
|
|
1267
|
+
dim = dimension_ref_in(buf)
|
|
1268
|
+
break if dim
|
|
1256
1269
|
break if buf.include?("<sheetData") || buf.include?(":sheetData")
|
|
1270
|
+
# <dimension> precedes <sheetData>; a part with neither is not searched to its end.
|
|
1271
|
+
break if buf.bytesize > DIMENSION_SCAN_LIMIT
|
|
1257
1272
|
end
|
|
1258
1273
|
dim
|
|
1259
1274
|
elsif @sheet_source.respond_to?(:call)
|
|
@@ -1261,20 +1276,20 @@ module Xlsxrb
|
|
|
1261
1276
|
dim = nil
|
|
1262
1277
|
@sheet_source.call do |chunk|
|
|
1263
1278
|
buf << chunk
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
break
|
|
1267
|
-
end
|
|
1279
|
+
dim = dimension_ref_in(buf)
|
|
1280
|
+
break if dim
|
|
1268
1281
|
break if buf.include?("<sheetData") || buf.include?(":sheetData")
|
|
1282
|
+
# <dimension> precedes <sheetData>; a part with neither is not searched to its end.
|
|
1283
|
+
break if buf.bytesize > DIMENSION_SCAN_LIMIT
|
|
1269
1284
|
end
|
|
1270
1285
|
dim
|
|
1271
1286
|
elsif @sheet_source.respond_to?(:seek) && @sheet_source.respond_to?(:pos)
|
|
1272
1287
|
pos = @sheet_source.pos
|
|
1273
1288
|
buf = @sheet_source.read(65_536) || ""
|
|
1274
1289
|
@sheet_source.seek(pos, IO::SEEK_SET)
|
|
1275
|
-
buf
|
|
1290
|
+
dimension_ref_in(buf)
|
|
1276
1291
|
else
|
|
1277
|
-
raw_sheet_xml
|
|
1292
|
+
dimension_ref_in(raw_sheet_xml)
|
|
1278
1293
|
end
|
|
1279
1294
|
end
|
|
1280
1295
|
end
|
data/lib/xlsxrb/stream_writer.rb
CHANGED
|
@@ -1254,7 +1254,9 @@ module Xlsxrb
|
|
|
1254
1254
|
|
|
1255
1255
|
# Sets the worksheet background image (<picture>).
|
|
1256
1256
|
#
|
|
1257
|
-
# @param val [String, Hash, Elements::Image, nil] Image binary data,
|
|
1257
|
+
# @param val [String, Hash, Elements::Image, nil] Image binary data, or a Hash with +:data+ and
|
|
1258
|
+
# +:ext+. A String is always the image itself: to use a file, pass its bytes, as in
|
|
1259
|
+
# +{ data: File.binread(path), ext: "jpg" }+.
|
|
1258
1260
|
# @return [untyped]
|
|
1259
1261
|
# @api public
|
|
1260
1262
|
#: (untyped val) -> untyped
|
|
@@ -2656,7 +2658,9 @@ module Xlsxrb
|
|
|
2656
2658
|
|
|
2657
2659
|
# Sets the worksheet background image (<picture>).
|
|
2658
2660
|
#
|
|
2659
|
-
# @param val [String, Hash, Elements::Image, nil] Image binary data,
|
|
2661
|
+
# @param val [String, Hash, Elements::Image, nil] Image binary data, or a Hash with +:data+ and
|
|
2662
|
+
# +:ext+. A String is always the image itself: to use a file, pass its bytes, as in
|
|
2663
|
+
# +{ data: File.binread(path), ext: "jpg" }+.
|
|
2660
2664
|
# @return [untyped]
|
|
2661
2665
|
# @api public
|
|
2662
2666
|
#: (untyped val) -> untyped
|
|
@@ -2669,10 +2673,9 @@ module Xlsxrb
|
|
|
2669
2673
|
|
|
2670
2674
|
data = val
|
|
2671
2675
|
inferred_ext = nil
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
elsif data.is_a?(Hash)
|
|
2676
|
+
# SECURITY NOTE: A String is the image itself and is never read as a path. Reading it
|
|
2677
|
+
# as one let text that names a file put that file's bytes into the workbook (ADR 0012).
|
|
2678
|
+
if data.is_a?(Hash)
|
|
2676
2679
|
inferred_ext = data[:ext] || (data[:filename] && File.extname(data[:filename]).delete_prefix("."))
|
|
2677
2680
|
data = data[:file_data] || data[:data]
|
|
2678
2681
|
elsif data.is_a?(Elements::Image)
|
data/lib/xlsxrb/version.rb
CHANGED
|
@@ -1284,7 +1284,9 @@ module Xlsxrb
|
|
|
1284
1284
|
|
|
1285
1285
|
# Sets the worksheet background image (<picture>).
|
|
1286
1286
|
#
|
|
1287
|
-
# @param val [String, Hash, Elements::Image, nil] Image binary data,
|
|
1287
|
+
# @param val [String, Hash, Elements::Image, nil] Image binary data, or a Hash with +:data+ and
|
|
1288
|
+
# +:ext+. A String is always the image itself: to use a file, pass its bytes, as in
|
|
1289
|
+
# +{ data: File.binread(path), ext: "jpg" }+.
|
|
1288
1290
|
# @return [untyped]
|
|
1289
1291
|
# @api public
|
|
1290
1292
|
#: (untyped val) -> untyped
|
|
@@ -1296,10 +1298,9 @@ module Xlsxrb
|
|
|
1296
1298
|
|
|
1297
1299
|
data = val
|
|
1298
1300
|
inferred_ext = nil
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
elsif data.is_a?(Hash)
|
|
1301
|
+
# SECURITY NOTE: A String is the image itself and is never read as a path. Reading it
|
|
1302
|
+
# as one let text that names a file put that file's bytes into the workbook (ADR 0012).
|
|
1303
|
+
if data.is_a?(Hash)
|
|
1303
1304
|
inferred_ext = data[:ext] || (data[:filename] && File.extname(data[:filename]).delete_prefix("."))
|
|
1304
1305
|
data = data[:file_data] || data[:data]
|
|
1305
1306
|
elsif data.is_a?(Elements::Image)
|