From 418816cd899f996fec0ac006681c34b4775f6f6c Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan.bosch@dovecot.fi>
Date: Sat, 30 Sep 2017 21:19:11 +0200
Subject: [PATCH] lib-sieve: redirect action: Always set
 X-Sieve-Redirected-From header to sieve_user_email if configured.

Set it to the final recipient only when there is an envelope and sieve_user_email is unconfigured.
---
 src/lib-sieve/cmd-redirect.c |  3 ++-
 tests/execute/smtp.svtest    | 46 +++++++++++++++++++++++++++++++++++-
 2 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/src/lib-sieve/cmd-redirect.c b/src/lib-sieve/cmd-redirect.c
index 322ea1584..32e01b7ae 100644
--- a/src/lib-sieve/cmd-redirect.c
+++ b/src/lib-sieve/cmd-redirect.c
@@ -362,7 +362,8 @@ static int act_redirect_send
 		/* Prepend sieve headers (should not affect signatures) */
 		rfc2822_header_append(hdr,
 			"X-Sieve", SIEVE_IMPLEMENTATION, FALSE, NULL);
-		if ( (aenv->flags & SIEVE_EXECUTE_FLAG_NO_ENVELOPE) == 0 )
+		if ( svinst->user_email == NULL &&
+			(aenv->flags & SIEVE_EXECUTE_FLAG_NO_ENVELOPE) == 0 )
 			user_email = sieve_message_get_final_recipient(msgctx);
 		else
 			user_email = sieve_get_user_email(aenv->svinst);
diff --git a/tests/execute/smtp.svtest b/tests/execute/smtp.svtest
index 83098cf59..e19540905 100644
--- a/tests/execute/smtp.svtest
+++ b/tests/execute/smtp.svtest
@@ -36,6 +36,11 @@ test "Redirect" {
 	if not envelope :is "from" "sirius@example.org" {
 		test_fail "envelope sender incorrect";
 	}
+
+	if not header :contains "x-sieve-redirected-from"
+		"timo@example.net" {
+		test_fail "x-sieve-redirected-from header is incorrect";
+	}
 }
 
 test_result_reset;
@@ -66,6 +71,11 @@ test "Redirect from <>" {
 	if not envelope :is "from" "" {
 		test_fail "envelope sender incorrect";
 	}
+
+	if not header :contains "x-sieve-redirected-from"
+		"timo@example.net" {
+		test_fail "x-sieve-redirected-from header is incorrect";
+	}
 }
 
 test_result_reset;
@@ -99,6 +109,11 @@ test "Redirect from [recipient]" {
 	if not envelope :is "from" "timo@example.net" {
 		test_fail "envelope sender incorrect";
 	}
+
+	if not header :contains "x-sieve-redirected-from"
+		"timo@example.net" {
+		test_fail "x-sieve-redirected-from header is incorrect";
+	}
 }
 
 test_result_reset;
@@ -133,8 +148,12 @@ test "Redirect from [original recipient]" {
 	if not envelope :is "from" "tss@example.net" {
 		test_fail "envelope sender incorrect";
 	}
-}
 
+	if not header :contains "x-sieve-redirected-from"
+		"timo@example.net" {
+		test_fail "x-sieve-redirected-from header is incorrect";
+	}
+}
 
 test_result_reset;
 test_set "message" text:
@@ -168,6 +187,11 @@ test "Redirect from [<explicit>]" {
 	if not envelope :is "from" "backscatter@example.net" {
 		test_fail "envelope sender incorrect";
 	}
+
+	if not header :contains "x-sieve-redirected-from"
+		"timo@example.net" {
+		test_fail "x-sieve-redirected-from header is incorrect";
+	}
 }
 
 test_result_reset;
@@ -201,6 +225,11 @@ test "Redirect from [<>]" {
 	if not envelope :is "from" "" {
 		test_fail "envelope sender incorrect";
 	}
+
+	if not header :contains "x-sieve-redirected-from"
+		"timo@example.net" {
+		test_fail "x-sieve-redirected-from header is incorrect";
+	}
 }
 
 test_result_reset;
@@ -235,6 +264,11 @@ test "Redirect from <> with [<explicit>]" {
 	if not envelope :is "from" "" {
 		test_fail "envelope sender incorrect";
 	}
+
+	if not header :contains "x-sieve-redirected-from"
+		"timo@example.net" {
+		test_fail "x-sieve-redirected-from header is incorrect";
+	}
 }
 
 test_result_reset;
@@ -265,6 +299,11 @@ test "Redirect from [user email - fallback default]" {
 	if not envelope :is "from" "timo@example.net" {
 		test_fail "envelope sender incorrect";
 	}
+
+	if not header :contains "x-sieve-redirected-from"
+		"timo@example.net" {
+		test_fail "x-sieve-redirected-from header is incorrect";
+	}
 }
 
 test_result_reset;
@@ -300,5 +339,10 @@ test "Redirect from [user email]" {
 	if not envelope :is "from" "t.sirainen@example.net" {
 		test_fail "envelope sender incorrect";
 	}
+
+	if not header :contains "x-sieve-redirected-from"
+		"t.sirainen@example.net" {
+		test_fail "x-sieve-redirected-from header is incorrect";
+	}
 }
 
-- 
GitLab