Security: Avoid relative path traversal to execute ppt2png and escape shell command used to execute PPT converter

preprodparkur
Angel Fernando Quiroz Campos 2 years ago committed by NicoDucou
parent 72d8241e60
commit dff68e4afb
  1. 34
      main/webservices/additional_webservices.php

@ -28,18 +28,26 @@ function wsConvertPpt($pptData)
} }
$fileData = $pptData['file_data']; $fileData = $pptData['file_data'];
// Clean filename to avoid hacks. Prevents "&" and ";" to be used in filename, notably // Clean filename to avoid hacks. Prevents "&" and ";" to be used in filename, notably
$sanitizedFileName = Security::sanitizeExecParam($pptData['file_name']);
if (strpos($pptData['file_name'], '..') !== false) {
return false;
}
$sanitizedFileName = $pptData['file_name'];
$dataInfo = pathinfo($sanitizedFileName); $dataInfo = pathinfo($sanitizedFileName);
$fileName = basename($sanitizedFileName, '.'.$dataInfo['extension']); $fileName = basename($sanitizedFileName, '.'.$dataInfo['extension']);
// Add additional cleaning of .php and .htaccess files // Add additional cleaning of .php and .htaccess files
$fullFileName = Security::filter_filename($sanitizedFileName); $fullFileName = Security::filter_filename($sanitizedFileName);
$size = Security::sanitizeExecParam($pptData['service_ppt2lp_size']); $size = $pptData['service_ppt2lp_size'];
$w = '800'; $w = '800';
$h = '600'; $h = '600';
if (!empty($size)) { if (!empty($size)) {
list($w, $h) = explode('x', $size); list($w, $h) = explode('x', $size);
} }
$w = (int) $w;
$h = (int) $h;
$tempArchivePath = api_get_path(SYS_ARCHIVE_PATH); $tempArchivePath = api_get_path(SYS_ARCHIVE_PATH);
$tempPath = $tempArchivePath.'wsConvert/'.$fileName.'/'; $tempPath = $tempArchivePath.'wsConvert/'.$fileName.'/';
$tempPathNewFiles = $tempArchivePath.'wsConvert/'.$fileName.'-n/'; $tempPathNewFiles = $tempArchivePath.'wsConvert/'.$fileName.'-n/';
@ -54,8 +62,12 @@ function wsConvertPpt($pptData)
$file = base64_decode($fileData); $file = base64_decode($fileData);
file_put_contents($tempPath.$fullFileName, $file); file_put_contents($tempPath.$fullFileName, $file);
$cmd = pptConverterGetCommandBaseParams(); $cmd = pptConverterGetCommandBaseParams(
$cmd .= ' -w '.$w.' -h '.$h.' -d oogie "'.$tempPath.$fullFileName.'" "'.$tempPathNewFiles.$fileName.'.html"'; $w,
$h,
$tempPath.$fullFileName,
$tempPathNewFiles.$fileName.'.html'
);
//$perms = api_get_permissions_for_new_files(); //$perms = api_get_permissions_for_new_files();
chmod($tempPathNewFiles.$fileName, $perms); chmod($tempPathNewFiles.$fileName, $perms);
@ -135,21 +147,27 @@ function pptConverterDirectoriesCreate($tempPath, $tempPathNewFiles, $fileName,
* *
* @return string $cmd * @return string $cmd
*/ */
function pptConverterGetCommandBaseParams() function pptConverterGetCommandBaseParams(int $w, int $h, string $inputPath, string $outputPath)
{ {
$cd = '';
if (IS_WINDOWS_OS) { // IS_WINDOWS_OS has been defined in main_api.lib.php if (IS_WINDOWS_OS) { // IS_WINDOWS_OS has been defined in main_api.lib.php
$converterPath = str_replace('/', '\\', api_get_path(SYS_PATH).'main/inc/lib/ppt2png'); $converterPath = str_replace('/', '\\', api_get_path(SYS_PATH).'main/inc/lib/ppt2png');
$classPath = $converterPath.';'.$converterPath.'/jodconverter-2.2.2.jar;'.$converterPath.'/jodconverter-cli-2.2.2.jar'; $classPath = $converterPath.';'.$converterPath.'/jodconverter-2.2.2.jar;'.$converterPath.'/jodconverter-cli-2.2.2.jar';
$cmd = 'java -Dfile.encoding=UTF-8 -cp "'.$classPath.'" DokeosConverter'; $cmd = 'java -Dfile.encoding=UTF-8 -cp "'.$classPath.'"';
} else { } else {
$converterPath = api_get_path(SYS_PATH).'main/inc/lib/ppt2png'; $converterPath = api_get_path(SYS_PATH).'main/inc/lib/ppt2png';
$classPath = ' -Dfile.encoding=UTF-8 -cp .:jodconverter-2.2.2.jar:jodconverter-cli-2.2.2.jar'; $classPath = ' -Dfile.encoding=UTF-8 -cp .:jodconverter-2.2.2.jar:jodconverter-cli-2.2.2.jar';
$cmd = 'cd '.$converterPath.' && java '.$classPath.' DokeosConverter'; $cd = 'cd '.$converterPath.' && ';
$cmd = 'java '.$classPath;
} }
$cmd .= ' DokeosConverter';
$cmd .= ' -p '.api_get_setting('service_ppt2lp', 'port'); $cmd .= ' -p '.api_get_setting('service_ppt2lp', 'port');
$cmd .= ' -w '.$w.' -h '.$h;
$cmd .= ' -d oogie '.Security::sanitizeExecParam($inputPath).' '.Security::sanitizeExecParam($outputPath);
return $cmd; return $cd.escapeshellcmd($cmd);
} }
$webPath = api_get_path(WEB_PATH); $webPath = api_get_path(WEB_PATH);

Loading…
Cancel
Save