From: Ruben Vorderman Date: Wed, 13 May 2026 10:20:33 +0000 (+0200) Subject: bpo-45509: Check gzip headers for corrupted fields (GH-29028) X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dd94457893a1dd2c99c2405e197f54a7692cbe09;p=thirdparty%2FPython%2Fcpython.git bpo-45509: Check gzip headers for corrupted fields (GH-29028) Check the header checksum it the HCRC field is present. --- diff --git a/Lib/gzip.py b/Lib/gzip.py index 971063aa24f8..a89ebf806c85 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -484,40 +484,63 @@ def _read_exact(fp, n): return data +def _read_until_null(fp, append_to): + '''Read until the first encountered null byte in fp. + Append to given byte array object''' + while True: + s = fp.read(1) + append_to += s + if not s or s == b'\000': + break + + def _read_gzip_header(fp): '''Read a gzip header from `fp` and progress to the end of the header. Returns last mtime if header was present or None otherwise. ''' magic = fp.read(2) - if magic == b'': + if not magic: return None if magic != b'\037\213': raise BadGzipFile('Not a gzipped file (%r)' % magic) - - (method, flag, last_mtime) = struct.unpack("