From bc8c1fc794dcc6db156aefe941813d6422e50886 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan.bosch@open-xchange.com>
Date: Thu, 5 May 2022 14:40:47 +0200
Subject: [PATCH] lib-sieve: enotify extension: mailto: Enforce subject header
 limit in Unicode codepoints rather than bytes.

---
 .../plugins/enotify/mailto/ntfy-mailto.c      |  6 +--
 tests/extensions/enotify/mailto.svtest        | 41 +++++++++++++++++++
 2 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/src/lib-sieve/plugins/enotify/mailto/ntfy-mailto.c b/src/lib-sieve/plugins/enotify/mailto/ntfy-mailto.c
index cb7265f41..fb345d52c 100644
--- a/src/lib-sieve/plugins/enotify/mailto/ntfy-mailto.c
+++ b/src/lib-sieve/plugins/enotify/mailto/ntfy-mailto.c
@@ -608,15 +608,15 @@ ntfy_mailto_send(const struct sieve_enotify_exec_env *nenv,
 
 	/* Determine subject */
 	if (nact->message != NULL) {
-		/* FIXME: handle UTF-8 */
-		subject = str_sanitize(nact->message, NTFY_MAILTO_MAX_SUBJECT);
+		subject = str_sanitize_utf8(nact->message,
+					    NTFY_MAILTO_MAX_SUBJECT);
 	} else if (subject == NULL) {
 		const char *const *hsubject;
 
 		/* Fetch subject from original message */
 		if (mail_get_headers_utf8(msgdata->mail, "subject",
 					  &hsubject) > 0) {
-			subject = str_sanitize(
+			subject = str_sanitize_utf8(
 				t_strdup_printf("Notification: %s", hsubject[0]),
 				NTFY_MAILTO_MAX_SUBJECT);
 		} else {
diff --git a/tests/extensions/enotify/mailto.svtest b/tests/extensions/enotify/mailto.svtest
index 68d8daac8..1ca8e7cb2 100644
--- a/tests/extensions/enotify/mailto.svtest
+++ b/tests/extensions/enotify/mailto.svtest
@@ -1,4 +1,5 @@
 require "vnd.dovecot.testsuite";
+require "encoded-character";
 require "enotify";
 require "relational";
 require "envelope";
@@ -539,3 +540,43 @@ decoded: ${decoded}
 ;
 	}
 }
+
+/*
+ * Long message
+ */
+
+test_result_reset;
+
+test_set "message" text:
+From: stephan@example.org
+To: nico@frop.example.org
+Subject: Frop!
+
+Klutsefluts.
+.
+;
+
+test "Long message" {
+	notify :message "以つわづ聞65絵げま公存ょ愛益イラヌ旅絵平ネ聞玉ゆッ夜能セメヲ映紙ナネヲラ記50聞3里ヤ配石スりを先天ヤヌオク沢天7統併イよけげ最処輪金直ちス。揮わ却写よゃ限傷じば豊銘らぽのほ談造げ発変だ際記タヨリ構始月ヨセ表軽すづル理特院ざれ男録カチ毎応ひくトこ人復比イふ。日ミノエ置止イ記場クウ見月リシヒス書電ほん開学井ワス逃6賞割セツ政転あトほれ撮追緩せばほ護区私丘甘めょはつ。交ルク秀57俸愚牡獣1定ネ投式チホイフ会2掲ねトぼて保品とじざち直金アチヒ調校タ更下べとー士聞テケオ軽記ホカ名北りまげほ弁分じク崎脅ねめン格抱診露竜わルぶイ。佐かだ可管ユラネ起呈佐ぞりづせ出階ふ方膨さびぞ中声不終多散れよどら何集あ砂岡ワ変愛細く。陣あず続的ルん得法をけり科同索ゃうン屋戸51俸愚牡獣8常ヱアケ択先レフ度前ヌカヒ本拡ケモ私但へやドび系球域メタヨネ全能ツヒ書指挙患慣づ。"
+		"mailto:stephan@example.org";
+
+	if not test_result_execute {
+		test_fail "failed to execute notify";
+	}
+
+	test_message :smtp 0;
+
+	if not allof(
+		header :contains "subject"
+			"以つわづ聞65絵げま公存ょ愛益イラヌ旅絵平ネ聞玉ゆッ夜能セメヲ映紙ナネヲ",
+		header :contains "subject"
+			"金アチヒ調校タ更下べとー士聞テケオ軽記ホカ名北りまげほ弁分じク崎脅ねめン格抱") {
+		test_fail "Subject header is too limited";
+	}
+	if header :contains "subject" "ねめン格抱診露" {
+		test_fail "Subject header is unlimited";
+	}
+	if not header :matches "subject" "*${unicode:2026}" {
+		test_fail "Subject is missing ellipsis";
+	}
+}
-- 
GitLab