diff --git a/CHANGELOG.md b/CHANGELOG.md index 6651ae978fc..732d381814c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ - Fix double scrollbar when composing a mail with many plain text lines (#7760) - Fix decoding mail parts with multiple base64-encoded text blocks (#9290) - Fix bug where some messages could get malformed in an import from a MBOX file (#9510) +- Fix invalid line break characters in multi-line text in Sieve scripts (#9543) ## Release 1.6.7 diff --git a/plugins/managesieve/Changelog b/plugins/managesieve/Changelog index a7745951c34..f26d7058641 100644 --- a/plugins/managesieve/Changelog +++ b/plugins/managesieve/Changelog @@ -1,3 +1,4 @@ +- Fix invalid line break characters in multi-line text in Sieve scripts (#9543) - Fix javascript error when relational or spamtest extension is not enabled (#9139) - Support an array in managesieve_host option (#9447) diff --git a/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php b/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php index b82e24876df..7028b7f96e8 100644 --- a/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php +++ b/plugins/managesieve/lib/Roundcube/rcube_sieve_script.php @@ -1204,6 +1204,8 @@ public static function escape_string($str) $str = array_pop($str); } + $str = (string) $str; + // multi-line string if (preg_match('/[\r\n\0]/', $str)) { return sprintf("text:\r\n%s\r\n.\r\n", self::escape_multiline_string($str)); @@ -1222,7 +1224,7 @@ public static function escape_string($str) */ public static function escape_multiline_string($str) { - $str = preg_split('/(\r?\n)/', $str, -1, \PREG_SPLIT_DELIM_CAPTURE); + $str = preg_split('/\r?\n/', $str); foreach ($str as $idx => $line) { // dot-stuffing @@ -1231,7 +1233,7 @@ public static function escape_multiline_string($str) } } - return implode('', $str); + return implode("\r\n", $str); } /** diff --git a/plugins/managesieve/tests/ManagesieveScriptTest.php b/plugins/managesieve/tests/ManagesieveScriptTest.php index ab65d145559..a793b4d9377 100644 --- a/plugins/managesieve/tests/ManagesieveScriptTest.php +++ b/plugins/managesieve/tests/ManagesieveScriptTest.php @@ -84,4 +84,21 @@ public function test_tokenizer($num, $input, $output) $this->assertSame(trim($output), trim($res)); } + + public function test_escape_string() + { + $output = \rcube_sieve_script::escape_string([]); + $this->assertSame('""', $output); + $output = \rcube_sieve_script::escape_string(['"']); + $this->assertSame('"\""', $output); + $output = \rcube_sieve_script::escape_string('\\a'); + $this->assertSame('"\\\\a"', $output); + $output = \rcube_sieve_script::escape_string(['"', 'b']); + $this->assertSame('["\"","b"]', $output); + + // Multiline text + $input = "line1\r\nline2\n.line3\r\nline4"; + $output = \rcube_sieve_script::escape_string($input); + $this->assertSame("text:\r\nline1\r\nline2\r\n..line3\r\nline4\r\n.\r\n", $output); + } }