py.warning: 只有一行,但调试需要堆栈跟踪
py.warning: Just one line, but stacktrace needed for debugging
我在我的日志中看到这个警告:
py.warnings._decompression_bomb_check +2261: WARNING [1091] /usr/lib64/python2.7/site-packages/PIL/Image.py:2261: DecompressionBombWarning: Image size (139332960 pixels) exceeds limit of 89478485 pixels, could be decompression bomb DOS attack.
不幸的是,仅这一行并不能帮助我进行调试。
我想查看堆栈跟踪和请求等其他数据-URL。
我们使用 Django 1.8 和 Python 2.7
以上警告发生在生产系统上。大约每周一次。
我不知道如何重现它,因为我不知道产生警告的 URL。
更新
警告很明确。但是我不知道巨大的图像存储在哪里。我正在搜索产生此警告的大文件或 URL 的文件名。
我认为找到的唯一方法是:在生产环境中添加一些调试线并等待几天直到再次产生警告。
在我看来,可持续地做到这一点的最佳方法是同时使用 django-debug-toolbar. It gives you very detailed logs, request details, SQLs fired and what not on every URL of your app. The installation process is pretty trivial。试一试,看看你是否喜欢它!
如果您这样做 绝对需要 手动完成,我建议您定义自己的中间件来完成它。像
class CustomErrorHandlingMiddleware(self):
def process_exception(self, request, exception):
# Handle exceptions here
# Log to file or console as necessary
return HttpResponse(...)
来自 Django 文档,
Django calls process_exception() when a view raises an exception. process_exception() should return either None or an HttpResponse object. If it returns an HttpResponse object, the template response and response middleware will be applied and the resulting response returned to the browser. Otherwise, default exception handling kicks in.
建议记录文件名:
复制/usr/lib64/python2.7/site-packages/PIL/Image.py
打开此文件并编辑以下内容:
def _open_core(fp, filename, prefix):
# ...
_decompression_bomb_check(im.size)
更改为:
_decompression_bomb_check(im.size, filename)
以及以下内容:
def _decompression_bomb_check(size):
# ...
if pixels > MAX_IMAGE_PIXELS:
warnings.warn(
至:
def _decompression_bomb_check(size, filename):
# ...
if pixels > MAX_IMAGE_PIXELS:
warnings.warn(
"Filename: %s"
"Image size (%d pixels) exceeds limit of %d pixels, "
"could be decompression bomb DOS attack." %
(filename, pixels, MAX_IMAGE_PIXELS),
DecompressionBombWarning)
在 Internet 上搜索 DecompressionBomb 图像示例。
使用此示例测试您对 Image.py
的编辑。
如果日志记录满足您的需要,备份原始文件 Image.py 并将其替换为编辑版本。
然后也测试您的生产系统。
我在我的日志中看到这个警告:
py.warnings._decompression_bomb_check +2261: WARNING [1091] /usr/lib64/python2.7/site-packages/PIL/Image.py:2261: DecompressionBombWarning: Image size (139332960 pixels) exceeds limit of 89478485 pixels, could be decompression bomb DOS attack.
不幸的是,仅这一行并不能帮助我进行调试。
我想查看堆栈跟踪和请求等其他数据-URL。
我们使用 Django 1.8 和 Python 2.7
以上警告发生在生产系统上。大约每周一次。
我不知道如何重现它,因为我不知道产生警告的 URL。
更新
警告很明确。但是我不知道巨大的图像存储在哪里。我正在搜索产生此警告的大文件或 URL 的文件名。
我认为找到的唯一方法是:在生产环境中添加一些调试线并等待几天直到再次产生警告。
在我看来,可持续地做到这一点的最佳方法是同时使用 django-debug-toolbar. It gives you very detailed logs, request details, SQLs fired and what not on every URL of your app. The installation process is pretty trivial。试一试,看看你是否喜欢它!
如果您这样做 绝对需要 手动完成,我建议您定义自己的中间件来完成它。像
class CustomErrorHandlingMiddleware(self):
def process_exception(self, request, exception):
# Handle exceptions here
# Log to file or console as necessary
return HttpResponse(...)
来自 Django 文档,
Django calls process_exception() when a view raises an exception. process_exception() should return either None or an HttpResponse object. If it returns an HttpResponse object, the template response and response middleware will be applied and the resulting response returned to the browser. Otherwise, default exception handling kicks in.
建议记录文件名:
复制/usr/lib64/python2.7/site-packages/PIL/Image.py
打开此文件并编辑以下内容:
def _open_core(fp, filename, prefix):
# ...
_decompression_bomb_check(im.size)
更改为:
_decompression_bomb_check(im.size, filename)
以及以下内容:
def _decompression_bomb_check(size):
# ...
if pixels > MAX_IMAGE_PIXELS:
warnings.warn(
至:
def _decompression_bomb_check(size, filename):
# ...
if pixels > MAX_IMAGE_PIXELS:
warnings.warn(
"Filename: %s"
"Image size (%d pixels) exceeds limit of %d pixels, "
"could be decompression bomb DOS attack." %
(filename, pixels, MAX_IMAGE_PIXELS),
DecompressionBombWarning)
在 Internet 上搜索 DecompressionBomb 图像示例。
使用此示例测试您对 Image.py
的编辑。
如果日志记录满足您的需要,备份原始文件 Image.py 并将其替换为编辑版本。
然后也测试您的生产系统。