Commits

Thomas Waldmann committed b9450db Draft

Attachment handler: catch all Zip-related errors

I had faced the problem with attached .ppt file: MoinMoin
tried to check if the file is a Zip file, but zipfile module
throwed BadZipfile("Bad magic number for central directory").

Since, according to
http://docs.python.org/dev/library/zipfile.html
zipfile doesn't have a common base exception we can try to handle,
we can only enumerate all possible exceptions. Currently, they are
zipfile.BadZipfile, zipfile.LargeZipFile and RuntimeError. With
Python 3.2 there will be zipfile.BadZipFile.

Signed-off-by: Eygene Ryabinkin <rea@codelabs.ru>

  • Participants
  • Parent commits c98ec45

Comments (0)

Files changed (1)

MoinMoin/action/AttachFile.py

                         links.append(fmt.url(1, getAttachUrl(pagename, file, request, do='unzip')) +
                                      fmt.text(label_unzip) +
                                      fmt.url(0))
-            except RuntimeError:
+            except (RuntimeError, zipfile.BadZipfile, zipfile.LargeZipFile):
                 # We don't want to crash with a traceback here (an exception
                 # here could be caused by an uploaded defective zip file - and
                 # if we crash here, the user does not get a UI to remove the
                 # RuntimeError is raised by zipfile stdlib module in case of
                 # problems (like inconsistent slash and backslash usage in the
                 # archive).
+                # BadZipfile/LargeZipFile are raised when there are some
+                # specific problems with the archive file.
                 logging.exception("An exception within zip file attachment handling occurred:")
 
             html.append(fmt.listitem(1))
                         'filelist': ', '.join(not_overwritten), }
             else:
                 msg = _("Attachment '%(filename)s' unzipped.") % {'filename': filename}
-    except RuntimeError, err:
+    except (RuntimeError, zipfile.BadZipfile, zipfile.LargeZipFile), err:
         # We don't want to crash with a traceback here (an exception
         # here could be caused by an uploaded defective zip file - and
         # if we crash here, the user does not get a UI to remove the
         # RuntimeError is raised by zipfile stdlib module in case of
         # problems (like inconsistent slash and backslash usage in the
         # archive).
+        # BadZipfile/LargeZipFile are raised when there are some
+        # specific problems with the archive file.
         logging.exception("An exception within zip file attachment handling occurred:")
         msg = _("A severe error occurred:") + ' ' + str(err)
 
                 request.write(wikiutil.escape("%-46s %s %12d\n" % (zinfo.filename, date, zinfo.file_size)))
             request.write("</pre>")
             return
-    except RuntimeError:
+    except (RuntimeError, zipfile.BadZipfile, zipfile.LargeZipFile):
         # We don't want to crash with a traceback here (an exception
         # here could be caused by an uploaded defective zip file - and
         # if we crash here, the user does not get a UI to remove the
         # RuntimeError is raised by zipfile stdlib module in case of
         # problems (like inconsistent slash and backslash usage in the
         # archive).
+        # BadZipfile/LargeZipFile are raised when there are some
+        # specific problems with the archive file.
         logging.exception("An exception within zip file attachment handling occurred:")
         return