From b4ebb1a28dc87c297721299a635e836dcaa273b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Fri, 17 Nov 2023 10:07:44 +0100 Subject: [PATCH] fix(core): Account for non-ASCII chars in filename on binary data download (#7742) https://n8nio.sentry.io/issues/4641538638 --- .../src/controllers/binaryData.controller.ts | 5 +++-- .../test/integration/binaryData.api.test.ts | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/controllers/binaryData.controller.ts b/packages/cli/src/controllers/binaryData.controller.ts index 00f6a85942..505815bc14 100644 --- a/packages/cli/src/controllers/binaryData.controller.ts +++ b/packages/cli/src/controllers/binaryData.controller.ts @@ -41,8 +41,9 @@ export class BinaryDataController { if (mimeType) res.setHeader('Content-Type', mimeType); - if (action === 'download') { - res.setHeader('Content-Disposition', `attachment; filename="${fileName}"`); + if (action === 'download' && fileName) { + const encodedFilename = encodeURIComponent(fileName); + res.setHeader('Content-Disposition', `attachment; filename="${encodedFilename}"`); } return await this.binaryDataService.getAsStream(binaryDataId); diff --git a/packages/cli/test/integration/binaryData.api.test.ts b/packages/cli/test/integration/binaryData.api.test.ts index f8b5b8a4d0..6a3a314c73 100644 --- a/packages/cli/test/integration/binaryData.api.test.ts +++ b/packages/cli/test/integration/binaryData.api.test.ts @@ -87,6 +87,26 @@ describe('GET /binary-data', () => { }); }); + describe('should handle non-ASCII filename [filesystem]', () => { + test('on request to download', async () => { + const nonAsciiFileName = 'äöüß.png'; + + const res = await authOwnerAgent + .get('/binary-data') + .query({ + id: fsBinaryDataId, + fileName: nonAsciiFileName, + mimeType, + action: 'download', + }) + .expect(200); + + expect(res.headers['content-disposition']).toBe( + `attachment; filename="${encodeURIComponent(nonAsciiFileName)}"`, + ); + }); + }); + describe('should return 404 on file not found [filesystem]', () => { test.each(['view', 'download'])('on request to %s', async (action) => { binaryDataService.getAsStream.mockImplementation(throwFileNotFound);