PHP 中的用户输入过滤

发布于 2024-07-26 00:53:18 字数 246 浏览 7 评论 0 原文

我目前正在开发一个应用程序,该应用程序要求用户提交显示在网站上的帖子和评论。 众所周知,用户输入不可信,因此我使用 htmlspecialchars($string,ENT_QUOTES) 来处理用户的帖子和评论。

现在,我想忽略一些特定的 html 标签。 例如
gt;
和其他一些标签。 我怎样才能做到这一点,以便 htmlspecialchars 在过滤其他标签时忽略一些标签。

Am currently working on an application that requires users to submit posts and comments which is displayed on the site. As we all know that user input can't be trusted so i used htmlspecialchars($string,ENT_QUOTES) to process user's posts and comments.

Now, i want some certain html tags ignored. such as <b><br /> and a few more tags. How can i do it so that htmlspecialchars ignores some tags while it filters the others.

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

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

发布评论

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

评论(6

落花浅忆 2024-08-02 00:53:18

解决方案a)
使用 strip_tags 代替 htmlspecialchars,并将所需的标签列入白名单。
更好的解决方案b)
使用 bbcodes,并为所需标签指定别名,例如:[b]bold[/b]

solution a)
use strip_tags insted of htmlspecialchars, and whitelist the needed tags.
better solution b)
Use bbcodes, and give aliases to the wanted tags, e.g: [b]bold[/b]

月隐月明月朦胧 2024-08-02 00:53:18

仅允许某些 HTML 标记而不允许任何脚本注入等可能性是非常非常困难的。

我实际上建议避免这种情况并使用生成 HTML 的东西,例如 这个 UBB 代码解析器(或类似)。 甚至是 Markdown (关闭 HTML 选项)。

这使得攻击者没有机会攻击您的网站,如果网站面向公众,这一点非常重要。

如果您允许甚至某些 HTML 通过,那么顽固的攻击者很可能会找到绕过它的方法。

It is very, very difficult to allow only some HTML tags without allowing any possibility of script injection or the like.

I would actually recommend avoiding this and using something that generates HTML such as this UBB code parser (or similar). Or even Markdown (with HTML option turned off).

That gives no scope for attackers to hit your site, which is very important if it is public-facing.

If you allow even some HTML through, chances are that a determined attacker will find a way round it.

黄昏下泛黄的笔记 2024-08-02 00:53:18

由于当前 HTML 过滤器存在缺陷或不安全,您是否厌倦了使用 BBCode?

--> HTML Purifier

HTML Purifier 是一个用 PHP 编写的符合标准的 HTML 过滤器库。 HTML Purifier 不仅会通过彻底审核、安全且宽松的白名单来删除所有恶意代码(通常称为 XSS),...

Tired of using BBCode due to the current landscape of deficient or insecure HTML filters?

--> HTML Purifier

HTML Purifier is a standards-compliant HTML filter library written in PHP. HTML Purifier will not only remove all malicious code (better known as XSS) with a thoroughly audited, secure yet permissive whitelist, ...

孤城病女 2024-08-02 00:53:18

您可以替换带引号的字符串以重新插入允许的标签。 例如,对于 标签:

$string = str_replace(array('<b>', </>), array('<b>', '</b>'), $string);

我只允许非常独特、完整的标签尽可能安全。 即,如果不需要,请不要使用正则表达式,它可能会导致非常讨厌的错误。

You can replace the quoted string to re-insert the allowed tags. For <b> tags for example:

$string = str_replace(array('<b>', </>), array('<b>', '</b>'), $string);

I would only allow very distinct, complete tags to be as secure as possible. I.e. Don't use regular expressions if you don't have to, it can lead to very nasty bugs.

摇划花蜜的午后 2024-08-02 00:53:18

我强烈建议您使用 Zend_Filter 来过滤用户输入。 具体参见:
http://framework.zend.com/手册/en/zend.filter.html#zend.filter.introduction.using

I would heavily recommend you use Zend_Filter for filtering through user input. Specifically, see:
http://framework.zend.com/manual/en/zend.filter.html#zend.filter.introduction.using

烟花肆意 2024-08-02 00:53:18

这并不像您想象的那么简单,因为 htmlspecialchars() 也不是 htmlentities() 提供了忽略某些标签的任何选项(这两个函数甚至不知道标签概念的含义)。

您可以使用其他一些方法来允许用户格式化他们的帖子,例如 BBCode纺织降价。 有适用于所有这些的 PHP 解析器。

如果您必须坚持使用 html 标签,您可以采取一些预处理来重新格式化允许的标签,以便它们不会受到 htmlspecialchars()。 然后,您可以对结果进行后处理,将格式更改回正常的 HTML 标签。 以下示例通过简单的 标记可视化此过程。 请注意,使用正则表达式处理 HTML 很容易出错,而且并不总是正确的方法 - 在本示例中我将使用它只是为了简单起见。

$input = preg_replace('~<(/?\w+([^>]*?))>~', '|#$1#|', $input);
$input = htmlspecialchars($input);
$inoput = preg_replace('~|#(/?\w+(.*?))#|~', '<$1>', $input);

这尚未经过测试,肯定需要做更多的工作。

This isn't as simple as you might thing because neither htmlspecialchars() nor htmlentities() provides any options to ignore certain tags (both functions don't even know the meaning of the notion of tags).

You could use some other means to allow the users to format their posts, e.g. BBCode, Textile or Markdown. There are PHP parsers available for all of them.

If you'll have to stick with html-tags you could resort to some preprocessing that reformats the allowed tags so that they will not be affected by htmlspecialchars(). You can then postprocess the result to change back the format to normal HTML-tags. The following sample visualizes this process for a simple <a>-tag. Please be aware that processing HTML with regular expressions is error-prone and not always the way to go - I'll use it just for the sake of simplicity in this example.

$input = preg_replace('~<(/?\w+([^>]*?))>~', '|#$1#|', $input);
$input = htmlspecialchars($input);
$inoput = preg_replace('~|#(/?\w+(.*?))#|~', '<$1>', $input);

This is untested and will surely require a lot more work.

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