Repository navigation
Conversation
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#10815
|
|
||
| ## 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()`. |
There was a problem hiding this comment.
- Item as Helpers:
- Sort the items alphabetically, without line breaks
- It is advisable to specify the FQCN for the class.
rahul05ranjan
left a comment
There was a problem hiding this comment.
I don't have a PHP runtime on this machine, so I ported the sanitizer's string logic 1:1 (the $bad list plus the str_replace fixpoint loop — it's pure string manipulation, no PHP-specific semantics involved) and checked it against every case in the new data provider. All ten expectations reproduce, and the old behavior they're fixing is genuinely nasty:
..//..//etc/passwdwith$relativePath = truesanitized to//etc/passwdon main — the traversal sequences are gone, but the surviving leading slashes mean the "relative" path still resolves to the filesystem root...///..///etc/passwdgot worse (////etc/passwd), and</etc/passwdcollapsed to a clean/etc/passwdonce the<was stripped — a leading absolute path created by the filter itself, which is exactly the kind of thing this helper exists to prevent.
With the ltrim the same inputs come out as etc/passwd, internal (approved//folder/file.txt) and trailing (approved/folder/) slashes are preserved as the tests pin, and the default mode is untouched since it already strips all slashes via the $bad additions.
I also confirmed the changelog's claim about Security::sanitizeFilename() — system/Security/Security.php just does helper('security'); return sanitize_filename(...), so the fix propagates there automatically.
One documentation-level note: approved//folder/file.txt keeping its doubled internal slash is now pinned as intended behavior, which is worth knowing for anyone hoping this would normalize paths rather than only de-root them.
|
Yes, this is not an ideal result. I would prefer to introduce a new service class for working with paths — |
michalsn
left a comment
There was a problem hiding this comment.
I agree with this focused fix. Since leading slashes do not represent a valid relative path, I consider this a bug fix suitable for develop. Please address @neznaika0 changelog comment - after that, I think this is ready to merge.
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.
Description
Fixes #10815.
sanitize_filename()could leave leading forward slashes when$relativePathwastrue. For example,..//..//etc/passwdbecame//etc/passwd. Remove leading slashes after filtering so this now returnsetc/passwd. Internal and trailing separators and default-mode behavior are preserved. The deprecatedSecurity::sanitizeFilename()alias receives the same fix.Added regression cases for the reported inputs, slashes exposed by filtering, empty results, preserved relative paths, and the Security alias. Updated the user guide, PHPDoc, and 4.7.7 changelog.
Validation
Checklist: