Skip to content

Commit 47a63e9

Browse files
author
Your Name
committed
Add decompression size limit to FITS gzip decoder
gzip.decompress() was called without any size limit, allowing a crafted FITS file with a gzip bomb to cause unbounded memory allocation (OOM/DoS). Add a limit based on the expected image dimensions, similar to MAX_TEXT_CHUNK in the PNG decoder. Security: CWE-409 (Decompression Bomb)
1 parent 3a44ba1 commit 47a63e9

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/PIL/FitsImagePlugin.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,19 @@ class FitsGzipDecoder(ImageFile.PyDecoder):
128128

129129
def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
130130
assert self.fd is not None
131+
132+
# Limit decompressed size to prevent decompression bomb DoS.
133+
# Each pixel uses 4 bytes in the decompressed FITS BINTABLE format.
134+
max_expected = self.state.xsize * self.state.ysize * 4
135+
max_decompressed = max(max_expected * 2, 1024 * 1024) # at least 1 MB
136+
131137
value = gzip.decompress(self.fd.read())
138+
if len(value) > max_decompressed:
139+
msg = (
140+
f"FITS gzip decompressed data too large: "
141+
f"{len(value)} bytes > {max_decompressed} bytes limit"
142+
)
143+
raise ValueError(msg)
132144

133145
rows = []
134146
offset = 0

0 commit comments

Comments
 (0)