From a3f439f809ff7fed584e6e30e9b883d81a128261 Mon Sep 17 00:00:00 2001 From: Mohamed Rasik Farid Date: Mon, 23 Nov 2026 22:19:26 +0530 Subject: [PATCH 1/2] fix: remove leading slashes from sanitized relative paths Remove leading forward slashes after filename filtering so relative mode cannot leave an absolute Unix path. Preserve internal and trailing separators and the default mode. Add regression coverage for leading separators, traversal remnants, filtering, empty results, and the deprecated Security alias. Update the user guide and changelog. Fixes codeigniter4/CodeIgniter4#10815 --- docs/changelogs/v4.7.7.md | 2 ++ docs/helpers/security_helper.md | 2 +- system/Helpers/security_helper.php | 5 +++-- tests/system/Helpers/SecurityHelperTest.php | 25 ++++++++++++++++----- tests/system/Security/SecurityTest.php | 8 +++++++ 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 2d4d421a770e..b6b7c3fb97d8 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -23,6 +23,8 @@ Release Date: Unreleased ## Bugs Fixed +- **Security Helper:** 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 `Security::sanitizeFilename()`. + - **API:** Fixed HTTP 500 errors in `ResponseTrait::paginate()` when using a Query Builder with a very large page number or `$perPage = 0`. - **Cache:** Fixed a bug where `PredisHandler::increment()` and `PredisHandler::decrement()` updated the unused `'data'` hash field instead of `'__ci_value'`, causing `get()` to return stale values for existing counters. - **CORS:** Fixed a bug where `Cors::hasResponseHeaders()` matched header substrings via `str_contains()`, causing false positives when existing exposed headers shared a prefix with configured exposed headers. 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(); From b74c25d5eab02432b4c8132170d500ab06574532 Mon Sep 17 00:00:00 2001 From: Mohamed Rasik Farid Date: Tue, 24 Nov 2026 19:14:00 +0530 Subject: [PATCH 2/2] docs: address sanitizer changelog review Classify the sanitizer fix under Helpers, place it with the alphabetically ordered component entries, remove the extra blank line, and qualify CodeIgniter\Security\Security. Focused security tests pass: 97 tests, 161 assertions. Documentation navigation check passes. --- docs/changelogs/v4.7.7.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index b6b7c3fb97d8..db35ffa0732f 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -23,8 +23,6 @@ Release Date: Unreleased ## Bugs Fixed -- **Security Helper:** 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 `Security::sanitizeFilename()`. - - **API:** Fixed HTTP 500 errors in `ResponseTrait::paginate()` when using a Query Builder with a very large page number or `$perPage = 0`. - **Cache:** Fixed a bug where `PredisHandler::increment()` and `PredisHandler::decrement()` updated the unused `'data'` hash field instead of `'__ci_value'`, causing `get()` to return stale values for existing counters. - **CORS:** Fixed a bug where `Cors::hasResponseHeaders()` matched header substrings via `str_contains()`, causing false positives when existing exposed headers shared a prefix with configured exposed headers. @@ -39,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.