diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 2d4d421a770e..db35ffa0732f 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -37,6 +37,7 @@ Release Date: Unreleased - **Helpers:** Fixed a bug where `excerpt()` threw a `TypeError` when `$phrase` was not found in `$text` or when `$phrase` was `null` and `$radius` was an odd integer. When `$phrase` is not found, `excerpt()` now falls back to extracting an excerpt from the beginning of `$text`. - **Helpers:** Fixed a bug where `format_number()` with `$precision = 0` appended a trailing comma in locales using a comma as the decimal separator. - **Helpers:** Fixed a bug where `parse_subdomain()` incorrectly treated hostnames with ports (such as IP addresses and two-part TLD domains) as having unexpected subdomains. +- **Helpers:** Fixed a bug where `sanitize_filename()` preserved leading forward slashes when `$relativePath` was `true`, including slashes exposed by removing parent-directory sequences. The same applies to `CodeIgniter\Security\Security::sanitizeFilename()`. - **Helpers:** Fixed a bug in `plural()` where the regex for `matrix`, `vertex`, and `index` lacked non-capturing grouping and an end-of-string anchor for the `ix` alternative, causing incorrect pluralization of words like `complex`, `sex`, and strings containing `matrix` before the end. Expanded `plural()` and `singular()` to support additional words ending in `ix` or `ex` (`apex`, `appendix`, `codex`, `cortex`, `latex`, `murex`, `vortex`). - **Helpers:** Fixed bugs in `ArrayHelper::dotKeyExists()` where `'0'` path segments caused early loop termination, non-array intermediate values threw a `TypeError`, and paths with segments after a wildcard failed when the wildcard matched multiple elements. - **Helpers:** Fixed escaping of attribute values in HTML media helpers to prevent malformed HTML and attribute injection. diff --git a/docs/helpers/security_helper.md b/docs/helpers/security_helper.md index 9fed2f6a59f1..e1cda095e339 100644 --- a/docs/helpers/security_helper.md +++ b/docs/helpers/security_helper.md @@ -28,7 +28,7 @@ The following functions are available: Provides protection against directory traversal. -Backslashes are always removed. If `$relativePath` is `true`, forward slashes are preserved to allow relative paths such as `path/to/file.txt`, while parent-directory sequences (`../`) are removed. +Backslashes are always removed. If `$relativePath` is `true`, forward slashes are preserved to allow relative paths such as `path/to/file.txt`, while parent-directory sequences (`../`) and leading forward slashes are removed. !!! note "Note" When passing relative paths to this helper, use forward slashes (`/`) as directory separators, including on Windows. diff --git a/system/Helpers/security_helper.php b/system/Helpers/security_helper.php index 3bbd9c3b2849..1cb37655ccd9 100644 --- a/system/Helpers/security_helper.php +++ b/system/Helpers/security_helper.php @@ -24,7 +24,8 @@ * If it is acceptable for the user input to include relative paths, * e.g. file/in/some/approved/folder.txt, you can set the second optional * parameter, $relativePath to TRUE. - * Only forward slashes are preserved, backslashes are always removed. + * Only forward slashes within the relative path are preserved. + * Leading forward slashes and all backslashes are removed. * * @param string $filename Input file name * @param bool $relativePath Whether to preserve paths @@ -79,7 +80,7 @@ function sanitize_filename(string $filename, bool $relativePath = false): string $filename = str_replace($bad, '', $filename); } while ($old !== $filename); - return $filename; + return $relativePath ? ltrim($filename, '/') : $filename; } } diff --git a/tests/system/Helpers/SecurityHelperTest.php b/tests/system/Helpers/SecurityHelperTest.php index 9d5d60a903e4..c6c0da2c102d 100644 --- a/tests/system/Helpers/SecurityHelperTest.php +++ b/tests/system/Helpers/SecurityHelperTest.php @@ -66,12 +66,25 @@ public static function provideSanitizeFilenamePaths(): iterable $eightBackslashes = str_repeat($backslash, 8); return [ - 'empty filename' => ['', false, ''], - 'escaped NUL in filename' => ['a' . $backslash . '0b', false, 'a0b'], - 'escaped NUL in relative path' => ['a' . $backslash . '0b', true, 'a0b'], - 'literal NUL in filename' => ['a' . chr(0) . 'b', false, 'ab'], - 'literal NUL in relative path' => ['a' . chr(0) . 'b', true, 'ab'], - 'strip forward path' => [ + 'empty filename' => ['', false, ''], + 'empty relative path' => ['', true, ''], + 'absolute path in relative mode' => ['/etc/passwd', true, 'etc/passwd'], + 'multiple leading slashes' => ['////etc/passwd', true, 'etc/passwd'], + 'double-slash traversal' => ['..//..//etc/passwd', true, 'etc/passwd'], + 'triple-slash traversal' => ['..///..///etc/passwd', true, 'etc/passwd'], + 'leading slash exposed by filters' => [' [$backslash . '/etc/passwd', true, 'etc/passwd'], + 'only forward slashes' => ['////', true, ''], + 'only traversal and slashes' => ['..//..///', true, ''], + 'preserve internal slashes' => ['approved//folder/file.txt', true, 'approved//folder/file.txt'], + 'preserve trailing slash' => ['approved/folder/', true, 'approved/folder/'], + 'absolute path in default mode' => ['/etc/passwd', false, 'etcpasswd'], + 'double-slash traversal in default mode' => ['..//..//etc/passwd', false, 'etcpasswd'], + 'escaped NUL in filename' => ['a' . $backslash . '0b', false, 'a0b'], + 'escaped NUL in relative path' => ['a' . $backslash . '0b', true, 'a0b'], + 'literal NUL in filename' => ['a' . chr(0) . 'b', false, 'ab'], + 'literal NUL in relative path' => ['a' . chr(0) . 'b', true, 'ab'], + 'strip forward path' => [ 'approved/folder/file.txt', false, 'approvedfolderfile.txt', diff --git a/tests/system/Security/SecurityTest.php b/tests/system/Security/SecurityTest.php index 5c8c98a9e50a..031259dd5a2e 100644 --- a/tests/system/Security/SecurityTest.php +++ b/tests/system/Security/SecurityTest.php @@ -317,6 +317,14 @@ public function testSanitizeFilename(): void $this->assertSame('foo', $security->sanitizeFilename($filename)); } + public function testSanitizeFilenameRemovesLeadingSlashes(): void + { + $security = $this->createMockSecurity(); + + $this->assertSame('etc/passwd', $security->sanitizeFilename('..//..//etc/passwd', true)); + $this->assertSame('etc/passwd', $security->sanitizeFilename('/etc/passwd', true)); + } + public function testSanitizeFilenameBackslashTraversal(): void { $security = $this->createMockSecurity();