Skip to content

fix: sanitize client-provided filename in UploadedFile::move() to prevent path traversal#10282

Open
gr8man wants to merge 4 commits into
codeigniter4:developfrom
gr8man:fix/uploadedfile-path-traversal
Open

fix: sanitize client-provided filename in UploadedFile::move() to prevent path traversal#10282
gr8man wants to merge 4 commits into
codeigniter4:developfrom
gr8man:fix/uploadedfile-path-traversal

Conversation

@gr8man
Copy link
Copy Markdown

@gr8man gr8man commented Jun 6, 2026

Description
Currently, when developers call $file->move($targetDir) without explicitly providing the $name parameter, the system defaults to $this->getName(), which returns the original, unverified filename provided by the client in the $_FILES array. This creates a severe Path Traversal (Arbitrary File Write) vulnerability. If a malicious client uploads a file named ../../public/shell.php, the built-in move_uploaded_file() function will resolve the path and successfully write the script outside the intended upload directory, leading to potential Remote Code Execution (RCE).

This PR mitigates the vulnerability by applying the sanitize_filename() helper to the client-provided $name fallback in UploadedFile::move().
Crucially, the sanitization is applied only when the developer does not explicitly pass the $name argument. This ensures full backward compatibility for developers who manually and intentionally specify valid relative path structures (e.g., $file->move($dir, 'subdir/file.jpg')).

A new unit test (testMovePathTraversal) was added to FileMovingTest.php to ensure that path traversal attempts (like ../public/shell.php) are properly neutralized into safe filenames (like publicshell.php) and kept strictly within the intended destination directory.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated

@mergeable
Copy link
Copy Markdown

mergeable Bot commented Jun 6, 2026

Hi there, gr8man! 👋

Thank you for sending this PR!

We expect the following in all Pull Requests (PRs).

Important

We expect all code changes or bug-fixes to be accompanied by one or more tests added to our test suite to prove the code works.

If pull requests do not comply with the above, they will likely be closed. Since we are a team of volunteers, we don't have any more time to work
on the framework than you do. Please make it as painless for your contributions to be included as possible.

See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md

Sincerely, the mergeable bot 🤖

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant