Repository navigation
Raise ValueError for invalid PNG text keys - #10322
Merged
Merged
Conversation
radarhere
force-pushed
the
png-text-key-check
branch
from
October 28, 2026 01:31
785d9c1 to
0e06724
Compare
radarhere
force-pushed
the
png-text-key-check
branch
from
October 28, 2026 01:31
0e06724 to
b9e4533
Compare
radarhere
enabled auto-merge (squash)
October 28, 2026 01:33
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PngInfo.add_text()andPngInfo.add_itxt()write whatever key they are given. The PNG specification requires text keys to be 1 to 79 characters long, and a key ends at the first null character, so some keys can't be read back. I saved a 1x1 image with each key and value"value", then read it with Pillow and with libpng 1.6.43 (png_get_text):"a\0b":{"a": "b\x00value"}.UnidentifiedImageError), because it reads the rest of the key as the compression method.pngcheckreports all of these files as errors.Changes proposed in this pull request:
add_text()andadd_itxt()raise aValueErrorfor a key that is empty, longer than 79 characters or contains a null character. They raise it before anything is added to thePngInfo. Keys of up to 79 characters still work.test_invalid_text_keychecks those keys as str and bytes, for tEXt, zTXt and iTXt, through both methods.test_text_key_max_lengthround-trips 79-character keys.The specification also forbids control characters and leading, trailing or consecutive spaces in keys. Pillow and libpng read those keys back unchanged, though, so I left them alone rather than break existing code that uses them.
The full test suite passes. ruff, black, mypy and sphinx-lint are clean on the changed files.