From 9b1cd4120845758d578f4aaa716d50c98df527dd Mon Sep 17 00:00:00 2001 From: chaodu-agent <274062505+chaodu-agent@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:25:44 -0500 Subject: [PATCH 1/3] fix: accept percent-encoded runtime path prefixes in wss mint (#168) The mint refused any runtime containing `%`, so a valid percent-encoded path prefix such as `ws://host/pod%20a` stopped minting. The runtime is now parsed for percent escapes: `%XX` is allowed in the path prefix only, malformed escapes are refused, and the decoded path must not carry userinfo, query, fragment or control characters. The admin credential is also checked against the decoded runtime, so an encoded credential cannot be persisted in grants.json. Left open on #168 for the owner: minimum credential length, trust root alignment between mint and dial, and the two LOW items on error body detail and redaction. Tests: - wss_mint_accepts_a_percent_encoded_runtime_prefix (fails on main) - a_percent_encoded_credential_inside_the_runtime_is_refused_before_any_request (fails on main) - a_malformed_or_smuggling_percent_escape_in_the_runtime_is_refused (passes on main, guards the refusal) Rust: 601 passed, 1 failed (proc::tests::a_pid_that_does_not_exist_signals_nothing_and_does_not_panic), which also fails on current main. Swift: swift build and swift test, 852 executed, 0 failures. --- poc/reverse-attach-linux/src/attach/client.rs | 42 +++++++++++++++++-- .../src/attach/mint_tests.rs | 42 +++++++++++++++++++ 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/poc/reverse-attach-linux/src/attach/client.rs b/poc/reverse-attach-linux/src/attach/client.rs index 619db7a..54ae7a3 100644 --- a/poc/reverse-attach-linux/src/attach/client.rs +++ b/poc/reverse-attach-linux/src/attach/client.rs @@ -65,6 +65,28 @@ fn redact(text: &str, credential: &str) -> String { } } +/// Decodes `%XX` escapes, or returns None when an escape is not two hex digits. +fn percent_decode(text: &str) -> Option { + let bytes = text.as_bytes(); + let mut out = Vec::with_capacity(bytes.len()); + let mut i = 0; + while i < bytes.len() { + if bytes[i] == b'%' { + let hex = bytes.get(i + 1..i + 3)?; + if !hex.iter().all(u8::is_ascii_hexdigit) { + return None; + } + let byte = u8::from_str_radix(std::str::from_utf8(hex).ok()?, 16).ok()?; + out.push(byte); + i += 3; + } else { + out.push(bytes[i]); + i += 1; + } + } + Some(String::from_utf8_lossy(&out).into_owned()) +} + pub(crate) fn mint( runtime: &str, session: &str, @@ -111,10 +133,22 @@ fn mint_inner( let rest = strip_trailing_slashes(rest); // The runtime string is never echoed below: it must not carry userinfo, a query or a // fragment, so nothing in it can be a second credential or a redirect of the request. + // Percent escapes are allowed in the path prefix only. The decoded path must pass the + // same check, so `%40` or `%3F` cannot smuggle userinfo or a query past it. + let has_bad_char = |s: &str| { + s.chars().any(|c| { + c.is_control() || c.is_whitespace() || matches!(c, '@' | '?' | '#' | '\\') + }) + }; + let host_part = rest.split('/').next().unwrap_or(rest); + let decoded = percent_decode(rest); if rest.is_empty() || rest.starts_with('/') - || rest.chars().any(|c| { - c.is_control() || c.is_whitespace() || matches!(c, '@' | '?' | '#' | '\\' | '%') + || host_part.contains('%') + || has_bad_char(rest) + || decoded.as_deref().map_or(true, |d| { + d.chars() + .any(|c| c.is_control() || matches!(c, '@' | '?' | '#' | '\\')) }) { return Err( @@ -130,7 +164,9 @@ fn mint_inner( // admin credential must not be a substring of either: refuse before any request, and // without echoing either value. if !admin_credential.is_empty() - && (runtime.contains(admin_credential) || session.contains(admin_credential)) + && (runtime.contains(admin_credential) + || decoded.as_deref().is_some_and(|d| d.contains(admin_credential)) + || session.contains(admin_credential)) { return Err( "runtime and session must not contain the admin credential; the runtime address \ diff --git a/poc/reverse-attach-linux/src/attach/mint_tests.rs b/poc/reverse-attach-linux/src/attach/mint_tests.rs index 5edda2a..28bdcaa 100644 --- a/poc/reverse-attach-linux/src/attach/mint_tests.rs +++ b/poc/reverse-attach-linux/src/attach/mint_tests.rs @@ -573,3 +573,45 @@ fn only_ws_and_wss_runtimes_are_accepted() { assert!(err.contains("unsupported runtime scheme"), "{err}"); } } + +#[test] +fn wss_mint_accepts_a_percent_encoded_runtime_prefix() { + // A valid percent-encoded path prefix is sent as written, not refused. + let p = pki(); + let server = fake(Some(&p), ok_reply); + let runtime = format!("wss://127.0.0.1:{}/pod%20a", server.port); + mint_with(&trust_for(&p), &runtime, "s", CRED, 60).expect("wss mint with %20 prefix"); + assert!(server.requests()[0].starts_with("POST /pod%20a/admin/sessions/s/tools-attach ")); +} + +#[test] +fn a_percent_encoded_credential_inside_the_runtime_is_refused_before_any_request() { + // The runtime is persisted in grants.json, so the credential must be caught in its + // decoded form too. + let p = pki(); + let server = fake(None, ok_reply); + let encoded: String = CRED.bytes().map(|b| format!("%{b:02X}")).collect(); + let runtime = format!("ws://127.0.0.1:{}/{encoded}", server.port); + let err = mint_with(&trust_for(&p), &runtime, "s", CRED, 60).unwrap_err(); + assert!(err.contains("must not contain"), "{err}"); + assert_no_credential(&err); + assert!(server.requests().is_empty()); +} + +#[test] +fn a_malformed_or_smuggling_percent_escape_in_the_runtime_is_refused() { + let p = pki(); + let server = fake(None, ok_reply); + for runtime in [ + format!("ws://127.0.0.1:{}/pod%zz", server.port), + format!("ws://127.0.0.1:{}/pod%2", server.port), + format!("ws://127.0.0.1:{}/pod%40evil", server.port), + format!("ws://127.0.0.1:{}/pod%3Fq", server.port), + format!("ws://127.0.0.1:{}/pod%00", server.port), + format!("ws://127.0.0.1%3A{}", server.port), + ] { + let err = mint_with(&trust_for(&p), &runtime, "s", CRED, 60).unwrap_err(); + assert!(err.contains("no user info, query or fragment"), "{err}"); + } + assert!(server.requests().is_empty()); +} From 8b09e3a139bc4282e75f178b010ec79d5b54facc Mon Sep 17 00:00:00 2001 From: chaodu-agent <274062505+chaodu-agent@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:47:14 -0500 Subject: [PATCH 2/3] style: rustfmt the percent-decoding check in wss mint (#168) cargo fmt --check failed in CI on the #168 change. Formatting only. Re-run on this head: cargo fmt --check passes. cargo test: 601 passed, 1 failed (proc::tests::a_pid_that_does_not_exist_signals_nothing_and_does_not_panic, also fails on current main). Swift: swift build, swift test: 852 executed, 0 failures. --- poc/reverse-attach-linux/src/attach/client.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/poc/reverse-attach-linux/src/attach/client.rs b/poc/reverse-attach-linux/src/attach/client.rs index 54ae7a3..5996413 100644 --- a/poc/reverse-attach-linux/src/attach/client.rs +++ b/poc/reverse-attach-linux/src/attach/client.rs @@ -136,9 +136,8 @@ fn mint_inner( // Percent escapes are allowed in the path prefix only. The decoded path must pass the // same check, so `%40` or `%3F` cannot smuggle userinfo or a query past it. let has_bad_char = |s: &str| { - s.chars().any(|c| { - c.is_control() || c.is_whitespace() || matches!(c, '@' | '?' | '#' | '\\') - }) + s.chars() + .any(|c| c.is_control() || c.is_whitespace() || matches!(c, '@' | '?' | '#' | '\\')) }; let host_part = rest.split('/').next().unwrap_or(rest); let decoded = percent_decode(rest); @@ -165,7 +164,9 @@ fn mint_inner( // without echoing either value. if !admin_credential.is_empty() && (runtime.contains(admin_credential) - || decoded.as_deref().is_some_and(|d| d.contains(admin_credential)) + || decoded + .as_deref() + .is_some_and(|d| d.contains(admin_credential)) || session.contains(admin_credential)) { return Err( From ece9a9f28661a167dfeee998398e958fc10d0fec Mon Sep 17 00:00:00 2001 From: chaodu-agent <274062505+chaodu-agent@users.noreply.github.com> Date: Mon, 23 Nov 2026 19:16:28 -0500 Subject: [PATCH 3/3] fix: use is_none_or in wss mint runtime check (#168) CI clippy (-D warnings) rejected map_or(true, ..) as unnecessary_map_or. Same behaviour. Re-run on this head: cargo fmt --check passes; cargo clippy --all-targets -D warnings passes; cargo test: 601 passed, 1 failed (proc::tests::a_pid_that_does_not_exist_signals_nothing_and_does_not_panic, also fails on current main). One run also failed liveness_tests::calls_running_when_the_socket_closes_are_cancelled under load; it passes 5 of 5 when run alone and passed on the next full run. Swift: swift build, swift test: 852 executed, 0 failures. --- poc/reverse-attach-linux/src/attach/client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/poc/reverse-attach-linux/src/attach/client.rs b/poc/reverse-attach-linux/src/attach/client.rs index 5996413..41f73f1 100644 --- a/poc/reverse-attach-linux/src/attach/client.rs +++ b/poc/reverse-attach-linux/src/attach/client.rs @@ -145,7 +145,7 @@ fn mint_inner( || rest.starts_with('/') || host_part.contains('%') || has_bad_char(rest) - || decoded.as_deref().map_or(true, |d| { + || decoded.as_deref().is_none_or(|d| { d.chars() .any(|c| c.is_control() || matches!(c, '@' | '?' | '#' | '\\')) })