@@ -89,8 +89,25 @@ public function __construct($options = null, $initialize = true, $error_messages
89
89
'readfile_chunk_size ' => 10 * 1024 * 1024 , // 10 MiB
90
90
// Defines which files can be displayed inline when downloaded:
91
91
'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 ' => '- ' ,
94
111
// The php.ini settings upload_max_filesize and post_max_size
95
112
// take precedence over the following max_file_size setting:
96
113
'max_file_size ' => null ,
@@ -527,6 +544,16 @@ protected function trim_file_name($file_path, $name, $size, $type, $error,
527
544
// into different directories or replacing hidden system files.
528
545
// Also remove control characters and spaces (\x00..\x20) around the filename:
529
546
$ 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
+ }
530
557
// Use a timestamp for empty filenames:
531
558
if (!$ name ) {
532
559
$ name = str_replace ('. ' , '- ' , microtime (true ));
0 commit comments