Skip to content

Commit

Permalink
FFWEB-3210: Fix upload validation SSH key file issue
Browse files Browse the repository at this point in the history
Fix upload validation SSH key file issue
  • Loading branch information
Rayn93 authored Nov 7, 2024
1 parent 03fc054 commit b657309
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
### Fix
- Support MSI - checking all inventory sources during export feed
- Fix feed path for UI export type
- Fix upload validation SSH key file issue

## [v5.0.0] - 2024.07.08
### BREAKING
Expand Down
53 changes: 53 additions & 0 deletions src/Model/Config/Backend/Rsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,66 @@

namespace Omikron\Factfinder\Model\Config\Backend;

use Exception;
use Magento\Config\Model\Config\Backend\File;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\MediaStorage\Model\File\Uploader;

/**
* @SuppressWarnings(PHPMD)
*/
class Rsa extends File
{
public function beforeSave()
{
$value = $this->getValue();
$file = $this->getFileData();

if (!empty($file)) {
$uploadDir = $this->_getUploadDir();
try {
/** @var Uploader $uploader */
$uploader = $this->_uploaderFactory->create(['fileId' => $file]);
$uploader->setAllowedExtensions($this->_getAllowedExtensions());
$uploader->setAllowRenameFiles(true);
$uploader->addValidateCallback('size', $this, 'validateMaxSize');
$result = $uploader->save($uploadDir);
} catch (Exception $e) {
throw new LocalizedException(__('%1', $e->getMessage()));
}
if ($result !== false) {
$filename = $result['file'];
if ($this->_addWhetherScopeInfo()) {
$filename = $this->_prependScopeInfo($filename);
}
$this->setValue($filename);
}
} else {
if (is_array($value) && !empty($value['delete'])) {
$this->setValue('');
} elseif (is_array($value) && !empty($value['value'])) {
$this->setValueAfterValidation($value['value']);
} else {
$this->unsValue();
}
}

return $this;
}

protected function getUploadDirPath($uploadDir)
{
return $this->_filesystem->getDirectoryWrite(DirectoryList::CONFIG)->getAbsolutePath($uploadDir);
}

private function setValueAfterValidation(string $value): void
{
// avoid intercepting value
if (preg_match('/[^a-z0-9_\/\\-\\.]+/i', $value)) {
throw new LocalizedException(__('Invalid file name'));
}

$this->setValue($value);
}
}

0 comments on commit b657309

Please sign in to comment.