Issue #7 resolved

ez_setup.py should validate tar file

Christian Heimes
created an issue

ez_setup._extractall() should validate the tar file members according to http://docs.python.org/3/library/tarfile.html#tarfile.TarFile.extractall

I suggest that _extractall() shall raise an error if

  • a member is neither a directory nor a regular file (e.g. symlink, device)

  • member.name starts with '/' or contains '../' in order to prevent directory traversal attacks

I also propose to mask out problematic bits like SUID. After all ez_setup.py is usually run with root permission.

    for tarinfo in members:
        if tarinfo.name.startswith('/') or '../' in tarinfo.name:
            raise ValueError("Absolute file names or directory traversal forbidden: %s"
                                       % tarinfo.name)
        if tarinfo.isdir():
            # Extract directories with a safe mode.
            directories.append(tarinfo)
            tarinfo = copy.copy(tarinfo)
            tarinfo.mode = 448  # decimal for oct 0700
        elif tarinfo.isreg():
            tarinfo.mode &= 511 # 0777, mask out SUID, SGID, VTX
        else:
            raise ValueError("unsupported file type for file %s" % tarinfo.name)  

Comments (8)

  1. Jason R. Coombs

    I don't feel right adding security features to a bootstrap wrapper. If these practices are good to employ in general, is there a reason they're not implemented in Python? In other words, why isn't there a 'safe_extract_all' in Python?

    I see now the default extract behavior has changed to be secure (though the docs are ambiguous about which versions are safe). My preference would be to use zip files for distribution and add a compatibility wrapper for older Pythons (while supported by Setuptools) to prevent extraction outside of the designated target.

  2. Arfrever Frehtes Taifersar Arahesis

    Please still provide tarballs. ez_setup.py does not need to use them. Unix users (e.g. who manually download and unpack tarballs and run setup.py) might prefer tarballs, since tar is always present in system, while unzip would have to be manually installed.

  3. Log in to comment