[英]Is 'image' rule secure enough?
因此,我試圖為我的網站創建一個上傳個人資料圖片,而且我想知道image
規則在Laravel中是否足夠安全。
考慮以下代碼:
input = [
'profile_picture' => Input::file('profile_picture'),
];
$rules = ['profile_picture' => 'required|image'];
$validator = Validator::make($input, $rules);
if ($validator->fails()) {
return 'It\'s not an image!';
}
非常簡單,我要讓您關注的是rules
數組中的image
規則。
Laravel似乎通過其MIME類型來驗證圖像。 眾所周知,可以從客戶端進行編輯,然后使用篡改數據或Burp代理之類的內容將其發送到服務器。
您說如何知道它通過其MIME類型驗證圖像? 好吧...看下面的這段代碼,這是Laravel用於驗證的Validator.php
類。 它包含了我認為的每條規則。 具體來說,這是validateImage
函數,這是image
規則。
驗證器/ validateImage():
/**
* Validate the MIME type of a file is an image MIME type.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function validateImage($attribute, $value)
{
return $this->validateMimes($attribute, $value, array('jpeg', 'png', 'gif', 'bmp'));
}
就像Doc-block所說的那樣,它通過其MIME類型對其進行驗證。
這樣足夠安全嗎? 任何問題都歡迎!
不,這是不安全的。 至少您不能確定它是否是真實圖像,因為可以操縱MIME類型。 通常有幫助的是使用getimagesize()
,如果文件不是圖像,它將返回false
。 因此,您可以使用以下規則擴展驗證器:
Validator::extend('real_image', function($attribute, $value, $parameters){
if($value instanceof Symfony\Component\HttpFoundation\File\UploadedFile){
if(getimagesize($value->getRealPath() !== false){
return true;
}
}
return false;
}
並將其添加到您的驗證規則:
$rules = ['profile_picture' => 'required|image|real_image'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.