首頁 php教程 PHP开发 Yii2的XSS攻擊防範策略分析

Yii2的XSS攻擊防範策略分析

Dec 23, 2016 pm 04:46 PM

本文實例講述了Yii2的XSS攻擊防範策略。分享給大家參考,具體如下:

XSS 漏洞修復

原則: 不相信客戶輸入的資料
注意: 攻擊代碼不一定在<script></script>中

① 將重要的cookie標記為http only, 這樣的話Javascript 中的document.cookie語句就不能取得到cookie了.
② 只允許使用者輸入我們期望的資料。 例如: 年齡的textbox中,只允許使用者輸入數字。 而數字之外的字元都被過濾掉。
③ 對資料進行Html Encode 處理
④ 過濾或移除特殊的Html標籤, 例如: script, iframe , 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);
}
登入後複製

 ://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_QUOTES Willoth both double and single quotes

ENT_QUOTES Willoth both double and single quotesp. 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 Replace Character U. 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 fors. 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.🜎

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 ]
    ]
  ]
)
登入後複製

Convert special characters to HTML entities
<?php
$new = htmlspecialchars("<a href=&#39;test&#39;>Test</a>", ENT_QUOTES);
echo $new; // <a href=&#39;test&#39;>Test</a>
?>
登入後複製

   

The translations performed are:

' (single quote) becomes ' (or ') only when ENT_QUOTES is set.

< (less than) becomes <

> (greater than) becomes >

string htmlentities (
      string $string
      [, int $flags = ENT_COMPAT | ENT_HTML401
      [, string $encoding = ini_get("default_charset")
      [, bool $double_encode = true ]
    ]
  ]
)
登入後複製

   

🜥 html


<?php
$str = "A &#39;quote&#39; is <b>bold</b>";
// Outputs: A &#39;quote&#39; is <b>bold</b>
echo htmlentities($str);
// Outputs: A &#39;quote&#39; 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 &#39;<a href="mycgi?foo=&#39;, urlencode($userinput), &#39;">&#39;;
?>
登入後複製
<?php
$query_string = &#39;foo=&#39; . urlencode($foo) . &#39;&bar=&#39; . urlencode($bar);
echo &#39;<a href="mycgi?&#39; . htmlentities($query_string) . &#39;">&#39;;
?>
登入後複製


希望本文所述對大家基於Yii框架的PHP程式設計有所幫助。

更多Yii2的XSS攻擊防範策略分析相關文章請關注PHP中文網!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)