From 664b3792a4d97fcca5a047d759b8fb6b43138779 Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Mon, 15 Apr 2019 15:11:03 +0300 Subject: [PATCH] v1.17.2 --- lib/api/messages.js | 41 +++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/api/messages.js b/lib/api/messages.js index 259a8c4d..8a5aa9b3 100644 --- a/lib/api/messages.js +++ b/lib/api/messages.js @@ -4,6 +4,8 @@ const config = require('wild-config'); const log = require('npmlog'); const libmime = require('libmime'); const Joi = require('../joi'); +const uuid = require('uuid'); +const os = require('os'); const MongoPaging = require('mongo-cursor-pagination'); const addressparser = require('nodemailer/lib/addressparser'); const MailComposer = require('nodemailer/lib/mail-composer'); @@ -2436,6 +2438,20 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler) => { keepBcc: true }; + if (data.html && typeof data.html === 'string') { + let fromAddress = (data.from && data.from.address).toString() || os.hostname(); + data.html = data.html.replace(/(]* src\s*=[\s"']*)(data:[^"'>\s]+)/gi, (match, prefix, dataUri) => { + let cid = uuid.v4() + '-attachments@' + fromAddress.split('@').pop(); + data.attachments.push( + processDataUrl({ + path: dataUri, + cid + }) + ); + return prefix + 'cid:' + cid; + }); + } + // ensure plaintext content if html is provided if (data.html && !data.text) { try { @@ -4002,3 +4018,28 @@ function parseAddresses(data) { walk([].concat(data || [])); return Array.from(addresses); } + +function processDataUrl(element) { + let parts = (element.path || element.href).match(/^data:((?:[^;]*;)*(?:[^,]*)),(.*)$/i); + if (!parts) { + return element; + } + + element.content = /\bbase64$/i.test(parts[1]) ? Buffer.from(parts[2], 'base64') : Buffer.from(decodeURIComponent(parts[2])); + + if ('path' in element) { + element.path = false; + } + + if ('href' in element) { + element.href = false; + } + + parts[1].split(';').forEach(item => { + if (/^\w+\/[^/]+$/i.test(item)) { + element.contentType = element.contentType || item.toLowerCase(); + } + }); + + return element; +} diff --git a/package.json b/package.json index 390fe436..bd9b692d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wildduck", - "version": "1.17.1", + "version": "1.17.2", "description": "IMAP/POP3 server built with Node.js and MongoDB", "main": "server.js", "scripts": {