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/input.rb
CHANGED
|
@@ -1,252 +1,292 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
# This wraps a Minitar::Reader with convenience methods and wrapped stream management;
|
|
4
|
+
# Input only works with data streams that can be rewound.
|
|
5
|
+
#
|
|
6
|
+
# ### Security Notice
|
|
7
|
+
#
|
|
8
|
+
# Constructing a Minitar::Input will use Kernel.open if the provided input is not
|
|
9
|
+
# a readable stream object. Using an untrusted value for input may allow a malicious
|
|
10
|
+
# user to execute arbitrary system commands. It is the **caller's responsibility** to
|
|
11
|
+
# ensure that the input value is safe.
|
|
12
|
+
#
|
|
13
|
+
# * [CWE-073](https://cwe.mitre.org/data/definitions/73.html)
|
|
14
|
+
# * [CWE-078](https://cwe.mitre.org/data/definitions/78.html)
|
|
15
|
+
# * [CWE-088](https://cwe.mitre.org/data/definitions/88.html)
|
|
16
|
+
#
|
|
17
|
+
# This notice applies to Minitar::Input.open, Minitar::Input.each_entry, and
|
|
18
|
+
# Minitar::Input.new.
|
|
19
|
+
class Minitar::Input
|
|
20
|
+
include Enumerable
|
|
21
|
+
|
|
22
|
+
# With no associated block, `Input.open` is a synonym for `Input.new`.
|
|
23
|
+
#
|
|
24
|
+
# If a block is given, the new Input will be yielded to the block as an argument and
|
|
25
|
+
# the Input object will automatically be closed when the block terminates (this also
|
|
26
|
+
# closes the wrapped stream object). The return value will be the value of the block.
|
|
8
27
|
#
|
|
9
|
-
#
|
|
28
|
+
# :call-seq:
|
|
29
|
+
# Minitar::Input.open(io) -> input
|
|
30
|
+
# Minitar::Input.open(io) { |input| block } -> obj
|
|
31
|
+
def self.open(input)
|
|
32
|
+
stream = new(input)
|
|
33
|
+
|
|
34
|
+
if block_given?
|
|
35
|
+
# This exception context must remain, otherwise the stream closes on open even if
|
|
36
|
+
# a block is not given.
|
|
37
|
+
begin
|
|
38
|
+
yield stream
|
|
39
|
+
ensure
|
|
40
|
+
stream.close
|
|
41
|
+
end
|
|
42
|
+
else
|
|
43
|
+
stream
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Iterates over each entry in the provided input. This wraps the common pattern of:
|
|
10
48
|
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
49
|
+
# ```ruby
|
|
50
|
+
# Minitar::Input.open(io) do |i|
|
|
51
|
+
# inp.each do |entry|
|
|
52
|
+
# # ...
|
|
53
|
+
# end
|
|
54
|
+
# end
|
|
55
|
+
# ```
|
|
15
56
|
#
|
|
16
|
-
#
|
|
17
|
-
# * {CWE-078}[https://cwe.mitre.org/data/definitions/78.html]
|
|
18
|
-
# * {CWE-088}[https://cwe.mitre.org/data/definitions/88.html]
|
|
57
|
+
# If a block is not provided, an enumerator will be created with the same behaviour.
|
|
19
58
|
#
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
# closes the wrapped stream object). The return value will be the value of the block.
|
|
30
|
-
#
|
|
31
|
-
# :call-seq:
|
|
32
|
-
# Minitar::Input.open(io) -> input
|
|
33
|
-
# Minitar::Input.open(io) { |input| block } -> obj
|
|
34
|
-
def self.open(input)
|
|
35
|
-
stream = new(input)
|
|
36
|
-
|
|
37
|
-
if block_given?
|
|
38
|
-
# This exception context must remain, otherwise the stream closes on open even if
|
|
39
|
-
# a block is not given.
|
|
40
|
-
begin
|
|
41
|
-
yield stream
|
|
42
|
-
ensure
|
|
43
|
-
stream.close
|
|
44
|
-
end
|
|
45
|
-
else
|
|
46
|
-
stream
|
|
59
|
+
# :call-seq:
|
|
60
|
+
# Minitar::Input.each_entry(io) -> enumerator
|
|
61
|
+
# Minitar::Input.each_entry(io) { |entry| block } -> obj
|
|
62
|
+
def self.each_entry(input)
|
|
63
|
+
return to_enum(__method__, input) unless block_given?
|
|
64
|
+
|
|
65
|
+
Input.open(input) do |stream|
|
|
66
|
+
stream.each do |entry|
|
|
67
|
+
yield entry
|
|
47
68
|
end
|
|
48
69
|
end
|
|
70
|
+
end
|
|
49
71
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
Input.open(input) do |stream|
|
|
67
|
-
stream.each do |entry|
|
|
68
|
-
yield entry
|
|
69
|
-
end
|
|
72
|
+
# Creates a new Input object. If `input` is a stream object that responds to #read,
|
|
73
|
+
# then it will simply be wrapped. Otherwise, one will be created and opened using
|
|
74
|
+
# Kernel.open. When Input#close is called, the stream object wrapped will be closed.
|
|
75
|
+
#
|
|
76
|
+
# An exception will be raised if the stream that is wrapped does not support
|
|
77
|
+
# rewinding.
|
|
78
|
+
#
|
|
79
|
+
# :call-seq:
|
|
80
|
+
# Minitar::Input.new(io) -> input
|
|
81
|
+
# Minitar::Input.new(path) -> input
|
|
82
|
+
def initialize(input)
|
|
83
|
+
@io =
|
|
84
|
+
if input.respond_to?(:read)
|
|
85
|
+
input
|
|
86
|
+
else
|
|
87
|
+
::Kernel.open(input, "rb")
|
|
70
88
|
end
|
|
71
|
-
end
|
|
72
89
|
|
|
73
|
-
|
|
74
|
-
# then it will simply be wrapped. Otherwise, one will be created and opened using
|
|
75
|
-
# Kernel#open. When Input#close is called, the stream object wrapped will be closed.
|
|
76
|
-
#
|
|
77
|
-
# An exception will be raised if the stream that is wrapped does not support
|
|
78
|
-
# rewinding.
|
|
79
|
-
#
|
|
80
|
-
# :call-seq:
|
|
81
|
-
# Minitar::Input.new(io) -> input
|
|
82
|
-
# Minitar::Input.new(path) -> input
|
|
83
|
-
def initialize(input)
|
|
84
|
-
@io =
|
|
85
|
-
if input.respond_to?(:read)
|
|
86
|
-
input
|
|
87
|
-
else
|
|
88
|
-
::Kernel.open(input, "rb")
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
raise Minitar::NonSeekableStream unless Minitar.seekable?(@io, :rewind)
|
|
92
|
-
|
|
93
|
-
@tar = Reader.new(@io)
|
|
94
|
-
end
|
|
90
|
+
raise Minitar::NonSeekableStream unless Minitar.seekable?(@io, :rewind)
|
|
95
91
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
#
|
|
99
|
-
# If not provided a block, creates an enumerator with the same semantics.
|
|
100
|
-
def each_entry
|
|
101
|
-
return to_enum unless block_given?
|
|
92
|
+
@tar = Minitar::Reader.new(@io)
|
|
93
|
+
end
|
|
102
94
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
95
|
+
# When provided a block, iterates through each entry in the archive. When finished,
|
|
96
|
+
# rewinds to the beginning of the stream.
|
|
97
|
+
#
|
|
98
|
+
# If not provided a block, creates an enumerator with the same semantics.
|
|
99
|
+
def each_entry
|
|
100
|
+
return to_enum unless block_given?
|
|
101
|
+
|
|
102
|
+
@tar.each do |entry|
|
|
103
|
+
yield entry
|
|
108
104
|
end
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
# statistical information (+stats+).
|
|
114
|
-
#
|
|
115
|
-
# The +action+ will be one of:
|
|
116
|
-
#
|
|
117
|
-
# <tt>:dir</tt>:: The +entry+ is a directory.
|
|
118
|
-
# <tt>:file_start</tt>:: The +entry+ is a file; the extract of the file is just
|
|
119
|
-
# beginning.
|
|
120
|
-
# <tt>:file_progress</tt>:: Yielded every 4096 bytes during the extract of the
|
|
121
|
-
# +entry+.
|
|
122
|
-
# <tt>:file_done</tt>:: Yielded when the +entry+ is completed.
|
|
123
|
-
#
|
|
124
|
-
# The +stats+ hash contains the following keys:
|
|
125
|
-
#
|
|
126
|
-
# <tt>:current</tt>:: The current total number of bytes read in the +entry+.
|
|
127
|
-
# <tt>:currinc</tt>:: The current number of bytes read in this read cycle.
|
|
128
|
-
# <tt>:entry</tt>:: The entry being extracted; this is a Reader::EntryStream, with
|
|
129
|
-
# all methods thereof.
|
|
130
|
-
def extract_entry(destdir, entry, options = {}, &) # :yields: action, name, stats
|
|
131
|
-
stats = {
|
|
132
|
-
current: 0,
|
|
133
|
-
currinc: 0,
|
|
134
|
-
entry: entry
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
# extract_entry is not vulnerable to prefix '/' vulnerabilities, but it is
|
|
138
|
-
# vulnerable to relative path directories. This code will break this vulnerability.
|
|
139
|
-
# For this version, we are breaking relative paths HARD by throwing an exception.
|
|
140
|
-
#
|
|
141
|
-
# Future versions may permit relative paths as long as the file does not leave
|
|
142
|
-
# +destdir+.
|
|
143
|
-
#
|
|
144
|
-
# However, squeeze consecutive '/' characters together.
|
|
145
|
-
full_name = entry.full_name.squeeze("/")
|
|
146
|
-
|
|
147
|
-
if /\.{2}(?:\/|\z)/.match?(full_name)
|
|
148
|
-
raise SecureRelativePathError, "Path contains '..'"
|
|
149
|
-
end
|
|
105
|
+
ensure
|
|
106
|
+
@tar.rewind
|
|
107
|
+
end
|
|
108
|
+
alias_method :each, :each_entry
|
|
150
109
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
110
|
+
# Extracts the current `entry` to `destdir`. If a block is provided, it yields an
|
|
111
|
+
# `action` Symbol, the full name of the file being extracted (`name`), and a Hash of
|
|
112
|
+
# statistical information (`stats`).
|
|
113
|
+
#
|
|
114
|
+
# The `action` will be one of:
|
|
115
|
+
#
|
|
116
|
+
# - `:dir`: The `entry` is a directory.
|
|
117
|
+
# - `:file_start`: The `entry` is a file; the extract of the file is just beginning.
|
|
118
|
+
# - `:file_progress`: Yielded every 4096 bytes during the extract of the `entry`.
|
|
119
|
+
# - `:file_done`: Yielded when the `entry` is completed.
|
|
120
|
+
#
|
|
121
|
+
# The `stats` hash may contain the following keys:
|
|
122
|
+
#
|
|
123
|
+
# - `:current`: The current total number of bytes read in the `entry`.
|
|
124
|
+
# - `:currinc`: The current number of bytes read in this read cycle.
|
|
125
|
+
# - `:entry`: The entry being extracted; this is a Minitar::Reader::EntryStream.
|
|
126
|
+
def extract_entry(destdir, entry, options = {}, &) # :yields: action, name, stats
|
|
127
|
+
stats = {
|
|
128
|
+
current: 0,
|
|
129
|
+
currinc: 0,
|
|
130
|
+
entry: entry
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
# extract_entry is not vulnerable to prefix '/' vulnerabilities; check and reject
|
|
134
|
+
# relative path directories on both Windows and Unix-like systems. This is a hard
|
|
135
|
+
# rejection by throwing exceptions. Future versions may permit relative paths as long
|
|
136
|
+
# as the file does not leave `destdir`.
|
|
137
|
+
full_name = normalize_entry_name(entry.full_name)
|
|
138
|
+
check_relative_path!(full_name)
|
|
139
|
+
|
|
140
|
+
if entry.directory?
|
|
141
|
+
extract_directory(destdir, full_name, entry, stats, options, &)
|
|
142
|
+
else # it's a file
|
|
143
|
+
extract_file(destdir, full_name, entry, stats, options, &)
|
|
156
144
|
end
|
|
145
|
+
end
|
|
157
146
|
|
|
158
|
-
|
|
159
|
-
|
|
147
|
+
# Returns false if the wrapped data stream is open.
|
|
148
|
+
def closed? = @io.closed?
|
|
160
149
|
|
|
161
|
-
|
|
162
|
-
|
|
150
|
+
# Returns the Minitar::Reader object for direct access.
|
|
151
|
+
attr_reader :tar
|
|
163
152
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
153
|
+
# Closes both the Minitar::Reader object and the wrapped data stream.
|
|
154
|
+
def close
|
|
155
|
+
@io.close
|
|
156
|
+
@tar.close
|
|
157
|
+
end
|
|
169
158
|
|
|
170
|
-
|
|
159
|
+
private
|
|
171
160
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
161
|
+
# Normalizes an entry name as the filesystem will interpret it.
|
|
162
|
+
#
|
|
163
|
+
# On Unix-like systems (`File::ALT_SEPARATOR.nil?`), consecutive `/` are always
|
|
164
|
+
# squeezed to a single `/`.
|
|
165
|
+
#
|
|
166
|
+
# On Windows, the normalization _and check_ are more extensive:
|
|
167
|
+
#
|
|
168
|
+
# - All `\` are normalized to `/` and squeezed.
|
|
169
|
+
# - Drive designators `[A-Z]:` are rejected with UnsafeFileNameError.
|
|
170
|
+
# - Files with `:` are rejected with UnsafeFileNameError (NTFS alternate data streams
|
|
171
|
+
# are removed with `File.basename`, causing file aliasing).
|
|
172
|
+
# - Each path component is examined to see if, aside from `..` and `.`, it contains
|
|
173
|
+
# a trailing dot or space. These are rejected with UnsafeFileNameError due to file
|
|
174
|
+
# aliasing.
|
|
175
|
+
def normalize_entry_name(name, alt_separator = File::ALT_SEPARATOR)
|
|
176
|
+
return name.squeeze("/") unless alt_separator
|
|
177
|
+
|
|
178
|
+
name = name.tr(alt_separator, "/").squeeze("/")
|
|
179
|
+
|
|
180
|
+
# A drive designator looks like a directory (`C:` is a drive, not a stream name), so
|
|
181
|
+
# it is called out separately from the general `:` check below.
|
|
182
|
+
if name.match?(/\A[A-Za-z]:/)
|
|
183
|
+
raise Minitar::UnsafeFileNameError, "Path begins with a drive designator"
|
|
180
184
|
end
|
|
181
185
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
yield :dir, full_name, stats if block_given?
|
|
186
|
+
if name.include?(":")
|
|
187
|
+
raise Minitar::UnsafeFileNameError, "Path contains ':'"
|
|
188
|
+
end
|
|
186
189
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
else
|
|
194
|
-
File.unlink(dest.chomp("/")) if File.symlink?(dest.chomp("/"))
|
|
190
|
+
# Win32 trims trailing dots and spaces from path components, so `...`, `.. `, and
|
|
191
|
+
# `dirname..` alias a different name (often the containing directory) than the
|
|
192
|
+
# literal name in the archive. `.` and `..` are left alone: `.` is harmless and `..`
|
|
193
|
+
# is handled by #check_relative_path!.
|
|
194
|
+
name.split("/").each do |part|
|
|
195
|
+
next if part == "." || part == ".."
|
|
195
196
|
|
|
196
|
-
|
|
197
|
-
|
|
197
|
+
if part.match?(/[ .]\z/)
|
|
198
|
+
raise Minitar::UnsafeFileNameError, "Path component ends with '.' or ' '"
|
|
198
199
|
end
|
|
200
|
+
end
|
|
199
201
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
202
|
+
name
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Raises Minitar::SecureRelativePathError if any path component of the (normalized)
|
|
206
|
+
# `name` is `..`.
|
|
207
|
+
def check_relative_path!(name)
|
|
208
|
+
if %r{(?:\A|/)\.\.(?:/|\z)}.match?(name)
|
|
209
|
+
raise Minitar::SecureRelativePathError, "Path contains '..'"
|
|
204
210
|
end
|
|
211
|
+
end
|
|
205
212
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
213
|
+
def fsync_dir(dirname)
|
|
214
|
+
# make sure this hits the disc
|
|
215
|
+
dir = IO.open(dirname, "rb")
|
|
216
|
+
dir.fsync
|
|
217
|
+
rescue # ignore IOError if it's an unpatched (old) Ruby
|
|
218
|
+
nil
|
|
219
|
+
ensure
|
|
220
|
+
dir&.close rescue nil # standard:disable Style/RescueModifier
|
|
221
|
+
end
|
|
209
222
|
|
|
210
|
-
|
|
223
|
+
def extract_directory(destdir, full_name, entry, stats, options)
|
|
224
|
+
dest = File.join(destdir, full_name)
|
|
211
225
|
|
|
212
|
-
|
|
226
|
+
yield :dir, full_name, stats if block_given?
|
|
213
227
|
|
|
214
|
-
|
|
228
|
+
if Minitar.dir?(dest)
|
|
215
229
|
begin
|
|
216
|
-
FileUtils.chmod(
|
|
230
|
+
FileUtils.chmod(entry.mode, dest)
|
|
217
231
|
rescue
|
|
218
232
|
nil
|
|
219
233
|
end
|
|
234
|
+
else
|
|
235
|
+
File.unlink(dest.chomp("/")) if File.symlink?(dest.chomp("/"))
|
|
220
236
|
|
|
221
|
-
|
|
237
|
+
FileUtils.mkdir_p(dest, mode: entry.mode)
|
|
238
|
+
FileUtils.chmod(entry.mode, dest)
|
|
239
|
+
end
|
|
222
240
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
241
|
+
if options.fetch(:fsync, true)
|
|
242
|
+
fsync_dir(dest)
|
|
243
|
+
fsync_dir(File.join(dest, ".."))
|
|
244
|
+
end
|
|
245
|
+
end
|
|
227
246
|
|
|
228
|
-
|
|
229
|
-
|
|
247
|
+
def extract_file(destdir, full_name, entry, stats, options)
|
|
248
|
+
destdir = File.join(destdir, File.dirname(full_name))
|
|
249
|
+
FileUtils.mkdir_p(destdir, mode: 0o755)
|
|
230
250
|
|
|
231
|
-
|
|
232
|
-
end
|
|
251
|
+
destfile = File.join(destdir, File.basename(full_name))
|
|
233
252
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
253
|
+
File.unlink(destfile) if File.symlink?(destfile)
|
|
254
|
+
|
|
255
|
+
# Errno::ENOENT
|
|
256
|
+
begin
|
|
257
|
+
FileUtils.chmod(0o600, destfile)
|
|
258
|
+
rescue
|
|
259
|
+
nil
|
|
260
|
+
end
|
|
239
261
|
|
|
240
|
-
|
|
262
|
+
yield :file_start, full_name, stats if block_given?
|
|
241
263
|
|
|
242
|
-
|
|
243
|
-
|
|
264
|
+
File.open(destfile, "wb", entry.mode) do |os|
|
|
265
|
+
loop do
|
|
266
|
+
data = entry.read(4096)
|
|
267
|
+
break unless data
|
|
268
|
+
|
|
269
|
+
stats[:currinc] = os.write(data)
|
|
270
|
+
stats[:current] += stats[:currinc]
|
|
244
271
|
|
|
245
|
-
|
|
246
|
-
fsync_dir(File.join(File.dirname(destfile), ".."))
|
|
272
|
+
yield :file_progress, full_name, stats if block_given?
|
|
247
273
|
end
|
|
248
274
|
|
|
249
|
-
|
|
275
|
+
if options.fetch(:fsync, true)
|
|
276
|
+
yield :file_fsync, full_name, stats if block_given?
|
|
277
|
+
os.fsync
|
|
278
|
+
end
|
|
250
279
|
end
|
|
280
|
+
|
|
281
|
+
FileUtils.chmod(entry.mode, destfile)
|
|
282
|
+
|
|
283
|
+
if options.fetch(:fsync, true)
|
|
284
|
+
yield :dir_fsync, full_name, stats if block_given?
|
|
285
|
+
|
|
286
|
+
fsync_dir(File.dirname(destfile))
|
|
287
|
+
fsync_dir(File.join(File.dirname(destfile), ".."))
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
yield :file_done, full_name, stats if block_given?
|
|
251
291
|
end
|
|
252
292
|
end
|
data/lib/minitar/output.rb
CHANGED
|
@@ -1,89 +1,85 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
3
|
+
# Wraps a Minitar::Writer with convenience methods and wrapped stream management. If the
|
|
4
|
+
# stream provided to Output does not support random access, only Writer#add_file_simple
|
|
5
|
+
# and Writer#mkdir are guaranteed to work.
|
|
6
|
+
#
|
|
7
|
+
# ### Security Notice
|
|
8
|
+
#
|
|
9
|
+
# Constructing a Minitar::Output will use Kernel.open if the provided output is not
|
|
10
|
+
# a readable stream object. Using an untrusted value for output may allow a malicious
|
|
11
|
+
# user to execute arbitrary system commands. It is the **caller's responsibility** to
|
|
12
|
+
# ensure that the output value is safe.
|
|
13
|
+
#
|
|
14
|
+
# * [CWE-073](https://cwe.mitre.org/data/definitions/73.html)
|
|
15
|
+
# * [CWE-078](https://cwe.mitre.org/data/definitions/78.html)
|
|
16
|
+
# * [CWE-088](https://cwe.mitre.org/data/definitions/88.html)
|
|
17
|
+
#
|
|
18
|
+
# This notice applies to Minitar::Output.open, Minitar::Output.tar, and
|
|
19
|
+
# Minitar::Output.new.
|
|
20
|
+
class Minitar::Output
|
|
21
|
+
# With no associated block, `Output.open` is a synonym for `Output.new`.
|
|
16
22
|
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
23
|
+
# If a block is given, the new Output will be yielded to the block as an argument and
|
|
24
|
+
# the Output object will automatically be closed when the block terminates (this also
|
|
25
|
+
# closes the wrapped stream object). The return value will be the value of the block.
|
|
20
26
|
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
# the Output object will automatically be closed when the block terminates (this also
|
|
28
|
-
# closes the wrapped stream object). The return value will be the value of the block.
|
|
29
|
-
#
|
|
30
|
-
# :call-seq:
|
|
31
|
-
# Minitar::Output.open(io) -> output
|
|
32
|
-
# Minitar::Output.open(io) { |output| block } -> obj
|
|
33
|
-
def self.open(output)
|
|
34
|
-
stream = new(output)
|
|
35
|
-
return stream unless block_given?
|
|
27
|
+
# :call-seq:
|
|
28
|
+
# Minitar::Output.open(io) -> output
|
|
29
|
+
# Minitar::Output.open(io) { |output| block } -> obj
|
|
30
|
+
def self.open(output)
|
|
31
|
+
stream = new(output)
|
|
32
|
+
return stream unless block_given?
|
|
36
33
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
end
|
|
34
|
+
# This exception context must remain, otherwise the stream closes on open even if
|
|
35
|
+
# a block is not given.
|
|
36
|
+
begin
|
|
37
|
+
yield stream
|
|
38
|
+
ensure
|
|
39
|
+
stream.close
|
|
44
40
|
end
|
|
41
|
+
end
|
|
45
42
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
43
|
+
# Output.tar is a wrapper for Output.open that yields the owned tar object instead of
|
|
44
|
+
# the Output object. If a block is not provided, an enumerator will be created with the
|
|
45
|
+
# same behaviour.
|
|
46
|
+
#
|
|
47
|
+
# :call-seq:
|
|
48
|
+
# Minitar::Output.tar(io) -> enumerator
|
|
49
|
+
# Minitar::Output.tar(io) { |tar| block } -> obj
|
|
50
|
+
def self.tar(output)
|
|
51
|
+
return to_enum(__method__, output) unless block_given?
|
|
55
52
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
end
|
|
53
|
+
Output.open(output) do |stream|
|
|
54
|
+
yield stream.tar
|
|
59
55
|
end
|
|
56
|
+
end
|
|
60
57
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
end
|
|
74
|
-
@tar = Minitar::Writer.new(@io)
|
|
58
|
+
# Creates a new Output object. If `output` is a stream object that responds to #write,
|
|
59
|
+
# then it will simply be wrapped. Otherwise, one will be created and opened using
|
|
60
|
+
# Kernel#open. When Output#close is called, the stream object wrapped will be closed.
|
|
61
|
+
#
|
|
62
|
+
# :call-seq:
|
|
63
|
+
# Minitar::Output.new(io) -> output
|
|
64
|
+
# Minitar::Output.new(path) -> output
|
|
65
|
+
def initialize(output)
|
|
66
|
+
@io = if output.respond_to?(:write)
|
|
67
|
+
output
|
|
68
|
+
else
|
|
69
|
+
::Kernel.open(output, "wb")
|
|
75
70
|
end
|
|
71
|
+
@tar = Minitar::Writer.new(@io)
|
|
72
|
+
end
|
|
76
73
|
|
|
77
|
-
|
|
78
|
-
|
|
74
|
+
# Returns the Writer object for direct access.
|
|
75
|
+
attr_reader :tar
|
|
79
76
|
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
# Returns false if the wrapped data stream is open.
|
|
78
|
+
def closed? = @io.closed?
|
|
82
79
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
end
|
|
80
|
+
# Closes the Writer object and the wrapped data stream.
|
|
81
|
+
def close
|
|
82
|
+
@tar.close
|
|
83
|
+
@io.close
|
|
88
84
|
end
|
|
89
85
|
end
|