diff --git a/poc/reverse-attach-linux/src/attach/client.rs b/poc/reverse-attach-linux/src/attach/client.rs index 619db7a..41f73f1 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,21 @@ 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().is_none_or(|d| { + d.chars() + .any(|c| c.is_control() || matches!(c, '@' | '?' | '#' | '\\')) }) { return Err( @@ -130,7 +163,11 @@ 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()); +}