简体   繁体   English

用户输入过滤 - 我是否需要过滤HTML?

[英]User input filtering - do I need to filter HTML?

Note: I take care of SQL injection and output escaping elsewhere - this question is about input filtering only, thanks. 注意:我在其他地方处理SQL注入和输出转义 - 这个问题仅涉及输入过滤,谢谢。

I'm in the middle of refactoring my user input filtering functions. 我正在重构我的用户输入过滤功能。 Before passing the GET/POST parameter to a type-specific filter with filter_var() I do the following: 在使用filter_var()将GET / POST参数传递给特定于类型的过滤器之前,我执行以下操作:

Now the question: does it still make sense to pass the parameter to a filter like htmLawed or HTML Purifier , or can I think of the input as safe? 现在的问题是:将参数传递给像htmLawedHTML Purifier这样的过滤器仍然有意义,还是我认为输入是安全的? It seems to me that these two differ mostly on the granularity of allowed HTML elements and attributes (which I'm not interested into, as I remove everything), but htmLawed docs have a section about ' dangerous characters ' that suggests there might be a reason to use it. 在我看来,这两者主要区别于允许的HTML元素和属性的粒度(我不感兴趣,因为我删除了所有内容),但是htmLawed文档有一个关于“ 危险字符 ”的部分,表明可能存在使用它的原因。 In this case, what would be a sane configuration for it? 在这种情况下,它会是一个理智的配置吗?

There are many different approaches to XSS that are secure. XSS有许多不同的安全方法。 The only why to know if your approach holds water is to test though exploitation. 知道你的方法是否存在水的唯一原因是通过剥削来测试。 I recommend using a Free XSS vulnerability Scanner *, or the open source wapiti . 我建议使用Free XSS漏洞扫描程序 *或开源wapiti

To be honest I'll never use strip_tags() becuase you don't always need html tags to execute javascript! 说实话,我永远不会使用strip_tags()因为你并不总是需要html标签来执行javascript! I like htmlspecialchars($var,ENT_QUOTES); 我喜欢htmlspecialchars($var,ENT_QUOTES); .

For instance this is vulnerable to xss: 例如,这很容易受到xss的攻击:

print('<A HREF="http://www.xssed.com/'.strip_tags($_REQUEST[xss]).'">link</a>');

You don't need <> to execute javascript in this case because you can use onmouseover, here is an example attack: 在这种情况下你不需要<>执行javascript,因为你可以使用onmouseover,这是一个示例攻击:

$_REQUEST[xss]='" onMouseOver="alert(/xss/)"';

The ENT_QUOTES will take care of the double quotes which will patch this XSS vulnerability. ENT_QUOTES将处理双引号,这将修补此XSS漏洞。

*I am affiliated with this site/service. *我隶属于这个网站/服务。

我认为你所做的是安全的,至少从我的观点来看,没有HTML代码可以通过你的过滤器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM