Skip to content

gh-162741: Fix tarfile errors="strict" failing on names cut mid-character - #162742

Open
ilcm96 wants to merge 1 commit into
python:mainfrom
ilcm96:tarfile-strict-deferred-error
Open

ilcm96 wants to merge 1 commit into
python:mainfrom
ilcm96:tarfile-strict-deferred-error

Conversation

@ilcm96

@ilcm96 ilcm96 commented Nov 27, 2026 •

Copy link
Copy Markdown

With errors="strict", tarfile could not read an archive whose ustar name, linkname, uname or gname field is cut in the middle of a multibyte character, even when a GNU longname/longlink header or a pax header (extended or global) replaces that field. tarfile writes such fields itself with GNU_FORMAT for paths longer than 100 bytes, and so do GNU tar and bsdtar.

TarInfo._frombuf() now keeps the decoding error of a text field and stores the field decoded with surrogateescape in the meantime. The error is dropped when the field is replaced. Otherwise TarInfo.frombuf() and TarInfo.fromtarfile() raise the original exception as before: before any later error, and without skipping the member. All headers read for a member are checked, so a _proc_member() override that returns a new object cannot leak unchecked values.

The fields of GNU longname/longlink and pax headers themselves are never used and are no longer checked. Error handlers that do not raise give the same results as before.

Note that the private _frombuf() no longer raises the decoding error of a text field that surrogateescape can decode, so overriding it to return a new object, or calling it directly, skips the check.

Tests cover GNU longnames and longlinks, pax-replaced ustar fields (including a global path, GNU.sparse.name and a ustar prefix), fields that are not replaced, the order of errors, reading again after an error, and subclasses.

I used an AI assistant to explore the code and draft the tests. I reviewed all changes and tested them on Linux, macOS and Windows.

…-character

With errors="strict", tarfile could not read an archive whose ustar name, linkname, uname or gname field holds a value cut in the middle of a multibyte character, even when a GNU longname or longlink header or a pax header (extended or global) replaces that field. Such fields are written for paths longer than 100 bytes, for example by tarfile itself with GNU_FORMAT and by GNU tar and bsdtar in the gnu and pax formats.

TarInfo._frombuf() now keeps the decoding error of a text field and stores the field decoded with "surrogateescape" in the meantime. The error is forgotten when the field is replaced. Otherwise it is raised once the member has been read, as before: before any later error and without skipping the member. All headers read for the member are checked, so a subclass whose _proc_member() returns a new object still gets the error. The fields of GNU longname, longlink and pax headers themselves are not used and are no longer checked. Error handlers that do not raise give the same results as before.
@ilcm96
ilcm96 requested a review from ethanfurman as a code owner November 27, 2026 13:14
@python-cla-bot

python-cla-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant