三年基础工程存在问题问题 Yii:app和Html:encode的区别

Yii2的XSS攻击防范策略分析
本文实例讲述了Yii2的XSS攻击防范策略。分享给大家供大家参考,具体如下:
XSS 漏洞修复
原则: 不相信客户输入的数据
注意: 攻击代码不一定在&script&&/script&中
① 将重要的cookie标记为http only, 这样的话Javascript 中的document.cookie语句就不能获取到cookie了.
② 只允许用户输入我们期望的数据。 例如: 年龄的textbox中,只允许用户输入数字。 而数字之外的字符都过滤掉。
③ 对数据进行Html Encode 处理
④ 过滤或移除特殊的Html标签, 例如: script, iframe , & for &, & for &, &quot 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.
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 &
$new = htmlspecialchars("&a href='test'&Test&/a&", ENT_QUOTES);
echo $ // &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 ]
$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);
URL 编码是为了符合url的规范。因为在标准的url规范中中文和很多的字符是不允许出现在url中的。
例如在baidu中搜索"测试汉字"。 URL会变成
/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())不同。
echo '&a href="mycgi?foo=', urlencode($userinput), '"&';
$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程序设计有所帮助。
顶一下(0) 踩一下(0)
热门标签:Yii2的XSS攻击防范策略分析
作者:wjtlht928
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Yii2的XSS攻击防范策略,较为详细的分析了XSS攻击的原理及Yii2相应的防范策略,需要的朋友可以参考下
本文实例讲述了Yii2的XSS攻击防范策略。分享给大家供大家参考,具体如下:
XSS 漏洞修复
原则: 不相信客户输入的数据
注意: 攻击代码不一定在&script&&/script&中
① 将重要的cookie标记为http only, 这样的话Javascript 中的document.cookie语句就不能获取到cookie了.
② 只允许用户输入我们期望的数据。 例如: 年龄的textbox中,只允许用户输入数字。 而数字之外的字符都过滤掉。
③ 对数据进行Html Encode 处理
④ 过滤或移除特殊的Html标签, 例如: script, iframe , & for &, & for &, &quot 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 &
$new = htmlspecialchars("&a href='test'&Test&/a&", ENT_QUOTES);
echo $ // &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 ]
$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);
URL 编码是为了符合url的规范。因为在标准的url规范中中文和很多的字符是不允许出现在url中的。
例如在baidu中搜索"测试汉字"。 URL会变成
/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())不同。
echo '&a href="mycgi?foo=', urlencode($userinput), '"&';
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '&a href="mycgi?' . htmlentities($query_string) . '"&';
更多关于Yii相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《》
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具可以发吗?
09:40:25&&23579人阅读
JS/DHTML/CSS(112)
最常见的做法是采用正则表达式替换的方法,将特殊字符如 & & & 等进行替换,htmlencode的时候这样替换还比较容易,但发过来htmldecode的时候就不一定好用了,因为需要反转的情况很多,出了常见的&&&以外,还有&&&&等数十个字符实体,还有AB中文或者中文之类以字符的Unicode编码的十进制或16进制表示的转义,难以全部列举,用逐个替换不仅代码冗长而且低效,还容易漏掉某些字符。
代码如下:
function htmlencode(s){
var div = document.createElement('div');
div.appendChild(document.createTextNode(s));
return div.innerHTML;
function htmldecode(s){
var div = document.createElement('div');
div.innerHTML =
return div.innerText || div.textC
相当简洁!
编码原理就是创建TextNode节点,附加到容器中,再取容器的innerHTML.
解码原理是将字符串赋給容器的innerHTML,再取innerText或textContent.
测试一下:
document.onclick = function (){
//&p& & &/p&
alert(htmlencode('&p& & &/p&'));
//&p& & & ABC 中文 中文 &/p&
alert(htmldecode('&p& & & ABC 中文 中文 &/p&'));
}效果不错。&
htmldecode对入参有要求,如果入参不是合法的encode后的结果,可能无法得到预期结果。
我在google搜索,在cnblogs找到一篇和我一样思路的,原来已经有别人这样想了=||=,不过他的htmldecode代码有错误。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:901605次
积分:10530
积分:10530
排名:第1223名
原创:165篇
转载:73篇
评论:247条

我要回帖

更多关于 锂电池基础科学问题 的文章

 

随机推荐