Python 的 zipfile 模块默认是不安全的——我构建了一个更安全的解压工具

1作者: HiveSecurity4 个月前
我在 Python 标准库中发现了一些出乎意料的不安全因素。 如果你对不受信任的 ZIP 文件使用 zipfile.extractall() 函数,你基本上是在信任: * 文件路径不会逃逸你的目标目录 * 归档文件不是巨大的 ZIP 炸弹 * 不存在诸如符号链接或嵌套技巧之类的奇怪边缘情况 事实证明……这些都无法保证。 所以我最终构建了一个小的“安全提取”包装器,它: * 阻止路径穿越(Zip Slip) * 强制执行总的未压缩大小限制 * 限制文件数量 * 避免在目标目录之外进行提取 没什么特别的,只是应该开箱即用的防御性默认设置。 在这里写了一个简短的分解 + 代码: [https://hivesecurity.gitlab.io/blog/zipguard-safe-zip-extraction-python/](https://hivesecurity.gitlab.io/blog/zipguard-safe-zip-extraction-python/) 好奇其他人是如何处理这个问题的——你们是自己编写检查,还是依赖其他东西?
查看原文
I ran into something surprisingly unsafe in Python’s standard library.<p>If you use zipfile.extractall() on untrusted ZIP files, you&#x27;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:&#x2F;&#x2F;hivesecurity.gitlab.io&#x2F;blog&#x2F;zipguard-safe-zip-extraction-python&#x2F;<p>Curious how others are handling this — do you roll your own checks, or rely on something else?