Skip to content

Commit

Permalink
[Task]: Mime type check on Profile Avatar upload (#14125)
Browse files Browse the repository at this point in the history
* Task: mime type check on avatar upload

* Task: change to use getTypeFromMimeMapping

* Task: add missing import
  • Loading branch information
kingjia90 committed Feb 1, 2023
1 parent 5e90431 commit 75a448e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions bundles/AdminBundle/Controller/Admin/UserController.php
Expand Up @@ -21,12 +21,14 @@
use Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse;
use Pimcore\Controller\KernelControllerEventInterface;
use Pimcore\Logger;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject;
use Pimcore\Model\Element;
use Pimcore\Model\User;
use Pimcore\Tool;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
Expand Down Expand Up @@ -797,6 +799,15 @@ public function uploadImageAction(Request $request)
throw $this->createAccessDeniedHttpException('Only admin users are allowed to modify admin users');
}

//Check if uploaded file is an image
$avatarFile = $request->files->get('Filedata');

$assetType = Asset::getTypeFromMimeMapping($avatarFile->getMimeType(), $avatarFile);

if (!$avatarFile instanceof UploadedFile || $assetType !== 'image') {
throw new \Exception('Unsupported file format.');
}

$userObj->setImage($_FILES['Filedata']['tmp_name']);

// set content-type to text/html, otherwise (when application/json is sent) chrome will complain in
Expand Down
Expand Up @@ -250,6 +250,9 @@ pimcore.settings.profile.panel = Class.create({
Ext.getCmp("pimcore_profile_delete_image_" + this.currentUser.id).setVisible(true);
pimcore.helpers.reloadUserImage(this.currentUser.id);
this.currentUser.hasImage = true;
}.bind(this),
function () {
Ext.MessageBox.alert(t('error'), t("unsupported_filetype"));
}.bind(this)
);
}.bind(this)
Expand Down
Expand Up @@ -177,6 +177,9 @@ pimcore.settings.user.user.settings = Class.create({
Ext.getCmp("pimcore_user_delete_image_" + this.currentUser.id).setVisible(true);
pimcore.helpers.reloadUserImage(this.currentUser.id);
this.currentUser.hasImage = true;
}.bind(this),
function () {
Ext.MessageBox.alert(t('error'), t("unsupported_filetype"));
}.bind(this)
);
}.bind(this)
Expand Down

0 comments on commit 75a448e

Please sign in to comment.