我应该允许用户生成内容的 HTML 标签白名单?

发布于 01-03 01:04 字数 146 浏览 3 评论 0原文

总之,

我正在使用 PHP 构建一个小型网站。在该网站中,我收到用户生成的文本内容。我想允许一些安全的 HTML 标签(例如,格式化)以及 MathML。如何为 strip_tags() 函数编译白名单?是否有我可以使用的公认的白名单?

All,

I am building a small site using PHP. In the site, I receive user-generated text content. I want to allow some safe HTML tag (e.g., formatting) as well as MathML. How do I go about compiling a white list for a strip_tags() function? Is there a well accepted white list I can use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少跟Wǒ拽2025-01-10 01:04:22

标准的 strip_tags 函数对于安全性来说是不够的,因为它根本不验证属性。明确使用更完整的库来完全净化 HTML,例如 HTML Purifier

The standard strip_tags function is not enough for security, since it doesn't validate attributes at all. Use a more complete library explicitly for the purpose of completely sanitizing HTML like HTML Purifier.

夜清冷一曲。2025-01-10 01:04:22

如果您的目标是不允许 javascript 通过,那么您的标签白名单将非常接近空集。
请记住,几乎所有标签都可以具有 事件属性,其中包含在事件发生时要执行的 JavaScript 代码。指定事件发生。

如果您不想走 HTMLPurifier 类型的路线,请考虑使用不同的语言,例如 markdown< /a> (本网站使用的)或其他类似 wiki 的标记语言;但是,请确保禁用任何可能允许的直通 HTML 的使用。

If your aim is to not allow javascript through, then your whitelist of tags is going to be pretty close to the empty set.
Remember that pretty much all tags can have event attributes that contain javascript code to be executed when the specified event occurs.

If you don't want to go down the HTMLPurifier kind of route, consider a different language, such as markdown (that this site uses) or some other wiki-like markup language; however, be sure to disable any use of passthrough HTML that may be allowed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文