From 050fbf53252a37d400d76fb17467e9c5ff0c61cc Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Tue, 9 Jan 2018 06:40:55 +0000
Subject: [PATCH] If there are non US-ASCII characters in the headers, first
 check if they're UTF-8 and if so, convert using uft8_ascii(), otherwise, just
 replace every non-US-ASCII byte with a question mark.

RFC*822 does not allow anything but US-ASCII in headers.
---
 exec/load/822header.js | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/exec/load/822header.js b/exec/load/822header.js
index 4912a04185..deb29e6ccf 100644
--- a/exec/load/822header.js
+++ b/exec/load/822header.js
@@ -1,4 +1,5 @@
 const RFC822HEADER = 0xb0;  // from smbdefs.h
+require("utf8_ascii.js", 'utf8_ascii');
 
 MsgBase.HeaderPrototype.get_rfc822_header=function(force_update)
 {
@@ -83,6 +84,17 @@ MsgBase.HeaderPrototype.get_rfc822_header=function(force_update)
 		// Unwrap headers
 		this.rfc822=this.rfc822.replace(/\s*\r\n\s+/g, " ");
 		this.rfc822 += "\r\n";
+		// Illegal characters in header?
+		if (this.rfc822.search(/[^\x01-\x7f]/) != -1) {
+			/* Is it UTF-8? */
+			if (this.rfc822.replace(/[\xc0-\xfd][\x80-\xbf]+/g,'').search(/[^\x01-\x7f]/) == -1) {
+				this.rfc822 = utf8_ascii(this.rfc822);
+			}
+			else {
+				// If not, just strip them all...
+				this.rfc822 = this.rfc822.replace(/[^\x01-\x7f]/g, '?');
+			}
+		}
 	}
 	return this.rfc822;
 };
-- 
GitLab