Skip to content

Commit a3f439f

Browse files
authored
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 #10815
1 parent 08039be commit a3f439f

5 files changed

Lines changed: 33 additions & 9 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Release Date: Unreleased
2323

2424
## Bugs Fixed
2525

26+
- **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()`.
27+
2628
- **API:** Fixed HTTP 500 errors in `ResponseTrait::paginate()` when using a Query Builder with a very large page number or `$perPage = 0`.
2729
- **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.
2830
- **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.

‎docs/helpers/security_helper.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The following functions are available:
2828

2929
Provides protection against directory traversal.
3030

31-
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.
31+
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.
3232

3333
!!! note "Note"
3434
When passing relative paths to this helper, use forward slashes (`/`) as directory separators, including on Windows.

‎system/Helpers/security_helper.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
* If it is acceptable for the user input to include relative paths,
2525
* e.g. file/in/some/approved/folder.txt, you can set the second optional
2626
* parameter, $relativePath to TRUE.
27-
* Only forward slashes are preserved, backslashes are always removed.
27+
* Only forward slashes within the relative path are preserved.
28+
* Leading forward slashes and all backslashes are removed.
2829
*
2930
* @param string $filename Input file name
3031
* @param bool $relativePath Whether to preserve paths
@@ -79,7 +80,7 @@ function sanitize_filename(string $filename, bool $relativePath = false): string
7980
$filename = str_replace($bad, '', $filename);
8081
} while ($old !== $filename);
8182

82-
return $filename;
83+
return $relativePath ? ltrim($filename, '/') : $filename;
8384
}
8485
}
8586

‎tests/system/Helpers/SecurityHelperTest.php‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,25 @@ public static function provideSanitizeFilenamePaths(): iterable
6666
$eightBackslashes = str_repeat($backslash, 8);
6767

6868
return [
69-
'empty filename' => ['', false, ''],
70-
'escaped NUL in filename' => ['a' . $backslash . '0b', false, 'a0b'],
71-
'escaped NUL in relative path' => ['a' . $backslash . '0b', true, 'a0b'],
72-
'literal NUL in filename' => ['a' . chr(0) . 'b', false, 'ab'],
73-
'literal NUL in relative path' => ['a' . chr(0) . 'b', true, 'ab'],
74-
'strip forward path' => [
69+
'empty filename' => ['', false, ''],
70+
'empty relative path' => ['', true, ''],
71+
'absolute path in relative mode' => ['/etc/passwd', true, 'etc/passwd'],
72+
'multiple leading slashes' => ['////etc/passwd', true, 'etc/passwd'],
73+
'double-slash traversal' => ['..//..//etc/passwd', true, 'etc/passwd'],
74+
'triple-slash traversal' => ['..///..///etc/passwd', true, 'etc/passwd'],
75+
'leading slash exposed by filters' => ['</etc/passwd', true, 'etc/passwd'],
76+
'leading slash exposed by backslash' => [$backslash . '/etc/passwd', true, 'etc/passwd'],
77+
'only forward slashes' => ['////', true, ''],
78+
'only traversal and slashes' => ['..//..///', true, ''],
79+
'preserve internal slashes' => ['approved//folder/file.txt', true, 'approved//folder/file.txt'],
80+
'preserve trailing slash' => ['approved/folder/', true, 'approved/folder/'],
81+
'absolute path in default mode' => ['/etc/passwd', false, 'etcpasswd'],
82+
'double-slash traversal in default mode' => ['..//..//etc/passwd', false, 'etcpasswd'],
83+
'escaped NUL in filename' => ['a' . $backslash . '0b', false, 'a0b'],
84+
'escaped NUL in relative path' => ['a' . $backslash . '0b', true, 'a0b'],
85+
'literal NUL in filename' => ['a' . chr(0) . 'b', false, 'ab'],
86+
'literal NUL in relative path' => ['a' . chr(0) . 'b', true, 'ab'],
87+
'strip forward path' => [
7588
'approved/folder/file.txt',
7689
false,
7790
'approvedfolderfile.txt',

‎tests/system/Security/SecurityTest.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,14 @@ public function testSanitizeFilename(): void
317317
$this->assertSame('foo', $security->sanitizeFilename($filename));
318318
}
319319

320+
public function testSanitizeFilenameRemovesLeadingSlashes(): void
321+
{
322+
$security = $this->createMockSecurity();
323+
324+
$this->assertSame('etc/passwd', $security->sanitizeFilename('..//..//etc/passwd', true));
325+
$this->assertSame('etc/passwd', $security->sanitizeFilename('/etc/passwd', true));
326+
}
327+
320328
public function testSanitizeFilenameBackslashTraversal(): void
321329
{
322330
$security = $this->createMockSecurity();

0 commit comments

Comments
 (0)