Skip to content

Validate keywords and reject null bytes in text chunk fields - #738

Open
Ramya-9353 wants to merge 1 commit into
image-rs:masterfrom
Ramya-9353:text-chunk-field-validation
Open

Ramya-9353 wants to merge 1 commit into
image-rs:masterfrom
Ramya-9353:text-chunk-field-validation

Conversation

@Ramya-9353

Copy link
Copy Markdown

Repro: TEXtChunk::new("Author\0forged", "real") encodes to a chunk that decodes back as keyword Author, text forged\0real. ITXtChunk::new("Comment\0\0\0en\0Author\0Trusted Author", "the real text") decodes as keyword Comment, language tag en, translated keyword Author, text Trusted Author\0\0\0\0\0the real text. A null byte in ITXtChunk::language_tag or translated_keyword shifts the following fields the same way.
Cause: the three EncodableTextChunk::encode impls checked only that the keyword is 1 to 79 bytes, and the iTXt language tag and translated keyword were not checked at all, so a null byte ended its own field early and the bytes after it were read as the fields that follow.
Fix: validate the keyword against the printable Latin-1 and spacing rules of 11.3.4.2 and reject a null byte in the language tag and translated keyword, in encode where the field layout is written.

A caller embedding externally supplied metadata ("Author", "Comment") therefore emitted chunks whose decoded fields it never asked for, with the genuine text hidden behind the injected null bytes, which is why the check belongs in the encoder rather than at each call site. Tests cover all four fields and fail on master. The iCCP profile name is length-checked only as well, but its one caller passes a constant, so I have left it.

The keyword of tEXt/zTXt/iTXt and the language tag and translated
keyword of iTXt are terminated by a null byte in the encoded chunk, so a
null byte inside one of those values ended the field early and every
field after it was read out of the remainder of the same string. Only
the keyword length was checked before. Validate the keyword against the
printable Latin-1 and spacing rules of the specification and reject a
null byte in the two remaining iTXt fields, inside encode where the
field layout is written.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant