Python Glossary Русский

Python 3.12+tarfileexception

tarfile.LinkOutsideDestinationError

exceptions

The 'data' filter exception for a link in the archive that leads outside the destination folder via '..'.

Syntax

except tarfile.LinkOutsideDestinationError:

The relative-link sibling of AbsoluteLinkError: 'up' → '../../etc' looks harmless but leads writes outside. The 'tar' filter lets it through; 'data' refuses it. The message includes the absolute target path, so for user-facing output use e.tarinfo.name and e.tarinfo.linkname.

Examples

import tarfile
link = tarfile.TarInfo('up')
link.type = tarfile.SYMTYPE
link.linkname = '../../etc'
print(tarfile.tar_filter(link, 'out').linkname)  # → ../../etc — 'tar' пропускает
try:
    tarfile.data_filter(link, 'out')
except tarfile.LinkOutsideDestinationError as e:
    print(e.tarinfo.name, e.tarinfo.linkname)  # → up ../../etc

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy