本文实例讲述了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 三者的区别:
http://php.net/manual/zh/function.htmlspecialchars.php
http://php.net/manual/zh/function.htmlentities.php
http://cn2.php.net/manual/zh/function.urlencode.php
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.<br />
ENT_HTML401 Handle code as HTML 4.01.<br />
ENT_XML1 Handle code as XML 1.<br />
ENT_XHTML Handle code as XHTML.<br />
ENT_HTML5 Handle code as HTML 5.</p>
<p><strong>htmlspecialchars</strong></p>
<p>Convert special characters to HTML entities</p>
<div class=\"phpstudycode\">
<pre class=\"brush:php;\">
string htmlspecialchars (
string $string
[, int $flags = ENT_COMPAT | ENT_HTML401
[, string $encoding = ini_get(\"default_charset\")
[, bool $double_encode = true ]
]
]
)
</pre>
</div>
<p>The translations performed are:</p>
<p>& (ampersand) becomes &<br />
\" (double quote) becomes " when ENT_NOQUOTES is not set.<br />
\' (single quote) becomes ' (or ') only when ENT_QUOTES is set.<br />
< (less than) becomes <<br />
> (greater than) becomes ></p>
<div class=\"phpstudycode\">
<pre class=\"brush:php;\">
<?php
$new = htmlspecialchars(\"<a href=\'test\'>Test</a>\", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?>
</pre>
</div>
<p><strong>htmlentities</strong></p>
<p>Convert all applicable characters to HTML entities</p>
<div class=\"phpstudycode\">
<pre class=\"brush:php;\">
string htmlentities (
string $string
[, int $flags = ENT_COMPAT | ENT_HTML401
[, string $encoding = ini_get(\"default_charset\")
[, bool $double_encode = true ]
]
]
)
</pre>
</div>
<div class=\"phpstudycode\">
<pre class=\"brush:php;\">
<?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);
?>
</pre>
</div>
<p><strong>urlencode</strong></p>
<p>URL 编码是为了符合url的规范。因为在标准的url规范中中文和很多的字符是不允许出现在url中的。</p>
<p>例如在baidu中搜索\"测试汉字\"。 URL会变成<br />
http://www.baidu.com/s?wd=%B2%E2%CA%D4%BA%BA%D7%D6&rsv_bp=0&rsv_spt=3&inputT=7477</p>
<p>所谓URL编码就是: 把所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+)<br />
此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+)。此编码与 WWW 表单 POST 数据的编码方式是一样的,同时与 application/x-www-form-urlencoded 的媒体类型编码方式一样。由于历史原因,此编码在将空格编码为加号(+)方面与 RFC1738 编码(参见 rawurlencode())不同。</p>
<div class=\"phpstudycode\">
<pre class=\"brush:php;\">
<?php
echo \'<a href=\"mycgi?foo=\', urlencode($userinput), \'\">\';
?>
</pre>
</div>
<div class=\"phpstudycode\">
<pre class=\"brush:php;\">
<?php
$query_string = \'foo=\' . urlencode($foo) . \'&bar=\' . urlencode($bar);
echo \'<a href=\"mycgi?\' . htmlentities($query_string) . \'\">\';
?>
</pre>
</div>
<p>更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》</p>
<p>希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。</p>
</div>
</section>
<script type=\"text/javascript\">
(function() {
var s = \"_\" + Math.random().toString(36).slice(2);
document.write(\'<div style=\"\" id=\"\' + s + \'\"></div>\');
(window.slotbydup = window.slotbydup || []).push({
id: \"u4263905\",
container: s
});
})();
</script>
<section class=\"xgwz\">
<b>【热门文章】</b>
<ul>
<li><a href=\"/b.php/58001.html\">浅析node.js中close事件</a></li><li><a href=\"/b.php/58002.html\">javascript模拟订火车票和退票示例</a></li><li><a href=\"/b.php/58003.html\">php判断并删除空目录及空子目录的方法</a></li><li><a href=\"/b.php/58004.html\">win10无法打开这个应用解决办法</a></li><li><a href=\"/b.php/58005.html\">浅析XMLHttpRequest的缓存问题</a></li><li><a href=\"/b.php/58006.html\">一系列Bootstrap导航条使用方法分享</a></li><li><a href=\"/b.php/58007.html\">JS简单获取及显示当前时间的方法</a></li><li><a href=\"/b.php/58008.html\">C#实现自定义定时组件的方法</a></li><li><a href=\"/b.php/58010.html\">Win7/Win8.1用户升级Win10 这个通知是升级Win10通行证</a></li><li><a href=\"/b.php/58011.html\">Android WebView 优化之路</a></li><li><a href=\"/b.php/58012.html\">JavaScript的作用域和块级作用域概念理解</a></li><li><a href=\"/b.php/58013.html\">mysql常用监控脚本命令整理</a></li><li><a href=\"/b.php/58014.html\">JS实现冒泡排序,插入排序和快速排序并排序输出</a></li><li><a href=\"/b.php/58015.html\">asp.net微信开发(永久素材管理)</a></li><li><a href=\"/b.php/58016.html\">浅析mongodb中group分组</a></li><li><a href=\"/b.php/58017.html\">Ubuntu系统上使用LVM调整硬盘分区的教程</a></li><li><a href=\"/b.php/58018.html\">iOS应用开发中UITabBarController标签栏控制器使用进阶</a></li><li><a href=\"/b.php/58019.html\">php验证session无效的解决方法</a></li><li><a href=\"/b.php/58020.html\">Android中AutoCompleteTextView与TextWatcher结合小实例</a></li><li><a href=\"/b.php/58021.html\">C#抽象类和接口的区别分析</a></li><li><a href=\"/b.php/58022.html\">CentOS 一键配置rsync服务器脚本</a></li><li><a href=\"/b.php/58023.html\">oracle远程连接服务器出现 ORA-12170 TNS:连接超时 解决办法</a></li><li><a href=\"/b.php/58024.html\">Linux查看系统信息的常用命令</a></li><li><a href=\"/b.php/58025.html\">解析C++编程中异常相关的堆栈展开和throw()异常规范</a></li></ul>
</section>
<section class=\"xgwz\">
<b>【热门文章】</b>
<ul>
<li><a href=\"/c.php/4553.html\">关于css布局问题</a></li><li><a href=\"/c.php/4554.html\">怎么将flask里面的request.args这个ImmutableMultiDict变成MutiDict类型</a></li><li><a href=\"/c.php/4555.html\">多重数组转换对象?</a></li><li><a href=\"/c.php/4556.html\">CSS如何实现gauge效果?</a></li><li><a href=\"/c.php/4557.html\">node.js 路径问题</a></li><li><a href=\"/c.php/4558.html\">nwjs 怎么实现像钉钉这种任务栏通知。</a></li><li><a href=\"/c.php/4559.html\">怎样解读这个正则表达式 </a></li><li><a href=\"/c.php/4560.html\">SQL问题请教大神</a></li><li><a href=\"/c.php/4561.html\">Laravel用ajax提交这个表单,控制器code应该怎么写?</a></li><li><a href=\"/c.php/4562.html\">在win平台做lamp开发测试,用vagrant+virtualbox,还是用vmware+docker方便?</a></li><li><a href=\"/c.php/4563.html\">网页端使用PHP生成随机的用户头像?</a></li><li><a href=\"/c.php/4564.html\">jsonp 有什么限制?</a></li><li><a href=\"/c.php/4565.html\">下面是段注入的代码,要 怎么解决好</a></li><li><a href=\"/c.php/4566.html\">url forwarding 使原网页缺少元素</a></li><li><a href=\"/c.php/4567.html\">Linux tar打包文件会忽略 .git 和 .htaccess 文件</a></li><li><a href=\"/c.php/4568.html\">maven编译包含groovy文件的项目出现编码问题</a></li><li><a href=\"/c.php/4569.html\">min.js和js区别</a></li><li><a href=\"/c.php/4570.html\">请问大家如何利用lucence等搜索引擎优化mysql数据库查询?</a></li><li><a href=\"/c.php/4571.html\">PHP如何实现网站消息和用户私信即时通讯?</a></li><li><a href=\"/c.php/4572.html\">jquery里error事件</a></li></ul>
</section>
<section class=\"cont pl\" id=\"comment\"><b></b>
<div id=\"SOHUCS\" sid=\"art_104965\"></div>
</section>
<div class=\"search\">
<form action=\"http://zhannei.baidu.com/cse/search\" method=\"get\" target=\"_blank\" class=\"bdcs-search-form\" id=\"bdcs-search-form\">
<input name=\"s\" value=\"12351952642737355179\" type=\"hidden\">
<input name=\"entry\" value=\"1\" type=\"hidden\">
<input name=\"ie\" value=\"gbk\" type=\"hidden\">
<input name=\"nsid\" value=\"1\" type=\"hidden\">
<input type=\"text\" placeholder=\"请输入您感兴趣的关键字\" value=\"\" id=\"search_txt1\" maxlength=\"18\" class=\"search_txt\" name=\"q\">
<input class=\"search_btn\" value=\"搜 索\" type=\"submit\">
</form>
</div>
<nav class=\"nav-foot\">
<ul>
<li><a href=\"/jiaotong/huoche/\">火车</a></li>
<li><a href=\"/jiaotong/gaotie/\">高铁</a></li>
<li><a href=\"/jiaotong/qiche/\">汽车</a></li>
<li><a href=\"/jiaotong/gongjiao/\">公交</a></li>
<li><a href=\"/jiaotong/zijia/\">自驾</a></li>
<li><a href=\"/jiaotong/licheng/\">里程</a></li>
<li> <a href=\"/jiaotong/jingdian/\">景点</a></li>
<li><a href=\"/jiaotong/gonglue/\">攻略</a></li>
<li><a href=\"/jiaotong/wen/\">问路</a></li>
<li><a href=\"/\">计算机</a></li>
</ul>
<ul>
<li><a href=\"/\">首页</a></li>
<li><a href=\"/jiaotong/huoche/\">火车</a></li>
<li><a href=\"/jiaotong/gaotie/\">高铁</a></li>
<li><a href=\"/jiaotong/qiche/\">汽车</a></li>
<li><a href=\"/jiaotong/gongjiao/\">公交</a></li>
</ul>
</nav>
<footer class=\"footer-min\">
<div class=\"app\">
<a href=\"javascript:void(0)\" class=\"pc\">电脑版</a> - <a href=\"/\">返回首页</a></div>
<div class=\"copyright\">Copyright ©2017 <a href=\"/\">交通频道</a> All Rights Reserved</div>
</footer>
<div class=\"clearfix\"></div>
<div class=\"asd\"><span id=\"asd-footer\" class=\"jbTestPos\"><script>gx(4);</script></span></div>
<script>
var path_url=\"/b.php/85988.html\";
</script>
<script type=\"text/javascript\" src=\"/img/jquery-1.10.2.min.js\"></script>
<script type=\"text/javascript\" src=\"/img/menuclick.js\"></script>
<br>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement(\"script\");
hm.src = \"https://hm.baidu.com/hm.js?4e18701aa680bab2e8eb968e32500cf0\";
var s = document.getElementsByTagName(\"script\")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</div>
</body>
</html>
本文地址:https://www.stayed.cn/item/3851
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我