fix(Email Trigger (IMAP) Node): UTF-8 attachments are not correctly named (#6856)

This commit is contained in:
Michael Kret 2023-08-07 13:33:06 +03:00 committed by GitHub
parent 8126181e18
commit 72814d1f0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 107 additions and 58 deletions

View file

@ -12,6 +12,7 @@ import type {
INodeTypeBaseDescription, INodeTypeBaseDescription,
INodeTypeDescription, INodeTypeDescription,
ITriggerResponse, ITriggerResponse,
JsonObject,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow'; import { NodeOperationError } from 'n8n-workflow';
@ -19,9 +20,10 @@ import type { ImapSimple, ImapSimpleOptions, Message } from 'imap-simple';
import { connect as imapConnect, getParts } from 'imap-simple'; import { connect as imapConnect, getParts } from 'imap-simple';
import type { Source as ParserSource } from 'mailparser'; import type { Source as ParserSource } from 'mailparser';
import { simpleParser } from 'mailparser'; import { simpleParser } from 'mailparser';
import rfc2047 from 'rfc2047';
import isEmpty from 'lodash/isEmpty'; import isEmpty from 'lodash/isEmpty';
import find from 'lodash/find'; import find from 'lodash/find';
import type { ICredentialsDataImap } from '../../../credentials/Imap.credentials'; import type { ICredentialsDataImap } from '../../../credentials/Imap.credentials';
import { isCredentialsDataImap } from '../../../credentials/Imap.credentials'; import { isCredentialsDataImap } from '../../../credentials/Imap.credentials';
@ -32,14 +34,14 @@ export async function parseRawEmail(
): Promise<INodeExecutionData> { ): Promise<INodeExecutionData> {
const responseData = await simpleParser(messageEncoded); const responseData = await simpleParser(messageEncoded);
const headers: IDataObject = {}; const headers: IDataObject = {};
const addidtionalData: IDataObject = {};
for (const header of responseData.headerLines) { for (const header of responseData.headerLines) {
headers[header.key] = header.line; headers[header.key] = header.line;
} }
// @ts-ignore addidtionalData.headers = headers;
responseData.headers = headers; addidtionalData.headerLines = undefined;
// @ts-ignore
responseData.headerLines = undefined;
const binaryData: IBinaryKeyData = {}; const binaryData: IBinaryKeyData = {};
if (responseData.attachments) { if (responseData.attachments) {
@ -51,12 +53,12 @@ export async function parseRawEmail(
attachment.contentType, attachment.contentType,
); );
} }
// @ts-ignore
responseData.attachments = undefined; addidtionalData.attachments = undefined;
} }
return { return {
json: responseData as unknown as IDataObject, json: { ...responseData, ...addidtionalData },
binary: Object.keys(binaryData).length ? binaryData : undefined, binary: Object.keys(binaryData).length ? binaryData : undefined,
} as INodeExecutionData; } as INodeExecutionData;
} }
@ -260,7 +262,7 @@ export class EmailReadImapV2 implements INodeType {
} catch (error) { } catch (error) {
return { return {
status: 'Error', status: 'Error',
message: error.message, message: (error as Error).message,
}; };
} }
return { return {
@ -296,14 +298,15 @@ export class EmailReadImapV2 implements INodeType {
// Returns the email text // Returns the email text
const getText = async (parts: any[], message: Message, subtype: string) => { const getText = async (parts: IDataObject[], message: Message, subtype: string) => {
if (!message.attributes.struct) { if (!message.attributes.struct) {
return ''; return '';
} }
const textParts = parts.filter((part) => { const textParts = parts.filter((part) => {
return ( return (
part.type.toUpperCase() === 'TEXT' && part.subtype.toUpperCase() === subtype.toUpperCase() (part.type as string).toUpperCase() === 'TEXT' &&
(part.subtype as string).toUpperCase() === subtype.toUpperCase()
); );
}); });
@ -312,7 +315,7 @@ export class EmailReadImapV2 implements INodeType {
} }
try { try {
return await connection.getPartData(message, textParts[0]); return (await connection.getPartData(message, textParts[0])) as string;
} catch { } catch {
return ''; return '';
} }
@ -321,7 +324,7 @@ export class EmailReadImapV2 implements INodeType {
// Returns the email attachments // Returns the email attachments
const getAttachment = async ( const getAttachment = async (
imapConnection: ImapSimple, imapConnection: ImapSimple,
parts: any[], parts: IDataObject[],
message: Message, message: Message,
): Promise<IBinaryData[]> => { ): Promise<IBinaryData[]> => {
if (!message.attributes.struct) { if (!message.attributes.struct) {
@ -330,20 +333,33 @@ export class EmailReadImapV2 implements INodeType {
// Check if the message has attachments and if so get them // Check if the message has attachments and if so get them
const attachmentParts = parts.filter((part) => { const attachmentParts = parts.filter((part) => {
return part.disposition && part.disposition.type.toUpperCase() === 'ATTACHMENT'; return (
part.disposition &&
((part.disposition as IDataObject)?.type as string).toUpperCase() === 'ATTACHMENT'
);
}); });
const decodeFilename = (filename: string) => {
const regex = /=\?([\w-]+)\?Q\?.*\?=/i;
if (regex.test(filename)) {
return rfc2047.decode(filename);
}
return filename;
};
const attachmentPromises = []; const attachmentPromises = [];
let attachmentPromise; let attachmentPromise;
for (const attachmentPart of attachmentParts) { for (const attachmentPart of attachmentParts) {
attachmentPromise = imapConnection attachmentPromise = imapConnection
.getPartData(message, attachmentPart) .getPartData(message, attachmentPart)
.then(async (partData) => { .then(async (partData) => {
// Return it in the format n8n expects // if filename contains utf-8 encoded characters, decode it
return this.helpers.prepareBinaryData( const fileName = decodeFilename(
partData as Buffer, ((attachmentPart.disposition as IDataObject)?.params as IDataObject)
attachmentPart.disposition.params.filename as string, ?.filename as string,
); );
// Return it in the format n8n expects
return this.helpers.prepareBinaryData(partData as Buffer, fileName);
}); });
attachmentPromises.push(attachmentPromise); attachmentPromises.push(attachmentPromise);
@ -440,7 +456,7 @@ export class EmailReadImapV2 implements INodeType {
) { ) {
staticData.lastMessageUid = message.attributes.uid; staticData.lastMessageUid = message.attributes.uid;
} }
const parts = getParts(message.attributes.struct!); const parts = getParts(message.attributes.struct as IDataObject[]) as IDataObject[];
newEmail = { newEmail = {
json: { json: {
@ -454,14 +470,15 @@ export class EmailReadImapV2 implements INodeType {
return part.which === 'HEADER'; return part.which === 'HEADER';
}); });
messageBody = messageHeader[0].body; messageBody = messageHeader[0].body as IDataObject;
for (propertyName of Object.keys(messageBody as IDataObject)) { for (propertyName of Object.keys(messageBody)) {
if (messageBody[propertyName].length) { if ((messageBody[propertyName] as IDataObject[]).length) {
if (topLevelProperties.includes(propertyName)) { if (topLevelProperties.includes(propertyName)) {
newEmail.json[propertyName] = messageBody[propertyName][0]; newEmail.json[propertyName] = (messageBody[propertyName] as string[])[0];
} else { } else {
(newEmail.json.metadata as IDataObject)[propertyName] = (newEmail.json.metadata as IDataObject)[propertyName] = (
messageBody[propertyName][0]; messageBody[propertyName] as string[]
)[0];
} }
} }
} }
@ -501,7 +518,7 @@ export class EmailReadImapV2 implements INodeType {
// Return base64 string // Return base64 string
newEmail = { newEmail = {
json: { json: {
raw: part.body, raw: part.body as string,
}, },
}; };
@ -525,7 +542,9 @@ export class EmailReadImapV2 implements INodeType {
let searchCriteria = ['UNSEEN'] as Array<string | string[]>; let searchCriteria = ['UNSEEN'] as Array<string | string[]>;
if (options.customEmailConfig !== undefined) { if (options.customEmailConfig !== undefined) {
try { try {
searchCriteria = JSON.parse(options.customEmailConfig as string); searchCriteria = JSON.parse(options.customEmailConfig as string) as Array<
string | string[]
>;
} catch (error) { } catch (error) {
throw new NodeOperationError(this.getNode(), 'Custom email config is not valid JSON.'); throw new NodeOperationError(this.getNode(), 'Custom email config is not valid JSON.');
} }
@ -568,7 +587,7 @@ export class EmailReadImapV2 implements INodeType {
} }
} catch (error) { } catch (error) {
this.logger.error('Email Read Imap node encountered an error fetching new emails', { this.logger.error('Email Read Imap node encountered an error fetching new emails', {
error, error: error as Error,
}); });
// Wait with resolving till the returnedPromise got resolved, else n8n will be unhappy // Wait with resolving till the returnedPromise got resolved, else n8n will be unhappy
// if it receives an error before the workflow got activated // if it receives an error before the workflow got activated
@ -611,8 +630,10 @@ export class EmailReadImapV2 implements INodeType {
} }
}); });
conn.on('error', async (error) => { conn.on('error', async (error) => {
const errorCode = error.code.toUpperCase(); const errorCode = ((error as JsonObject).code as string).toUpperCase();
this.logger.verbose(`IMAP connection experienced an error: (${errorCode})`, { error }); this.logger.verbose(`IMAP connection experienced an error: (${errorCode})`, {
error: error as Error,
});
// eslint-disable-next-line @typescript-eslint/no-use-before-define // eslint-disable-next-line @typescript-eslint/no-use-before-define
await closeFunction(); await closeFunction();
this.emitError(error as Error); this.emitError(error as Error);
@ -627,9 +648,7 @@ export class EmailReadImapV2 implements INodeType {
let reconnectionInterval: NodeJS.Timeout | undefined; let reconnectionInterval: NodeJS.Timeout | undefined;
if (options.forceReconnect !== undefined) { const handleReconect = async () => {
reconnectionInterval = setInterval(
async () => {
this.logger.verbose('Forcing reconnect to IMAP server'); this.logger.verbose('Forcing reconnect to IMAP server');
try { try {
isCurrentlyReconnecting = true; isCurrentlyReconnecting = true;
@ -642,7 +661,11 @@ export class EmailReadImapV2 implements INodeType {
} finally { } finally {
isCurrentlyReconnecting = false; isCurrentlyReconnecting = false;
} }
}, };
if (options.forceReconnect !== undefined) {
reconnectionInterval = setInterval(
handleReconect,
(options.forceReconnect as number) * 1000 * 60, (options.forceReconnect as number) * 1000 * 60,
); );
} }

View file

@ -796,6 +796,7 @@
"@types/promise-ftp": "^1.3.4", "@types/promise-ftp": "^1.3.4",
"@types/redis": "^2.8.11", "@types/redis": "^2.8.11",
"@types/request-promise-native": "~1.0.15", "@types/request-promise-native": "~1.0.15",
"@types/rfc2047": "^2.0.1",
"@types/showdown": "^1.9.4", "@types/showdown": "^1.9.4",
"@types/snowflake-sdk": "^1.6.12", "@types/snowflake-sdk": "^1.6.12",
"@types/ssh2-sftp-client": "^5.1.0", "@types/ssh2-sftp-client": "^5.1.0",
@ -854,6 +855,7 @@
"promise-ftp": "^1.3.5", "promise-ftp": "^1.3.5",
"pyodide": "^0.23.4", "pyodide": "^0.23.4",
"redis": "^3.1.1", "redis": "^3.1.1",
"rfc2047": "^4.0.1",
"rhea": "^1.0.11", "rhea": "^1.0.11",
"rss-parser": "^3.7.0", "rss-parser": "^3.7.0",
"semver": "^7.5.4", "semver": "^7.5.4",

View file

@ -135,7 +135,7 @@ importers:
dependencies: dependencies:
axios: axios:
specifier: ^0.21.1 specifier: ^0.21.1
version: 0.21.4 version: 0.21.4(debug@4.3.2)
packages/@n8n_io/eslint-config: packages/@n8n_io/eslint-config:
devDependencies: devDependencies:
@ -216,7 +216,7 @@ importers:
version: 7.28.1 version: 7.28.1
axios: axios:
specifier: ^0.21.1 specifier: ^0.21.1
version: 0.21.4 version: 0.21.4(debug@4.3.2)
basic-auth: basic-auth:
specifier: ^2.0.1 specifier: ^2.0.1
version: 2.0.1 version: 2.0.1
@ -574,7 +574,7 @@ importers:
version: link:../@n8n/client-oauth2 version: link:../@n8n/client-oauth2
axios: axios:
specifier: ^0.21.1 specifier: ^0.21.1
version: 0.21.4 version: 0.21.4(debug@4.3.2)
concat-stream: concat-stream:
specifier: ^2.0.0 specifier: ^2.0.0
version: 2.0.0 version: 2.0.0
@ -834,7 +834,7 @@ importers:
version: 10.2.0(vue@3.3.4) version: 10.2.0(vue@3.3.4)
axios: axios:
specifier: ^0.21.1 specifier: ^0.21.1
version: 0.21.4 version: 0.21.4(debug@4.3.2)
codemirror-lang-html-n8n: codemirror-lang-html-n8n:
specifier: ^1.0.0 specifier: ^1.0.0
version: 1.0.0 version: 1.0.0
@ -1134,6 +1134,9 @@ importers:
redis: redis:
specifier: ^3.1.1 specifier: ^3.1.1
version: 3.1.2 version: 3.1.2
rfc2047:
specifier: ^4.0.1
version: 4.0.1
rhea: rhea:
specifier: ^1.0.11 specifier: ^1.0.11
version: 1.0.24 version: 1.0.24
@ -1234,6 +1237,9 @@ importers:
'@types/request-promise-native': '@types/request-promise-native':
specifier: ~1.0.15 specifier: ~1.0.15
version: 1.0.18 version: 1.0.18
'@types/rfc2047':
specifier: ^2.0.1
version: 2.0.1
'@types/showdown': '@types/showdown':
specifier: ^1.9.4 specifier: ^1.9.4
version: 1.9.4 version: 1.9.4
@ -4448,7 +4454,7 @@ packages:
dependencies: dependencies:
'@segment/loosely-validate-event': 2.0.0 '@segment/loosely-validate-event': 2.0.0
auto-changelog: 1.16.4 auto-changelog: 1.16.4
axios: 0.21.4 axios: 0.21.4(debug@4.3.2)
axios-retry: 3.3.1 axios-retry: 3.3.1
bull: 3.29.3 bull: 3.29.3
lodash.clonedeep: 4.5.0 lodash.clonedeep: 4.5.0
@ -6484,6 +6490,10 @@ packages:
form-data: 2.5.1 form-data: 2.5.1
dev: true dev: true
/@types/rfc2047@2.0.1:
resolution: {integrity: sha512-slgtykv+XXME7EperkdqfdBBUGcs28ru+a21BK0zOQY4IoxE7tEqvIcvAFAz5DJVxyOmoAUXo30Oxpm3KS+TBQ==}
dev: true
/@types/sanitize-html@2.9.0: /@types/sanitize-html@2.9.0:
resolution: {integrity: sha512-4fP/kEcKNj2u39IzrxWYuf/FnCCwwQCpif6wwY6ROUS1EPRIfWJjGkY3HIowY1EX/VbX5e86yq8AAE7UPMgATg==} resolution: {integrity: sha512-4fP/kEcKNj2u39IzrxWYuf/FnCCwwQCpif6wwY6ROUS1EPRIfWJjGkY3HIowY1EX/VbX5e86yq8AAE7UPMgATg==}
dependencies: dependencies:
@ -8074,14 +8084,6 @@ packages:
is-retry-allowed: 2.2.0 is-retry-allowed: 2.2.0
dev: false dev: false
/axios@0.21.4:
resolution: {integrity: sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==}
dependencies:
follow-redirects: 1.15.2(debug@4.3.4)
transitivePeerDependencies:
- debug
dev: false
/axios@0.21.4(debug@4.3.2): /axios@0.21.4(debug@4.3.2):
resolution: {integrity: sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==} resolution: {integrity: sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==}
dependencies: dependencies:
@ -8090,6 +8092,15 @@ packages:
- debug - debug
dev: false dev: false
/axios@0.27.2:
resolution: {integrity: sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==}
dependencies:
follow-redirects: 1.15.2(debug@4.3.2)
form-data: 4.0.0
transitivePeerDependencies:
- debug
dev: false
/axios@0.27.2(debug@3.2.7): /axios@0.27.2(debug@3.2.7):
resolution: {integrity: sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==} resolution: {integrity: sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==}
dependencies: dependencies:
@ -8106,6 +8117,7 @@ packages:
form-data: 4.0.0 form-data: 4.0.0
transitivePeerDependencies: transitivePeerDependencies:
- debug - debug
dev: true
/babel-core@7.0.0-bridge.0(@babel/core@7.22.9): /babel-core@7.0.0-bridge.0(@babel/core@7.22.9):
resolution: {integrity: sha512-poPX9mZH/5CSanm50Q+1toVci6pv5KSRv/5TWCwtzQS5XEwn40BcCrgIeMFWP9CKKIniKXNxoIOnOq4VVlGXhg==} resolution: {integrity: sha512-poPX9mZH/5CSanm50Q+1toVci6pv5KSRv/5TWCwtzQS5XEwn40BcCrgIeMFWP9CKKIniKXNxoIOnOq4VVlGXhg==}
@ -11686,6 +11698,7 @@ packages:
optional: true optional: true
dependencies: dependencies:
debug: 4.3.4(supports-color@8.1.1) debug: 4.3.4(supports-color@8.1.1)
dev: true
/for-each@0.3.3: /for-each@0.3.3:
resolution: {integrity: sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==} resolution: {integrity: sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==}
@ -12683,6 +12696,11 @@ packages:
dependencies: dependencies:
safer-buffer: 2.1.2 safer-buffer: 2.1.2
/iconv-lite@0.4.5:
resolution: {integrity: sha512-LQ4GtDkFagYaac8u4rE73zWu7h0OUUmR0qVBOgzLyFSoJhoDG2xV9PZJWWyVVcYha/9/RZzQHUinFMbNKiOoAA==}
engines: {node: '>=0.8.0'}
dev: false
/iconv-lite@0.6.3: /iconv-lite@0.6.3:
resolution: {integrity: sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==} resolution: {integrity: sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
@ -17040,7 +17058,7 @@ packages:
resolution: {integrity: sha512-aXYe/D+28kF63W8Cz53t09ypEORz+ULeDCahdAqhVrRm2scbOXFbtnn0GGhvMpYe45grepLKuwui9KxrZ2ZuMw==} resolution: {integrity: sha512-aXYe/D+28kF63W8Cz53t09ypEORz+ULeDCahdAqhVrRm2scbOXFbtnn0GGhvMpYe45grepLKuwui9KxrZ2ZuMw==}
engines: {node: '>=14.17.0'} engines: {node: '>=14.17.0'}
dependencies: dependencies:
axios: 0.27.2(debug@4.3.4) axios: 0.27.2
transitivePeerDependencies: transitivePeerDependencies:
- debug - debug
dev: false dev: false
@ -18075,6 +18093,12 @@ packages:
resolution: {integrity: sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==} resolution: {integrity: sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==}
engines: {iojs: '>=1.0.0', node: '>=0.10.0'} engines: {iojs: '>=1.0.0', node: '>=0.10.0'}
/rfc2047@4.0.1:
resolution: {integrity: sha512-x5zHBAZtSSZDuBNAqGEAVpsQFV+YUluIkMWVaYRMEeGoLPxNVMmg67TxRnXwmRmCB7QaneyrkWXeKqbjfcK6RA==}
dependencies:
iconv-lite: 0.4.5
dev: false
/rfdc@1.3.0: /rfdc@1.3.0:
resolution: {integrity: sha512-V2hovdzFbOi77/WajaSMXk2OLm+xNIeQdMMuB7icj7bk6zi2F8GGAxigcnDFpJHbNyNcgyJDiP+8nOrY5cZGrA==} resolution: {integrity: sha512-V2hovdzFbOi77/WajaSMXk2OLm+xNIeQdMMuB7icj7bk6zi2F8GGAxigcnDFpJHbNyNcgyJDiP+8nOrY5cZGrA==}