|
| 1 | +diff --git a/Lib/tarfile.py b/Lib/tarfile.py |
| 2 | +index 7a6158c2eb9..bc5ec8bd582 100755 |
| 3 | +--- a/Lib/tarfile.py |
| 4 | ++++ b/Lib/tarfile.py |
| 5 | +@@ -840,6 +840,9 @@ _NAMED_FILTERS = { |
| 6 | + # Sentinel for replace() defaults, meaning "don't change the attribute" |
| 7 | + _KEEP = object() |
| 8 | + |
| 9 | ++# Header length is digits followed by a space. |
| 10 | ++_header_length_prefix_re = re.compile(br"([0-9]{1,20}) ") |
| 11 | ++ |
| 12 | + class TarInfo(object): |
| 13 | + """Informational class which holds the details about an |
| 14 | + archive member given by a tar header block. |
| 15 | +@@ -1399,41 +1402,59 @@ class TarInfo(object): |
| 16 | + else: |
| 17 | + pax_headers = tarfile.pax_headers.copy() |
| 18 | + |
| 19 | +- # Check if the pax header contains a hdrcharset field. This tells us |
| 20 | +- # the encoding of the path, linkpath, uname and gname fields. Normally, |
| 21 | +- # these fields are UTF-8 encoded but since POSIX.1-2008 tar |
| 22 | +- # implementations are allowed to store them as raw binary strings if |
| 23 | +- # the translation to UTF-8 fails. |
| 24 | +- match = re.search(br"\d+ hdrcharset=([^\n]+)\n", buf) |
| 25 | +- if match is not None: |
| 26 | +- pax_headers["hdrcharset"] = match.group(1).decode("utf-8") |
| 27 | +- |
| 28 | +- # For the time being, we don't care about anything other than "BINARY". |
| 29 | +- # The only other value that is currently allowed by the standard is |
| 30 | +- # "ISO-IR 10646 2000 UTF-8" in other words UTF-8. |
| 31 | +- hdrcharset = pax_headers.get("hdrcharset") |
| 32 | +- if hdrcharset == "BINARY": |
| 33 | +- encoding = tarfile.encoding |
| 34 | +- else: |
| 35 | +- encoding = "utf-8" |
| 36 | +- |
| 37 | + # Parse pax header information. A record looks like that: |
| 38 | + # "%d %s=%s\n" % (length, keyword, value). length is the size |
| 39 | + # of the complete record including the length field itself and |
| 40 | +- # the newline. keyword and value are both UTF-8 encoded strings. |
| 41 | +- regex = re.compile(br"(\d+) ([^=]+)=") |
| 42 | ++ # the newline. |
| 43 | + pos = 0 |
| 44 | +- while True: |
| 45 | +- match = regex.match(buf, pos) |
| 46 | +- if not match: |
| 47 | +- break |
| 48 | ++ encoding = None |
| 49 | ++ raw_headers = [] |
| 50 | ++ while len(buf) > pos and buf[pos] != 0x00: |
| 51 | ++ if not (match := _header_length_prefix_re.match(buf, pos)): |
| 52 | ++ raise InvalidHeaderError("invalid header") |
| 53 | ++ try: |
| 54 | ++ length = int(match.group(1)) |
| 55 | ++ except ValueError: |
| 56 | ++ raise InvalidHeaderError("invalid header") |
| 57 | ++ # Headers must be at least 5 bytes, shortest being '5 x=\n'. |
| 58 | ++ # Value is allowed to be empty. |
| 59 | ++ if length < 5: |
| 60 | ++ raise InvalidHeaderError("invalid header") |
| 61 | ++ if pos + length > len(buf): |
| 62 | ++ raise InvalidHeaderError("invalid header") |
| 63 | + |
| 64 | +- length, keyword = match.groups() |
| 65 | +- length = int(length) |
| 66 | +- if length == 0: |
| 67 | ++ header_value_end_offset = match.start(1) + length - 1 # Last byte of the header |
| 68 | ++ keyword_and_value = buf[match.end(1) + 1:header_value_end_offset] |
| 69 | ++ raw_keyword, equals, raw_value = keyword_and_value.partition(b"=") |
| 70 | ++ |
| 71 | ++ # Check the framing of the header. The last character must be '\n' (0x0A) |
| 72 | ++ if not raw_keyword or equals != b"=" or buf[header_value_end_offset] != 0x0A: |
| 73 | + raise InvalidHeaderError("invalid header") |
| 74 | +- value = buf[match.end(2) + 1:match.start(1) + length - 1] |
| 75 | ++ raw_headers.append((length, raw_keyword, raw_value)) |
| 76 | ++ |
| 77 | ++ # Check if the pax header contains a hdrcharset field. This tells us |
| 78 | ++ # the encoding of the path, linkpath, uname and gname fields. Normally, |
| 79 | ++ # these fields are UTF-8 encoded but since POSIX.1-2008 tar |
| 80 | ++ # implementations are allowed to store them as raw binary strings if |
| 81 | ++ # the translation to UTF-8 fails. For the time being, we don't care about |
| 82 | ++ # anything other than "BINARY". The only other value that is currently |
| 83 | ++ # allowed by the standard is "ISO-IR 10646 2000 UTF-8" in other words UTF-8. |
| 84 | ++ # Note that we only follow the initial 'hdrcharset' setting to preserve |
| 85 | ++ # the initial behavior of the 'tarfile' module. |
| 86 | ++ if raw_keyword == b"hdrcharset" and encoding is None: |
| 87 | ++ if raw_value == b"BINARY": |
| 88 | ++ encoding = tarfile.encoding |
| 89 | ++ else: # This branch ensures only the first 'hdrcharset' header is used. |
| 90 | ++ encoding = "utf-8" |
| 91 | ++ |
| 92 | ++ pos += length |
| 93 | + |
| 94 | ++ # If no explicit hdrcharset is set, we use UTF-8 as a default. |
| 95 | ++ if encoding is None: |
| 96 | ++ encoding = "utf-8" |
| 97 | ++ |
| 98 | ++ # After parsing the raw headers we can decode them to text. |
| 99 | ++ for length, raw_keyword, raw_value in raw_headers: |
| 100 | + # Normally, we could just use "utf-8" as the encoding and "strict" |
| 101 | + # as the error handler, but we better not take the risk. For |
| 102 | + # example, GNU tar <= 1.23 is known to store filenames it cannot |
| 103 | +@@ -1441,17 +1462,16 @@ class TarInfo(object): |
| 104 | + # hdrcharset=BINARY header). |
| 105 | + # We first try the strict standard encoding, and if that fails we |
| 106 | + # fall back on the user's encoding and error handler. |
| 107 | +- keyword = self._decode_pax_field(keyword, "utf-8", "utf-8", |
| 108 | ++ keyword = self._decode_pax_field(raw_keyword, "utf-8", "utf-8", |
| 109 | + tarfile.errors) |
| 110 | + if keyword in PAX_NAME_FIELDS: |
| 111 | +- value = self._decode_pax_field(value, encoding, tarfile.encoding, |
| 112 | ++ value = self._decode_pax_field(raw_value, encoding, tarfile.encoding, |
| 113 | + tarfile.errors) |
| 114 | + else: |
| 115 | +- value = self._decode_pax_field(value, "utf-8", "utf-8", |
| 116 | ++ value = self._decode_pax_field(raw_value, "utf-8", "utf-8", |
| 117 | + tarfile.errors) |
| 118 | + |
| 119 | + pax_headers[keyword] = value |
| 120 | +- pos += length |
| 121 | + |
| 122 | + # Fetch the next header. |
| 123 | + try: |
| 124 | +@@ -1466,7 +1486,7 @@ class TarInfo(object): |
| 125 | + |
| 126 | + elif "GNU.sparse.size" in pax_headers: |
| 127 | + # GNU extended sparse format version 0.0. |
| 128 | +- self._proc_gnusparse_00(next, pax_headers, buf) |
| 129 | ++ self._proc_gnusparse_00(next, raw_headers) |
| 130 | + |
| 131 | + elif pax_headers.get("GNU.sparse.major") == "1" and pax_headers.get("GNU.sparse.minor") == "0": |
| 132 | + # GNU extended sparse format version 1.0. |
| 133 | +@@ -1488,15 +1508,24 @@ class TarInfo(object): |
| 134 | + |
| 135 | + return next |
| 136 | + |
| 137 | +- def _proc_gnusparse_00(self, next, pax_headers, buf): |
| 138 | ++ def _proc_gnusparse_00(self, next, raw_headers): |
| 139 | + """Process a GNU tar extended sparse header, version 0.0. |
| 140 | + """ |
| 141 | + offsets = [] |
| 142 | +- for match in re.finditer(br"\d+ GNU.sparse.offset=(\d+)\n", buf): |
| 143 | +- offsets.append(int(match.group(1))) |
| 144 | + numbytes = [] |
| 145 | +- for match in re.finditer(br"\d+ GNU.sparse.numbytes=(\d+)\n", buf): |
| 146 | +- numbytes.append(int(match.group(1))) |
| 147 | ++ for _, keyword, value in raw_headers: |
| 148 | ++ if keyword == b"GNU.sparse.offset": |
| 149 | ++ try: |
| 150 | ++ offsets.append(int(value.decode())) |
| 151 | ++ except ValueError: |
| 152 | ++ raise InvalidHeaderError("invalid header") |
| 153 | ++ |
| 154 | ++ elif keyword == b"GNU.sparse.numbytes": |
| 155 | ++ try: |
| 156 | ++ numbytes.append(int(value.decode())) |
| 157 | ++ except ValueError: |
| 158 | ++ raise InvalidHeaderError("invalid header") |
| 159 | ++ |
| 160 | + next.sparse = list(zip(offsets, numbytes)) |
| 161 | + |
| 162 | + def _proc_gnusparse_01(self, next, pax_headers): |
| 163 | +@@ -2875,4 +2904,4 @@ def main(): |
| 164 | + print('{!r} file created.'.format(tar_name)) |
| 165 | + |
| 166 | + if __name__ == '__main__': |
| 167 | +- main() |
| 168 | ++ main() |
| 169 | +\ No newline at end of file |
| 170 | +diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py |
| 171 | +index 3df64c78032..cadd3f35808 100644 |
| 172 | +--- a/Lib/test/test_tarfile.py |
| 173 | ++++ b/Lib/test/test_tarfile.py |
| 174 | +@@ -1113,6 +1113,47 @@ class PaxReadTest(LongnameTest, ReadTest, unittest.TestCase): |
| 175 | + finally: |
| 176 | + tar.close() |
| 177 | + |
| 178 | ++ def test_pax_header_bad_formats(self): |
| 179 | ++ # The fields from the pax header have priority over the |
| 180 | ++ # TarInfo. |
| 181 | ++ pax_header_replacements = ( |
| 182 | ++ b" foo=bar\n", |
| 183 | ++ b"0 \n", |
| 184 | ++ b"1 \n", |
| 185 | ++ b"2 \n", |
| 186 | ++ b"3 =\n", |
| 187 | ++ b"4 =a\n", |
| 188 | ++ b"1000000 foo=bar\n", |
| 189 | ++ b"0 foo=bar\n", |
| 190 | ++ b"-12 foo=bar\n", |
| 191 | ++ b"000000000000000000000000036 foo=bar\n", |
| 192 | ++ ) |
| 193 | ++ pax_headers = {"foo": "bar"} |
| 194 | ++ |
| 195 | ++ for replacement in pax_header_replacements: |
| 196 | ++ with self.subTest(header=replacement): |
| 197 | ++ tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, |
| 198 | ++ encoding="iso8859-1") |
| 199 | ++ try: |
| 200 | ++ t = tarfile.TarInfo() |
| 201 | ++ t.name = "pax" # non-ASCII |
| 202 | ++ t.uid = 1 |
| 203 | ++ t.pax_headers = pax_headers |
| 204 | ++ tar.addfile(t) |
| 205 | ++ finally: |
| 206 | ++ tar.close() |
| 207 | ++ |
| 208 | ++ with open(tmpname, "rb") as f: |
| 209 | ++ data = f.read() |
| 210 | ++ self.assertIn(b"11 foo=bar\n", data) |
| 211 | ++ data = data.replace(b"11 foo=bar\n", replacement) |
| 212 | ++ |
| 213 | ++ with open(tmpname, "wb") as f: |
| 214 | ++ f.truncate() |
| 215 | ++ f.write(data) |
| 216 | ++ |
| 217 | ++ with self.assertRaisesRegex(tarfile.ReadError, r"file could not be opened successfully"): |
| 218 | ++ tarfile.open(tmpname, encoding="iso8859-1") |
| 219 | + |
| 220 | + class WriteTestBase(TarTest): |
| 221 | + # Put all write tests in here that are supposed to be tested |
0 commit comments