Skip to content

Commit 88ea45e

Browse files
committed
AC-479: Input validation for File Upload in Customer Address
* jenkin test failure fixes
1 parent 884b6ab commit 88ea45e

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

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

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ class Uploader
209209
* @param DriverPool|null $driverPool
210210
* @param TargetDirectory|null $targetDirectory
211211
* @param Filesystem|null $filesystem
212-
* @param LoggerInterface|null $logger
213212
* @throws \DomainException
214213
*/
215214
public function __construct(
@@ -218,8 +217,7 @@ public function __construct(
218217
DirectoryList $directoryList = null,
219218
DriverPool $driverPool = null,
220219
TargetDirectory $targetDirectory = null,
221-
Filesystem $filesystem = null,
222-
LoggerInterface $logger = null
220+
Filesystem $filesystem = null
223221
) {
224222
$this->directoryList = $directoryList ?: ObjectManager::getInstance()->get(DirectoryList::class);
225223
$this->targetDirectory = $targetDirectory ?: ObjectManager::getInstance()->get(TargetDirectory::class);
@@ -234,7 +232,6 @@ public function __construct(
234232
}
235233
$this->fileMime = $fileMime ?: ObjectManager::getInstance()->get(Mime::class);
236234
$this->driverPool = $driverPool ?: ObjectManager::getInstance()->get(DriverPool::class);
237-
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
238235
}
239236

240237
/**
@@ -326,7 +323,7 @@ private function validateDestination(string $destinationFolder): void
326323
}
327324
if ($this->_allowCreateFolders) {
328325
$this->createDestinationFolder($destinationFolder);
329-
} elseif (!$this->targetDirectory
326+
} elseif (!$this->getTargetDirectory()
330327
->getDirectoryWrite(DirectoryList::ROOT)
331328
->isWritable($destinationFolder)
332329
) {
@@ -359,24 +356,66 @@ protected function _moveFile($tmpPath, $destPath)
359356
$rootCode = DirectoryList::PUB;
360357

361358
try {
362-
if (strpos($destPath, $this->directoryList->getPath($rootCode)) !== 0) {
359+
if (strpos($destPath, $this->getDirectoryList()->getPath($rootCode)) !== 0) {
363360
$rootCode = DirectoryList::ROOT;
364361
}
365362

366-
$destPath = str_replace($this->directoryList->getPath($rootCode), '', $destPath);
367-
$directory = $this->targetDirectory->getDirectoryWrite($rootCode);
363+
$destPath = str_replace($this->getDirectoryList()->getPath($rootCode), '', $destPath);
364+
$directory = $this->getTargetDirectory()->getDirectoryWrite($rootCode);
368365

369366
return $this->getFileDriver()->rename(
370367
$tmpPath,
371368
$directory->getAbsolutePath($destPath),
372369
$directory->getDriver()
373370
);
374371
} catch (FileSystemException $exception) {
375-
$this->logger->critical($exception->getMessage());
372+
$this->getLogger()->critical($exception->getMessage());
376373
return false;
377374
}
378375
}
379376

377+
/**
378+
* Get logger instance.
379+
*
380+
* @deprecated
381+
* @return LoggerInterface
382+
*/
383+
private function getLogger(): LoggerInterface
384+
{
385+
if (!$this->logger) {
386+
$this->logger = ObjectManager::getInstance()->get(LoggerInterface::class);
387+
}
388+
return $this->logger;
389+
}
390+
391+
/**
392+
* Retrieves target directory.
393+
*
394+
* @return TargetDirectory
395+
*/
396+
private function getTargetDirectory(): TargetDirectory
397+
{
398+
if (!isset($this->targetDirectory)) {
399+
$this->targetDirectory = ObjectManager::getInstance()->get(TargetDirectory::class);
400+
}
401+
402+
return $this->targetDirectory;
403+
}
404+
405+
/**
406+
* Retrieves directory list.
407+
*
408+
* @return DirectoryList
409+
*/
410+
private function getDirectoryList(): DirectoryList
411+
{
412+
if (!isset($this->directoryList)) {
413+
$this->directoryList = ObjectManager::getInstance()->get(DirectoryList::class);
414+
}
415+
416+
return $this->directoryList;
417+
}
418+
380419
/**
381420
* Validate file before save
382421
*
@@ -740,7 +779,7 @@ private function createDestinationFolder(string $destinationFolder)
740779
$destinationFolder = substr($destinationFolder, 0, -1);
741780
}
742781

743-
$rootDirectory = $this->targetDirectory->getDirectoryWrite(DirectoryList::ROOT);
782+
$rootDirectory = $this->getTargetDirectory()->getDirectoryWrite(DirectoryList::ROOT);
744783

745784
if (!$rootDirectory->isDirectory($destinationFolder)) {
746785
$result = $rootDirectory->getDriver()->createDirectory($destinationFolder);
@@ -809,8 +848,8 @@ public static function getDispersionPath($fileName)
809848
$dispersionPath = '/' . ('.' == $fileName[$char] ? '_' : $fileName[$char]);
810849
} else {
811850
$dispersionPath = self::_addDirSeparator(
812-
$dispersionPath
813-
) . ('.' == $fileName[$char] ? '_' : $fileName[$char]);
851+
$dispersionPath
852+
) . ('.' == $fileName[$char] ? '_' : $fileName[$char]);
814853
}
815854
$char++;
816855
}

0 commit comments

Comments
 (0)