From 4288c67c07ed2659db890ea63fdd91ff610e34a9 Mon Sep 17 00:00:00 2001 From: amertak Date: Tue, 23 Mar 2021 19:24:35 +0100 Subject: [PATCH 1/3] fix: Support other encoding than UTF-8 --- index.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 5f0f5fe..8efbcee 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,8 @@ var AWS = require('aws-sdk'); +/*global TextDecoder */ + console.log("AWS Lambda SES Forwarder // @arithmetric // Version 5.0.0"); // Configure the S3 bucket and key prefix for stored raw emails, and the @@ -195,7 +197,28 @@ exports.fetchMessage = function(data) { return reject( new Error("Error: Failed to load message body from S3.")); } - data.emailData = result.Body.toString(); + + const naivelyReadBody = result.Body.toString(); + const encodingMatches = naivelyReadBody.match(/text\/plain;\s*charset=([^;]+);/); + if (encodingMatches) { + try { + const encoding = encodingMatches[1] + const textDecoder = new TextDecoder(encoding); + + data.emailData = textDecoder.decode(result.Body) + } catch (err) { + data.log({ + level: "error", + message: "Could not decode body with desired encoding, fallback for naively read body.", + error: err, + stack: err.stack + }); + data.emailData = naivelyReadBody; + } + } else { + data.emailData = naivelyReadBody; + } + return resolve(data); }); }); @@ -274,6 +297,9 @@ exports.processMessage = function(data) { // Remove Message-ID header. header = header.replace(/^message-id:[\t ]?(.*)\r?\n/mgi, ''); + // JS only supports UTF-8 kinda, we also converted the text of the email into UTF8 in fetchMessage function + header = header.replace(/text\/plain;\s*charset=([^;]+);/gi, 'text/plain; charset=UTF-8;') + // Remove all DKIM-Signature headers to prevent triggering an // "InvalidParameterValue: Duplicate header 'DKIM-Signature'" error. // These signatures will likely be invalid anyways, since the From From 75abd79bf131ee5bd527d726bf78005677fb5c86 Mon Sep 17 00:00:00 2001 From: amertak Date: Thu, 25 Mar 2021 17:23:02 +0100 Subject: [PATCH 2/3] chore: handle body/header text/any --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8efbcee..bc59b73 100644 --- a/index.js +++ b/index.js @@ -298,7 +298,8 @@ exports.processMessage = function(data) { header = header.replace(/^message-id:[\t ]?(.*)\r?\n/mgi, ''); // JS only supports UTF-8 kinda, we also converted the text of the email into UTF8 in fetchMessage function - header = header.replace(/text\/plain;\s*charset=([^;]+);/gi, 'text/plain; charset=UTF-8;') + header = header.replace(/content-type:\s*text\/([^;]+);\s*charset=([^;\s]+)/gi, 'Content-Type: text/$1; charset=UTF-8') + body = body.replace(/content-type:\s*text\/([^;]+);\s*charset=([^;\s]+)/gi, 'Content-Type: text/$1; charset=UTF-8') // Remove all DKIM-Signature headers to prevent triggering an // "InvalidParameterValue: Duplicate header 'DKIM-Signature'" error. From d0e34404fed062f0c403ec587ce9804c4a242330 Mon Sep 17 00:00:00 2001 From: amertak Date: Thu, 25 Mar 2021 17:30:41 +0100 Subject: [PATCH 3/3] refactor: reuse regex and replacement text --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index bc59b73..e169dee 100644 --- a/index.js +++ b/index.js @@ -297,9 +297,12 @@ exports.processMessage = function(data) { // Remove Message-ID header. header = header.replace(/^message-id:[\t ]?(.*)\r?\n/mgi, ''); + const charsetRegex = /content-type:\s*text\/([^;]+);\s*charset=([^;\s]+)/gi + const replacementText = 'Content-Type: text/$1; charset=UTF-8' + // JS only supports UTF-8 kinda, we also converted the text of the email into UTF8 in fetchMessage function - header = header.replace(/content-type:\s*text\/([^;]+);\s*charset=([^;\s]+)/gi, 'Content-Type: text/$1; charset=UTF-8') - body = body.replace(/content-type:\s*text\/([^;]+);\s*charset=([^;\s]+)/gi, 'Content-Type: text/$1; charset=UTF-8') + header = header.replace(charsetRegex, replacementText) + body = body.replace(charsetRegex, replacementText) // Remove all DKIM-Signature headers to prevent triggering an // "InvalidParameterValue: Duplicate header 'DKIM-Signature'" error.