這篇文章主要介紹了Yii2的XSS攻擊防範策略,較為詳細的分析了XSS攻擊的原理及Yii2相應的防範策略,需要的朋友可以參考下
本文實例講述了Yii2的XSS攻擊防範策略。分享給大家供大家參考,具體如下:
XSS 漏洞修復
#原則: 不相信客戶輸入的資料
注意: 攻擊代碼不一定在< ;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.
ENT_NOQUOTES Will. 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 FFplacement Character FFFDFD; 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。 , 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_XHTMLHandleHan as HTML 5.
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 ESis set .
< (less than) becomes <
> (greater than) becomes >
<?php $new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES); echo $new; // <a href='test'>Test</a> ?>
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); ?>
URL 編碼是為了符合url的規範。因為在標準的url規範中中文和很多的字元是不允許出現在url中的。
例如在baidu中搜尋"測試漢字"。 URL會變成
http://www.baidu.com/s?wd=���i���&rsv_bp=0&rsv_spt=3&inputT=7477
#所謂URL編碼就是: 把所有非字母數字字元都將被替換成百分號(%)後跟兩位十六進制數,空格則編碼為加號( )
<?php echo '<a href="mycgi?foo=', urlencode($userinput), '">'; ?>
<?php $query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar); echo '<a href="mycgi?' . htmlentities($query_string) . '">'; ?>
#以上就是本文的全部內容,希望對大家的學習有幫助,更多相關內容請關注PHP中文網!
相關推薦:
如何實作Yii清理快取#如何處理Yii2.0 Basic程式碼中路由連結被轉義以上是對於Yii2的XSS攻擊防範策略的方法解析的詳細內容。更多資訊請關注PHP中文網其他相關文章!