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
48 changes: 34 additions & 14 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5729,28 +5729,44 @@ clients keep them in one machine-local document, read it, and publish the
validates the document and applies every edit.

`remote.connections` accepts `{ document: string|null }` and returns
`{ connections, storage: { relativePath: "remote/connections.json",
`{ connections, sections, storage: { relativePath: "remote/connections.json",
maximumBytes: 262144 } }`. `connections` is the list in the user's order;
each entry is `{ id, name, connection, recentFolders? }`, where `connection`
is the object `remote.probe` takes and `recentFolders` holds at most 10
normalized absolute host paths, most recent first.
each entry is `{ id, name, connection, color?, group?, recentFolders? }`,
where `connection` is the object `remote.probe` takes and `recentFolders`
holds at most 10 normalized absolute host paths, most recent first. `color`
is one of `red`, `orange`, `yellow`, `green`, `blue` and `purple`; clients
pick the shade for their light and dark themes. `group` is the name of the
group the window lists the entry under. Neither field reaches SSH.

`sections` is the order the window shows the entries in:
`[{ group: string|null, ids: [id] }]`, one section per group in the order the
groups first appear in the list, then a `null` section for the entries
without a group. Groups that differ only in letter case share a section,
named with the first entry's spelling. An empty list has no sections; a list
without groups has the single `null` section. Clients render the sections as
given and do not regroup or reorder the entries.

`remote.editConnections` accepts `{ document }` with exactly one of:

- `save: { id?, name, connection }` normalizes and checks the connection,
then adds an entry with a new id or replaces the entry `id` in place.
`name` is trimmed, 1 to 128 characters without control characters, and
unique among the entries regardless of case. Replacing an entry with
another `host`, `port`, `user` or `proxyJump` clears its recent folders,
which belong to the target and not to the name.
- `save: { id?, name, connection, color?, group? }` normalizes and checks
the connection, then adds an entry with a new id or replaces the entry `id`
in place. `name` is trimmed, 1 to 128 characters without control
characters, and unique among the entries regardless of case. Replacing an
entry with another `host`, `port`, `user` or `proxyJump` clears its recent
folders, which belong to the target and not to the name. A missing `color`
or `group` saves the entry without one, so a save always carries the
entry's current values. `group` is trimmed, and a blank group is no group;
otherwise it is at most 64 characters without control characters, and it
takes the spelling of another entry's group that differs only in letter
case.
- `remove: { id }` removes the entry; an absent id changes nothing.
- `rememberFolder: { id, path }` moves `path` to the front of the entry's
recent folders and keeps 10.

The result is `{ changed, document: string|null, connections, id? }`; `id`
names the entry a save wrote. An unchanged edit returns a `null` document and
must not create a source. Requests never carry a secret: a password field is
an unknown field and is rejected.
The result is `{ changed, document: string|null, connections, sections,
id? }`; `id` names the entry a save wrote. An unchanged edit returns a `null`
document and must not create a source. Requests never carry a secret: a
password field is an unknown field and is rejected.

Bounds: 256 KiB per document and 128 entries. A document that cannot be
decoded, has duplicate keys, unknown fields, another version, a malformed
Expand All @@ -5759,6 +5775,10 @@ refused edit is `invalid_request`. Both carry `details` as the JSON
`{ field, reason }`, where `reason` is `required`, `invalid`, `tooLong`,
`duplicate`, `full` (128 entries) or `unknown` (no entry has the id).

The document stays at version 1 and omits `color` and `group` when they are
unset. A build from before these fields refuses a document that sets one as
having an unknown field.

Native clients keep the document below their platform user-preference
directory, never on a host, in a workspace or in an installed package. This
user-settings resource does not affect code signatures or update deltas.
Expand Down
132 changes: 128 additions & 4 deletions rust/crates/remote/src/client/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
//! validates and edits it; the platform adapter reads it and publishes edits
//! atomically.
//!
//! - An entry may carry a [`Label`]: an accent color and a group. Neither
//! reaches SSH; [`Connections::sections`] turns the groups into the
//! headings the window lists the entries under.
//!
//! Every refusal names the field and the reason, so a client can explain each
//! one in the user's language next to the field it concerns.

Expand All @@ -39,6 +43,9 @@ const MAX_CONNECTIONS: usize = 128;
/// Longest connection name, in characters.
const MAX_NAME_CHARS: usize = 128;

/// Longest group name, in characters.
const MAX_GROUP_CHARS: usize = 64;

/// Longest host, user, private key path or jump host list, in bytes.
const MAX_FIELD_BYTES: usize = 1024;

Expand Down Expand Up @@ -81,6 +88,46 @@ impl Authentication {
}
}

/// Accent color the window marks an entry with. Clients pick the shade for
/// their light and dark themes.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum Color {
/// Red.
Red,
/// Orange.
Orange,
/// Yellow.
Yellow,
/// Green.
Green,
/// Blue.
Blue,
/// Purple.
Purple,
}

/// How the window marks and groups an entry. `None` leaves the entry
/// without a color or outside every group.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Label {
/// Accent color of the entry.
pub color: Option<Color>,
/// Group the entry is listed under. Trimmed before it is saved; a blank
/// group is no group.
pub group: Option<String>,
}

/// The entries listed under one heading, in the user's order.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Section<'a> {
/// The group's name; `None` for the entries without a group.
pub group: Option<&'a str>,
/// Ids of the entries in the section.
pub ids: Vec<&'a str>,
}

/// One SSH connection: the host, the account and how to authenticate.
/// Names stay in [`SavedConnection`]; secrets are never part of it.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
Expand Down Expand Up @@ -338,6 +385,12 @@ pub struct SavedConnection {
/// Name the user gave, unique among the saved connections.
name: String,
connection: SshConnection,
/// Accent color the window marks the entry with.
#[serde(default, skip_serializing_if = "Option::is_none")]
color: Option<Color>,
/// Group the entry is listed under. Groups compare regardless of case.
#[serde(default, skip_serializing_if = "Option::is_none")]
group: Option<String>,
/// Host folders opened over this connection, most recent first.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
recent_folders: Vec<String>,
Expand All @@ -347,6 +400,9 @@ impl SavedConnection {
fn check(&self) -> Result<(), ConnectionError> {
check_id(&self.id)?;
check_name(&self.name)?;
if let Some(group) = &self.group {
check_group(group)?;
}
self.connection.check()?;
if self.recent_folders.len() > MAX_RECENT_FOLDERS {
return Err(ConnectionError::at("recentFolders", Reason::Full));
Expand Down Expand Up @@ -440,21 +496,71 @@ impl Connections {
self.connections.iter().find(|entry| entry.id == id)
}

/// Saves `connection` under `name`, replacing the entry `id` or, without
/// one, appending a new entry with an id from `new_id`. Returns the id.
/// The entries by group: one section per group, in the order the groups
/// first appear, then the entries without a group. An empty list has no
/// sections. Groups that differ only in case share the section, which
/// shows the first entry's spelling.
pub fn sections(&self) -> Vec<Section<'_>> {
let mut sections: Vec<Section<'_>> = Vec::new();
let mut ungrouped = Vec::new();
for entry in &self.connections {
let Some(group) = entry.group.as_deref() else {
ungrouped.push(entry.id.as_str());
continue;
};
match sections
.iter_mut()
.find(|section| section.group.is_some_and(|name| same_name(name, group)))
{
Some(section) => section.ids.push(&entry.id),
None => sections.push(Section {
group: Some(group),
ids: vec![&entry.id],
}),
}
}
if !ungrouped.is_empty() {
sections.push(Section {
group: None,
ids: ungrouped,
});
}
sections
}

/// Saves `connection` under `name` with `label`, replacing the entry
/// `id` or, without one, appending a new entry with an id from `new_id`.
/// Returns the id.
///
/// Renaming keeps the remembered folders; pointing the entry at another
/// host, port, user or jump host forgets them. `new_id` is called until
/// it returns an unused valid id, at most 16 times.
/// host, port, user or jump host forgets them. A group that another entry
/// already uses with other letter case takes that entry's spelling, so
/// the window shows one heading. `new_id` is called until it returns an
/// unused valid id, at most 16 times.
pub fn save(
&mut self,
id: Option<&str>,
name: &str,
connection: SshConnection,
label: Label,
mut new_id: impl FnMut() -> String,
) -> Result<String, ConnectionError> {
let name = name.trim();
check_name(name)?;
let group = match label.group.as_deref().map(str::trim) {
None | Some("") => None,
Some(group) => {
check_group(group)?;
let spelling = self
.connections
.iter()
.filter(|other| Some(other.id.as_str()) != id)
.filter_map(|other| other.group.as_deref())
.find(|other| same_name(other, group))
.unwrap_or(group);
Some(spelling.to_owned())
}
};
let connection = connection.normalized();
connection.check()?;
if self
Expand All @@ -476,6 +582,8 @@ impl Connections {
}
entry.name = name.to_owned();
entry.connection = connection;
entry.color = label.color;
entry.group = group;
Ok(entry.id.clone())
}
None => {
Expand All @@ -490,6 +598,8 @@ impl Connections {
id: id.clone(),
name: name.to_owned(),
connection,
color: label.color,
group,
recent_folders: Vec::new(),
});
Ok(id)
Expand Down Expand Up @@ -546,6 +656,20 @@ fn check_name(name: &str) -> Result<(), ConnectionError> {
Ok(())
}

/// A trimmed group of at most 64 characters without control characters.
fn check_group(group: &str) -> Result<(), ConnectionError> {
if group.is_empty() {
return Err(ConnectionError::at("group", Reason::Required));
}
if group.chars().count() > MAX_GROUP_CHARS {
return Err(ConnectionError::at("group", Reason::TooLong));
}
if group.trim() != group || group.chars().any(char::is_control) {
return Err(ConnectionError::at("group", Reason::Invalid));
}
Ok(())
}

/// Names that differ only in letter case would look the same in a title.
fn same_name(left: &str, right: &str) -> bool {
left.to_lowercase() == right.to_lowercase()
Expand Down
Loading
Loading