如何在程序中防范XSS漏洞攻击?

已邀请:

zkbhj - 凯冰科技站长

赞同来自:

原则: 不相信客户输入的数据
注意: 攻击代码不一定在《script》《/script》中
 
  • 将重要的cookie标记为http only, 这样的话Javascript 中的document.cookie语句就不能获取到cookie了.
  • 只允许用户输入我们期望的数据。 例如: 年龄的textbox中,只允许用户输入数字。 而数字之外的字符都过滤掉。
  • 对数据进行Html Encode 处理
  • 过滤或移除特殊的Html标签, 例如: script, iframe , < for <, > for >, &quot for
  • 过滤JavaScript 事件的标签。例如 "onclick=", "onfocus" 等等。

 
Yii中的XSS防范
<h2>Profile of <?php echo CHtml::encode($user->name) ?></h2>
此方法的源码:
    /**
* Encodes special characters into HTML entities.
* The [[\yii\base\Application::charset|application charset]] will be used for encoding.
* @param string $content the content to be encoded
* @param boolean $doubleEncode whether to encode HTML entities in `$content`. If false,
* HTML entities in `$content` will not be further encoded.
* @return string the encoded content
* @see decode()
* @see http://www.php.net/manual/en/f ... s.php
*/
public static function encode($content, $doubleEncode = true)
{
return htmlspecialchars($content, ENT_QUOTES | ENT_SUBSTITUTE, Yii::$app->charset, $doubleEncode);
}

要回复问题请先登录注册