From c77d9f5abf021f29fa96b5720b7b84adbd199092 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Mon, 29 Jan 2024 10:20:41 +0100 Subject: [PATCH 1/4] Cleanup uploaded files for PSR-15 handlers --- src/Event/Http/Psr15Handler.php | 2 ++ src/Event/Http/Psr7Bridge.php | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Event/Http/Psr15Handler.php b/src/Event/Http/Psr15Handler.php index 79ea99e1e..b263ef928 100644 --- a/src/Event/Http/Psr15Handler.php +++ b/src/Event/Http/Psr15Handler.php @@ -16,6 +16,8 @@ public function __construct(RequestHandlerInterface $psr15Handler) public function handleRequest(HttpRequestEvent $event, Context $context): HttpResponse { + Psr7Bridge::cleanupUploadedFiles(); + $request = Psr7Bridge::convertRequest($event, $context); $response = $this->psr15Handler->handle($request); diff --git a/src/Event/Http/Psr7Bridge.php b/src/Event/Http/Psr7Bridge.php index 2db1406d0..05532bd03 100644 --- a/src/Event/Http/Psr7Bridge.php +++ b/src/Event/Http/Psr7Bridge.php @@ -18,6 +18,8 @@ */ final class Psr7Bridge { + private const UPLOADED_FILES_PREFIX = 'bref_upload_'; + /** * Create a PSR-7 server request from an AWS Lambda HTTP event. */ @@ -106,7 +108,7 @@ private static function parseBodyAndUploadedFiles(HttpRequestEvent $event): arra $parsedBody = []; foreach ($document->getParts() as $part) { if ($part->isFile()) { - $tmpPath = tempnam(sys_get_temp_dir(), 'bref_upload_'); + $tmpPath = tempnam(sys_get_temp_dir(), self::UPLOADED_FILES_PREFIX); if ($tmpPath === false) { throw new RuntimeException('Unable to create a temporary directory'); } @@ -166,4 +168,19 @@ private static function parseKeyAndInsertValueInArray(array &$array, string $key $pointer = $value; } + + /** + * Cleanup previously uploaded files. + */ + public static function cleanupUploadedFiles(): void + { + $tmpFiles = glob(sys_get_temp_dir() . '/' . self::UPLOADED_FILES_PREFIX . '*'); + if ($tmpFiles !== false) { + foreach ($tmpFiles as $file) { + if(is_file($file)) { + unlink($file); + } + } + } + } } From f789bc75b21975220c8ef92c63d2c5dd19c2c278 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Mon, 29 Jan 2024 10:57:23 +0100 Subject: [PATCH 2/4] Fix CS --- src/Event/Http/Psr7Bridge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Event/Http/Psr7Bridge.php b/src/Event/Http/Psr7Bridge.php index 05532bd03..0820845e3 100644 --- a/src/Event/Http/Psr7Bridge.php +++ b/src/Event/Http/Psr7Bridge.php @@ -177,7 +177,7 @@ public static function cleanupUploadedFiles(): void $tmpFiles = glob(sys_get_temp_dir() . '/' . self::UPLOADED_FILES_PREFIX . '*'); if ($tmpFiles !== false) { foreach ($tmpFiles as $file) { - if(is_file($file)) { + if (is_file($file)) { unlink($file); } } From ca4ac8a0dd8f89d0ad0eaa441e7af6a4ef4a61d9 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Wed, 31 Jan 2024 11:03:01 +0100 Subject: [PATCH 3/4] More precise glob pattern Recommended by @smaury in https://github.com/brefphp/bref/commit/c77d9f5abf021f29fa96b5720b7b84adbd199092#r137983026 --- src/Event/Http/Psr7Bridge.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Event/Http/Psr7Bridge.php b/src/Event/Http/Psr7Bridge.php index 0820845e3..da2a63c33 100644 --- a/src/Event/Http/Psr7Bridge.php +++ b/src/Event/Http/Psr7Bridge.php @@ -174,7 +174,9 @@ private static function parseKeyAndInsertValueInArray(array &$array, string $key */ public static function cleanupUploadedFiles(): void { - $tmpFiles = glob(sys_get_temp_dir() . '/' . self::UPLOADED_FILES_PREFIX . '*'); + // See https://github.com/brefphp/bref/commit/c77d9f5abf021f29fa96b5720b7b84adbd199092#r137983026 + $tmpFiles = glob(sys_get_temp_dir() . '/' . self::UPLOADED_FILES_PREFIX . '[A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9][A-Za-z0-9]'); + if ($tmpFiles !== false) { foreach ($tmpFiles as $file) { if (is_file($file)) { From 23f817432dd646321b81d754a05e744f7814d2f3 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Wed, 31 Jan 2024 12:39:06 +0100 Subject: [PATCH 4/4] Ignore warnings --- src/Event/Http/Psr7Bridge.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Event/Http/Psr7Bridge.php b/src/Event/Http/Psr7Bridge.php index da2a63c33..667282016 100644 --- a/src/Event/Http/Psr7Bridge.php +++ b/src/Event/Http/Psr7Bridge.php @@ -180,7 +180,8 @@ public static function cleanupUploadedFiles(): void if ($tmpFiles !== false) { foreach ($tmpFiles as $file) { if (is_file($file)) { - unlink($file); + // Silence warnings, we don't want to crash the whole runtime + @unlink($file); } } }