Skip to content

Commit

Permalink
fix(parser): allow - (hyphen) to appear in usernames (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
westernwontons authored Jun 12, 2023
1 parent 4fb9045 commit 8009cc9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `crossbeam-channel` dependency removed from notify by disabling its feature
in order to avoid a `tokio::spawn` issue (https://github.com/notify-rs/notify/issues/380)

### Fixed

- usernames with `-` (hyphen) we're rejected as invalid

## [0.20.0-alpha.7]

### Added
Expand Down
30 changes: 29 additions & 1 deletion distant-net/src/common/destination/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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) =
Expand Down Expand Up @@ -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("[email protected]: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(":[email protected]:22").unwrap();
Expand Down

0 comments on commit 8009cc9

Please sign in to comment.