Skip to content

fix: remove leading slashes from sanitized relative paths - #10820

Open
MohamedRF wants to merge 2 commits into
codeigniter4:developfrom
MohamedRF:fix/sanitize-filename-relative-path
Open

MohamedRF wants to merge 2 commits into
codeigniter4:developfrom
MohamedRF:fix/sanitize-filename-relative-path

Conversation

@MohamedRF

Copy link
Copy Markdown
Contributor

Description
Fixes #10815.

sanitize_filename() could leave leading forward slashes when $relativePath was true. For example, ..//..//etc/passwd became //etc/passwd. Remove leading slashes after filtering so this now returns etc/passwd. Internal and trailing separators and default-mode behavior are preserved. The deprecated Security::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

  • Focused PHPUnit tests: 97 tests, 161 assertions passed on PHP 8.5.12.
  • Eight new regression cases failed before the fix.
  • File-scoped PHP CS Fixer and Rector checks passed.
  • Full CI matrix and coverage measurement left to CI.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage (regression tests pass; coverage not measured locally)
  • User guide updated
  • Conforms to style guide

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
@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 23, 2026

@neznaika0 neznaika0 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change; it needs to be sent to the 4.8 branch.

UPD: leave in 4.7.7 branch

Comment thread docs/changelogs/v4.7.7.md Outdated

## 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()`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Item as Helpers:
  • Sort the items alphabetically, without line breaks
  • It is advisable to specify the FQCN for the class.

@rahul05ranjan rahul05ranjan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/passwd with $relativePath = true sanitized to //etc/passwd on main — the traversal sequences are gone, but the surviving leading slashes mean the "relative" path still resolves to the filesystem root.
  • ..///..///etc/passwd got worse (////etc/passwd), and </etc/passwd collapsed to a clean /etc/passwd once 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.

@neznaika0

Copy link
Copy Markdown
Contributor

Yes, this is not an ideal result.

I would prefer to introduce a new service class for working with paths — Path. It would provide static methods for all cases of path normalization and protection. It can be used to improve performance on Windows (currently there is confusion with slashes in paths).

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@neznaika0 neznaika0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: sanitize_filename() keeps leading slashes in relative mode

4 participants