pallets/markupsafe

GitHub: pallets/markupsafe

一个 Python 库,通过对特殊字符进行转义来安全地在 HTML 和 XML 中嵌入不受信任的字符串,从而防止注入攻击。

Stars: 691 | Forks: 180

# MarkupSafe MarkupSafe 实现了一个能对字符进行转义的文本对象,因此可安全地在 HTML 和 XML 中使用。具有特殊含义的字符会被替换,从而使其显示为实际的字符。这能有效缓解注入攻击,这意味着不受信任的用户输入可以安全地显示在页面上。 ## 示例 ``` >>> from markupsafe import Markup, escape >>> # escape replaces special characters and wraps in Markup >>> escape("") Markup('<script>alert(document.cookie);</script>') >>> # wrap in Markup to mark text "safe" and prevent escaping >>> Markup("Hello") Markup('hello') >>> escape(Markup("Hello")) Markup('hello') >>> # Markup is a str subclass >>> # methods and operators escape their arguments >>> template = Markup("Hello {name}") >>> template.format(name='"World"') Markup('Hello "World"') ``` ## 贡献 请查阅我们[详细的贡献文档][contrib],了解多种贡献方式,包括报告问题、请求新功能、提出或解答问题以及提交 PR。
标签:HTML转义, Pallets, Web安全, XML安全, XSS防御, 前端安全, 后端安全, 字符串过滤, 安全组件, 数据清洗, 文本处理, 标记语言, 模板引擎, 注入攻击缓解, 蓝队分析, 逆向工具