Skip to content

Commit eaa1cdc

Browse files
Merge pull request #6432 from magento-engcom/fix-file-extension-bug
Added method to handle file extension for validating files being uploaded.
2 parents 2742d15 + 71baefb commit eaa1cdc

File tree

2 files changed

+35
-24
lines changed
  • app/code/Magento/Customer/Model/Metadata/Form
  • lib/internal/Magento/Framework/File

2 files changed

+35
-24
lines changed

app/code/Magento/Customer/Model/Metadata/Form/File.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ protected function _validateByRules($value)
182182
{
183183
$label = $value['name'];
184184
$rules = $this->getAttribute()->getValidationRules();
185-
$extension = $this->fileProcessor->getStat($value['name'])['extension'];
185+
$extension = $this->getFileExtension($value['name']);
186186
$fileExtensions = ArrayObjectSearch::getArrayElementByName(
187187
$rules,
188188
'file_extensions'
@@ -220,6 +220,17 @@ protected function _validateByRules($value)
220220
return [];
221221
}
222222

223+
/**
224+
* Get file extension from the file if it exists, otherwise, get from filename.
225+
*
226+
* @param string $fileName
227+
* @return string
228+
*/
229+
private function getFileExtension(string $fileName): string
230+
{
231+
return pathinfo($fileName, PATHINFO_EXTENSION);
232+
}
233+
223234
/**
224235
* Helper function that checks if the file was uploaded.
225236
*

lib/internal/Magento/Framework/File/Uploader.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -716,31 +716,31 @@ private function validateFileId(array $fileId): void
716716
if (isset($fileId['tmp_name'])) {
717717
$tmpName = trim($fileId['tmp_name']);
718718

719-
$allowedFolders = [
720-
sys_get_temp_dir(),
721-
$this->directoryList->getPath(DirectoryList::MEDIA),
722-
$this->directoryList->getPath(DirectoryList::VAR_DIR),
723-
$this->directoryList->getPath(DirectoryList::TMP),
724-
$this->directoryList->getPath(DirectoryList::UPLOAD),
725-
];
726-
727-
$disallowedFolders = [
728-
$this->directoryList->getPath(DirectoryList::LOG),
729-
];
730-
731-
foreach ($allowedFolders as $allowedFolder) {
732-
$dir = $this->filesystem->getDirectoryReadByPath($allowedFolder);
733-
if ($dir->isExist($tmpName)) {
734-
$isValid = true;
735-
break;
719+
if (preg_match('/\.\.(\\\|\/)/', $tmpName) !== 1) {
720+
$allowedFolders = [
721+
sys_get_temp_dir(),
722+
$this->directoryList->getPath(DirectoryList::MEDIA),
723+
$this->directoryList->getPath(DirectoryList::VAR_DIR),
724+
$this->directoryList->getPath(DirectoryList::TMP),
725+
$this->directoryList->getPath(DirectoryList::UPLOAD),
726+
];
727+
728+
$disallowedFolders = [
729+
$this->directoryList->getPath(DirectoryList::LOG),
730+
];
731+
732+
foreach ($allowedFolders as $allowedFolder) {
733+
if (stripos($tmpName, $allowedFolder) === 0) {
734+
$isValid = true;
735+
break;
736+
}
736737
}
737-
}
738738

739-
foreach ($disallowedFolders as $disallowedFolder) {
740-
$dir = $this->filesystem->getDirectoryReadByPath($disallowedFolder);
741-
if ($dir->isExist($tmpName)) {
742-
$isValid = false;
743-
break;
739+
foreach ($disallowedFolders as $disallowedFolder) {
740+
if (stripos($tmpName, $disallowedFolder) === 0) {
741+
$isValid = false;
742+
break;
743+
}
744744
}
745745
}
746746
}

0 commit comments

Comments
 (0)