Skip to content

Commit 049d445

Browse files
committed
fix: Support file uploads for non-POST HTTP methods (PATCH, PUT, DELETE)
1 parent a8b61f2 commit 049d445

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

flight/net/UploadedFile.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,32 @@ public function moveTo(string $targetPath): void
114114
throw new Exception($this->getUploadErrorMessage($this->error));
115115
}
116116

117+
// Check if this is a legitimate uploaded file (POST method uploads)
117118
$isUploadedFile = is_uploaded_file($this->tmpName) === true;
118-
if (
119-
$isUploadedFile === true
120-
&&
121-
move_uploaded_file($this->tmpName, $targetPath) === false
122-
) {
123-
throw new Exception('Cannot move uploaded file'); // @codeCoverageIgnore
124-
} elseif ($isUploadedFile === false && getenv('PHPUNIT_TEST')) {
119+
120+
if ($isUploadedFile === true) {
121+
// Standard POST upload - use move_uploaded_file for security
122+
if (move_uploaded_file($this->tmpName, $targetPath) === false) {
123+
throw new Exception('Cannot move uploaded file'); // @codeCoverageIgnore
124+
}
125+
} elseif (getenv('PHPUNIT_TEST')) {
125126
rename($this->tmpName, $targetPath);
127+
} elseif (file_exists($this->tmpName) === true && is_readable($this->tmpName) === true) {
128+
// Handle non-POST uploads (PATCH, PUT, DELETE) or other valid temp files
129+
// Verify the file is in a valid temp directory for security
130+
$tempDir = sys_get_temp_dir();
131+
$uploadTmpDir = ini_get('upload_tmp_dir') ?: $tempDir;
132+
133+
if (strpos(realpath($this->tmpName), realpath($uploadTmpDir)) === 0 ||
134+
strpos(realpath($this->tmpName), realpath($tempDir)) === 0) {
135+
if (rename($this->tmpName, $targetPath) === false) {
136+
throw new Exception('Cannot move uploaded file');
137+
}
138+
} else {
139+
throw new Exception('Invalid temporary file location');
140+
}
141+
} else {
142+
throw new Exception('Temporary file does not exist or is not readable');
126143
}
127144
}
128145

0 commit comments

Comments
 (0)