Skip to content

Commit

Permalink
Fix invalid line break characters in multi-line text in Sieve scripts (
Browse files Browse the repository at this point in the history
  • Loading branch information
alecpl committed Jul 18, 2024
1 parent 8653e47 commit cc72002
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions plugins/managesieve/Changelog
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
6 changes: 4 additions & 2 deletions plugins/managesieve/lib/Roundcube/rcube_sieve_script.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand All @@ -1231,7 +1233,7 @@ public static function escape_multiline_string($str)
}
}

return implode('', $str);
return implode("\r\n", $str);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions plugins/managesieve/tests/ManagesieveScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit cc72002

Please sign in to comment.