From 5812df4e6acf64aaf6cb89717ea737f26e000210 Mon Sep 17 00:00:00 2001 From: Nagy Botond <75550932+westernwontons@users.noreply.github.com> Date: Mon, 12 Jun 2023 22:14:43 +0300 Subject: [PATCH] fix(parser): allow `-` (hyphen) to appear in usernames --- distant-net/src/common/destination/parser.rs | 30 +++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/distant-net/src/common/destination/parser.rs b/distant-net/src/common/destination/parser.rs index cdb11a2..4fb5f74 100644 --- a/distant-net/src/common/destination/parser.rs +++ b/distant-net/src/common/destination/parser.rs @@ -69,7 +69,7 @@ fn parse_scheme(s: &str) -> PResult<&str> { fn parse_username_password(s: &str) -> PResult<(Option<&str>, Option<&str>)> { let (auth, remaining) = s.split_once('@').ok_or("Auth missing @")?; - let (auth, username) = maybe(parse_until(|c| !c.is_alphanumeric()))(auth)?; + let (auth, username) = maybe(parse_until(|c| !c.is_alphanumeric() && c != '-'))(auth)?; let (auth, password) = maybe(prefixed( parse_char(':'), parse_until(|c| !c.is_alphanumeric()), @@ -331,6 +331,24 @@ mod tests { assert_eq!(username_password.1, Some("password")); } + #[test] + fn should_return_username_with_hyphen_and_password() { + let (s, username_password) = + parse_username_password("some-user:password@").unwrap(); + assert_eq!(s, ""); + assert_eq!(username_password.0, Some("some-user")); + assert_eq!(username_password.1, Some("password")); + } + + #[test] + fn should_return_username_password_if_username_starts_or_ends_with_hyphen() { + let (s, username_password) = + parse_username_password("-some-user-:password@").unwrap(); + assert_eq!(s, ""); + assert_eq!(username_password.0, Some("-some-user-")); + assert_eq!(username_password.1, Some("password")); + } + #[test] fn should_consume_up_to_the_ending_sequence() { let (s, username_password) = @@ -653,6 +671,16 @@ mod tests { assert_eq!(destination.port, Some(22)); } + #[test] + fn parse_should_succeed_if_given_username_has_hyphen() { + let destination = parse("some-user@example.com:22").unwrap(); + assert_eq!(destination.scheme, None); + assert_eq!(destination.username.as_deref(), Some("some-user")); + assert_eq!(destination.password, None); + assert_eq!(destination.host, "example.com"); + assert_eq!(destination.port, Some(22)); + } + #[test] fn parse_should_succeed_if_given_password_host_and_port() { let destination = parse(":password@example.com:22").unwrap();