Skip to content

Commit d3552ef

Browse files
committed
Fix remote file overwrite and arbitrary file injection in the file system
1 parent 408f54d commit d3552ef

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"require": {
1111
"php": ">=8.1.0",
1212
"cakephp/filesystem": "^3.0",
13-
"monolog/monolog": "^2.0"
13+
"monolog/monolog": "^2.0",
14+
"ondrej-vrto/php-filename-sanitize": "^1.4"
1415
},
1516
"require-dev": {
1617
"phpunit/phpunit": "~10.0"

src/Resumable.php

+10-13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Dilab\Network\Response;
99
use Monolog\Logger;
1010
use Monolog\Handler\StreamHandler;
11+
use OndrejVrto\FilenameSanitize\FilenameSanitize;
1112

1213
class Resumable
1314
{
@@ -159,18 +160,14 @@ public function getExtension()
159160
}
160161

161162
/**
162-
* Makes sure the orginal extension never gets overriden by user defined filename.
163+
* Creates a safe name
163164
*
164-
* @param string User defined filename
165-
* @param string Original filename
166-
* @return string Filename that always has an extension from the original file
165+
* @param string $name Original name
166+
* @return string A safer name
167167
*/
168-
private function createSafeFilename($filename, $originalFilename)
168+
private function createSafeName(string $name): string
169169
{
170-
$filename = $this->removeExtension($filename);
171-
$extension = $this->findExtension($originalFilename);
172-
173-
return sprintf('%s.%s', $filename, $extension);
170+
return FilenameSanitize::of($name)->get();
174171
}
175172

176173
public function handleTestChunk()
@@ -227,9 +224,9 @@ private function createFileAndDeleteTmp($identifier, $filename)
227224

228225
// if the user has set a custom filename
229226
if (null !== $this->filename) {
230-
$finalFilename = $this->createSafeFilename($this->filename, $filename);
227+
$finalFilename = $this->createSafeName($this->filename);
231228
} else {
232-
$finalFilename = $filename;
229+
$finalFilename = $this->createSafeName($filename);
233230
}
234231

235232
// replace filename reference by the final file
@@ -288,7 +285,7 @@ public function tmpChunkDir($identifier)
288285
if (!empty($this->instanceId)){
289286
$tmpChunkDir .= $this->instanceId . DIRECTORY_SEPARATOR;
290287
}
291-
$tmpChunkDir .= $identifier;
288+
$tmpChunkDir .= $this->createSafeName($identifier);
292289
$this->ensureDirExists($tmpChunkDir);
293290
return $tmpChunkDir;
294291
}
@@ -318,7 +315,7 @@ private function ensureDirExists($path)
318315

319316
public function tmpChunkFilename($filename, $chunkNumber)
320317
{
321-
return $filename . '.' . str_pad($chunkNumber, 4, 0, STR_PAD_LEFT);
318+
return $this->createSafeName($filename) . '.' . str_pad($chunkNumber, 4, 0, STR_PAD_LEFT);
322319
}
323320

324321
public function getExclusiveFileHandle($name)

0 commit comments

Comments
 (0)