Yii2的XSS攻擊防范策略分析
本文實(shí)例講述了Yii2的XSS攻擊防范策略。分享給大家供大家參考,具體如下:
XSS 漏洞修復(fù)
原則: 不相信客戶輸入的數(shù)據(jù)
注意: 攻擊代碼不一定在<script></script>中
① 將重要的cookie標(biāo)記為http only, 這樣的話Javascript 中的document.cookie語(yǔ)句就不能獲取到cookie了.
② 只允許用戶輸入我們期望的數(shù)據(jù)。 例如: 年齡的textbox中,只允許用戶輸入數(shù)字。 而數(shù)字之外的字符都過(guò)濾掉。
③ 對(duì)數(shù)據(jù)進(jìn)行Html Encode 處理
④ 過(guò)濾或移除特殊的Html標(biāo)簽, 例如: script, iframe , < for <, > for >, " for
⑤ 過(guò)濾JavaScript 事件的標(biāo)簽。例如 "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 三者的區(qū)別:
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 >
<?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的規(guī)范。因?yàn)樵跇?biāo)準(zhǔn)的url規(guī)范中中文和很多的字符是不允許出現(xiàn)在url中的。
例如在baidu中搜索"測(cè)試漢字"。 URL會(huì)變成
http://www.baidu.com/s?wd=%B2%E2%CA%D4%BA%BA%D7%D6&rsv_bp=0&rsv_spt=3&inputT=7477
所謂URL編碼就是: 把所有非字母數(shù)字字符都將被替換成百分號(hào)(%)后跟兩位十六進(jìn)制數(shù),空格則編碼為加號(hào)(+)
此字符串中除了 -_. 之外的所有非字母數(shù)字字符都將被替換成百分號(hào)(%)后跟兩位十六進(jìn)制數(shù),空格則編碼為加號(hào)(+)。此編碼與 WWW 表單 POST 數(shù)據(jù)的編碼方式是一樣的,同時(shí)與 application/x-www-form-urlencoded 的媒體類型編碼方式一樣。由于歷史原因,此編碼在將空格編碼為加號(hào)(+)方面與 RFC1738 編碼(參見(jiàn) 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) . '">'; ?>
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
Yii2表單事件之Ajax提交實(shí)現(xiàn)方法
這篇文章主要介紹了Yii2表單事件之Ajax提交實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Yii2框架中ajax提交的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05
Laravel框架Eloquent ORM刪除數(shù)據(jù)操作示例
這篇文章主要介紹了Laravel框架Eloquent ORM刪除數(shù)據(jù)操作,結(jié)合實(shí)例形式分析了laravel框架ORM三種刪除數(shù)據(jù)的方式,需要的朋友可以參考下2019-12-12
ThinkPHP實(shí)現(xiàn)靜態(tài)緩存和動(dòng)態(tài)緩存示例代碼
本篇文章主要介紹了ThinkPHP實(shí)現(xiàn)靜態(tài)緩存和動(dòng)態(tài)緩存示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Zend Framework緩存Cache用法簡(jiǎn)單實(shí)例
這篇文章主要介紹了Zend Framework緩存Cache用法,以簡(jiǎn)單實(shí)例形式分析了Zend Framework使用緩存的具體步驟,配置文件與Cache緩存具體使用技巧,需要的朋友可以參考下2016-03-03
php實(shí)現(xiàn)當(dāng)前頁(yè)面點(diǎn)擊下載文件的實(shí)例代碼
下面小編就為大家?guī)?lái)一篇php實(shí)現(xiàn)當(dāng)前頁(yè)面點(diǎn)擊下載文件的實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11

