Skip to content

Bug: sanitize_filename() keeps leading slashes in relative mode #10815

Open

Description

@BL4CK570RM

PHP Version

8.4

CodeIgniter4 Version

4.7.7-dev (commit 08039be). I also checked 4.7.6 and the same code is there.

CodeIgniter4 Installation Method

Git

Which operating systems have you tested for this bug?

Linux

Which server did you use?

cli

Environment

development

Database

Not required for the CLI reproduction.

What happened?

With $relativePath = true, sanitize_filename() removes ../ but deliberately keeps /. A payload can collapse into a leading-slash path:

sanitize_filename('..//..//etc/passwd', true); // "//etc/passwd"
sanitize_filename('..///..///etc/passwd', true); // "////etc/passwd"
sanitize_filename('/etc/passwd', true); // "/etc/passwd"

The function is a sanitizer, not a path canonicalizer, so maybe callers are expected to check the result. I couldn't find a framework caller using relative mode in a sensitive sink, so I'm reporting it as a hardening issue rather than a live traversal. Still, the output is easy to misuse.

Steps to Reproduce

I left a runnable script at bug-reports/poc/f04-sanitize-filename-relative-path.php. It loads the real helper and includes a copy of remove_invisible_characters(), so no Composer install is required.

From the project root:

php bug-reports/poc/f04-sanitize-filename-relative-path.php

The output for relative mode:

in: '..//..//etc/passwd' out: '//etc/passwd'
in: '..///..///etc/passwd' out: '////etc/passwd'
in: '/etc/passwd' out: '/etc/passwd'
in: 'normal/file.txt' out: 'normal/file.txt'

The default mode ($relativePath = false) removes the slashes and returns etcpasswd.

Expected Output

For relative mode, I'd expect something that can't be read as an absolute path:

in: '..//..//etc/passwd' out: 'etc/passwd'
in: '..///..///etc/passwd' out: 'etc/passwd'
in: '/etc/passwd' out: 'etc/passwd'
in: 'normal/file.txt' out: 'normal/file.txt'

The exact normalization is a design choice, but leading separators and .. segments shouldn't survive.

Anything else?

After the removal loop, normalize the relative path:

if ($relativePath) {
$filename = preg_replace('#/+#', '/', $filename);
$filename = ltrim($filename, '/\');

$segments = array_filter(
    explode('/', $filename),
    static fn (string $segment): bool => $segment !== '' && $segment !== '.' && $segment !== '..',
);

$filename = implode('/', $segments);

}

A containment check at the sink is still a good idea:

$base = realpath('writable/uploads');
$path = realpath($base . '/' . $name);

if ($path === false || ! str_starts_with($path, $base . DIRECTORY_SEPARATOR)) {
throw new RuntimeException('Invalid path');
}

If changing the output is too much for a patch release, document that relative mode is not a containment guarantee and that callers must normalize and check the final path. A test with the payloads above would cover the regression.

Activity

  1. added
    bug
    on Nov 23, 2026
  2. added a commit that references this issue on Nov 23, 2026
    a3f439f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions