Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(core): email and hostname lexers were too permissive
Elijah Potter0d2b7442c3a9
diff --git a/harper-core/src/lexing/email_address.rs b/harper-core/src/lexing/email_address.rsindex df2eb20ac..92038116d 100644--- a/harper-core/src/lexing/email_address.rs+++ b/harper-core/src/lexing/email_address.rs@@ -16,6 +16,10 @@ pub fn lex_email_address(source: &[char]) -> Option<FoundToken> {let domain_part_len = lex_hostname(&source[at_loc + 1..])?;+ if domain_part_len == 0 {+ return None;+ }+Some(FoundToken {next_index: at_loc + 1 + domain_part_len,token: TokenKind::EmailAddress,@@ -155,6 +159,18 @@ mod tests {}}+ #[test]+ fn does_not_allow_empty_domain() {+ for local in example_local_parts() {+ // Generate invalid email address+ let mut address = local.clone();+ address.push('@');+ address.push(' ');++ assert!(lex_email_address(&address).is_none());+ }+ }+/// Tests that the email parser will not throw a panic under some random/// situations.#[test]diff --git a/harper-core/src/lexing/hostname.rs b/harper-core/src/lexing/hostname.rsindex 5534fbe5f..961955da5 100644--- a/harper-core/src/lexing/hostname.rs+++ b/harper-core/src/lexing/hostname.rs@@ -28,6 +28,13 @@ pub fn lex_hostname_token(source: &[char]) -> Option<FoundToken> {pub fn lex_hostname(source: &[char]) -> Option<usize> {let mut passed_chars = 0;+ // The beginning has different requirements from the rest of the hostname.+ let first = source.first()?;++ if !matches!(first, 'A'..='Z' | 'a'..='z' | '0'..='9' ) {+ return None;+ }+for label in source.split(|c| *c == '.') {for c in label {passed_chars += 1;@@ -78,4 +85,10 @@ pub mod tests {assert_eq!(lex_hostname(&domain), Some(domain.len()));}}++ #[test]+ fn hyphen_cannot_open_hostname() {+ let host: Vec<_> = "-something.com".chars().collect();+ assert!(lex_hostname(&host).is_none())+ }}