使用url验证检测XSS


Detecting XSS using url validation

什么是一个好的url验证函数在php中检测xss?

我尝试了FILTER_URL函数在php,但仍然允许url:

http://example.com?<script></script>

您可以尝试以下四个测试:

// Set the patterns we'll test against
$patterns = array(
    // Match any attribute starting with "on" or xmlns
    '#(<[^>]+['x00-'x20'"'''/])(on|xmlns)[^>]*>?#iUu',
    // Match javascript:, livescript:, vbscript: and mocha: protocols
    '!((java|live|vb)script|mocha):('w)*!iUu',
    '#-moz-binding['x00-'x20]*:#u',
    // Match style attributes
    '#(<[^>]+['x00-'x20'"'''/])style=[^>]*>?#iUu',
    // Match unneeded tags
    '#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>?#i'
);

而不是试图检测XSS攻击,只要确保使用适当的消毒。

您可以使用htmlspecialchars()strip_tags(),尽管它们不会完全阻止任何事情。

结合使用这些方法以及其他启发式方法。理想情况下,编写自己的函数,通过一系列检查传递内容。Filter_input也可以在里面使用