推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tabris17
V2EX  ›  Python

HTMLParser 处理 HTML Entities 和主流浏览器不一致

  •  
  •   tabris17 · Oct 23, 2015 · 2584 views
    This topic created in 3933 days ago, the information mentioned may be changed or developed.
    在 WEB 项目里用 HTMLParser 实现了一个 HTMLPurifier 来过滤不安全的 HTML 代码,由于 HTMLParser 的 unescape 函数在处理&#;转义时和浏览器不一致,导致可以绕过 HTMLPurifier 来注入 XSS 代码。

    HTMLParser 对『:』这类 HTML Entities 要求必须是『;』结尾的。而主流浏览器允许没有『;』

    如下代码:
    <a href="javascript&58alert()">XSS</a>
    对浏览器来说等同于:
    <a href="javascript&58;alert()">XSS</a>

    而 HTMLParser 的 unescape 就无法处理『&58 』的转义。

    差点踩着坑。

    想问问 Python 下有没有靠谱的 HTMLPurifier 组件?还有类似的 XSS 的坑还有那些?
    1 replies    2015-10-23 17:21:03 +08:00
    winkidney
        1
    winkidney  
       Oct 23, 2015
    你搜搜 html escape ,有各种现成的库, django 也自带,各种框架应该都有实现
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   954 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:56 · PVG 03:56 · LAX 12:56 · JFK 15:56
    ♥ Do have faith in what you're doing.