Skip to content

gh-133890: Handle UnicodeEncodeError in tarfile #134147

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2439,7 +2439,7 @@ def _get_extract_tarinfo(self, member, filter_function, path):
unfiltered = tarinfo
try:
tarinfo = filter_function(tarinfo, path)
except (OSError, FilterError) as e:
except (OSError, UnicodeEncodeError, FilterError) as e:
self._handle_fatal_error(e)
except ExtractError as e:
self._handle_nonfatal_error(e)
Expand All @@ -2460,7 +2460,7 @@ def _extract_one(self, tarinfo, path, set_attrs, numeric_owner):
self._extract_member(tarinfo, os.path.join(path, tarinfo.name),
set_attrs=set_attrs,
numeric_owner=numeric_owner)
except OSError as e:
except (OSError, UnicodeEncodeError) as e:
self._handle_fatal_error(e)
except ExtractError as e:
self._handle_nonfatal_error(e)
Expand Down
49 changes: 45 additions & 4 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3490,11 +3490,12 @@ class ArchiveMaker:
with t.open() as tar:
... # `tar` is now a TarFile with 'filename' in it!
"""
def __init__(self):
def __init__(self, **kwargs):
self.bio = io.BytesIO()
self.tar_kwargs = dict(kwargs)

def __enter__(self):
self.tar_w = tarfile.TarFile(mode='w', fileobj=self.bio)
self.tar_w = tarfile.TarFile(mode='w', fileobj=self.bio, **self.tar_kwargs)
return self

def __exit__(self, *exc):
Expand Down Expand Up @@ -4073,7 +4074,10 @@ def test_tar_filter(self):
# that in the test archive.)
with tarfile.TarFile.open(tarname) as tar:
for tarinfo in tar.getmembers():
filtered = tarfile.tar_filter(tarinfo, '')
try:
filtered = tarfile.tar_filter(tarinfo, '')
except UnicodeEncodeError:
continue
self.assertIs(filtered.name, tarinfo.name)
self.assertIs(filtered.type, tarinfo.type)

Expand All @@ -4084,11 +4088,48 @@ def test_data_filter(self):
for tarinfo in tar.getmembers():
try:
filtered = tarfile.data_filter(tarinfo, '')
except tarfile.FilterError:
except (tarfile.FilterError, UnicodeEncodeError):
continue
self.assertIs(filtered.name, tarinfo.name)
self.assertIs(filtered.type, tarinfo.type)

@unittest.skipIf(sys.platform == 'win32', 'requires native bytes paths')
def test_filter_unencodable(self):
# Sanity check using a valid path.
tarinfo = tarfile.TarInfo(os_helper.TESTFN)
filtered = tarfile.tar_filter(tarinfo, '')
self.assertIs(filtered.name, tarinfo.name)
filtered = tarfile.data_filter(tarinfo, '')
self.assertIs(filtered.name, tarinfo.name)

tarinfo = tarfile.TarInfo('test\x00')
self.assertRaises(ValueError, tarfile.tar_filter, tarinfo, '')
self.assertRaises(ValueError, tarfile.data_filter, tarinfo, '')
tarinfo = tarfile.TarInfo('\ud800')
self.assertRaises(UnicodeEncodeError, tarfile.tar_filter, tarinfo, '')
self.assertRaises(UnicodeEncodeError, tarfile.data_filter, tarinfo, '')

@unittest.skipIf(sys.platform == 'win32', 'requires native bytes paths')
def test_extract_unencodable(self):
# Create a member with name \xed\xa0\x80 which is UTF-8 encoded
# lone surrogate \ud800.
with ArchiveMaker(encoding='ascii', errors='surrogateescape') as arc:
arc.add('\udced\udca0\udc80')
with os_helper.temp_cwd() as tmp:
tar = arc.open(encoding='utf-8', errors='surrogatepass',
errorlevel=1)
self.assertEqual(tar.getnames(), ['\ud800'])
with self.assertRaises(UnicodeEncodeError):
tar.extractall()
self.assertEqual(os.listdir(), [])

tar = arc.open(encoding='utf-8', errors='surrogatepass',
errorlevel=0, debug=1)
with support.captured_stderr() as stderr:
tar.extractall()
self.assertEqual(os.listdir(), [])
self.assertIn('tarfile: UnicodeEncodeError ', stderr.getvalue())

def test_change_default_filter_on_instance(self):
tar = tarfile.TarFile(tarname, 'r')
def strict_filter(tarinfo, path):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The :mod:`tarfile` module now handles :exc:`UnicodeEncodeError` in the same
way as :exc:`OSError` when cannot extract a member.
Loading