From 0f31e2bf2eac3dbec5183ce40d0d7febf9797908 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 09:49:41 -0600 Subject: [PATCH] fix(id): invite names refuse control and format characters (#1770) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Invite::new now rejects category Cc (char::is_control) and category Cf characters on the raw name, before trimming, so an edge tab/newline/CR is refused rather than trimmed away and an embedded control or invisible format character cannot ride the invite onto the /invite trust prompt or into the prefilled petname. The Cf set is petname's existing is_format_char predicate (Unicode 16), now pub(crate) and shared instead of duplicated; ordinary spaces keep the long-standing trim. parse/from_query inherit the refusal through Invite::new, including percent-encoded controls. Tests: direct Cc embedded/edge cases, representative Cf (soft hyphen, ZWSP, bidi override, BOM, tag char), percent-encoded tab/newline/CR/Cf through both from_query and parse, and space-trimming preserved. Local cargo test could not run in the laptop environment (lib-test target fails identically on clean main under default features; all-features additionally needs the nix-built web/dist) — verification is via the PR's nix checks per the fleet CI-first rule. --- pkgs/id/src/invite.rs | 88 ++++++++++++++++++++++++++++++++++++++++++ pkgs/id/src/petname.rs | 5 ++- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/pkgs/id/src/invite.rs b/pkgs/id/src/invite.rs index 267bb157aa..af389bad69 100644 --- a/pkgs/id/src/invite.rs +++ b/pkgs/id/src/invite.rs @@ -27,9 +27,22 @@ impl Invite { /// # Errors /// /// Fails if either key is not 64 hex characters, or the name is empty or too long. + /// Also fails if the name carries a control (category Cc) or format + /// (category Cf) character anywhere, checked on the raw name before + /// trimming: an edge tab or newline is refused, not trimmed away — + /// the name lands on the `/invite` trust prompt and is prefilled as + /// a petname, and petnames refuse the same characters (#1770). pub fn new(key: &str, node: &str, name: &str) -> Result { let key = hex_key(key).context("the account key")?; let node = hex_key(node).context("the node ID")?; + ensure!( + !name.chars().any(char::is_control), + "an invite name cannot contain control characters" + ); + ensure!( + !name.chars().any(crate::petname::is_format_char), + "an invite name cannot contain format characters" + ); let name = name.trim(); ensure!(!name.is_empty(), "an invite needs a name"); ensure!( @@ -268,4 +281,79 @@ mod tests { .unwrap(); assert_eq!(invite.name, "Ann"); } + + #[test] + fn control_characters_are_refused_before_trimming() { + // Embedded Cc, and Cc at the edges where trim() would + // otherwise have removed it silently (#1770). + for name in [ + "Ann\tLee", + "Ann\nLee", + "Ann\rLee", + "Ann\u{7}Lee", + "\tAnn", + "Ann\n", + "\rAnn Lee", + "\u{b}Ann", + ] { + assert!( + Invite::new(KEY, NODE, name).is_err(), + "control character accepted in {name:?}" + ); + } + } + + #[test] + fn format_characters_are_refused() { + // Representative Cf: soft hyphen, zero-width space, a bidi + // override, the BOM/zero-width no-break space, a tag char. + for name in [ + "Ann\u{ad}Lee", + "Ann\u{200b}Lee", + "Ann\u{202e}Lee", + "\u{feff}Ann", + "Ann\u{e0041}", + ] { + assert!( + Invite::new(KEY, NODE, name).is_err(), + "format character accepted in {name:?}" + ); + } + } + + #[test] + fn control_and_format_characters_are_refused_through_queries() { + // The same refusals through the percent-encoded parse paths: + // %09 tab, %0A newline, %0D CR, %E2%80%8B U+200B, %C2%AD + // U+00AD — at the edges as well as embedded. + for encoded in [ + "Ann%09Lee", + "Ann%0ALee", + "Ann%0DLee", + "%09Ann", + "Ann%0A", + "Ann%E2%80%8BLee", + "%C2%ADAnn", + ] { + let query = format!("key={KEY}&node={NODE}&name={encoded}"); + assert!( + Invite::from_query(&query).is_err(), + "encoded control/format character accepted in {encoded}" + ); + assert!( + Invite::parse(&format!("id:invite?{query}")).is_err(), + "encoded control/format character accepted via parse in {encoded}" + ); + } + } + + #[test] + fn ordinary_spaces_still_trim() { + // The Cc/Cf refusal is on the raw name; plain spaces keep the + // long-standing trim behaviour (#1770). + let invite = Invite::new(KEY, NODE, " Ann Lee ").unwrap(); + assert_eq!(invite.name, "Ann Lee"); + let invite = Invite::from_query(&format!("key={KEY}&node={NODE}&name=+Ann+Lee+")).unwrap(); + assert_eq!(invite.name, "Ann Lee"); + } } diff --git a/pkgs/id/src/petname.rs b/pkgs/id/src/petname.rs index a120842ba2..8d25fa0222 100644 --- a/pkgs/id/src/petname.rs +++ b/pkgs/id/src/petname.rs @@ -239,8 +239,9 @@ fn looks_like(name: &str) -> anyhow::Error { /// `char::is_control` covers only category Cc, and Cf characters are /// invisible or reorder the text around them, so a name carrying one /// can render exactly like another name. Ranges are the Cf set of -/// Unicode 16. -fn is_format_char(c: char) -> bool { +/// Unicode 16. Shared with the other name boundaries (invite names, +/// #1770) so every surface refuses the same set. +pub(crate) fn is_format_char(c: char) -> bool { matches!(c, '\u{00AD}' | '\u{0600}'..='\u{0605}'