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
43 changes: 40 additions & 3 deletions poc/reverse-attach-linux/src/attach/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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,
Expand Down Expand Up @@ -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(
Expand All @@ -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 \
Expand Down
42 changes: 42 additions & 0 deletions poc/reverse-attach-linux/src/attach/mint_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Loading