Skip to content

Commit b71fcba

Browse files
rustmailerdjc
authored andcommitted
fix(parser): allow trailing whitespace before closing parenthesis #190
Some real-world IMAP servers include extra whitespace before the closing parenthesis. This change makes the parser more robust by allowing zero or more spaces.
1 parent 38d72da commit b71fcba

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

imap-proto/src/parser/core.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use nom::{
22
branch::alt,
33
bytes::streaming::{escaped, tag, tag_no_case, take, take_while, take_while1},
4-
character::streaming::{char, digit1, one_of},
4+
character::streaming::{char, digit1, one_of, space0},
55
combinator::{map, map_res, opt},
66
multi::{separated_list0, separated_list1},
77
sequence::{delimited, preceded, tuple},
@@ -204,7 +204,13 @@ where
204204
F: FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>,
205205
E: nom::error::ParseError<&'a [u8]>,
206206
{
207-
delimited(char('('), separated_list1(char(' '), f), char(')'))
207+
delimited(
208+
char('('),
209+
separated_list1(char(' '), f),
210+
// Targeted lenience: Some real-world IMAP servers
211+
// insert extra whitespace before the closing parenthesis.
212+
tuple((space0, char(')'))),
213+
)
208214
}
209215

210216
pub fn parenthesized_list<'a, F, O, E>(f: F) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], Vec<O>, E>

imap-proto/src/parser/tests.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,13 @@ fn test_incomplete_fetch() {
680680
}
681681
}
682682

683+
#[test]
684+
fn test_fetch_response_whitespace_tolerance() {
685+
// Allow extra whitespace before the closing parenthesis.
686+
let input = b"* 11784 FETCH (UID 87567 INTERNALDATE \"06-May-2023 18:48:30 +0200\" RFC822.SIZE 5799845 )\r\n";
687+
assert!(parse_response(input).is_ok());
688+
}
689+
683690
#[test]
684691
fn test_continuation() {
685692
// regular RFC compliant

0 commit comments

Comments
 (0)