Skip to content

Commit 1b8e033

Browse files
Merge branch '7.4' into 8.0
* 7.4: (21 commits) [WebProfilerBundle] Fix hot reload support (FrankenPHP) [Serializer] Normalize static methods when they have groups [PropertyInfo] fix compatibility with phpdocumentor/reflection-docblock 6.x [DoctrineBridge] Respect schema_filter in schema listeners [HttpClient] Fix destructor throwing while timeout was handled [HttpClient] Test throwing destructor together with retryable [Mime] Update mime types [HttpFoundation] Reject invalid paths [HttpKernel] Fix handling empty MapUploadedFile arrays [FrameworkBundle] Add missing `useAttributeAsKey` calls [Di] Fix invalid reference behavior When calling UploadedFile::getErrorMessage() to a file which has no error and is uploaded successfully, it should not return an error [HttpKernel] Bypass mapping construction when `RedirectController::urlRedirectAction` is triggered cs fix [Console] fall back to 0 when getCode() does not provide an integer fix merge [FrameworkBundle] Fix accessing the test container when using KernelTestCase in non-debug mode Fix running HttpClient tests [FrameworkBundle] Fix clearing the HttpCache store in tests [DependencyInjection][HttpKernel] Fix parsing Target attributes on properties and on controllers ...
2 parents ecf2bc9 + 669ac23 commit 1b8e033

4 files changed

Lines changed: 53 additions & 11 deletions

File tree

File/UploadedFile.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,22 +204,30 @@ public function move(string $directory, ?string $name = null): File
204204

205205
switch ($this->error) {
206206
case \UPLOAD_ERR_INI_SIZE:
207-
throw new IniSizeFileException($this->getErrorMessage());
207+
throw new IniSizeFileException($this->getExceptionMessage());
208208
case \UPLOAD_ERR_FORM_SIZE:
209-
throw new FormSizeFileException($this->getErrorMessage());
209+
throw new FormSizeFileException($this->getExceptionMessage());
210210
case \UPLOAD_ERR_PARTIAL:
211-
throw new PartialFileException($this->getErrorMessage());
211+
throw new PartialFileException($this->getExceptionMessage());
212212
case \UPLOAD_ERR_NO_FILE:
213-
throw new NoFileException($this->getErrorMessage());
213+
throw new NoFileException($this->getExceptionMessage());
214214
case \UPLOAD_ERR_CANT_WRITE:
215-
throw new CannotWriteFileException($this->getErrorMessage());
215+
throw new CannotWriteFileException($this->getExceptionMessage());
216216
case \UPLOAD_ERR_NO_TMP_DIR:
217-
throw new NoTmpDirFileException($this->getErrorMessage());
217+
throw new NoTmpDirFileException($this->getExceptionMessage());
218218
case \UPLOAD_ERR_EXTENSION:
219-
throw new ExtensionFileException($this->getErrorMessage());
219+
throw new ExtensionFileException($this->getExceptionMessage());
220220
}
221221

222-
throw new FileException($this->getErrorMessage());
222+
throw new FileException($this->getExceptionMessage());
223+
}
224+
225+
/**
226+
* Retrieves a user-friendly error message for file upload issues, if any.
227+
*/
228+
public function getErrorMessage(): string
229+
{
230+
return \UPLOAD_ERR_OK !== $this->error ? $this->getExceptionMessage() : '';
223231
}
224232

225233
/**
@@ -268,7 +276,7 @@ private static function parseFilesize(string $size): int|float
268276
/**
269277
* Returns an informative upload error message.
270278
*/
271-
public function getErrorMessage(): string
279+
private function getExceptionMessage(): string
272280
{
273281
static $errors = [
274282
\UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).',

Request.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,16 @@ public static function create(string $uri, string $method = 'GET', array $parame
389389
$server['PHP_AUTH_PW'] = $components['pass'];
390390
}
391391

392-
if (!isset($components['path'])) {
392+
if ('' === $path = $components['path'] ?? '') {
393393
$components['path'] = '/';
394+
} elseif (!isset($components['scheme']) && !isset($components['host']) && '/' !== $path[0]) {
395+
if (false !== $pos = strpos($path, '/')) {
396+
$path = substr($path, 0, $pos);
397+
}
398+
399+
if (str_contains($path, ':')) {
400+
throw new BadRequestException('Invalid URI: Path is malformed.');
401+
}
394402
}
395403

396404
switch (strtoupper($method)) {

Tests/File/UploadedFileTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,32 @@ public function testErrorIsOkByDefault()
120120
$this->assertEquals(\UPLOAD_ERR_OK, $file->getError());
121121
}
122122

123+
public function testInvalidFile()
124+
{
125+
$file = new UploadedFile(
126+
__DIR__.'/Fixtures/test.gif',
127+
'original.gif',
128+
'image/gif',
129+
);
130+
131+
$this->expectException(FileException::class);
132+
$this->expectExceptionMessage('The file "original.gif" was not uploaded due to an unknown error.');
133+
134+
$file->move(__DIR__.'/Fixtures/directory');
135+
}
136+
137+
public function testNoErrorMessageIfErrorIsUploadErrOk()
138+
{
139+
$file = new UploadedFile(
140+
__DIR__.'/Fixtures/test.gif',
141+
'original.gif',
142+
'image/gif',
143+
null
144+
);
145+
146+
$this->assertSame('', $file->getErrorMessage());
147+
}
148+
123149
public function testGetClientOriginalName()
124150
{
125151
$file = new UploadedFile(

Tests/RequestTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2713,6 +2713,7 @@ public static function provideMalformedUrls(): array
27132713
["https\x80://example.com", 'Invalid URI: Scheme is malformed.'],
27142714
['http>://example.com', 'Invalid URI: Scheme is malformed.'],
27152715
['0http://example.com', 'Invalid URI: Scheme is malformed.'],
2716+
[':path', 'Invalid URI: Path is malformed.'],
27162717
];
27172718
}
27182719

@@ -2739,7 +2740,6 @@ public static function provideLegitimateUrls(): array
27392740
['http://[2001:db8::1]/path'],
27402741
['http://[::1]'],
27412742
['http://example.com/path'],
2742-
[':path'],
27432743
];
27442744
}
27452745

0 commit comments

Comments
 (0)