From c10524726e0a91445a0329cc9aa72b7467258381 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Mon, 23 Sep 2019 10:35:29 +0530 Subject: email: Tolerate non-ASCII non-UTF-8 characters in headers. * email/email.scm (email->headers+body): If non-ASCII non-UTF-8 characters occur in the headers, do not raise a decoding error. Work around using the substitute conversion strategy. * tests/email.scm ("tolerate non-ASCII characters in headers"): Rename to "decode utf-8 characters in headers". ("tolerate non-ascii non-utf-8 characters in headers"): New test. Reported-by: Christopher Baines --- email/email.scm | 5 ++++- tests/email.scm | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/email/email.scm b/email/email.scm index f276019..6ed285f 100644 --- a/email/email.scm +++ b/email/email.scm @@ -611,8 +611,11 @@ values. The returned headers is a string and body is a bytevector." ;; name, we relax this requirement. We assume an encoding of ;; UTF-8, and hope that everything turns out fine. Since UTF-8 ;; is a superset of ASCII, this should not affect standards - ;; conforming headers. + ;; conforming headers. If encoding is neither UTF-8 nor ASCII, + ;; we use the substitute conversion strategy and proceed without + ;; raising an error. (set-port-encoding! port "utf-8") + (set-port-conversion-strategy! port 'substitute) (let ((headers (read-while port get-line-with-delimiter (lambda (line) (not (or (string= line "\n") diff --git a/tests/email.scm b/tests/email.scm index 8aa9082..1346b20 100644 --- a/tests/email.scm +++ b/tests/email.scm @@ -282,7 +282,7 @@ foo foo " "utf-8"))) -(test-equal "tolerate non-ascii characters in headers" +(test-equal "decode utf-8 characters in headers" (parse-email (string->bytevector "From: foo@bar.org (Foo Bãr) @@ -297,6 +297,21 @@ body" "utf-8")) (address . "foo@bar.org")))) "body")) +(test-equal "tolerate non-ascii non-utf-8 characters in headers" + (parse-email + (string->bytevector + "From: foo@bar.org (Foo Bãr) + +body" "iso-8859-1")) + (make-email + `((content-type (type . text) + (subtype . plain) + (charset . "utf-8")) + (content-transfer-encoding . 7bit) + (from ((name . "Foo B�r") + (address . "foo@bar.org")))) + "body")) + (test-equal "parse name-addr email address" (parse-email-address "Foo ") '((name . "Foo") (address . "foo@example.org"))) -- cgit v1.2.3