【发布时间】:2010-09-20 00:29:35
【问题描述】:
我有一个正则表达式作为抵御 XSS 的第一道防线。
public static function standard_text($str)
{
// pL matches letters
// pN matches numbers
// pZ matches whitespace
// pPc matches underscores
// pPd matches dashes
// pPo matches normal puncuation
return (bool) preg_match('/^[\pL\pN\pZ\p{Pc}\p{Pd}\p{Po}]++$/uD', (string) $str);
}
其实是来自Kohana 2.3。
这会在公共输入的文本(从来没有 HTML)上运行,如果测试失败则拒绝输入。文本始终以htmlspecialchars() 显示(或者更具体地说,Kohana's flavour,它添加了字符集)。我还在输出中添加了strip_tags()。
客户端在输入一些带括号的文本时遇到问题。我考虑过修改或扩展助手,但我也有一个次要的想法——如果我允许双引号,真的有什么理由需要验证吗?
我可以只依靠转义输出吗?
【问题讨论】: