From 26040f04ade699334c7ace67287c544c8128fdfa Mon Sep 17 00:00:00 2001 From: Prowler Bot Date: Mon, 5 May 2025 16:23:33 +0200 Subject: [PATCH] fix(s3): add ContentType in upload_file (#7643) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Pedro Martín --- prowler/CHANGELOG.md | 1 + prowler/providers/aws/lib/s3/s3.py | 14 +++++++++++++- tests/providers/aws/lib/s3/s3_test.py | 10 +++++----- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/prowler/CHANGELOG.md b/prowler/CHANGELOG.md index 3d246298a1..001c5e2c9a 100644 --- a/prowler/CHANGELOG.md +++ b/prowler/CHANGELOG.md @@ -19,6 +19,7 @@ All notable changes to the **Prowler SDK** are documented in this file. - Improve compliance and dashboard [(#7596)](https://github.com/prowler-cloud/prowler/pull/7596) - Remove invalid parameter `create_file_descriptor` [(#7600)](https://github.com/prowler-cloud/prowler/pull/7600) - Remove first empty line in HTML output [(#7606)](https://github.com/prowler-cloud/prowler/pull/7606) +- Ensure that ContentType in upload_file matches the uploaded file’s format [(#7635)](https://github.com/prowler-cloud/prowler/pull/7635) --- diff --git a/prowler/providers/aws/lib/s3/s3.py b/prowler/providers/aws/lib/s3/s3.py index f399171c98..03308224fe 100644 --- a/prowler/providers/aws/lib/s3/s3.py +++ b/prowler/providers/aws/lib/s3/s3.py @@ -105,6 +105,12 @@ class S3: """ try: uploaded_objects = {"success": {}, "failure": {}} + extension_to_content_type = { + ".html": "text/html", + ".csv": "text/csv", + ".ocsf.json": "application/json", + ".asff.json": "application/json", + } # Keys are regular and/or compliance for key, output_list in outputs.items(): for output in output_list: @@ -115,6 +121,7 @@ class S3: bucket_directory = self.get_object_path(self._output_directory) basename = path.basename(output.file_descriptor.name) + file_extension = output.file_extension if key == "compliance": object_name = f"{bucket_directory}/{key}/{basename}" @@ -128,7 +135,12 @@ class S3: # into the local filesystem because S3 upload file is the recommended way. # https://aws.amazon.com/blogs/developer/uploading-files-to-amazon-s3/ self._session.upload_file( - output.file_descriptor.name, self._bucket_name, object_name + Filename=output.file_descriptor.name, + Bucket=self._bucket_name, + Key=object_name, + ExtraArgs={ + "ContentType": extension_to_content_type[file_extension] + }, ) if output.file_extension in uploaded_objects["success"]: diff --git a/tests/providers/aws/lib/s3/s3_test.py b/tests/providers/aws/lib/s3/s3_test.py index 2cede7364d..34c906d69d 100644 --- a/tests/providers/aws/lib/s3/s3_test.py +++ b/tests/providers/aws/lib/s3/s3_test.py @@ -93,7 +93,7 @@ class TestS3: Bucket=S3_BUCKET_NAME, Key=uploaded_object_name, )["ContentType"] - == "binary/octet-stream" + == "text/csv" ) @mock_aws @@ -132,7 +132,7 @@ class TestS3: Bucket=S3_BUCKET_NAME, Key=uploaded_object_name, )["ContentType"] - == "binary/octet-stream" + == "text/csv" ) remove(f"{CURRENT_DIRECTORY}/{csv_file}") @@ -171,7 +171,7 @@ class TestS3: Bucket=S3_BUCKET_NAME, Key=uploaded_object_name, )["ContentType"] - == "binary/octet-stream" + == "application/json" ) @mock_aws @@ -209,7 +209,7 @@ class TestS3: Bucket=S3_BUCKET_NAME, Key=uploaded_object_name, )["ContentType"] - == "binary/octet-stream" + == "text/html" ) @mock_aws @@ -290,7 +290,7 @@ class TestS3: Bucket=S3_BUCKET_NAME, Key=uploaded_object_name, )["ContentType"] - == "binary/octet-stream" + == "text/csv" ) def test_get_get_object_path_with_prowler(self):