Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions pkgs/id/src/directory_mail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,13 @@ fn expect(reader: &mut impl BufRead, code: u16) -> Result<()> {
"the mail relay closed the connection"
);
let line = line.trim_end();
let code_match = line.get(..3).and_then(|c| c.parse::<u16>().ok()) == Some(code);
let fourth = line.as_bytes().get(3);
ensure!(
line.get(..3).and_then(|c| c.parse::<u16>().ok()) == Some(code),
code_match && matches!(fourth, None | Some(&b' ') | Some(&b'-')),
"the mail relay replied: {line}"
);
if line.as_bytes().get(3) != Some(&b'-') {
if fourth != Some(&b'-') {
return Ok(());
}
}
Expand Down Expand Up @@ -454,4 +456,40 @@ mod tests {
let error = sink.send(&smuggled).unwrap_err();
assert!(error.to_string().contains("recipient"));
}

#[test]
fn expect_enforces_exact_terminator_shape() {
let mut good = std::io::Cursor::new(b"250 OK\r\n");
assert!(expect(&mut good, 250).is_ok());

let mut bare = std::io::Cursor::new(b"250\r\n");
assert!(expect(&mut bare, 250).is_ok());

let mut multiline = std::io::Cursor::new(b"250-first\r\n250 OK\r\n");
assert!(expect(&mut multiline, 250).is_ok());

let mut bad_code = std::io::Cursor::new(b"251 OK\r\n");
assert_eq!(
expect(&mut bad_code, 250).unwrap_err().to_string(),
"the mail relay replied: 251 OK"
);

let mut bad_term_digit = std::io::Cursor::new(b"2500 bogus\r\n");
assert_eq!(
expect(&mut bad_term_digit, 250).unwrap_err().to_string(),
"the mail relay replied: 2500 bogus"
);

let mut bad_term_alpha = std::io::Cursor::new(b"250OK\r\n");
assert_eq!(
expect(&mut bad_term_alpha, 250).unwrap_err().to_string(),
"the mail relay replied: 250OK"
);

let mut multiline_bad_term = std::io::Cursor::new(b"250-first\r\n2500 bogus\r\n");
assert_eq!(
expect(&mut multiline_bad_term, 250).unwrap_err().to_string(),
"the mail relay replied: 2500 bogus"
);
}
}
21 changes: 0 additions & 21 deletions plan.md

This file was deleted.