Skip to content

Commit

Permalink
#9253 Check image validity when creating announcement
Browse files Browse the repository at this point in the history
  • Loading branch information
NateWr authored and asmecher committed Nov 8, 2023
1 parent dd4c345 commit f999bb8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
9 changes: 6 additions & 3 deletions api/v1/announcements/PKPAnnouncementHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,13 @@ public function add($slimRequest, $response, $args)
try {
$announcementId = Repo::announcement()->add($announcement);
} catch (StoreTemporaryFileException $e) {
$announcement = Repo::announcement()->get($announcementId);
Repo::announcement()->delete($announcement);
$announcementId = $e->dataObject->getId();
if ($announcementId) {
$announcement = Repo::announcement()->get($announcementId);
Repo::announcement()->delete($announcement);
}
return $response->withStatus(400)->withJson([
'image' => __('api.400.errorUploadingImage')
'image' => [__('api.400.errorUploadingImage')]
]);
}

Expand Down
25 changes: 25 additions & 0 deletions classes/announcement/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
use PKP\context\Context;
use PKP\core\Core;
use PKP\core\exceptions\StoreTemporaryFileException;
use PKP\core\PKPString;
use PKP\file\FileManager;
use PKP\file\TemporaryFile;
use PKP\file\TemporaryFileManager;
use PKP\plugins\Hook;
use PKP\services\PKPSchemaService;
use PKP\user\User;
use PKP\validation\ValidatorFactory;

class Repository
Expand Down Expand Up @@ -230,6 +232,9 @@ protected function handleImageUpload(Announcement $announcement): void
$temporaryFileManager = new TemporaryFileManager();
$temporaryFile = $temporaryFileManager->getFile((int) $image['temporaryFileId'], $user?->getId());
$filePath = $this->getImageSubdirectory() . '/' . $this->getImageFilename($announcement, $temporaryFile);
if (!$this->isValidImage($temporaryFile, $filePath, $user, $announcement)) {
throw new StoreTemporaryFileException($temporaryFile, $filePath, $user, $announcement);
}
if ($this->storeTemporaryFile($temporaryFile, $filePath, $user->getId(), $announcement)) {
$announcement->setImage(
$this->getImageData($announcement, $temporaryFile)
Expand Down Expand Up @@ -324,4 +329,24 @@ protected function deleteImage(Announcement $announcement): void
);
}
}

/**
* Check that temporary file is an image
*/
protected function isValidImage(TemporaryFile $temporaryFile): bool
{
if (getimagesize($temporaryFile->getFilePath()) === false) {
return false;
}
$extension = pathinfo($temporaryFile->getOriginalFileName(), PATHINFO_EXTENSION);
$fileManager = new FileManager();
$extensionFromMimeType = $fileManager->getImageExtension(
PKPString::mime_content_type($temporaryFile->getFilePath())
);
if ($extensionFromMimeType !== '.' . $extension) {
return false;
}

return true;
}
}
13 changes: 0 additions & 13 deletions classes/file/PKPPublicFileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,6 @@ public function copySiteFile($sourceFile, $destFileName)
return $this->copyFile($sourceFile, $this->getSiteFilesPath() . '/' . $destFileName);
}

/**
* Copy a file to a context's public directory.
*
* @param string $sourceFile the source of the file to copy
* @param string $destFileName the destination file name
*
* @return bool
*/
public function copySiteFile($sourceFile, $destFileName)
{
return $this->copyFile($sourceFile, $this->getSiteFilesPath() . '/' . $destFileName);
}

/**
* Copy a file to a site's public directory.
*
Expand Down

0 comments on commit f999bb8

Please sign in to comment.