Skip to content

Commit

Permalink
Fixing filename sanitizer in Autoloader to allow Windows filepaths th…
Browse files Browse the repository at this point in the history
…rough. Fixes #12
  • Loading branch information
lonnieezell committed Mar 18, 2016
1 parent eac6a0c commit c7bb307
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 2 additions & 3 deletions system/Autoloader/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,6 @@ protected function requireFile($file)
* dashes with a single dash. Trim period, dash and underscore from beginning
* and end of filename.
*
* @todo Move to a helper?
*
* @param string $filename
*
* @return string The sanitized filename
Expand All @@ -358,7 +356,8 @@ public function sanitizeFilename(string $filename): string
// Plus the forward slash for directory separators since this might
// be a path.
// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_278
$filename = preg_replace('/[^a-zA-Z0-9\s\/\-\_\.]/', '', $filename);
// Modified to allow backslash and colons for on Windows machines.
$filename = preg_replace('/[^a-zA-Z0-9\s\/\-\_\.\:\\\\]/', '', $filename);

// Clean up our filename edges.
$filename = trim($filename, '.-_');
Expand Down
9 changes: 8 additions & 1 deletion tests/system/Autoloader/AutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,20 @@ public function testLoadLegacy()

public function testSanitizationSimply()
{
$test = '${../path}!#:/to/some/file.php_';
$test = '${../path}!#/to/some/file.php_';
$expected = '/path/to/some/file.php';

$this->assertEquals($expected, $this->loader->sanitizeFilename($test));
}

//--------------------------------------------------------------------

public function testSanitizationAllowsWindowsFilepaths()
{
$test = 'C:\path\to\some/file.php';

$this->assertEquals($test, $this->loader->sanitizeFilename($test));
}

//--------------------------------------------------------------------
}

0 comments on commit c7bb307

Please sign in to comment.