Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6796373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:27:35+00:00 2026-05-26T18:27:35+00:00

In the python documentation, it is adviced not to extract a tar archive without

  • 0

In the python documentation, it is adviced not to extract a tar archive without prior inspection. What is the best way to make sure an archive is safe using the tarfile python module? Should I just iterate over all the filename and check wether they contain absolute pathnames?

Would something like the following be sufficient?

import sys
import tarfile
with tarfile.open('sample.tar', 'r') as tarf:
    for n in tarf.names():
        if n[0] == '/' or n[0:2] == '..':
            print 'sample.tar contains unsafe filenames'
            sys.exit(1)
    tarf.extractall()

Edit

This script is not compatible with versions prior to 2.7. cf with and tarfile.

I now iterate over the members:

target_dir = "/target/"
with closing(tarfile.open('sample.tar', mode='r:gz')) as tarf:
    for m in tarf:
        pathn = os.path.abspath(os.path.join(target_dir, m.name))
        if not pathn.startswith(target_dir):
            print 'The tar file contains unsafe filenames. Aborting.'
            sys.exit(1)
        tarf.extract(m, path=tdir)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:27:36+00:00Added an answer on May 26, 2026 at 6:27 pm

    Almost, although it would still be possible to have a path like foo/../../.

    Better would be to use os.path.join and os.path.abspath, which together will correctly handle leading / and ..s anywhere in the path:

    target_dir = "/target/" # trailing slash is important
    with tarfile.open(…) as tarf:
        for n in tarf.names:
            if not os.path.abspath(os.path.join(target_dir, n)).startswith(target_dir):
                print "unsafe filenames!"
                sys.exit(1)
        tarf.extractall(path=target_dir)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Python documentation about the is operator says: The operators is and is not
I have been looking over the Python documentation for code formatting best practice for
The Python documentation claims that the following does not work on some platforms or
In the python documentation for struct, the word buffer is used without explanation: http://docs.python.org/library/struct.html
Python documentation to Popen states: Warning Use communicate() rather than .stdin.write, .stdout.read or .stderr.read
Using the Python Documentation I found the HTML parser but I have no idea
In the Python documentation it says: A thread can be flagged as a daemon
I can't tell from the Python documentation whether the re.compile(x) function may throw an
I was looking at the builtin object methods in the Python documentation , and
All the examples I've seen of sock.listen(5) in the python documentation suggest I should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.