Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelogs/v4.7.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/helpers/security_helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions system/Helpers/security_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand Down
25 changes: 19 additions & 6 deletions tests/system/Helpers/SecurityHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => ['</etc/passwd', true, 'etc/passwd'],
'leading slash exposed by backslash' => [$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',
Expand Down
8 changes: 8 additions & 0 deletions tests/system/Security/SecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading