Python 的 zipfile 默认是不安全的 – 我构建了一个更安全的解压工具。
我在Python的标准库中发现了一些令人意外的不安全之处。<p>如果你在不可信的ZIP文件上使用zipfile.extractall(),实际上你是在信任以下几点:<p>文件路径不会逃逸到目标目录之外<br>压缩文件不是巨大的ZIP炸弹<br>没有奇怪的边缘情况,比如符号链接或嵌套技巧<p>结果是……这些都没有得到保证。<p>因此,我最终构建了一个小型的“安全提取”包装器,能够:<p>阻止路径遍历(Zip Slip)<br>强制限制总解压缩大小<br>限制文件数量<br>避免在意图之外的目录中提取<p>这并不复杂,只是一些防御性默认设置,实际上应该是开箱即用的。<p>我在这里写了一篇简短的分析和代码:<br>https://hivesecurity.gitlab.io/blog/zipguard-safe-zip-extraction-python/<p>我很好奇其他人是如何处理这个问题的——你是自己实现检查,还是依赖其他方法?
查看原文
I ran into something surprisingly unsafe in Python’s standard library.<p>If you use zipfile.extractall() on untrusted ZIP files, you're basically trusting that:<p>file paths don’t escape your target directory
archives aren’t massive zip bombs
there aren’t weird edge cases like symlinks or nested tricks<p>Turns out… none of that is guaranteed.<p>So I ended up building a small “safe extraction” wrapper that:<p>blocks path traversal (Zip Slip)
enforces total uncompressed size limits
limits file count
avoids extracting outside the intended directory<p>Nothing fancy, just defensive defaults that probably should exist out of the box.<p>Wrote a short breakdown + code here:
https://hivesecurity.gitlab.io/blog/zipguard-safe-zip-extraction-python/<p>Curious how others are handling this — do you roll your own checks, or rely on something else?