From f9cfc8fb1fb34586b6d10c6ea1d47b325b8ab4f4 Mon Sep 17 00:00:00 2001 From: fenos Date: Tue, 14 Jan 2025 13:11:52 +0100 Subject: [PATCH] fix: disable content type parsers on upload --- src/http/routes/s3/index.ts | 9 +++++++++ src/test/s3-protocol.test.ts | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/http/routes/s3/index.ts b/src/http/routes/s3/index.ts index 6be0a5fe..65e15cc3 100644 --- a/src/http/routes/s3/index.ts +++ b/src/http/routes/s3/index.ts @@ -117,6 +117,15 @@ export default async function routes(fastify: FastifyInstance) { (route) => route.disableContentTypeParser ) + if (disableContentParser) { + localFastify.addContentTypeParser( + ['application/json', 'text/plain', 'application/xml'], + function (request, payload, done) { + done(null) + } + ) + } + localFastify.register(fastifyMultipart, { limits: { fields: 20, diff --git a/src/test/s3-protocol.test.ts b/src/test/s3-protocol.test.ts index 1a889384..221b54ad 100644 --- a/src/test/s3-protocol.test.ts +++ b/src/test/s3-protocol.test.ts @@ -562,6 +562,20 @@ describe('S3 Protocol', () => { expect(resp.$metadata.httpStatusCode).toEqual(200) }) + it('upload a broken JSON body using putObject ', async () => { + const bucketName = await createBucket(client) + + const putObject = new PutObjectCommand({ + Bucket: bucketName, + Key: 'test-1-put-object.jpg', + ContentType: 'application/json', + Body: '{"hello": "world"', // (no-closing tag) + }) + + const resp = await client.send(putObject) + expect(resp.$metadata.httpStatusCode).toEqual(200) + }) + it('upload a file using putObject with custom metadata', async () => { const bucketName = await createBucket(client)