Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block tile TIFF tags when saving #5839

Merged
merged 1 commit into from Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions Tests/test_file_libtiff.py
Expand Up @@ -920,6 +920,23 @@ def test_strip_planar_16bit_RGBa(self):
with Image.open("Tests/images/tiff_strip_planar_16bit_RGBa.tiff") as im:
assert_image_equal_tofile(im, "Tests/images/tiff_16bit_RGBa_target.png")

@pytest.mark.parametrize("compression", (None, "jpeg"))
def test_block_tile_tags(self, compression, tmp_path):
im = hopper()
out = str(tmp_path / "temp.tif")

tags = {
TiffImagePlugin.TILEWIDTH: 256,
TiffImagePlugin.TILELENGTH: 256,
TiffImagePlugin.TILEOFFSETS: 256,
TiffImagePlugin.TILEBYTECOUNTS: 256,
}
im.save(out, exif=tags, compression=compression)

with Image.open(out) as reloaded:
for tag in tags.keys():
assert tag not in reloaded.getexif()

def test_old_style_jpeg(self):
with Image.open("Tests/images/old-style-jpeg-compression.tif") as im:
assert_image_equal_tofile(im, "Tests/images/old-style-jpeg-compression.png")
Expand Down
8 changes: 7 additions & 1 deletion src/PIL/TiffImagePlugin.py
Expand Up @@ -89,7 +89,10 @@
ARTIST = 315
PREDICTOR = 317
COLORMAP = 320
TILEWIDTH = 322
TILELENGTH = 323
TILEOFFSETS = 324
TILEBYTECOUNTS = 325
SUBIFD = 330
EXTRASAMPLES = 338
SAMPLEFORMAT = 339
Expand Down Expand Up @@ -1649,6 +1652,7 @@ def _save(im, fp, filename):
}.items():
ifd.setdefault(tag, value)

blocklist = [TILEWIDTH, TILELENGTH, TILEOFFSETS, TILEBYTECOUNTS]
if libtiff:
if "quality" in encoderinfo:
quality = encoderinfo["quality"]
Expand Down Expand Up @@ -1680,7 +1684,7 @@ def _save(im, fp, filename):
# BITSPERSAMPLE, etc), passing arrays with a different length will result in
# segfaults. Block these tags until we add extra validation.
# SUBIFD may also cause a segfault.
blocklist = [
blocklist += [
REFERENCEBLACKWHITE,
SAMPLEFORMAT,
STRIPBYTECOUNTS,
Expand Down Expand Up @@ -1753,6 +1757,8 @@ def _save(im, fp, filename):
raise OSError(f"encoder error {s} when writing image file")

else:
for tag in blocklist:
del ifd[tag]
offset = ifd.save(fp)

ImageFile._save(
Expand Down