Skip to content

Commit 9cd4eee

Browse files
committed
Disallow uploading of potentially unsafe file extensions.
1 parent 3ff6ece commit 9cd4eee

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

inc/lib.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,18 @@ function sendString($content, $filename, $mimetype)
249249
exit;
250250
}
251251

252+
/**
253+
* Gets the file extension from a filename
254+
*
255+
* @param string $filename The filename
256+
* @return string The extension of the file
257+
*/
258+
function getExtensionFromFileName($filename)
259+
{
260+
$tmp = explode('.', $filename);
261+
return end($tmp);
262+
}
263+
252264
/**
253265
* Upload a file (from "<input type="file">) to a directory on the server
254266
*
@@ -266,13 +278,15 @@ function sendString($content, $filename, $mimetype)
266278
*/
267279
function uploadFile($file_array, $destination_directory, $destination_filename = null)
268280
{
281+
$unsafe_extensions = ['php', 'phtml', 'php3', 'ph3', 'php4', 'ph4', 'php5', 'ph5', 'phtm', 'sh', 'asp', 'cgi', 'py', 'pl', 'exe', 'aspx'];
282+
269283
if ((! isset($file_array['name'])) || (! isset($file_array['tmp_name'])) || (! isset($file_array['error']))) {
270284
throw new Exception(_('Ungültiges Array übergeben!'));
271285
}
272286

273-
//Dont allow to upload a PHP file.
274-
if(strpos($file_array['name'], ".php") != false
275-
|| strpos($destination_filename, ".php") != false)
287+
//Dont allow upload of files with potentially dangerous extension
288+
if (in_array(getExtensionFromFileName($file_array['name']), $unsafe_extensions)
289+
|| in_array(getExtensionFromFileName($destination_filename), $unsafe_extensions))
276290
{
277291
throw new \Exception(_("Es ist nicht erlaubt PHP Dateien hochzuladen!"));
278292
}

0 commit comments

Comments
 (0)