CVE-2024-24752 (published 2024-02-01T16:17Z)

Bref enable serverless PHP on AWS Lambda. 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 flow mimics what plain PHP does but it does not delete the temporary files when the request has been processed. An attacker could fill the Lambda instance disk by performing multiple MultiPart requests containing files. This vulnerability is patched in 2.1.13.

CVSS v3 base score: 6.5, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:N/A:H)

Weakness information:
cwe_id: CWE-400
cwe_name: Uncontrolled Resource Consumption
description: The product does not properly control the allocation and maintenance of a limited resource, thereby enabling an actor to influence the amount of resources consumed, eventually leading to the exhaustion of available resources.
extended_description: 
            Limited resources include memory, file system storage, database connection pool entries, and CPU. If an attacker can trigger the allocation of these limited resources, but the number or size of the resources is not controlled, then the attacker could cause a denial of service that consumes all available resources. This would prevent valid users from accessing the product, and it could potentially have an impact on the surrounding environment. For example, a memory exhaustion attack against an application could slow down the application as well as its host operating system.
            There are at least three distinct scenarios which can commonly lead to resource exhaustion:
               
                  Lack of throttling for the number of allocated resources
                  Losing all references to a resource before reaching the shutdown stage
                  Not closing/returning a resource after processing
               
            Resource exhaustion problems are often result due to an incorrect implementation of the following situations:
               
                  Error conditions and other exceptional circumstances.
                  Confusion over which part of the program is responsible for releasing the resource.
               
         
         

Fix information:
repo_url: https://github.com/brefphp/bref
commit_details: repo_url: https://github.com/brefphp/bref
author: Matthieu Napoli
author_date: 2024-01-31 14:59:12+01:00
msg: Support malformed multipart body

For example body containing broken array keys like `key0[key1][key2][`
repository: repo_url: https://github.com/brefphp/bref
repo_name: brefphp/bref
description: Serverless PHP on AWS Lambda
repo_language: PHP
owner: brefphp
file_changes: filename: Psr7Bridge.php
change_type: ModificationType.MODIFY
diff: @@ -93,36 +93,49 @@ final class Psr7Bridge
         return new HttpResponse($body, $response->getHeaders(), $response->getStatusCode());
     }
 
+    /**
+     * @return array{0: array<string, UploadedFile>, 1: array<string, mixed>|null}
+     */
     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()) {
+        if ($contentType === null || $event->getMethod() !== 'POST') {
+            return [[], null];
+        }
+
+        if (str_starts_with($contentType, 'application/x-www-form-urlencoded')) {
+            $parsedBody = [];
+            parse_str($event->getBody(), $parsedBody);
+            return [[], $parsedBody];
+        }
+
+        // Parse the body as multipart/form-data
+        $document = new Part("Content-type: $contentType\r\n\r\n" . $event->getBody());
+        if (!$document->isMultiPart()) {
+            return [[], null];
+        }
+        $files = [];
+        $queryString = '';
+        foreach ($document->getParts() as $part) {
+            if ($part->isFile()) {
                             $tmpPath = tempnam(sys_get_temp_dir(), self::UPLOADED_FILES_PREFIX);
-                            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());
-                        }
-                    }
+                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 {
+                // Temporarily store as a query string so that we can use PHP's native parse_str function to parse keys
+                $queryString .= urlencode($part->getName()) . '=' . urlencode($part->getBody()) . '&';
             }
         }
+        if ($queryString !== '') {
+            $parsedBody = [];
+            parse_str($queryString, $parsedBody);
+        } else {
+            $parsedBody = null;
+        }
         return [$files, $parsedBody];
     }
 

programming_language: PHP
filename: CommonHttpTest.php
change_type: ModificationType.MODIFY
diff: @@ -401,7 +401,7 @@ Year,Make,Model
 --testBoundary--\r
 ";
         $this->assertBody($body);
-        $this->assertParsedBody([]);
+        $this->assertParsedBody(null);
         $this->assertUploadedFile(
             'foo',
             'lorem.txt',
@@ -554,7 +554,7 @@ Year,Make,Model
 
     abstract protected function assertHasMultiHeader(bool $expected): void;
 
-    abstract protected function assertParsedBody(array $expected): void;
+    abstract protected function assertParsedBody(array|null $expected): void;
 
     abstract protected function assertSourceIp(string $expected): void;
 

programming_language: PHP
filename: HttpRequestEventTest.php
change_type: ModificationType.MODIFY
diff: @@ -112,7 +112,7 @@ class HttpRequestEventTest extends CommonHttpTest
         $this->assertEquals($expected, $this->event->getSourceIp());
     }
 
-    protected function assertParsedBody(array $expected): void
+    protected function assertParsedBody(array|null $expected): void
     {
         // Not applicable here since the class doesn't parse the body
     }

programming_language: PHP
filename: Psr7BridgeTest.php
change_type: ModificationType.MODIFY
diff: @@ -123,7 +123,7 @@ class Psr7BridgeTest extends CommonHttpTest
         // Not applicable here
     }
 
-    protected function assertParsedBody(array $expected): void
+    protected function assertParsedBody(array|null $expected): void
     {
         $this->assertEquals($expected, $this->request->getParsedBody());
     }

programming_language: PHP