Skip to content
Merged
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
11 changes: 6 additions & 5 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5707,9 +5707,9 @@ user. Text fields are trimmed and empty optional fields dropped first;
| --- | --- |
| `host` | 1 to 1024 bytes of ASCII letters, digits and `._-:%`, not starting with `-` |
| `port` | 1 to 65535; absent keeps the SSH configuration's port |
| `user` | at most 1024 bytes of ASCII letters, digits and `._-`, starting with a letter, digit or `_` |
| `user` | at most 1024 bytes: a login name of ASCII letters, digits and `._-`, starting with a letter, digit or `_`, or a Windows domain account `DOMAIN\name` or `name@domain`, where the domain is a host-name-like word of letters, digits and `._-` starting with a letter or digit |
| `identityFile` | at most 1024 bytes, no control characters; quoted for OpenSSH when it has spaces |
| `proxyJump` | `none`, or comma-separated `[user@]host[:port]`; absent keeps the SSH configuration's jump hosts |
| `proxyJump` | `none`, or comma-separated `[user@]host[:port]`, where `user` is a login name (ssh starts jump hosts through a shell, so no domain account); absent keeps the SSH configuration's jump hosts |
| `connectTimeoutSeconds` | 1 to 600; absent is 15 |
| `keepAliveSeconds` | 1 to 3600; absent keeps the transport's default |

Expand Down Expand Up @@ -5918,9 +5918,10 @@ connection?, fields }`:
operating system tag. `color` is the nearest connection color.
- `status` is `importable` (every setting is imported), `partial` (the
connection is imported without some settings), `exists` (the saved entry
`existingId` probably reaches the same host) or `unsupported` (not an SSH
host, or Lithe refuses its address). `connection` is what the import saves
and is absent for `unsupported`.
`existingId` probably reaches the same host), `unsupported` (not an SSH
host) or `invalid` (an SSH host with a setting Lithe refuses, which its
field marks `invalid`). `connection` is what the import saves and is absent
for `unsupported` and `invalid`.
- `fields` lists every setting of the host as `{ motty, value, lithe?,
skip? }`: `lithe` names the connection field it becomes, or `skip` says why
it is not imported: `secret` (stored in Motty's vault), `noSetting`,
Expand Down
49 changes: 49 additions & 0 deletions rust/crates/remote/src/client/connections/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ fn private_key_connections_offer_only_the_chosen_key() {
assert_eq!(option_value(&command, "BatchMode").as_deref(), Some("no"));
}

#[test]
fn windows_domain_users_reach_ssh_unchanged() {
// ssh splits the destination at the last `@`, so both spellings Windows
// OpenSSH servers take for a domain account arrive as the user.
for user in ["CONTOSO\\admin", "admin@contoso.example"] {
let command = command_line(json!({ "host": "192.0.2.10", "user": user }));
assert_eq!(
command[command.len() - 2],
format!("{user}@192.0.2.10"),
"user {user}"
);
}
}

#[test]
fn other_authentication_methods_select_matching_ssh_options() {
let password = command_line(json!({ "host": "h", "authentication": "password" }));
Expand Down Expand Up @@ -183,6 +197,41 @@ fn malformed_connections_name_the_field_before_ssh_starts() {
),
(json!({ "host": "box", "user": "a b" }), "user", Invalid),
(json!({ "host": "box", "user": "a:b" }), "user", Invalid),
// A domain account has one domain, before `\` or after `@`, and a
// login name; OpenSSH refuses a trailing backslash.
(
json!({ "host": "box", "user": "CONTOSO\\" }),
"user",
Invalid,
),
(json!({ "host": "box", "user": "\\admin" }), "user", Invalid),
(
json!({ "host": "box", "user": "A\\B\\admin" }),
"user",
Invalid,
),
(
json!({ "host": "box", "user": "CONTOSO\\admin@contoso.example" }),
"user",
Invalid,
),
(json!({ "host": "box", "user": "admin@" }), "user", Invalid),
(
json!({ "host": "box", "user": "@contoso" }),
"user",
Invalid,
),
(json!({ "host": "box", "user": "a@b@c" }), "user", Invalid),
(
json!({ "host": "box", "user": "-oProxyCommand=x\\admin" }),
"user",
Invalid,
),
(
json!({ "host": "box", "user": "CONTOSO\\-oProxyCommand=x" }),
"user",
Invalid,
),
(
json!({ "host": "box", "authentication": "privateKey" }),
"identityFile",
Expand Down
26 changes: 20 additions & 6 deletions rust/crates/remote/src/client/motty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,11 @@ pub enum Status {
Partial,
/// A saved connection probably reaches the same host already.
Exists,
/// The host is not an SSH host, or Lithe refuses its address.
/// The host is not an SSH host.
Unsupported,
/// An SSH host with a setting Lithe refuses; the field is marked
/// [`Skip::Invalid`].
Invalid,
}

/// Why a Motty setting is not imported.
Expand Down Expand Up @@ -222,7 +225,8 @@ pub struct Candidate {
/// The saved connection that probably reaches the same host.
#[serde(skip_serializing_if = "Option::is_none")]
pub existing_id: Option<String>,
/// The connection the import saves; `None` when it is unsupported.
/// The connection the import saves; `None` when it is unsupported or
/// invalid.
#[serde(skip_serializing_if = "Option::is_none")]
pub connection: Option<SshConnection>,
/// Every setting the host has, imported or not.
Expand Down Expand Up @@ -438,19 +442,29 @@ fn candidate(
}

let connection = connection.normalized();
let valid = ssh && connection.check().is_ok();
if ssh && !valid {
if let Some(field) = fields.iter_mut().find(|field| field.motty == "address") {
let refused = if ssh { connection.check().err() } else { None };
if let Some(error) = &refused {
// Each imported field names the connection field it becomes, which
// is the field a refusal names; a refusal of a field Motty does not
// set falls back to the address.
let blamed = fields
.iter()
.position(|field| field.lithe == Some(error.field))
.or_else(|| fields.iter().position(|field| field.motty == "address"));
if let Some(field) = blamed.map(|index| &mut fields[index]) {
field.lithe = None;
field.skip = Some(Skip::Invalid);
}
}
let valid = ssh && refused.is_none();
let existing_id = valid
.then(|| connections.existing(&connection))
.flatten()
.map(|entry| entry.id().to_owned());
let status = if !valid {
let status = if !ssh {
Status::Unsupported
} else if !valid {
Status::Invalid
} else if existing_id.is_some() {
Status::Exists
} else if fields.iter().any(|field| field.skip.is_some()) {
Expand Down
43 changes: 41 additions & 2 deletions rust/crates/remote/src/client/motty/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn settings_lithe_has_no_place_for_are_listed_with_the_reason() {
}

#[test]
fn other_protocols_and_refused_addresses_are_not_imported() {
fn other_protocols_and_refused_settings_are_not_imported() {
let database = Database::new(SUPPORTED_FORMAT);
database.host(
"t",
Expand All @@ -245,14 +245,53 @@ fn other_protocols_and_refused_addresses_are_not_imported() {
1,
json!({ "label": "Bad", "address": "-oProxyCommand=x" }),
);
database.host(
"user",
2,
json!({ "label": "Bad user", "address": "192.0.2.20", "user": "a b" }),
);
let report = database.report(&Connections::default());
let telnet = find(&report, "t");
assert_eq!(telnet.status, Status::Unsupported);
assert_eq!(telnet.connection, None);
assert_eq!(field(telnet, "protocol").skip, Some(Skip::Protocol));
let bad = find(&report, "bad");
assert_eq!(bad.status, Status::Unsupported);
assert_eq!(bad.status, Status::Invalid);
assert_eq!(bad.connection, None);
assert_eq!(field(bad, "address").skip, Some(Skip::Invalid));
// The refusal names the setting that Lithe refuses, not the address.
let user = find(&report, "user");
assert_eq!(user.status, Status::Invalid);
assert_eq!(user.connection, None);
assert_eq!(field(user, "user").skip, Some(Skip::Invalid));
assert_eq!(field(user, "user").lithe, None);
assert_eq!(field(user, "address").skip, None);
assert_eq!(field(user, "address").lithe, Some("host"));
}

#[test]
fn windows_domain_users_are_imported() {
let database = Database::new(SUPPORTED_FORMAT);
database.host(
"prefix",
0,
json!({ "label": "Prefix", "address": "192.0.2.30", "user": "CONTOSO\\admin" }),
);
database.host(
"suffix",
1,
json!({ "label": "Suffix", "address": "192.0.2.31", "user": "admin@contoso.example" }),
);
let report = database.report(&Connections::default());
for (key, user) in [
("prefix", "CONTOSO\\admin"),
("suffix", "admin@contoso.example"),
] {
let candidate = find(&report, key);
assert_eq!(candidate.status, Status::Importable, "host {key}");
let connection = candidate.connection.as_ref().expect("imported connection");
assert_eq!(connection.user.as_deref(), Some(user), "host {key}");
}
}

#[test]
Expand Down
38 changes: 30 additions & 8 deletions rust/crates/remote/src/client/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ enum OptionSyntax {
Count,
/// TCP port from 1 to 65535.
Port,
/// Login name, as checked by [`is_ssh_user`].
/// User name, as checked by [`is_ssh_user`].
User,
/// Local file path, already quoted by the caller where ssh_config needs it.
Path,
Expand Down Expand Up @@ -217,10 +217,10 @@ pub(crate) fn is_wsl_distribution(name: &str) -> bool {
name.len() <= MAX_WSL_DISTRIBUTION_BYTES && is_plain_name(name)
}

/// A Linux account name `wsl.exe --user` can take: the login names
/// [`is_ssh_user`] accepts, at most [`MAX_WSL_USER_BYTES`] long.
/// A Linux account name `wsl.exe --user` can take: a [`is_login_name`] at
/// most [`MAX_WSL_USER_BYTES`] long.
pub(crate) fn is_wsl_user(user: &str) -> bool {
user.len() <= MAX_WSL_USER_BYTES && is_ssh_user(user)
user.len() <= MAX_WSL_USER_BYTES && is_login_name(user)
}

/// Connection through the system OpenSSH client. Host aliases, jump hosts,
Expand Down Expand Up @@ -704,9 +704,30 @@ fn is_port(value: &str) -> bool {
is_decimal(value) && value.parse::<u16>().is_ok_and(|port| port != 0)
}

/// A login name that ssh cannot read as an option or a host separator.
/// A user name for an SSH destination: a [`is_login_name`], or a Windows
/// domain account written `DOMAIN\name` or `name@domain`, as Windows OpenSSH
/// servers expect it, where the domain is a plain name.
///
/// ssh splits `user@host` at the last `@`, so `name@domain@host` keeps the
/// domain in the user, and OpenSSH accepts a backslash in a user name unless
/// it is the last character. The destination and the `User` option reach ssh
/// as one argument each, never through a shell, and ssh's option parser keeps
/// a backslash that does not escape a quote, a backslash or a space.
pub(crate) fn is_ssh_user(user: &str) -> bool {
let mut characters = user.chars();
if let Some((domain, name)) = user.split_once('\\') {
return is_plain_name(domain) && is_login_name(name);
}
match user.split_once('@') {
Some((name, domain)) => is_login_name(name) && is_plain_name(domain),
None => is_login_name(user),
}
}

/// A POSIX login name: ASCII letters, digits, `.`, `_` and `-`, starting with
/// a letter, a digit or `_`. A shell reads none of its characters specially,
/// and ssh cannot read it as an option or a host separator.
pub(crate) fn is_login_name(name: &str) -> bool {
let mut characters = name.chars();
matches!(characters.next(), Some(first) if first.is_ascii_alphanumeric() || first == '_')
&& characters
.all(|character| character.is_ascii_alphanumeric() || "._-".contains(character))
Expand All @@ -718,7 +739,8 @@ pub(crate) fn is_jumps(value: &str) -> bool {
}

/// `[user@]host[:port]`, where `host` is a host name, an SSH alias or a
/// bracketed IPv6 address.
/// bracketed IPv6 address, and `user` is a [`is_login_name`]: ssh starts a
/// jump through a shell command line, where a backslash is an escape.
fn is_jump_host(jump: &str) -> bool {
let (user, address) = match jump.rsplit_once('@') {
Some((user, address)) => (Some(user), address),
Expand Down Expand Up @@ -746,7 +768,7 @@ fn is_jump_host(jump: &str) -> bool {
} else {
is_plain_name(host)
};
user.is_none_or(is_ssh_user) && host_valid && port.is_none_or(is_port)
user.is_none_or(is_login_name) && host_valid && port.is_none_or(is_port)
}

/// Letters, digits, `.`, `_` and `-`, starting with a letter or digit. Host
Expand Down
Loading