minitar 1.0.2 → 1.1.1

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 (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +326 -0
  3. data/CODE_OF_CONDUCT.md +167 -0
  4. data/CONTRIBUTING.md +312 -0
  5. data/CONTRIBUTORS.md +27 -0
  6. data/LICENCE.md +42 -0
  7. data/Manifest.txt +31 -8
  8. data/README.md +82 -0
  9. data/Rakefile +74 -20
  10. data/SECURITY.md +54 -0
  11. data/lib/minitar/input.rb +242 -189
  12. data/lib/minitar/output.rb +72 -64
  13. data/lib/minitar/pax_header.rb +111 -0
  14. data/lib/minitar/posix_header.rb +276 -239
  15. data/lib/minitar/reader.rb +226 -231
  16. data/lib/minitar/version.rb +5 -0
  17. data/lib/minitar/writer.rb +246 -299
  18. data/lib/minitar.rb +134 -115
  19. data/licenses/bsdl.txt +20 -0
  20. data/licenses/dco.txt +34 -0
  21. data/licenses/ruby.txt +52 -0
  22. data/test/fixtures/issue_46.tar.gz +0 -0
  23. data/test/fixtures/issue_62.tar.gz +0 -0
  24. data/test/fixtures/tar_input.tgz +0 -0
  25. data/test/fixtures/test_input_non_strict_octal.tgz +0 -0
  26. data/test/fixtures/test_input_relative.tgz +0 -0
  27. data/test/fixtures/test_input_space_octal.tgz +0 -0
  28. data/test/fixtures/test_minitar.tar.gz +0 -0
  29. data/test/minitest_helper.rb +12 -1
  30. data/test/support/minitar_test_helpers/fixtures.rb +38 -0
  31. data/test/support/minitar_test_helpers/header.rb +130 -0
  32. data/test/support/minitar_test_helpers/tarball.rb +324 -0
  33. data/test/support/minitar_test_helpers.rb +36 -0
  34. data/test/test_filename_boundary_conditions.rb +74 -0
  35. data/test/test_gnu_tar_compatibility.rb +92 -0
  36. data/test/test_integration_pack_unpack_cycle.rb +38 -0
  37. data/test/test_issue_46.rb +5 -23
  38. data/test/test_issue_62.rb +50 -0
  39. data/test/test_minitar.rb +168 -39
  40. data/test/test_path_normalization.rb +170 -0
  41. data/test/test_pax_header.rb +104 -0
  42. data/test/test_pax_support.rb +66 -0
  43. data/test/test_tar_header.rb +289 -75
  44. data/test/test_tar_input.rb +14 -61
  45. data/test/test_tar_output.rb +7 -9
  46. data/test/test_tar_reader.rb +17 -18
  47. data/test/test_tar_writer.rb +105 -126
  48. data/test/test_windows_extraction.rb +121 -0
  49. metadata +108 -95
  50. data/Code-of-Conduct.md +0 -128
  51. data/Contributing.md +0 -94
  52. data/History.md +0 -236
  53. data/Licence.md +0 -15
  54. data/README.rdoc +0 -92
  55. data/docs/bsdl.txt +0 -19
  56. data/docs/ruby.txt +0 -56
  57. data/test/support/tar_test_helpers.rb +0 -134
@@ -1,77 +1,85 @@
1
- require "minitar/writer"
1
+ # frozen_string_literal: true
2
2
 
3
- class Minitar
4
- # Wraps a Minitar::Writer with convenience methods and wrapped
5
- # stream management. If the stream provided to Output does not support random
6
- # access, only Writer#add_file_simple and Writer#mkdir are guaranteed to
7
- # work.
8
- class Output
9
- # With no associated block, +Output.open+ is a synonym for +Output.new+. If
10
- # the optional code block is given, it will be given the new Output as an
11
- # argument and the Output object will automatically be closed when the
12
- # block terminates (this also closes the wrapped stream object). In this
13
- # instance, +Output.open+ returns the value of the block.
14
- #
15
- # call-seq:
16
- # Minitar::Output.open(io) -> output
17
- # Minitar::Output.open(io) { |output| block } -> obj
18
- def self.open(output)
19
- stream = new(output)
20
- return stream unless block_given?
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`.
22
+ #
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.
26
+ #
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?
21
33
 
22
- # This exception context must remain, otherwise the stream closes on open
23
- # even if a block is not given.
24
- begin
25
- yield stream
26
- ensure
27
- stream.close
28
- 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
29
40
  end
41
+ end
30
42
 
31
- # Output.tar is a wrapper for Output.open that yields the owned tar object
32
- # instead of the Output object. If a block is not provided, an enumerator
33
- # will be created with the same behaviour.
34
- #
35
- # call-seq:
36
- # Minitar::Output.tar(io) -> enumerator
37
- # Minitar::Output.tar(io) { |tar| block } -> obj
38
- def self.tar(output)
39
- return to_enum(__method__, output) unless block_given?
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?
40
52
 
41
- Output.open(output) do |stream|
42
- yield stream.tar
43
- end
53
+ Output.open(output) do |stream|
54
+ yield stream.tar
44
55
  end
56
+ end
45
57
 
46
- # Creates a new Output object. If +output+ is a stream object that responds
47
- # to #write, then it will simply be wrapped. Otherwise, one will be created
48
- # and opened using Kernel#open. When Output#close is called, the stream
49
- # object wrapped will be closed.
50
- #
51
- # call-seq:
52
- # Minitar::Output.new(io) -> output
53
- # Minitar::Output.new(path) -> output
54
- def initialize(output)
55
- @io = if output.respond_to?(:write)
56
- output
57
- else
58
- ::Kernel.open(output, "wb")
59
- end
60
- @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")
61
70
  end
71
+ @tar = Minitar::Writer.new(@io)
72
+ end
62
73
 
63
- # Returns the Writer object for direct access.
64
- attr_reader :tar
74
+ # Returns the Writer object for direct access.
75
+ attr_reader :tar
65
76
 
66
- # Returns false if the wrapped data stream is open.
67
- def closed?
68
- @io.closed?
69
- end
77
+ # Returns false if the wrapped data stream is open.
78
+ def closed? = @io.closed?
70
79
 
71
- # Closes the Writer object and the wrapped data stream.
72
- def close
73
- @tar.close
74
- @io.close
75
- end
80
+ # Closes the Writer object and the wrapped data stream.
81
+ def close
82
+ @tar.close
83
+ @io.close
76
84
  end
77
85
  end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Implements the PAX Extended Header as a Ruby class. The header consists of following
4
+ # format:
5
+ #
6
+ # ```
7
+ # <decimal-length><space><ascii-keyword>=<value><newline>
8
+ # ```
9
+ #
10
+ # Where:
11
+ #
12
+ # - `decimal-length`: the total number of bytes for the PaxHeader record using ASCII
13
+ # decimal values; this includes the terminal newline (0x10).
14
+ # - `space` is a single literal ASCII space (0x20).
15
+ # - `ascii-keyword` is a PAX Extended Header keyword, which may be any ASCII character
16
+ # except newline (0x10) or equal sign (0x3D).
17
+ # - `=` is the literal ASCII equal sign (0x3D).
18
+ # - `value` is any series of bytes except newline (0x10).
19
+ # - `newline` is the literal ASCII newline (0x10).
20
+ #
21
+ # There are several keywords defined in the POSIX standard; some of them are supported in
22
+ # this class, but may not be supported by Minitar as a whole.
23
+ #
24
+ # Primary support for PAX extended headers is for extracting size information for large
25
+ # file support. Other features may be added in the future.
26
+ class Minitar::PaxHeader
27
+ BLOCK_SIZE = 512
28
+
29
+ attr_reader :attributes
30
+
31
+ class << self
32
+ # Creates a new PaxHeader from a data stream and posix header. Reads the PAX content
33
+ # based on the size specified in the posix header.
34
+ def from_stream(stream, posix_header)
35
+ raise ArgumentError, "Header must be a PAX header" unless posix_header.pax_header?
36
+
37
+ pax_block = (posix_header.size / BLOCK_SIZE.to_f).ceil * BLOCK_SIZE
38
+ pax_content = stream.read(pax_block)
39
+
40
+ raise Minitar::InvalidTarStream if pax_content.nil? || pax_content.bytesize < posix_header.size
41
+
42
+ actual_content = pax_content[0, posix_header.size]
43
+
44
+ from_data(actual_content)
45
+ end
46
+
47
+ # Creates a new PaxHeader from PAX content data.
48
+ def from_data(content) = new(parse_content(content))
49
+
50
+ private
51
+
52
+ def parse_content(content)
53
+ attributes = {}
54
+ offset = 0
55
+
56
+ while offset < content.bytesize
57
+ space_pos = content.index(" ", offset)
58
+ break unless space_pos
59
+
60
+ length_str = content[offset, space_pos - offset]
61
+
62
+ unless length_str.match?(/\A\d+\z/)
63
+ raise ArgumentError, "Invalid length format in PAX header: '#{length_str}'"
64
+ end
65
+
66
+ length = length_str.to_i
67
+ if offset + length > content.bytesize
68
+ raise ArgumentError, "Length beyond PAX header: '#{content[offset..]}'"
69
+ end
70
+ record = content[offset, length]
71
+
72
+ keyword_value = record[(space_pos - offset + 1)..-2]
73
+ if keyword_value.include?("=")
74
+ keyword, value = keyword_value.split("=", 2)
75
+ attributes[keyword] = value
76
+ end
77
+
78
+ offset += length
79
+ end
80
+ attributes
81
+ end
82
+ end
83
+
84
+ # Creates a new PaxHeader from attributes hash.
85
+ def initialize(attributes = {})
86
+ @attributes = attributes.transform_keys(&:to_s)
87
+ end
88
+
89
+ # The size value from PAX attributes
90
+ def size = @attributes["size"]&.to_i
91
+
92
+ # The path value from PAX attributes
93
+ def path = @attributes["path"]
94
+
95
+ # The mtime value from PAX attributes
96
+ def mtime = @attributes["mtime"]&.to_f
97
+
98
+ # Returns a string representation of the PAX header content.
99
+ def to_s
100
+ @attributes.map do |keyword, value|
101
+ keyword_value = " #{keyword}=#{value}\n"
102
+ record = keyword_value
103
+ begin
104
+ length = record.bytesize
105
+ length_str = length.to_s
106
+ record = "#{length_str}#{keyword_value}"
107
+ end while record.size != length # standard:disable Lint/Loop
108
+ record
109
+ end.join
110
+ end
111
+ end