Skip to content

Commit ad4aefd

Browse files
committed
SECURITY FIX: Only allow image file types by default.
This moves the image file types limit in the library file. This also adds a default setting to replace dots in filenames.
1 parent 3e82856 commit ad4aefd

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

server/php/UploadHandler.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,25 @@ public function __construct($options = null, $initialize = true, $error_messages
8989
'readfile_chunk_size' => 10 * 1024 * 1024, // 10 MiB
9090
// Defines which files can be displayed inline when downloaded:
9191
'inline_file_types' => '/\.(gif|jpe?g|png)$/i',
92-
// Defines which files (based on their names) are accepted for upload:
93-
'accept_file_types' => '/.+$/i',
92+
// Defines which files (based on their names) are accepted for upload.
93+
// By default, only allows file uploads with image file extensions.
94+
// Only change this setting after making sure that any allowed file
95+
// types cannot be executed by the webserver in the files directory,
96+
// e.g. PHP scripts, nor executed by the browser when downloaded,
97+
// e.g. HTML files with embedded JavaScript code.
98+
// Please also read the SECURITY.md document in this repository.
99+
'accept_file_types' => '/\.(gif|jpe?g|png)$/i',
100+
// Replaces dots in filenames with the given string.
101+
// Can be disabled by setting it to false or an empty string.
102+
// Note that this is a security feature for servers that support
103+
// multiple file extensions, e.g. the Apache AddHandler Directive:
104+
// https://httpd.apache.org/docs/current/mod/mod_mime.html#addhandler
105+
// Before disabling it, make sure that files uploaded with multiple
106+
// extensions cannot be executed by the webserver, e.g.
107+
// "example.php.png" with embedded PHP code, nor executed by the
108+
// browser when downloaded, e.g. "example.html.gif" with embedded
109+
// JavaScript code.
110+
'replace_dots_in_filenames' => '-',
94111
// The php.ini settings upload_max_filesize and post_max_size
95112
// take precedence over the following max_file_size setting:
96113
'max_file_size' => null,
@@ -527,6 +544,16 @@ protected function trim_file_name($file_path, $name, $size, $type, $error,
527544
// into different directories or replacing hidden system files.
528545
// Also remove control characters and spaces (\x00..\x20) around the filename:
529546
$name = trim($this->basename(stripslashes($name)), ".\x00..\x20");
547+
// Replace dots in filenames to avoid security issues with servers
548+
// that interpret multiple file extensions, e.g. "example.php.png":
549+
$replacement = $this->options['replace_dots_in_filenames'];
550+
if (!empty($replacement)) {
551+
$parts = explode('.', $name);
552+
if (count($parts) > 2) {
553+
$ext = array_pop($parts);
554+
$name = implode($replacement, $parts).'.'.$ext;
555+
}
556+
}
530557
// Use a timestamp for empty filenames:
531558
if (!$name) {
532559
$name = str_replace('.', '-', microtime(true));

server/php/index.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,4 @@
1212

1313
error_reporting(E_ALL | E_STRICT);
1414
require('UploadHandler.php');
15-
$upload_handler = new UploadHandler(array(
16-
17-
// SECURITY NOTICE:
18-
// Only change the accept_file_types setting after making sure that any
19-
// allowed file types cannot be executed by the webserver in the files
20-
// directory (e.g. PHP scripts), nor executed by the browser when downloaded
21-
// (e.g. HTML files with embedded JavaScript code).
22-
// e.g. in Apache, make sure the provided .htaccess file is present in the
23-
// files directory and .htaccess support has been enabled:
24-
// https://httpd.apache.org/docs/current/howto/htaccess.html
25-
26-
// By default, only allow file uploads with image file extensions:
27-
'accept_file_types' => '/\.(gif|jpe?g|png)$/i'
28-
));
15+
$upload_handler = new UploadHandler();

0 commit comments

Comments
 (0)