Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

Commit

Permalink
SECURITY FIX: Only allow image file types by default.
Browse files Browse the repository at this point in the history
This moves the image file types limit in the library file.
This also adds a default setting to replace dots in filenames.
  • Loading branch information
blueimp committed Oct 23, 2018
1 parent 3e82856 commit ad4aefd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
31 changes: 29 additions & 2 deletions server/php/UploadHandler.php
Expand Up @@ -89,8 +89,25 @@ public function __construct($options = null, $initialize = true, $error_messages
'readfile_chunk_size' => 10 * 1024 * 1024, // 10 MiB
// Defines which files can be displayed inline when downloaded:
'inline_file_types' => '/\.(gif|jpe?g|png)$/i',
// Defines which files (based on their names) are accepted for upload:
'accept_file_types' => '/.+$/i',
// Defines which files (based on their names) are accepted for upload.
// By default, only allows file uploads with image file extensions.
// Only change this setting after making sure that any allowed file
// types cannot be executed by the webserver in the files directory,
// e.g. PHP scripts, nor executed by the browser when downloaded,
// e.g. HTML files with embedded JavaScript code.
// Please also read the SECURITY.md document in this repository.
'accept_file_types' => '/\.(gif|jpe?g|png)$/i',
// Replaces dots in filenames with the given string.
// Can be disabled by setting it to false or an empty string.
// Note that this is a security feature for servers that support
// multiple file extensions, e.g. the Apache AddHandler Directive:
// https://httpd.apache.org/docs/current/mod/mod_mime.html#addhandler
// Before disabling it, make sure that files uploaded with multiple
// extensions cannot be executed by the webserver, e.g.
// "example.php.png" with embedded PHP code, nor executed by the
// browser when downloaded, e.g. "example.html.gif" with embedded
// JavaScript code.
'replace_dots_in_filenames' => '-',
// The php.ini settings upload_max_filesize and post_max_size
// take precedence over the following max_file_size setting:
'max_file_size' => null,
Expand Down Expand Up @@ -527,6 +544,16 @@ protected function trim_file_name($file_path, $name, $size, $type, $error,
// into different directories or replacing hidden system files.
// Also remove control characters and spaces (\x00..\x20) around the filename:
$name = trim($this->basename(stripslashes($name)), ".\x00..\x20");
// Replace dots in filenames to avoid security issues with servers
// that interpret multiple file extensions, e.g. "example.php.png":
$replacement = $this->options['replace_dots_in_filenames'];
if (!empty($replacement)) {
$parts = explode('.', $name);
if (count($parts) > 2) {
$ext = array_pop($parts);
$name = implode($replacement, $parts).'.'.$ext;
}
}
// Use a timestamp for empty filenames:
if (!$name) {
$name = str_replace('.', '-', microtime(true));
Expand Down
15 changes: 1 addition & 14 deletions server/php/index.php
Expand Up @@ -12,17 +12,4 @@

error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');
$upload_handler = new UploadHandler(array(

// SECURITY NOTICE:
// Only change the accept_file_types setting after making sure that any
// allowed file types cannot be executed by the webserver in the files
// directory (e.g. PHP scripts), nor executed by the browser when downloaded
// (e.g. HTML files with embedded JavaScript code).
// e.g. in Apache, make sure the provided .htaccess file is present in the
// files directory and .htaccess support has been enabled:
// https://httpd.apache.org/docs/current/howto/htaccess.html

// By default, only allow file uploads with image file extensions:
'accept_file_types' => '/\.(gif|jpe?g|png)$/i'
));
$upload_handler = new UploadHandler();

0 comments on commit ad4aefd

Please sign in to comment.