?
This document uses PHP Chinese website manual Release
SecurityClass包含幫助您創(chuàng)建安全應(yīng)用程序的方法,用于處理安全輸入數(shù)據(jù)。
XSS濾波
跨站點(diǎn)請(qǐng)求偽造%28CSRF%29
類(lèi)引用
CodeIgniter附帶了一個(gè)跨站點(diǎn)腳本預(yù)防過(guò)濾器,它尋找常用的技術(shù)來(lái)觸發(fā)JavaScript或其他類(lèi)型的代碼,這些代碼試圖劫持cookie或做其他惡意的事情。如果遇到任何不允許的情況,則通過(guò)將數(shù)據(jù)轉(zhuǎn)換為字符實(shí)體來(lái)安全地呈現(xiàn)。
若要通過(guò)XSS篩選器篩選數(shù)據(jù),請(qǐng)使用xss_clean()
方法:
$data = $this->security->xss_clean($data);
可選的第二個(gè)參數(shù),是[醫(yī)]圖像,允許使用此函數(shù)測(cè)試圖像是否可能受到XSS攻擊,這對(duì)于文件上傳安全性非常有用。如果第二個(gè)參數(shù)設(shè)置為true,而不是返回修改過(guò)的字符串,則如果圖像安全,則返回true;如果圖像包含瀏覽器可能試圖執(zhí)行的惡意信息,則返回false。
if ($this->security->xss_clean($file, TRUE) === FALSE){ // file failed the XSS test}
重要
如果您想過(guò)濾HTML屬性值,請(qǐng)html_escape()
改為使用!
您可以通過(guò)更改application/config/config.php以下列方式存檔:
$config['csrf_protection'] = TRUE;
如果您使用表單助手,然后form_open()
將自動(dòng)在窗體中插入隱藏的CSRF字段。如果沒(méi)有,那么您可以使用get_csrf_token_name()
和get_csrf_hash()
$csrf = array( 'name' => $this->security->get_csrf_token_name(), 'hash' => $this->security->get_csrf_hash());...<input type="hidden" name="<?=$csrf['name'];?>" value="<?=$csrf['hash'];?>" />
令牌可以在每次提交時(shí)重新生成(默認(rèn)),也可以在CSRF Cookie的整個(gè)生命周期內(nèi)保持不變。默認(rèn)的令牌重新生成提供了更嚴(yán)格的安全性,但可能會(huì)導(dǎo)致其他令牌無(wú)效(后退/前進(jìn)導(dǎo)航,多個(gè)選項(xiàng)卡/窗口,異步操作等)的可用性問(wèn)題。您可以通過(guò)編輯以下配置參數(shù)來(lái)改變此行為
$config['csrf_regenerate'] = TRUE;
選擇URI可以通過(guò)csrf保護(hù)列入白名單(例如期望外部發(fā)布內(nèi)容的API端點(diǎn))。您可以通過(guò)編輯'csrf_exclude_uris'配置參數(shù)來(lái)添加這些URI:
$config['csrf_exclude_uris'] = array('api/person/add');
正則表達(dá)式也被支持(不區(qū)分大小寫(xiě)):
$config['csrf_exclude_uris'] = array( 'api/record/[0-9]+', 'api/title/[a-z]+');
class CI_Securityxss_clean($str[, $is_image = FALSE])
參數(shù): | $ str(mixed) - 輸入字符串或字符串?dāng)?shù)組 |
---|---|
返回: | XSS清理數(shù)據(jù) |
返回類(lèi)型: | 雜 |
$ str(mixed) - 輸入字符串或字符串?dāng)?shù)組返回:XSS干凈數(shù)據(jù)返回類(lèi)型:混合嘗試從輸入數(shù)據(jù)中除去XSS漏洞并返回已清理的字符串。如果可選的第二個(gè)參數(shù)設(shè)置為true,如果圖像可以安全使用,它將返回布爾值TRUE;如果檢測(cè)到惡意數(shù)據(jù),則返回FALSE。重要此方法不適用于過(guò)濾HTML屬性值!html_escape()
改為使用。sanitize_filename($str[, $relative_path = FALSE])
參數(shù):$ str(string) - 文件名/路徑$ relative_path(bool) - 是否保留文件中的任何目錄pathReturns:清理文件名/路徑返回類(lèi)型:字符串
$ str(string) - 文件名/路徑
$ relative_path(bool) - 是否保留文件路徑中的任何目錄
Returns: Sanitized file name/path
Return type: string
Tries to sanitize filenames in order to prevent directory traversal attempts and other security threats, which is particularly useful for files that were supplied via user input.
$ filename = $ this-> security-> sanitize_filename($ this-> input-> post('filename'));
如果用戶(hù)輸入可以接受相對(duì)路徑,例如file / in / some / approved / folder.txt,則可以將第二個(gè)可選參數(shù)設(shè)置$relative_path
為T(mén)RUE。
$ filename = $ this-> security-> sanitize_filename($ this-> input-> post('filename'),TRUE);
get_csrf_token_name()
返回: | CSRF令牌名稱(chēng) |
---|---|
返回類(lèi)型: | 串 |
get_csrf_hash()
返回: | CSRF哈希 |
---|---|
返回類(lèi)型: | 串 |
entity_decode($str[, $charset = NULL])
參數(shù): | $ str(string) - 輸入字符串$ charset(string) - 輸入字符串的字符集 |
---|---|
返回: | 實(shí)體解碼的字符串 |
返回類(lèi)型: | 串 |
$ str(string) - 輸入字符串
$ charset(字符串) - 輸入字符串的字符集
Returns: Entity-decoded string
Return type: string
This method acts a lot like PHP’s own native `html_entity_decode()` function in ENT\_COMPAT mode, only it tries to detect HTML entities that don’t end in a semicolon because some browsers allow that.
如果$charset
參數(shù)為空,則配置$config['charset']
價(jià)值將被使用。
get_random_bytes($length)
參數(shù): | $ length(int) - 輸出長(zhǎng)度 |
---|---|
返回: | 隨機(jī)字節(jié)的二進(jìn)制流或失敗時(shí)為FALSE |
返回類(lèi)型: | 串 |
$ length(int) - 輸出長(zhǎng)度
Returns: A binary stream of random bytes or FALSE on failure
Return type: string
A convenience method for getting proper random bytes via `mcrypt_create_iv()`, `/dev/urandom` or `openssl_random_pseudo_bytes()` (in that order), if one of them is available.
用于生成CSRF和XSS令牌。
注
輸出不一定是加密安全的,這是最好的嘗試。