Unrestricted file upload to RCE in Drupal core (<7.74, <8.8.11, <8.9.9, <9.0.8). Drupal never munges a bare dangerous extension, so shell.php survives as shell.php.txt. Anonymous registration and authenticated editor vectors.
# CVE-2020-13671 - Drupal unsanitised filename upload
Drupal core does not munge a bare dangerous extension on an uploaded file, so a
file called `shell.php` is stored with `.php` still in its name. On a web
server that maps `.php` positionally, that file then executes.
Affects Drupal 7 < 7.74, 8.8 < 8.8.11, 8.9 < 8.9.9, and 9.0 < 9.0.8.
## The bug
`file_munge_filename()` defends against double extensions by underscoring any
*middle* extension that is not in the field's allowed list: `shell.php.gif`
becomes `shell.php_.gif`. Two gaps in the pre-fix code let a filename through
with a live `.php`:
- The munge only looks at middle parts. It splits on `.`, drops the basename
and the final extension, and inspects what is left. A single-extension name
like `shell.php` has nothing in the middle, so it is never touched.
- The munge was only called at all when the field restricted extensions
(`if (!empty($extensions))`).
A bare `shell.php` therefore reaches the second defence, the "rename potentially
executable files" block, unchanged. That block does this (Drupal 8.9.8,
`core/modules/file/file.module`):
```php
if (... preg_match(FILE_INSECURE_EXTENSION_REGEX, $file->getFilename()) && substr(..., -4) != '.txt') {
$file->setMimeType('text/plain');
$file->setFilename($file->getFilename() . '.txt'); // shell.php -> shell.php.txt
if (!empty($extensions)) {
$validators['file_validate_extensions'][0] .= ' txt'; // adds txt to the allowed list
```
It appends `.txt` and then **adds `txt` to the field's own allowed list**, so
`file_validate_extensions()` accepts `shell.php.txt`. The `.php` is left intact
in the middle. The regex it keys off is narrow and end-or-dot anchored:
```php
define('FILE_INSECURE_EXTENSION_REGEX', '/\.(phar|php|pl|py|cgi|asp|js)(\.|$)/i');
```
The 8.9.9 / 8.8.11 / 9.0.8 / 7.74 fix (commits by xjm, 2020-11-17) does three
things: it removes the unsafe extensions from the allowed list with
`array_diff($allowed, explode('|', 'phar|php