ziperu 0.2.3 → 0.3.0

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.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +151 -0
  3. data/Cargo.lock +2 -1
  4. data/README.md +35 -7
  5. data/ext/ziperu/Cargo.toml +9 -1
  6. data/ext/ziperu/src/archive.rs +246 -144
  7. data/ext/ziperu/src/entry_options.rs +1 -1
  8. data/ext/ziperu/src/errors.rs +2 -11
  9. data/ext/ziperu/src/extract.rs +332 -104
  10. data/ext/ziperu/src/gvl.rs +45 -26
  11. data/ext/ziperu/src/lib.rs +17 -0
  12. data/ext/ziperu/src/place.rs +379 -0
  13. data/ext/ziperu/src/rb.rs +14 -1
  14. data/ext/ziperu/src/reader.rs +100 -29
  15. data/ext/ziperu/src/sink.rs +2 -7
  16. data/ext/ziperu/src/source.rs +4 -7
  17. data/ext/ziperu/src/stream.rs +89 -11
  18. data/ext/ziperu/src/test_rng.rs +37 -0
  19. data/ext/ziperu/src/writer.rs +71 -25
  20. data/ext/ziperu/src/zipcrypto.rs +40 -12
  21. data/lib/zip.rb +3 -3
  22. data/lib/ziperu/central_directory.rb +4 -8
  23. data/lib/ziperu/constants.rb +8 -0
  24. data/lib/ziperu/crypto.rb +10 -2
  25. data/lib/ziperu/dos_time.rb +1 -1
  26. data/lib/ziperu/entry.rb +226 -109
  27. data/lib/ziperu/entry_set.rb +0 -5
  28. data/lib/ziperu/errors.rb +4 -2
  29. data/lib/ziperu/extra_field/zip64.rb +1 -3
  30. data/lib/ziperu/extra_field.rb +10 -10
  31. data/lib/ziperu/file/commit.rb +255 -0
  32. data/lib/ziperu/file/extraction.rb +280 -0
  33. data/lib/ziperu/file.rb +51 -398
  34. data/lib/ziperu/file_split.rb +1 -3
  35. data/lib/ziperu/filesystem/dir.rb +3 -9
  36. data/lib/ziperu/filesystem/file.rb +2 -6
  37. data/lib/ziperu/filesystem/zip_file_name_mapper.rb +2 -2
  38. data/lib/ziperu/input_stream.rb +28 -32
  39. data/lib/ziperu/ioextras/abstract_input_stream.rb +83 -31
  40. data/lib/ziperu/ioextras/abstract_output_stream.rb +1 -3
  41. data/lib/ziperu/ioextras/fake_io.rb +1 -3
  42. data/lib/ziperu/ioextras.rb +6 -0
  43. data/lib/ziperu/limits.rb +48 -0
  44. data/lib/ziperu/null_input_stream.rb +1 -1
  45. data/lib/ziperu/output_stream.rb +118 -31
  46. data/lib/ziperu/pass_thru_decompressor.rb +1 -3
  47. data/lib/ziperu/rails_streaming.rb +5 -5
  48. data/lib/ziperu/remote_io.rb +181 -32
  49. data/lib/ziperu/size_estimator.rb +27 -19
  50. data/lib/ziperu/verify_report.rb +62 -0
  51. data/lib/ziperu/version.rb +2 -2
  52. data/lib/ziperu.rb +19 -17
  53. metadata +8 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b5e977083967283ed8594aa131fccbbea2b8b766458c6e6fa8a3006356af57e1
4
- data.tar.gz: 62d85e136fc36d7618c8b58aa2fb8ed910f403a994c9a6f1e06a1e812cd13310
3
+ metadata.gz: 4d696a3e103aeb025bdb36d6f631ed295574f87a9a3059a5c2c6192858a1b9f8
4
+ data.tar.gz: 0e100226ee8f6c5410a23ebf742d1b9faec420f2b752fd5d63dbeeec8888a455
5
5
  SHA512:
6
- metadata.gz: d57553d22be136d860dda97bade7f0a8bffe3ea28ab0f0befc2b7d2c14ef674dc219522bd02884bb62e556f5c77f93374ed3a25dd7cb2af8b54136616a2f96e1
7
- data.tar.gz: 293a8363e37ac330bdf5bfef408d7b572f35d06254188a7e6795ce5da1cf64a01f18b45cc5434cf9a9c524b91d249599c3220fe67adbe06afafd80939cca6763
6
+ metadata.gz: a2c2fc7032401d846c5120591a35db950eba3da2dafd94a6817cf49ba5ee426191d675f986a9b9c75ecde81dcfba10f9cc0a66bc7ff2c4e02180909bb8e4796b
7
+ data.tar.gz: ab5c5a04297cd4ff90e5591aafa4af3b97432437b3f1abd9ba4257d2199cebe488de87a88641963d5ce3390ee195567b7edbfa6659e30802980503bd9babd1c3
data/CHANGELOG.md CHANGED
@@ -1,5 +1,156 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.0 (2026-11-14)
4
+
5
+ Security:
6
+
7
+ - Extraction wrote through a link already on disk below the
8
+ destination: with a junction `dest/linked` pointing elsewhere (no
9
+ privilege needed on Windows) or a symlink, the entry `linked/x` was
10
+ written outside the destination, and with `on_exists: :overwrite` a
11
+ file symlink at an entry's path had its target overwritten. Both
12
+ `extract_all` (native and Ruby paths) and `Entry#extract` now skip,
13
+ with a warning, an entry whose path has a link on the way or is one.
14
+ The destination itself may still be a link. On Linux 5.6 and later the
15
+ native extractor also opens each file's directory with `openat2`
16
+ (`RESOLVE_BENEATH | RESOLVE_NO_SYMLINKS`), so a link swapped in after
17
+ the check is refused by the kernel; other systems, and the Ruby path,
18
+ check just before each write (see SECURITY.md).
19
+ - `symlinks: :create` accepted a chain of links that each looked inside
20
+ the destination (`b -> .`, then `c -> b/..`, which is really the
21
+ destination's parent). A link target with `..` after a name is now
22
+ refused; leading `..`s are still allowed.
23
+ - `RemoteIO` sent its `headers:` (credentials, typically) to wherever a
24
+ redirect led, and followed a redirect from https to http. Headers now
25
+ go only to the origin of the URL given, unless `forward_headers: true`;
26
+ an https-to-http redirect raises, and only http and https URLs are
27
+ accepted.
28
+ - `RemoteIO` kept a whole file in memory, however large, when the server
29
+ ignored Range; past `max_full_download:` (64 MB by default, nil for no
30
+ limit) that now raises. Ranged answers must carry the Content-Range
31
+ asked for, and a file whose ETag (or Last-Modified) changes between
32
+ requests raises instead of being read as a mix of two versions; a
33
+ strong ETag is sent as If-Match.
34
+
35
+ Fixed, failures that destroyed data:
36
+
37
+ - An overwrite during extraction (`on_exists: :overwrite`, or a block
38
+ that says so) truncated the existing file, then deleted it if the
39
+ entry failed to extract (a bad CRC, a size limit, a full disk). The
40
+ new file is now written beside it and renamed over it once complete,
41
+ so the original survives a failure. The replacement is a new file:
42
+ it no longer keeps the old one's permissions or hard links.
43
+ - `File.open(path, write: other)` wrote straight over `other`, so a
44
+ failed commit left a partial archive there. It is now written beside
45
+ it and renamed into place.
46
+ - On Windows, a commit over an archive still open elsewhere moves the
47
+ old one aside first; if the new one then could not be renamed into
48
+ place, neither was left at the path. The old one is now put back.
49
+
50
+ Added:
51
+
52
+ - `File#verify(report: true)` returns a `Ziperu::VerifyReport` listing
53
+ the entries verified, the ones that failed (with the exception) and
54
+ the ones skipped and why (`:encrypted` without a password,
55
+ `:not_written` for an entry added since opening). `ok?` is false when
56
+ anything was skipped, so an archive with nothing checked no longer
57
+ passes for a sound one. Without `report:`, `verify` returns the
58
+ failures as before.
59
+ - `File#extraction_plan(dest, **options)` says what `extract_all` would
60
+ do, entry by entry, without writing anything: the path each entry goes
61
+ to, whether it would be extracted or why not (`:unsafe`,
62
+ `:through_link`, `:symlink_skipped`, `:symlink_refused`), whether
63
+ something is already there, which entries land on the same path once
64
+ sanitized, and the declared size (`declared_size`). It is built from the same plan
65
+ `extract_all` follows.
66
+ - `Ziperu::Limits::UPLOAD`, `STRICT` and `NONE`: ready-made sets of the
67
+ limit settings, as keywords for one archive or through
68
+ `Ziperu::Limits.apply` for all.
69
+
70
+ Faster (Windows, against 0.2.3):
71
+
72
+ - Opening an archive hands each entry's facts from the native core as
73
+ one small Array, yielded directly, rather than a Hash of fifteen keys:
74
+ 10,000 entries open in 36 ms instead of 44, with a fifth fewer objects.
75
+ - `gets` and `each_line` read from an offset into the buffer instead of
76
+ cutting each line off its front: a 15 MB CSV of 400,000 lines, 2.0 s to
77
+ 0.19 s (rubyzip: 1.6 s); 1000 small text entries, 140 to 64 ms.
78
+ - Small writes (`puts`, `<<` of a field) are gathered and handed to the
79
+ native writer 64 KB at a time: 80,000 writes of 100 bytes, 142 to
80
+ 73 ms.
81
+ - A commit that rewrites the archive (a remove, a rename) asks the native
82
+ core in one pass which entries carry fields a raw copy would lose, and
83
+ copies runs of untouched entries in one call. Removing one entry of
84
+ 10,000: 329 to 178 ms.
85
+
86
+ Fixed:
87
+
88
+ - An append (`OutputStream.open(path, append: true)`, or a commit that
89
+ only adds) that failed before its first entry was added damaged the
90
+ last entry already in the archive; the archive is now left as it was.
91
+ - `InputStream#read(n)` on a stream (a pipe or socket) took time in
92
+ proportion to `n` for every 64 KB read, so a very large `n` was slow or
93
+ failed to allocate.
94
+ - `RemoteIO`: a read spanning the oldest cached block and one not yet
95
+ fetched raised `NoMethodError` once the cache was full.
96
+ - `Zip::Deflater` and `Zip::PassThruCompressor`, kept for code that names
97
+ them, raised `NoMethodError` on the first write.
98
+ - A `Timeout` (or `Thread#raise`, or Ctrl-C) could be lost for good. When
99
+ it was delivered during the native core's own check for a pending
100
+ exception, that check dropped it. A `Timeout.timeout` around a loop of
101
+ reads then never fired, and the loop ran forever. The exception is now
102
+ raised. Seen as a CI test that hung about one run in five.
103
+ - A `Timeout` on a stream's read of a whole large entry
104
+ (`get_input_stream(name, &:read)`) took effect late. The interrupted
105
+ read first copied everything it had read so far, to keep it for the
106
+ next read: for a 768 MiB entry, longer than reading the entry on macOS.
107
+ It now keeps the String it was filling and copies only if another read
108
+ follows.
109
+
110
+ Changed:
111
+
112
+ - `InputStream#ungetc` returns nil, as `IO#ungetc` does.
113
+
114
+ Internal:
115
+
116
+ - RuboCop (`rake rubocop`, and a CI job) with a configuration that keeps
117
+ rubyzip's style in the ported files; clippy now requires a `SAFETY:`
118
+ comment on every unsafe block.
119
+ - `rake compat` gates test by test against COMPAT.md, not on the total:
120
+ a test that newly fails, a file that no longer loads or a file with
121
+ fewer passes fails the task, and COMPAT.md is only rewritten by a full
122
+ run without a regression (or with `COMPAT_ACCEPT=1`). COMPAT.md is
123
+ regenerated: 419 of 476 rubyzip tests pass.
124
+ - Seeded property tests in the Rust crate (`cargo test`) for the
125
+ extraction path check, the zip64 extra field parser and the ZipCrypto
126
+ round trip, 5,000 cases each.
127
+ - The native core exports the layout of its entry rows
128
+ (`Native::Archive::ROW_FIELDS`), built from one list on the Rust side
129
+ for both the rows and the Hashes; a test checks it against
130
+ `Entry::ROW_FIELDS`.
131
+ - `File` is split by job: committing lives in `file/commit.rb`,
132
+ extraction in `file/extraction.rb`, and which entries `extract_all`
133
+ writes, in what order and under which names, is an `ExtractionPlan`.
134
+ An unknown `sanitize:` now raises even for an empty archive.
135
+ - The regression tests that were filed by release (0.2.1, 0.2.2, the code
136
+ review, the quality pass) are regrouped by behaviour: failure
137
+ recovery, IO semantics, names and metadata, archive options.
138
+ - The nightly CI run of the whole suite under GC stress overran GitHub's
139
+ six hours every night. GC stress is now on only from each test's setup
140
+ to its teardown, not while Bundler, minitest and the gem load. The run
141
+ is split into four parallel jobs, and the seven scale tests, which take
142
+ more than a quarter of an hour each under stress, are left out. Every
143
+ push still runs the native-boundary tests under stress.
144
+ - The test that a `Timeout` interrupts native work no longer assumes a
145
+ 768 MiB read takes over 0.1 s, which failed on a fast Windows runner.
146
+ It times the work warm and repeats it under a fraction of that. For a
147
+ stream and for `extract_all` it then checks what was left behind (how
148
+ far the reader got, no finished file), not the clock: tidying up after
149
+ an interrupt takes time that varies with the machine. Under valgrind
150
+ it runs the interrupted paths without judging how promptly they stop:
151
+ valgrind runs one thread at a time, and the one raising the timeout can
152
+ wait a whole read for its turn.
153
+
3
154
  ## 0.2.3 (2026-11-11)
4
155
 
5
156
  Faster (Windows, against 0.2.2):
data/Cargo.lock CHANGED
@@ -879,11 +879,12 @@ dependencies = [
879
879
 
880
880
  [[package]]
881
881
  name = "ziperu"
882
- version = "0.2.3"
882
+ version = "0.3.0"
883
883
  dependencies = [
884
884
  "crc32fast",
885
885
  "flate2",
886
886
  "getrandom",
887
+ "libc",
887
888
  "magnus",
888
889
  "rayon",
889
890
  "rb-sys",
data/README.md CHANGED
@@ -19,15 +19,18 @@ the [`zip`](https://github.com/zip-rs/zip2) crate through
19
19
  [Magnus](https://github.com/matsadler/magnus). `require "zip"` aliases
20
20
  `Zip = Ziperu`, so existing code runs unchanged.
21
21
 
22
- On GitHub's Linux runners (Ruby 3.4, median of 5), against rubyzip 3.7.0:
22
+ ziperu 0.2.3 on GitHub's Linux runners (Ruby 3.4, median of 5), against
23
+ rubyzip 3.7.0:
23
24
 
24
25
  | scenario | rubyzip | ziperu | speedup |
25
26
  |---|---:|---:|---:|
26
- | read 1000 x 8 KB deflated entries | 80 ms | 14 ms | 5.8x |
27
- | open and list 10000 entries | 135 ms | 58 ms | 2.3x |
28
- | deflate one 64 MB entry | 188 ms | 77 ms | 2.4x |
29
- | extract_all 1000 entries | 169 ms | 80 ms | 2.1x |
30
- | add 1000 files from disk (`threads: 4`) | 214 ms | 60 ms | 3.6x |
27
+ | read 1000 x 8 KB deflated entries | 110 ms | 18 ms | 6.1x |
28
+ | inflate one 64 MB entry | 112 ms | 38 ms | 3.0x |
29
+ | open and list 10000 entries | 184 ms | 75 ms | 2.5x |
30
+ | deflate one 64 MB entry | 323 ms | 101 ms | 3.2x |
31
+ | `get_output_stream` 1000 entries and commit | 310 ms | 84 ms | 3.7x |
32
+ | append one entry to a 10000-entry archive | 1.16 s | 148 ms | 7.8x |
33
+ | add 1000 files from disk (`threads: 4`) | 348 ms | 89 ms | 3.9x |
31
34
 
32
35
  macOS and Windows, every scenario, and which calls release the GVL (what
33
36
  Puma users ask about) are in [docs/benchmarks.md](docs/benchmarks.md).
@@ -35,7 +38,7 @@ Puma users ask about) are in [docs/benchmarks.md](docs/benchmarks.md).
35
38
  - **Precompiled** for Linux (glibc and musl), macOS and Windows, Ruby 3.1 to 4.0. No toolchain, no runtime dependencies.
36
39
  - **Fast**: entries inflate and deflate in Rust with the GVL released, the central directory is parsed once, unchanged entries are copied byte for byte on commit, and a commit that only adds entries appends them in place.
37
40
  - **Reads everything**: Stored, Deflate, Deflate64, bzip2, zstd, LZMA, XZ, PPMd and the PKZIP 1.x methods; AES and ZipCrypto. Writes Deflate, Stored, bzip2, zstd, XZ, PPMd, AES and ZipCrypto.
38
- - **Safe by default**: no entry inflates past its declared size, extraction never escapes the destination, symlinks are opt-in, and limits on entry count, entry size, total size and compression ratio can be set up front. [SECURITY.md](SECURITY.md) has the details.
41
+ - **Safe by default**: no entry inflates past its declared size, extraction skips names that resolve outside the destination and never writes through a link already on disk, symlinks are opt-in, and limits on entry count, entry size, total size and compression ratio can be set up front. [SECURITY.md](SECURITY.md) has the details.
39
42
  - **Friendlier than rubyzip where it costs nothing**: a password on the archive (`File.open(path, password: ...)`), `add` from an IO, Pathname arguments, `File#verify`, progress callbacks, and errors that fail at the call site.
40
43
  - **Checked against the field**: rubyzip's own test suite, differential fuzzing against Python's `zipfile` and rubyzip, interoperability runs with Info-ZIP, 7-Zip and Python, and the test suites of gems built on rubyzip (roo, caxlsx, rubyXL, docx, write_xlsx, sablon, gepub) run with ziperu in its place.
41
44
 
@@ -236,6 +239,31 @@ Ziperu::File.open(upload, max_entries: 1_000, max_total_size: 1 << 30) { |zip| .
236
239
  Ziperu::OutputStream.open("out.zip", write_extended_timestamps: true) { |zos| ... }
237
240
  ```
238
241
 
242
+ `Ziperu::Limits` has ready-made sets of these: `UPLOAD` for archives
243
+ users upload, `STRICT` for small documents, `NONE` to clear them.
244
+
245
+ ```ruby
246
+ Ziperu::File.open(upload, **Ziperu::Limits::UPLOAD) { |zip| ... }
247
+ Ziperu::Limits.apply(Ziperu::Limits::STRICT) # the globals
248
+ ```
249
+
250
+ Look before you extract. `extraction_plan` says what `extract_all` would
251
+ do with each entry, without writing anything. `verify(report: true)` says
252
+ which entries were checked, which failed and which were skipped:
253
+
254
+ ```ruby
255
+ Ziperu::File.open(upload) do |zip|
256
+ plan = zip.extraction_plan("out", sanitize: :windows)
257
+ plan.reject(&:extract?).each { |item| warn "#{item.entry.name}: #{item.status}" }
258
+ plan.select(&:collides_with) # entries that land on the same path
259
+ plan.sum(&:declared_size) # declared bytes to be written
260
+
261
+ report = zip.verify(report: true)
262
+ report.ok? # false if anything failed or was skipped
263
+ report.skipped # { entry => :encrypted }, say
264
+ end
265
+ ```
266
+
239
267
  ## Migrating from rubyzip
240
268
 
241
269
  Change the Gemfile line and keep `require "zip"`: the `Zip` constant and
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "ziperu"
3
- version = "0.2.3"
3
+ version = "0.3.0"
4
4
  edition = "2021"
5
5
  publish = false
6
6
  rust-version = "1.88"
@@ -34,5 +34,13 @@ flate2 = { version = "1", default-features = false, features = ["zlib-rs"] }
34
34
  crc32fast = "1"
35
35
  getrandom = { version = "0.4", features = ["std"] }
36
36
 
37
+ # O_NOFOLLOW for extraction (already in the tree through the zip crate's
38
+ # dependencies).
39
+ [target.'cfg(unix)'.dependencies]
40
+ libc = "0.2"
41
+
42
+ [lints.clippy]
43
+ undocumented_unsafe_blocks = "warn"
44
+
37
45
  [build-dependencies]
38
46
  rb-sys-env = "0.2"