prevent directory traversal in paths

This commit is contained in:
Hanno Heinrichs
2016-01-28 22:40:54 +01:00
parent 7f82549e23
commit 6eeaf66e2c

View File

@@ -26,15 +26,21 @@
*/
function makeSecurePath($path) {
// check for bad characters, some are allowed with escaping
// but we generally don't want them in our directory-names,
// thx to aaronmueller for this snipped
$badchars = array(':', ';', '|', '&', '>', '<', '`', '$', '~', '?', "\0");
foreach ($badchars as $bc) {
$path = str_replace($bc, "", $path);
}
$search = array(
'#/+#',
'#\.+#',
'#\0+#'
'#\.+#'
);
$replace = array(
'/',
'.',
''
'.'
);
$path = preg_replace($search, $replace, $path);
// don't just replace a space with an escaped space
@@ -42,13 +48,5 @@ function makeSecurePath($path) {
$path = str_replace("\ ", " ", $path);
$path = str_replace(" ", "\ ", $path);
// check for bad characters, some are allowed with escaping
// but we generally don't want them in our directory-names,
// thx to aaronmueller for this snipped
$badchars = array(':', ';', '|', '&', '>', '<', '`', '$', '~', '?');
foreach ($badchars as $bc) {
$path = str_replace($bc, "", $path);
}
return $path;
}