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

  • SEARCH
  • Home
  • 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 9031389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:43:14+00:00 2026-06-16T07:43:14+00:00

This is related to the question about zip bombs , but having gzip or

  • 0

This is related to the question about zip bombs, but having gzip or bzip2 compression in mind, e.g. a web service accepting .tar.gz files.

Python provides a handy tarfile module that is convenient to use, but does not seem to provide protection against zipbombs.

In python code using the tarfile module, what would be the most elegant way to detect zip bombs, preferably without duplicating too much logic (e.g. the transparent decompression support) from the tarfile module?

And, just to make it a bit less simple: No real files are involved; the input is a file-like object (provided by the web framework, representing the file a user uploaded).

  • 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-06-16T07:43:15+00:00Added an answer on June 16, 2026 at 7:43 am

    I guess the answer is: There is no easy, readymade solution. Here is what I use now:

    class SafeUncompressor(object):
        """Small proxy class that enables external file object
        support for uncompressed, bzip2 and gzip files. Works transparently, and
        supports a maximum size to avoid zipbombs.
        """
        blocksize = 16 * 1024
    
        class FileTooLarge(Exception):
            pass
    
        def __init__(self, fileobj, maxsize=10*1024*1024):
            self.fileobj = fileobj
            self.name = getattr(self.fileobj, "name", None)
            self.maxsize = maxsize
            self.init()
    
        def init(self):
            import bz2
            import gzip
            self.pos = 0
            self.fileobj.seek(0)
            self.buf = ""
            self.format = "plain"
    
            magic = self.fileobj.read(2)
            if magic == '\037\213':
                self.format = "gzip"
                self.gzipobj = gzip.GzipFile(fileobj = self.fileobj, mode = 'r')
            elif magic == 'BZ':
                raise IOError, "bzip2 support in SafeUncompressor disabled, as self.bz2obj.decompress is not safe"
                self.format = "bz2"
                self.bz2obj = bz2.BZ2Decompressor()
            self.fileobj.seek(0)
    
    
        def read(self, size):
            b = [self.buf]
            x = len(self.buf)
            while x < size:
                if self.format == 'gzip':
                    data = self.gzipobj.read(self.blocksize)
                    if not data:
                        break
                elif self.format == 'bz2':
                    raw = self.fileobj.read(self.blocksize)
                    if not raw:
                        break
                    # this can already bomb here, to some extend.
                    # so disable bzip support until resolved.
                    # Also monitor http://stackoverflow.com/questions/13622706/how-to-protect-myself-from-a-gzip-or-bzip2-bomb for ideas
                    data = self.bz2obj.decompress(raw)
                else:
                    data = self.fileobj.read(self.blocksize)
                    if not data:
                        break
                b.append(data)
                x += len(data)
    
                if self.pos + x > self.maxsize:
                    self.buf = ""
                    self.pos = 0
                    raise SafeUncompressor.FileTooLarge, "Compressed file too large"
            self.buf = "".join(b)
    
            buf = self.buf[:size]
            self.buf = self.buf[size:]
            self.pos += len(buf)
            return buf
    
        def seek(self, pos, whence=0):
            if whence != 0:
                raise IOError, "SafeUncompressor only supports whence=0"
            if pos < self.pos:
                self.init()
            self.read(pos - self.pos)
    
        def tell(self):
            return self.pos
    

    It does not work well for bzip2, so that part of the code is disabled. The reason is that bz2.BZ2Decompressor.decompress can already produce an unwanted large chunk of data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Somewhat related to this question , but in the absence of any answer about
There is related question about processing $http in a service, but I wanted to
This is related to a similar question about BIND, but in this case I'm
I saw this related question about publishing toolchain but I know many people did
Looking at this related SO question , I can't help but wonder about the
This is related to this question I asked earlier about syntax highlighting user-defined blocks
This question is directly related to this SO question I posed about 15 minutes
This is a related question to this but with EXISTS instead of IN SELECT
This is a related question to: Android NFC start service In Android, can an
(this is related to another question about implementation on iPhone) I have a large

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.