diff --git a/htdocs/admin/tools/export_files.php b/htdocs/admin/tools/export_files.php index 604f4d5d9c7..4b373635528 100644 --- a/htdocs/admin/tools/export_files.php +++ b/htdocs/admin/tools/export_files.php @@ -51,7 +51,7 @@ $export_type = GETPOST('export_type', 'alpha'); $file = trim(GETPOST('zipfilename_template', 'alpha')); $compression = GETPOST('compression', 'aZ09'); -$file = dol_sanitizeFileName($file); +$file = dol_sanitizeFileName($file, '_', 1, 1); $file = preg_replace('/(\.zip|\.tar|\.tgz|\.gz|\.tar\.gz|\.bz2|\.zst)$/i', '', $file); $sortfield = GETPOST('sortfield', 'aZ09comma'); @@ -178,7 +178,7 @@ if ($compression == 'zip') { } elseif (in_array($compression, array('gz', 'bz', 'zstd'))) { $userlogin = ($user->login ? $user->login : 'unknown'); - $outputfile = $conf->admin->dir_temp.'/export_files.'.$userlogin.'.out'; // File used with popen method + $outputfile = $conf->admin->dir_temp.'/'.dol_sanitizeFileName('export_files.'.$userlogin.'.out'); // File used with popen method $file .= '.tar'; diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 4adcca1abf6..0e56d6a26d2 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -1636,22 +1636,27 @@ function dol_size($size, $type = '') * @param string $str String to clean * @param string $newstr String to replace bad chars with. * @param int $unaccent 1=Remove also accent (default), 0 do not remove them + * @param int $includequotes 1=Include simple quotes (double is already included by default) * @return string String cleaned * * @see dol_string_nospecial(), dol_string_unaccent(), dol_sanitizePathName() */ -function dol_sanitizeFileName($str, $newstr = '_', $unaccent = 1) +function dol_sanitizeFileName($str, $newstr = '_', $unaccent = 1, $includequotes = 0) { // List of special chars for filenames in windows are defined on page https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file // Char '>' '<' '|' '$' and ';' are special chars for shells. // Char '/' and '\' are file delimiters. // Chars '--' can be used into filename to inject special parameters like --use-compress-program to make command with file as parameter making remote execution of command $filesystem_forbidden_chars = array('<', '>', '/', '\\', '?', '*', '|', '"', ':', '°', '$', ';', '`'); + if ($includequotes) { + $filesystem_forbidden_chars[] = "'"; + } $tmp = dol_string_nospecial($unaccent ? dol_string_unaccent($str) : $str, $newstr, $filesystem_forbidden_chars); $tmp = preg_replace('/\-\-+/', '_', $tmp); $tmp = preg_replace('/\s+\-([^\s])/', ' _$1', $tmp); $tmp = preg_replace('/\s+\-$/', '', $tmp); $tmp = str_replace('..', '', $tmp); + return $tmp; }