9
我有一個正則表達式作爲針對XSS的第一道防線。正則表達式作爲針對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()
。
當他想輸入帶括號的文本時,客戶端有問題。我想過修改或擴展幫手,但我也有一個第二個想法 - 如果我允許雙引號,真的有什麼理由我需要驗證嗎?
我可以只依賴輸出上的轉義嗎?