minitar 1.1.0 → 1.2.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +103 -53
- data/CODE_OF_CONDUCT.md +155 -116
- data/CONTRIBUTING.md +154 -115
- data/LICENCE.md +8 -5
- data/Manifest.txt +2 -2
- data/README.md +20 -8
- data/Rakefile +7 -8
- data/SECURITY.md +16 -26
- data/lib/minitar/input.rb +243 -203
- data/lib/minitar/output.rb +69 -73
- data/lib/minitar/pax_header.rb +94 -94
- data/lib/minitar/posix_header.rb +273 -275
- data/lib/minitar/reader.rb +232 -220
- data/lib/minitar/version.rb +1 -1
- data/lib/minitar/writer.rb +247 -262
- data/lib/minitar.rb +97 -74
- data/test/test_path_normalization.rb +170 -0
- data/test/test_tar_reader.rb +17 -9
- data/test/test_windows_extraction.rb +121 -0
- metadata +32 -25
- data/docs/bsdl.txt +0 -19
- data/docs/ruby.txt +0 -56
data/lib/minitar.rb
CHANGED
|
@@ -4,45 +4,52 @@ require "fileutils"
|
|
|
4
4
|
require "rbconfig"
|
|
5
5
|
require "rbconfig/sizeof"
|
|
6
6
|
|
|
7
|
-
#
|
|
7
|
+
# ## Synopsis
|
|
8
8
|
#
|
|
9
9
|
# Using minitar is easy. The simplest case is:
|
|
10
10
|
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
11
|
+
# ```ruby
|
|
12
|
+
# require 'zlib'
|
|
13
|
+
# require 'minitar'
|
|
13
14
|
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
15
|
+
# # Packs everything that matches Find.find('tests'). test.tar will automatically be
|
|
16
|
+
# # closed by Minitar.pack.
|
|
17
|
+
# Minitar.pack('tests', File.open('test.tar', 'wb'))
|
|
17
18
|
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
19
|
+
# # Unpacks 'test.tar' to 'x', creating 'x' if necessary.
|
|
20
|
+
# Minitar.unpack('test.tar', 'x')
|
|
21
|
+
# ```
|
|
20
22
|
#
|
|
21
23
|
# A gzipped tar can be written with:
|
|
22
24
|
#
|
|
23
|
-
# # test.tgz will be closed automatically.
|
|
24
|
-
# Minitar.pack('tests', Zlib::GzipWriter.new(File.open('test.tgz', 'wb'))
|
|
25
25
|
#
|
|
26
|
-
#
|
|
27
|
-
#
|
|
26
|
+
# ```ruby
|
|
27
|
+
# # test.tgz will be closed automatically.
|
|
28
|
+
# Minitar.pack('tests', Zlib::GzipWriter.new(File.open('test.tgz', 'wb'))
|
|
29
|
+
#
|
|
30
|
+
# # test.tgz will be closed automatically.
|
|
31
|
+
# Minitar.unpack(Zlib::GzipReader.new(File.open('test.tgz', 'rb')), 'x')
|
|
32
|
+
# ```
|
|
28
33
|
#
|
|
29
34
|
# As the case above shows, one need not write to a file. However, it will sometimes
|
|
30
35
|
# require that one dive a little deeper into the API, as in the case of StringIO objects.
|
|
31
36
|
# Note that I'm not providing a block with Minitar::Output, as Minitar::Output#close
|
|
32
37
|
# automatically closes both the Output object and the wrapped data stream object.
|
|
33
38
|
#
|
|
34
|
-
#
|
|
35
|
-
#
|
|
36
|
-
#
|
|
37
|
-
#
|
|
38
|
-
#
|
|
39
|
-
#
|
|
40
|
-
#
|
|
41
|
-
#
|
|
42
|
-
#
|
|
43
|
-
#
|
|
39
|
+
# ```ruby
|
|
40
|
+
# begin
|
|
41
|
+
# sgz = Zlib::GzipWriter.new(StringIO.new(""))
|
|
42
|
+
# tar = Minitar::Output.new(sgz)
|
|
43
|
+
# Find.find('tests') do |entry|
|
|
44
|
+
# Minitar.pack_file(entry, tar)
|
|
45
|
+
# end
|
|
46
|
+
# ensure
|
|
47
|
+
# # Closes both tar and sgz.
|
|
48
|
+
# tar.close
|
|
49
|
+
# end
|
|
50
|
+
# ```
|
|
44
51
|
class Minitar
|
|
45
|
-
# The base class for any
|
|
52
|
+
# The base class for any Minitar error.
|
|
46
53
|
Error = Class.new(::StandardError)
|
|
47
54
|
# Raised when a wrapped data stream class is not seekable.
|
|
48
55
|
NonSeekableStream = Class.new(Error)
|
|
@@ -54,24 +61,28 @@ class Minitar
|
|
|
54
61
|
FileNameTooLong = Class.new(Error)
|
|
55
62
|
# The exception raised when a data stream ends before the amount of data expected in the
|
|
56
63
|
# archive's PosixHeader.
|
|
57
|
-
UnexpectedEOF = Class.new(
|
|
64
|
+
UnexpectedEOF = Class.new(Error)
|
|
58
65
|
# The exception raised when a file contains a relative path in secure mode (the default
|
|
59
66
|
# for this version).
|
|
60
67
|
SecureRelativePathError = Class.new(Error)
|
|
68
|
+
# The exception raised when an entry name cannot be safely written on the current
|
|
69
|
+
# platform (for example, a name containing `:` on Windows, which may address an NTFS
|
|
70
|
+
# alternate data stream).
|
|
71
|
+
UnsafeFileNameError = Class.new(Error)
|
|
61
72
|
# The exception raised when a file contains an invalid Posix header.
|
|
62
73
|
InvalidTarStream = Class.new(Error)
|
|
63
74
|
end
|
|
64
75
|
|
|
65
76
|
class << Minitar
|
|
66
|
-
# Tests if
|
|
67
|
-
#
|
|
68
|
-
def dir?(path)
|
|
77
|
+
# Tests if `path` refers to a directory. Fixes an apparently corrupted `stat()` call on
|
|
78
|
+
# Windows.
|
|
79
|
+
def dir?(path) # :nodoc:
|
|
69
80
|
path = "#{path}/" unless path.to_s.end_with?("/")
|
|
70
81
|
File.directory?(path)
|
|
71
82
|
end
|
|
72
83
|
|
|
73
|
-
# A convenience method for wrapping Minitar::Input.open (mode
|
|
74
|
-
# Minitar::Output.open (mode
|
|
84
|
+
# A convenience method for wrapping Minitar::Input.open (mode `r` or `rb`) and
|
|
85
|
+
# Minitar::Output.open (mode `w` or `wb`). No other modes are currently supported.
|
|
75
86
|
def open(dest, mode = "r", &)
|
|
76
87
|
case mode
|
|
77
88
|
when "r", "rb"
|
|
@@ -79,28 +90,34 @@ class << Minitar
|
|
|
79
90
|
when "w", "wb"
|
|
80
91
|
Minitar::Output.open(dest, &)
|
|
81
92
|
else
|
|
82
|
-
raise ArgumentError, "Unknown open mode for Minitar.open."
|
|
93
|
+
raise ArgumentError, "Unknown open mode #{mode.inspect} for Minitar.open."
|
|
83
94
|
end
|
|
84
95
|
end
|
|
85
96
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
97
|
+
WINDOWS_CONFIG = RbConfig::CONFIG["host_os"].to_s =~ /^(cygwin|mingw|mswin|windows)/i
|
|
98
|
+
WINDOWS_PLATFORM = RUBY_PLATFORM =~ /^(?:cygwin|mingw|mswin)/i
|
|
99
|
+
WINDOWS_SEPARATOR = File::ALT_SEPARATOR == "\\"
|
|
100
|
+
|
|
101
|
+
private_constant :WINDOWS_CONFIG, :WINDOWS_PLATFORM, :WINDOWS_SEPARATOR
|
|
102
|
+
|
|
103
|
+
def windows? = windows_platform? || windows_config? || File::ALT_SEPARATOR == "\\" # :nodoc:
|
|
104
|
+
def windows_config? = WINDOWS_CONFIG # :nodoc:
|
|
105
|
+
def windows_platform? = WINDOWS_PLATFORM # :nodoc:
|
|
106
|
+
def windows_separator? = WINDOWS_SEPARATOR # :nodoc:
|
|
90
107
|
|
|
91
|
-
# A convenience method to pack the provided
|
|
92
|
-
# either be a name or a Hash with the fields described below. When only a name is
|
|
108
|
+
# A convenience method to pack the provided `data` as a file named `entry`, which may
|
|
109
|
+
# either be a name String or a Hash with the fields described below. When only a name is
|
|
93
110
|
# provided, or only some Hash fields are provided, the default values will apply.
|
|
94
111
|
#
|
|
95
|
-
#
|
|
96
|
-
#
|
|
97
|
-
#
|
|
98
|
-
#
|
|
99
|
-
#
|
|
100
|
-
#
|
|
112
|
+
# - `:name` (**required**): The filename to be packed into the archive.
|
|
113
|
+
# - `:mode`: The mode to be applied. Defaults to `0o644` for files and `0o755` for
|
|
114
|
+
# directories.
|
|
115
|
+
# - `:uid`: The user owner of the file. Default is `nil`.
|
|
116
|
+
# - `:gid`: The group owner of the file. Default is `nil`.
|
|
117
|
+
# - `:mtime`: The modification Time of the file. Default is `Time.now`.
|
|
101
118
|
#
|
|
102
|
-
# If
|
|
103
|
-
#
|
|
119
|
+
# If `data` is `nil`, a directory will be created. Use an empty String for an empty
|
|
120
|
+
# file.
|
|
104
121
|
def pack_as_file(entry, data, outputter) # :yields: action, name, stats
|
|
105
122
|
outputter = outputter.tar if outputter.is_a?(Minitar::Output)
|
|
106
123
|
|
|
@@ -143,35 +160,36 @@ class << Minitar
|
|
|
143
160
|
end
|
|
144
161
|
end
|
|
145
162
|
|
|
146
|
-
# A convenience method to pack the file provided.
|
|
147
|
-
# which case various values for the file
|
|
148
|
-
#
|
|
163
|
+
# A convenience method to pack the file provided. `entry` may either be a filename
|
|
164
|
+
# string (which will in which case various values for the file will be obtained from
|
|
165
|
+
# `File#stat(entry)` or a Hash with the fields:
|
|
149
166
|
#
|
|
150
|
-
#
|
|
151
|
-
#
|
|
152
|
-
#
|
|
153
|
-
#
|
|
154
|
-
#
|
|
167
|
+
# - `:name` (**required**): The filename to be packed into the archive.
|
|
168
|
+
# - `:mode`: The mode to be applied.
|
|
169
|
+
# - `:uid`: The user owner of the file, ignored on Windows.
|
|
170
|
+
# - `:gid`: The group owner of the file, ignored on Windows.
|
|
171
|
+
# - `:mtime`: The modification Time of the file.
|
|
155
172
|
#
|
|
156
|
-
# During packing, if a block is provided, #pack_file yields an
|
|
173
|
+
# During packing, if a block is provided, #pack_file yields an `action` Symbol, the full
|
|
157
174
|
# name of the file being packed, and a Hash of statistical information, just as with
|
|
158
175
|
# Minitar::Input#extract_entry.
|
|
159
176
|
#
|
|
160
|
-
# The
|
|
161
|
-
# <tt>:dir</tt>:: The +entry+ is a directory.
|
|
162
|
-
# <tt>:file_start</tt>:: The +entry+ is a file; the extract of the file is just
|
|
163
|
-
# beginning.
|
|
164
|
-
# <tt>:file_progress</tt>:: Yielded every 4096 bytes during the extract of the +entry+.
|
|
165
|
-
# <tt>:file_done</tt>:: Yielded when the +entry+ is completed.
|
|
177
|
+
# The `action` will be one of:
|
|
166
178
|
#
|
|
167
|
-
#
|
|
168
|
-
#
|
|
169
|
-
#
|
|
170
|
-
#
|
|
171
|
-
#
|
|
172
|
-
#
|
|
173
|
-
#
|
|
174
|
-
#
|
|
179
|
+
# - `:dir`: The `entry` is a directory.
|
|
180
|
+
# - `:file_start`: The `entry` is a file; the extract of the file is just beginning.
|
|
181
|
+
# - `:file_progress`: Yielded every 4096 bytes during the extract of the file `entry`.
|
|
182
|
+
# - `file_done`: Yielded when the file `entry` is completed.
|
|
183
|
+
#
|
|
184
|
+
# The +stats+ hash may contain the following keys:
|
|
185
|
+
#
|
|
186
|
+
# - `:current`: The current total number of bytes read in the `entry`.
|
|
187
|
+
# - `:currinc`: The current number of bytes read in this read cycle.
|
|
188
|
+
# - `:name`: The filename packed into the tarchive.
|
|
189
|
+
# - `:mode`: The mode to be applied.
|
|
190
|
+
# - `:uid`: The user owner of the file, `nil` on Windows.
|
|
191
|
+
# - `:gid`: The group owner of the file, `nil` on Windows.
|
|
192
|
+
# - `:mtime`: The modification Time of the file.
|
|
175
193
|
def pack_file(entry, outputter) # :yields: action, name, stats
|
|
176
194
|
outputter = outputter.tar if outputter.is_a?(Minitar::Output)
|
|
177
195
|
|
|
@@ -219,11 +237,12 @@ class << Minitar
|
|
|
219
237
|
end
|
|
220
238
|
end
|
|
221
239
|
|
|
222
|
-
# A convenience method to pack files specified by
|
|
240
|
+
# A convenience method to pack files specified by `src` into `dest`. If `src` is an
|
|
223
241
|
# Array, then each file detailed therein will be packed into the resulting
|
|
224
|
-
# Minitar::Output stream
|
|
242
|
+
# Minitar::Output stream. If `recurse_dirs` is `true` (the default), then directories
|
|
243
|
+
# will be recursed.
|
|
225
244
|
#
|
|
226
|
-
# If
|
|
245
|
+
# If `src` is not an Array, it will be treated as the source of Find.find; all files
|
|
227
246
|
# matching will be packed.
|
|
228
247
|
def pack(src, dest, recurse_dirs = true, &block)
|
|
229
248
|
require "find"
|
|
@@ -246,8 +265,10 @@ class << Minitar
|
|
|
246
265
|
end
|
|
247
266
|
end
|
|
248
267
|
|
|
249
|
-
# A convenience method to unpack files from
|
|
250
|
-
#
|
|
268
|
+
# A convenience method to unpack files from `src` into the directory specified by
|
|
269
|
+
# `dest`.
|
|
270
|
+
#
|
|
271
|
+
# If `files` is not empty, it will act as a full name filter to restrict extraction.
|
|
251
272
|
def unpack(src, dest, files = [], options = {}, &block)
|
|
252
273
|
Minitar::Input.open(src) do |inp|
|
|
253
274
|
if File.exist?(dest) && !dir?(dest)
|
|
@@ -264,8 +285,8 @@ class << Minitar
|
|
|
264
285
|
end
|
|
265
286
|
end
|
|
266
287
|
|
|
267
|
-
# Check whether
|
|
268
|
-
def seekable?(io, methods = nil)
|
|
288
|
+
# Check whether `io` can seek without errors.
|
|
289
|
+
def seekable?(io, methods = nil) # :nodoc:
|
|
269
290
|
# The IO class throws an exception at runtime if we try to change position on
|
|
270
291
|
# a non-regular file.
|
|
271
292
|
if io.respond_to?(:stat)
|
|
@@ -283,4 +304,6 @@ require "minitar/posix_header"
|
|
|
283
304
|
require "minitar/pax_header"
|
|
284
305
|
require "minitar/input"
|
|
285
306
|
require "minitar/output"
|
|
307
|
+
require "minitar/reader"
|
|
308
|
+
require "minitar/writer"
|
|
286
309
|
require "minitar/version"
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "minitest_helper"
|
|
4
|
+
|
|
5
|
+
# Tests the private name normalization and relative path checks. These run on every
|
|
6
|
+
# platform because the alternate separator is passed as a parameter.
|
|
7
|
+
class TestPathNormalization < Minitest::Test
|
|
8
|
+
def input
|
|
9
|
+
@input ||= Minitar::Input.allocate
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def normalize(name, alt_separator)
|
|
13
|
+
input.send(:normalize_entry_name, name, alt_separator)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def check(name)
|
|
17
|
+
input.send(:check_relative_path!, name)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# --- check_relative_path! (all platforms) ---
|
|
21
|
+
|
|
22
|
+
def test_check_rejects_parent_components
|
|
23
|
+
[
|
|
24
|
+
"..", "../x", "a/..", "a/../b", "a/../../x", "a/b/.."
|
|
25
|
+
].each do |name|
|
|
26
|
+
assert_raises(Minitar::SecureRelativePathError, name) { check(name) }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def test_check_accepts_dots_within_a_component
|
|
31
|
+
[
|
|
32
|
+
"dirname../x", "..dirname/x", "a/dirname..", "a/..b/c", "...", "a/.../b", "a..b/c",
|
|
33
|
+
".hidden/x", "a/./b"
|
|
34
|
+
].each do |name|
|
|
35
|
+
check(name)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
pass
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# --- nil alt_separator (POSIX): nothing but squeeze ---
|
|
42
|
+
|
|
43
|
+
def test_nil_separator_keeps_backslashes_literal
|
|
44
|
+
[
|
|
45
|
+
"..\\x", "a\\..\\..\\x", "a\\..\\b", "..\\/x", "C:\\x", "C:x", "C:/x",
|
|
46
|
+
"\\\\host\\share\\x", "a\\b", "a.txt:stream", "...\\x", "dirname..\\x", "a. \\x"
|
|
47
|
+
].each do |name|
|
|
48
|
+
assert_equal name, normalize(name, nil)
|
|
49
|
+
check(name)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_nil_separator_squeezes_slashes
|
|
54
|
+
assert_equal "a/b/c", normalize("a//b///c", nil)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# --- "\\" alt_separator (Windows semantics) ---
|
|
58
|
+
|
|
59
|
+
def test_backslash_separator_rejects_traversal
|
|
60
|
+
[
|
|
61
|
+
"..\\x", "a\\..\\..\\x", "a\\..\\b", "..\\/x", "../x", "a/..\\x", "a\\..",
|
|
62
|
+
"\\..\\x", "\\dir\\..\\x", "dir\\..\\..\\x"
|
|
63
|
+
].each do |name|
|
|
64
|
+
assert_raises(Minitar::SecureRelativePathError, name) {
|
|
65
|
+
check(normalize(name, "\\"))
|
|
66
|
+
}
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def test_backslash_separator_rejects_dot_only_components
|
|
71
|
+
[
|
|
72
|
+
"...\\x", "....\\x", "a\\...\\b", "a\\...", ".. \\x", ".. .\\x", "a\\.. "
|
|
73
|
+
].each do |name|
|
|
74
|
+
assert_raises(Minitar::UnsafeFileNameError, name) { normalize(name, "\\") }
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def test_backslash_separator_leaves_dot_and_dotdot_components_to_the_path_check
|
|
79
|
+
assert_equal "a/./b", normalize("a\\.\\b", "\\")
|
|
80
|
+
assert_equal "a/../b", normalize("a\\..\\b", "\\")
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def test_backslash_separator_preserves_interior_dots
|
|
84
|
+
[
|
|
85
|
+
"a..b..c.txt", "a..b\\c.txt", "..a\\c.txt"
|
|
86
|
+
].each do |name|
|
|
87
|
+
assert_equal name.tr("\\", "/"), normalize(name, "\\")
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def test_backslash_separator_rejects_trailing_dots_and_spaces
|
|
92
|
+
[
|
|
93
|
+
"dirname..\\x", "dir.\\x", "dir \\x", "a\\file.", "a\\file ", "foo...\\x"
|
|
94
|
+
].each do |name|
|
|
95
|
+
assert_raises(Minitar::UnsafeFileNameError, name) { normalize(name, "\\") }
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def test_backslash_separator_accepts_ordinary_dots
|
|
100
|
+
assert_equal "a/./b", normalize("a\\.\\b", "\\")
|
|
101
|
+
assert_equal "dir..name/.hidden/a.b.txt/", normalize("dir..name\\.hidden\\a.b.txt\\", "\\")
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def test_backslash_separator_converts_and_squeezes
|
|
105
|
+
assert_equal "a/b", normalize("a\\b", "\\")
|
|
106
|
+
assert_equal "a/b/c/d", normalize("a\\\\b//c\\/d", "\\")
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def test_backslash_separator_rejects_drive_names
|
|
110
|
+
[
|
|
111
|
+
"C:\\x", "C:x", "C:/x", "c:\\x", "C:", "a\\C:\\x"
|
|
112
|
+
].each do |name|
|
|
113
|
+
assert_raises(Minitar::UnsafeFileNameError, name) { normalize(name, "\\") }
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def test_backslash_separator_turns_unc_names_into_relative_paths
|
|
118
|
+
{
|
|
119
|
+
"\\\\host\\share\\x" => "/host/share/x",
|
|
120
|
+
"//host/share/x" => "/host/share/x",
|
|
121
|
+
"\\/host/share" => "/host/share",
|
|
122
|
+
"\\\\host" => "/host"
|
|
123
|
+
}.each do |name, expected|
|
|
124
|
+
assert_equal expected, normalize(name, "\\")
|
|
125
|
+
check(expected)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def test_backslash_separator_rejects_alternate_data_streams
|
|
130
|
+
[
|
|
131
|
+
"a.txt:stream", "dir/a.txt:stream:$DATA", "dir:x/a", "a\\b:s"
|
|
132
|
+
].each do |name|
|
|
133
|
+
assert_raises(Minitar::UnsafeFileNameError, name) { normalize(name, "\\") }
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def test_unsafe_file_name_error_is_a_minitar_error
|
|
138
|
+
assert_operator Minitar::UnsafeFileNameError, :<, Minitar::Error
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# --- end-to-end on POSIX: guards against over-normalizing ---
|
|
142
|
+
|
|
143
|
+
def test_posix_extraction_keeps_backslash_names_literal
|
|
144
|
+
skip "POSIX only" if Minitar.windows_separator?
|
|
145
|
+
|
|
146
|
+
names = ["..\\x", "a\\..\\..\\x", "a\\b", "dirname../x"]
|
|
147
|
+
Dir.mktmpdir do |root|
|
|
148
|
+
dest = File.join(root, "dest")
|
|
149
|
+
Minitar.unpack(tar_with(names), dest)
|
|
150
|
+
|
|
151
|
+
assert_equal names.map { _1.sub(%r{/.*}, "") }.uniq.sort, Dir.children(dest).sort
|
|
152
|
+
assert_path_exists File.join(dest, "..\\x")
|
|
153
|
+
assert_path_exists File.join(dest, "dirname..", "x")
|
|
154
|
+
assert_equal ["dest"], Dir.children(root)
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
private
|
|
159
|
+
|
|
160
|
+
def tar_with(names)
|
|
161
|
+
io = StringIO.new(+"".b)
|
|
162
|
+
Minitar::Writer.open(io) do |w|
|
|
163
|
+
names.each do |name|
|
|
164
|
+
w.add_file_simple(name, mode: 0o644, size: 1) { _1.write("x") }
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
io.rewind
|
|
168
|
+
io
|
|
169
|
+
end
|
|
170
|
+
end
|
data/test/test_tar_reader.rb
CHANGED
|
@@ -28,7 +28,7 @@ class TestTarReader < Minitest::Test
|
|
|
28
28
|
sizes = [10, 0, 0, 0]
|
|
29
29
|
isdir = [false, false, true, true]
|
|
30
30
|
isfile = [true, true, false, false]
|
|
31
|
-
Minitar::Reader.
|
|
31
|
+
Minitar::Reader.open(StringIO.new(str)) do |is|
|
|
32
32
|
i = 0
|
|
33
33
|
is.each_entry do |entry|
|
|
34
34
|
assert_kind_of Minitar::Reader::EntryStream, entry
|
|
@@ -54,7 +54,7 @@ class TestTarReader < Minitest::Test
|
|
|
54
54
|
str = build_tar_file_header("lib/foo", "", 0o10644, content.bytesize) +
|
|
55
55
|
content + "\0" * (512 - content.bytesize)
|
|
56
56
|
str << "\0" * 1024
|
|
57
|
-
Minitar::Reader.
|
|
57
|
+
Minitar::Reader.open(StringIO.new(str)) do |is|
|
|
58
58
|
is.each_entry do |entry|
|
|
59
59
|
3.times do
|
|
60
60
|
entry.rewind
|
|
@@ -70,7 +70,7 @@ class TestTarReader < Minitest::Test
|
|
|
70
70
|
str = build_tar_file_header("lib/foo", "", 0o10644, content.bytesize) +
|
|
71
71
|
content + "\0" * (512 - content.bytesize)
|
|
72
72
|
str << "\0" * 1024
|
|
73
|
-
Minitar::Reader.
|
|
73
|
+
Minitar::Reader.open(StringIO.new(str)) do |is|
|
|
74
74
|
3.times do
|
|
75
75
|
is.rewind
|
|
76
76
|
i = 0
|
|
@@ -87,7 +87,7 @@ class TestTarReader < Minitest::Test
|
|
|
87
87
|
contents = ("a".."z").inject(+"") { |a, e| a << e * 100 }
|
|
88
88
|
str = build_tar_file_header("lib/foo", "", 0o10644, contents.bytesize) + contents
|
|
89
89
|
str += "\0" * (512 - (str.bytesize % 512))
|
|
90
|
-
Minitar::Reader.
|
|
90
|
+
Minitar::Reader.open(StringIO.new(str)) do |is|
|
|
91
91
|
is.each_entry do |entry|
|
|
92
92
|
assert_kind_of Minitar::Reader::EntryStream, entry
|
|
93
93
|
data = entry.read(3000) # bigger than contents.bytesize
|
|
@@ -95,7 +95,7 @@ class TestTarReader < Minitest::Test
|
|
|
95
95
|
assert entry.eof?
|
|
96
96
|
end
|
|
97
97
|
end
|
|
98
|
-
Minitar::Reader.
|
|
98
|
+
Minitar::Reader.open(StringIO.new(str)) do |is|
|
|
99
99
|
is.each_entry do |entry|
|
|
100
100
|
assert_kind_of Minitar::Reader::EntryStream, entry
|
|
101
101
|
data = entry.read(100)
|
|
@@ -105,7 +105,7 @@ class TestTarReader < Minitest::Test
|
|
|
105
105
|
assert entry.eof?
|
|
106
106
|
end
|
|
107
107
|
end
|
|
108
|
-
Minitar::Reader.
|
|
108
|
+
Minitar::Reader.open(StringIO.new(str)) do |is|
|
|
109
109
|
is.each_entry do |entry|
|
|
110
110
|
assert_kind_of Minitar::Reader::EntryStream, entry
|
|
111
111
|
data = entry.read
|
|
@@ -116,11 +116,19 @@ class TestTarReader < Minitest::Test
|
|
|
116
116
|
assert entry.eof?
|
|
117
117
|
end
|
|
118
118
|
end
|
|
119
|
+
Minitar::Reader.open(StringIO.new(str)) do |is|
|
|
120
|
+
is.each_entry do |entry|
|
|
121
|
+
assert_kind_of Minitar::Reader::EntryStream, entry
|
|
122
|
+
buffer = +""
|
|
123
|
+
entry.read(nil, buffer)
|
|
124
|
+
assert_equal contents, buffer
|
|
125
|
+
end
|
|
126
|
+
end
|
|
119
127
|
end
|
|
120
128
|
|
|
121
129
|
def test_eof_works
|
|
122
130
|
str = build_tar_file_header("bar", "baz", 0o644, 0)
|
|
123
|
-
Minitar::Reader.
|
|
131
|
+
Minitar::Reader.open(StringIO.new(str)) do |is|
|
|
124
132
|
is.each_entry do |entry|
|
|
125
133
|
assert_kind_of Minitar::Reader::EntryStream, entry
|
|
126
134
|
data = entry.read
|
|
@@ -132,7 +140,7 @@ class TestTarReader < Minitest::Test
|
|
|
132
140
|
end
|
|
133
141
|
end
|
|
134
142
|
str = build_tar_dir_header("foo", "bar", 0o12345)
|
|
135
|
-
Minitar::Reader.
|
|
143
|
+
Minitar::Reader.open(StringIO.new(str)) do |is|
|
|
136
144
|
is.each_entry do |entry|
|
|
137
145
|
assert_kind_of Minitar::Reader::EntryStream, entry
|
|
138
146
|
data = entry.read
|
|
@@ -146,7 +154,7 @@ class TestTarReader < Minitest::Test
|
|
|
146
154
|
str = build_tar_dir_header("foo", "bar", 0o12345)
|
|
147
155
|
str += build_tar_file_header("bar", "baz", 0o644, 0)
|
|
148
156
|
str += build_tar_file_header("bar", "baz", 0o644, 0)
|
|
149
|
-
Minitar::Reader.
|
|
157
|
+
Minitar::Reader.open(StringIO.new(str)) do |is|
|
|
150
158
|
is.each_entry do |entry|
|
|
151
159
|
assert_kind_of Minitar::Reader::EntryStream, entry
|
|
152
160
|
data = entry.read
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "minitest_helper"
|
|
4
|
+
|
|
5
|
+
# End-to-end extraction tests that depend on real Windows path semantics. These are
|
|
6
|
+
# skipped everywhere else.
|
|
7
|
+
class TestWindowsExtraction < Minitest::Test
|
|
8
|
+
def setup
|
|
9
|
+
skip "Windows only" unless Minitar.windows_separator?
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
TRAVERSAL_NAMES = [
|
|
13
|
+
"..\\escaped.txt", "a\\..\\..\\escaped.txt", "..\\/escaped.txt", "../escaped.txt"
|
|
14
|
+
].freeze
|
|
15
|
+
|
|
16
|
+
def test_traversal_names_are_rejected_and_nothing_escapes
|
|
17
|
+
TRAVERSAL_NAMES.each do |name|
|
|
18
|
+
with_dest do |root, dest|
|
|
19
|
+
assert_raises(Minitar::SecureRelativePathError, name) {
|
|
20
|
+
Minitar.unpack(tar_with(name), dest)
|
|
21
|
+
}
|
|
22
|
+
assert_equal [
|
|
23
|
+
"dest"
|
|
24
|
+
], Dir.children(root), name
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_drive_names_are_rejected
|
|
30
|
+
[
|
|
31
|
+
"C:\\escaped.txt", "C:escaped.txt", "C:/escaped.txt"
|
|
32
|
+
].each do |name|
|
|
33
|
+
with_dest do |root, dest|
|
|
34
|
+
assert_raises(Minitar::UnsafeFileNameError, name) {
|
|
35
|
+
Minitar.unpack(tar_with(name), dest)
|
|
36
|
+
}
|
|
37
|
+
assert_equal ["dest"], Dir.children(root), name
|
|
38
|
+
assert_empty Dir.children(dest)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_unc_names_extract_as_relative_paths_inside_destdir
|
|
44
|
+
[
|
|
45
|
+
"\\\\host\\share\\c.txt", "//host/share/c.txt"
|
|
46
|
+
].each do |name|
|
|
47
|
+
with_dest do |root, dest|
|
|
48
|
+
Minitar.unpack(tar_with(name), dest)
|
|
49
|
+
|
|
50
|
+
assert_equal ["dest"], Dir.children(root), name
|
|
51
|
+
assert_path_exists File.join(dest, "host", "share", "c.txt")
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def test_alternate_data_streams_are_rejected
|
|
57
|
+
with_dest do |root, dest|
|
|
58
|
+
assert_raises(Minitar::UnsafeFileNameError) {
|
|
59
|
+
Minitar.unpack(tar_with("a.txt:stream"), dest)
|
|
60
|
+
}
|
|
61
|
+
assert_empty Dir.children(dest)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def test_backslash_name_extracts_as_nested_path
|
|
66
|
+
with_dest do |_, dest|
|
|
67
|
+
Minitar.unpack(tar_with("a\\b\\c.txt"), dest)
|
|
68
|
+
|
|
69
|
+
assert_equal "x", File.read(File.join(dest, "a", "b", "c.txt"))
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def test_dots_within_a_component_are_accepted
|
|
74
|
+
with_dest do |_, dest|
|
|
75
|
+
Minitar.unpack(tar_with("dir..name\\c.txt"), dest)
|
|
76
|
+
|
|
77
|
+
assert_path_exists File.join(dest, "dir..name", "c.txt")
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def test_dot_only_and_trailing_dot_components_are_rejected
|
|
82
|
+
[
|
|
83
|
+
"...\\escaped.txt", "a\\...\\escaped.txt", ".. \\escaped.txt"
|
|
84
|
+
].each do |name|
|
|
85
|
+
with_dest do |root, dest|
|
|
86
|
+
assert_raises(Minitar::UnsafeFileNameError, name) {
|
|
87
|
+
Minitar.unpack(tar_with(name), dest)
|
|
88
|
+
}
|
|
89
|
+
assert_equal [
|
|
90
|
+
"dest"
|
|
91
|
+
], Dir.children(root), name
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
with_dest do |_, dest|
|
|
96
|
+
assert_raises(Minitar::UnsafeFileNameError) {
|
|
97
|
+
Minitar.unpack(tar_with("dirname..\\c.txt"), dest)
|
|
98
|
+
}
|
|
99
|
+
assert_empty Dir.children(dest)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
private
|
|
104
|
+
|
|
105
|
+
def with_dest
|
|
106
|
+
Dir.mktmpdir do |root|
|
|
107
|
+
dest = File.join(root, "dest")
|
|
108
|
+
Dir.mkdir(dest)
|
|
109
|
+
yield root, dest
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def tar_with(name)
|
|
114
|
+
io = StringIO.new(+"".b)
|
|
115
|
+
Minitar::Writer.open(io) do |w|
|
|
116
|
+
w.add_file_simple(name, mode: 0o644, size: 1) { _1.write("x") }
|
|
117
|
+
end
|
|
118
|
+
io.rewind
|
|
119
|
+
io
|
|
120
|
+
end
|
|
121
|
+
end
|