fix(core): Account for non-ASCII chars in filename on binary data download (#7742)

https://n8nio.sentry.io/issues/4641538638
This commit is contained in:
Iván Ovejero 2023-11-17 10:07:44 +01:00 committed by GitHub
parent abe36691ab
commit b4ebb1a28d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View file

@ -41,8 +41,9 @@ export class BinaryDataController {
if (mimeType) res.setHeader('Content-Type', mimeType); if (mimeType) res.setHeader('Content-Type', mimeType);
if (action === 'download') { if (action === 'download' && fileName) {
res.setHeader('Content-Disposition', `attachment; filename="${fileName}"`); const encodedFilename = encodeURIComponent(fileName);
res.setHeader('Content-Disposition', `attachment; filename="${encodedFilename}"`);
} }
return await this.binaryDataService.getAsStream(binaryDataId); return await this.binaryDataService.getAsStream(binaryDataId);

View file

@ -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]', () => { describe('should return 404 on file not found [filesystem]', () => {
test.each(['view', 'download'])('on request to %s', async (action) => { test.each(['view', 'download'])('on request to %s', async (action) => {
binaryDataService.getAsStream.mockImplementation(throwFileNotFound); binaryDataService.getAsStream.mockImplementation(throwFileNotFound);