当前位置: 移动技术网 > IT编程>开发语言>PHP > Yii2的XSS攻击防范策略分析

Yii2的XSS攻击防范策略分析

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例讲述了yii2的xss攻击防范策略。分享给大家供大家参考,具体如下:

xss 漏洞修复

原则: 不相信客户输入的数据
注意: 攻击代码不一定在<script></script>中

① 将重要的cookie标记为http only, 这样的话javascript 中的document.cookie语句就不能获取到cookie了.
② 只允许用户输入我们期望的数据。 例如: 年龄的textbox中,只允许用户输入数字。 而数字之外的字符都过滤掉。
③ 对数据进行html encode 处理
④ 过滤或移除特殊的html标签, 例如: script, iframe , < for <, > for >, " for
⑤ 过滤javascript 事件的标签。例如 "onclick=", "onfocus" 等等。

yii中的xss防范

<?php echo chtml::encode($user->name) ?>

此方法的源码:

/**
* 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/function.htmlspecialchars.php
*/
public static function encode($content, $doubleencode = true)
{
  return htmlspecialchars($content, ent_quotes | ent_substitute, yii::$app->charset, $doubleencode);
}

htmlspecialchars & htmlentities & urlencode 三者的区别:



available flags constants
constant name description
ent_compat will convert double-quotes and leave single-quotes alone.
ent_quotes will convert both double and single quotes.
ent_noquotes will leave both double and single quotes unconverted.
ent_ignore silently discard invalid code unit sequences instead of returning an empty string. using this flag is discouraged as it » may have security implications.
ent_substitute replace invalid code unit sequences with a unicode replacement character u+fffd (utf-8) or &#fffd; (otherwise) instead of returning an empty string.
ent_disallowed replace invalid code points for the given document type with a unicode replacement character u+fffd (utf-8) or &#fffd; (otherwise) instead of leaving them as is. this may be useful, for instance, to ensure the well-formedness of xml documents with embedded external content.
ent_html401 handle code as html 4.01.
ent_xml1 handle code as xml 1.
ent_xhtml handle code as xhtml.
ent_html5 handle code as html 5.

htmlspecialchars

convert special characters to html entities

string htmlspecialchars ( 
      string $string 
      [, int $flags = ent_compat | ent_html401 
      [, string $encoding = ini_get("default_charset") 
      [, bool $double_encode = true ]
    ]
  ] 
)

the translations performed are:

& (ampersand) becomes &
" (double quote) becomes " when ent_noquotes is not set.
' (single quote) becomes ' (or ') only when ent_quotes is set.
< (less than) becomes <
> (greater than) becomes >

<?php
$new = htmlspecialchars("<a href='test'>test</a>", ent_quotes);
echo $new; // <a href='test'>test</a>
?>

htmlentities

convert all applicable characters to html entities

string htmlentities ( 
      string $string 
      [, int $flags = ent_compat | ent_html401 
      [, string $encoding = ini_get("default_charset") 
      [, bool $double_encode = true ]
    ]
  ] 
)

<?php
$str = "a 'quote' is <b>bold</b>";
// outputs: a 'quote' is <b>bold</b>
echo htmlentities($str);
// outputs: a 'quote' is <b>bold</b>
echo htmlentities($str, ent_quotes);
?>

urlencode

url 编码是为了符合url的规范。因为在标准的url规范中中文和很多的字符是不允许出现在url中的。

例如在baidu中搜索"测试汉字"。 url会变成
http://www.baidu.com/s?wd=%b2%e2%ca%d4%ba%ba%d7%d6&rsv_bp=0&rsv_spt=3&inputt=7477

所谓url编码就是: 把所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+)
 此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+)。此编码与 www 表单 post 数据的编码方式是一样的,同时与 application/x-www-form-urlencoded 的媒体类型编码方式一样。由于历史原因,此编码在将空格编码为加号(+)方面与 rfc1738 编码(参见 rawurlencode())不同。

<?php
echo '<a href="mycgi?foo=', urlencode($userinput), '">';
?>

<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>

更多关于yii相关内容感兴趣的读者可查看本站专题:《yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于yii框架的php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网