Skip to content

Commit 067bc71

Browse files
authored
Merge branch 'develop' into fix/sqlsrv-forge-alter-table-escape
2 parents e0fc5fc + 59e5eea commit 067bc71

11 files changed

Lines changed: 284 additions & 27 deletions

File tree

‎.github/scripts/random-tests-config.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Files
2828
Filters
2929
Format
3030
HTTP
31-
# Helpers
31+
Helpers
3232
Honeypot
3333
HotReloader
3434
# I18n

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"psr/log": "^3.0"
1818
},
1919
"require-dev": {
20-
"boundwize/structarmed": "^0.20",
20+
"boundwize/structarmed": "^0.21",
2121
"codeigniter/phpstan-codeigniter": "^2.1",
2222
"fakerphp/faker": "^1.24",
2323
"kint-php/kint": "^6.1",

‎docs/changelogs/v4.7.7.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ Release Date: Unreleased
88

99
## Message Changes
1010

11+
- Added the `HTTP.invalidFileName` language string.
12+
1113
## Changes
1214

1315
## Deprecations
1416

1517
## Bugs Fixed
1618

1719
- **Database:** Fixed a bug where `SQLSRV\Forge::_alterTable()` interpolated table and column names without escaping in default constraint lookups.
20+
- **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names.
1821
- **Security:** Fixed a bug where `Security::__construct()` threw a `TypeError` when the CSRF cookie was an array, causing an HTTP 500 response for affected requests.
1922
- **Worker Mode:** Fixed a bug where every request that ended with an uncaught exception, including a `404 Page Not Found` without a 404 override, called `exit()` in the exception handler. This ended the worker script, so FrankenPHP booted a new one, and the worker loop skipped its cleanup after the request. The exception handler now returns to the worker loop.
2023

‎docs/libraries/uploaded_files.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ echo $clientPath; // dir/file.txt, or dir/sub_dir/file.txt
448448
Do not let user input directly decide the destination directory. The `$targetPath` argument of `move()` and the `$folderName` argument of `store()` are never sanitized, so an attacker-controlled value containing `../` can move the uploaded file outside the intended directory. Use directories controlled by your application.
449449

450450
Filenames are only sanitized by default: `move()` sanitizes the client-provided name when the `$name` argument is omitted, and `store()` generates a random name when `$fileName` is omitted. A caller-supplied `$name` or `$fileName` is NOT sanitized, so if you pass one, generate it yourself or sanitize it with `sanitize_filename`.
451+
On Windows, `move()` and `store()` reject names ending in a period or space or containing a colon, whether the name was supplied by the caller or chosen by default. Sanitization alone does not remove these forms. Use a generated name when possible.
451452

452453
Sanitizing a client-provided filename does not make it safe to keep in a web-accessible directory where scripts can execute. Some web servers select a handler from any extension in a filename, including an extension before the final one. Keep uploads outside the public directory with `store()` when possible. If they must be public, disable script execution in the upload directory and pass `$file->getRandomName()` as the second argument to `move()`.
453454

@@ -461,7 +462,7 @@ Each file can be moved to its new location with the aptly named `move()` method.
461462
$file->move(WRITEPATH . 'uploads');
462463
```
463464

464-
By default, the client-provided filename is sanitized and used.
465+
By default, the client-provided filename is sanitized and used. On Windows, an `HTTPException` is thrown if the name to be stored ends with a period or a space, or contains a colon. This also applies when you pass a name explicitly. Such names may be stored under a different filename or in an NTFS alternate data stream. Use a generated filename if the client-provided name is rejected.
465466

466467
#### with New Filename
467468

@@ -504,6 +505,7 @@ Moving an uploaded file can fail, with an `HTTPException`, under several circums
504505

505506
- the file has already been moved
506507
- the file did not upload successfully
508+
- on Windows, the filename ends with a period or a space, or contains a colon
507509
- the file move operation fails (e.g., improper permissions)
508510

509511
### Store Files
@@ -536,4 +538,5 @@ Moving an uploaded file can fail, with an `HTTPException`, under several circums
536538

537539
- the file has already been moved
538540
- the file did not upload successfully
541+
- on Windows, the supplied filename ends with a period or a space, or contains a colon
539542
- the file move operation fails (e.g., improper permissions)

‎system/Encryption/Handlers/OpenSSLHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function encrypt(#[SensitiveParameter] $data, #[SensitiveParameter] $para
9696
$encryptKey = \hash_hkdf($this->digest, $key, 0, $this->encryptKeyInfo);
9797

9898
// basic encryption
99-
$iv = ($ivSize = \openssl_cipher_iv_length($this->cipher)) ? \openssl_random_pseudo_bytes($ivSize) : null;
99+
$iv = ($ivSize = \openssl_cipher_iv_length($this->cipher)) ? random_bytes($ivSize) : null;
100100

101101
$data = \openssl_encrypt($data, $this->cipher, $encryptKey, OPENSSL_RAW_DATA, $iv);
102102

‎system/HTTP/Exceptions/HTTPException.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ public static function forInvalidFile(?string $path = null)
207207
return new static(lang('HTTP.invalidFile'));
208208
}
209209

210+
/**
211+
* For Uploaded file move
212+
*/
213+
public static function forInvalidFileName(): static
214+
{
215+
return new static(lang('HTTP.invalidFileName'));
216+
}
217+
210218
/**
211219
* For Uploaded file move
212220
*

‎system/HTTP/Files/UploadedFile.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ public function __construct(string $path, string $originalName, ?string $mimeTyp
119119
* untrusted input: a value containing "../" can move the file outside the intended
120120
* directory. Use a path controlled by your application.
121121
* @param string|null $name The name to rename the file to. When null, the client-provided name is used and sanitized.
122-
* A caller-supplied name is NOT sanitized.
122+
* A caller-supplied name is NOT sanitized. On Windows, names that may resolve to a
123+
* different file or an NTFS stream are rejected in either case.
123124
* @param bool $overwrite State for indicating whether to overwrite the previously generated file with the same
124125
* name or not.
125126
*
@@ -142,6 +143,11 @@ public function move(string $targetPath, ?string $name = null, bool $overwrite =
142143
helper('security');
143144
$name = sanitize_filename($this->getName());
144145
}
146+
147+
// Windows may store these names under a different filename or in an NTFS stream.
148+
if (is_windows() && (rtrim($name, '. ') !== $name || str_contains($name, ':'))) {
149+
throw HTTPException::forInvalidFileName();
150+
}
145151
$destination = $overwrite ? $targetPath . $name : $this->getDestination($targetPath . $name);
146152

147153
try {
@@ -346,6 +352,7 @@ public function isValid(): bool
346352
* it from untrusted input: a value containing "../" can move the file outside the
347353
* uploads directory.
348354
* @param string|null $fileName the name to rename the file to. A caller-supplied name is NOT sanitized.
355+
* On Windows, names that may resolve to a different file or an NTFS stream are rejected.
349356
*
350357
* @return string file full path
351358
*/

‎system/Language/en/HTTP.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@
6363
'disallowedAction' => 'The action you requested is not allowed.',
6464

6565
// Uploaded file moving
66-
'alreadyMoved' => 'The uploaded file has already been moved.',
67-
'invalidFile' => 'The original file is not a valid file.',
68-
'moveFailed' => 'Could not move file "{0}" to "{1}". Reason: {2}',
66+
'alreadyMoved' => 'The uploaded file has already been moved.',
67+
'invalidFile' => 'The original file is not a valid file.',
68+
'invalidFileName' => 'The uploaded filename is invalid.',
69+
'moveFailed' => 'Could not move file "{0}" to "{1}". Reason: {2}',
6970

7071
'uploadErrOk' => 'The file uploaded with success.',
7172
'uploadErrIniSize' => 'The file "%s" exceeds your upload_max_filesize ini directive.',

‎tests/system/AutoReview/CreateNewChangelogTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public function testCreateNewChangelog(string $mode): void
120120
);
121121
} finally {
122122
exec("git restore -- {$trackedPathArgs}");
123+
exec("git restore -- {$generatedPathArgs} 2>/dev/null");
123124
exec("git clean -f -- {$generatedPathArgs}");
124125
}
125126
}

0 commit comments

Comments
 (0)