Skip to content

Commit 3192ee8

Browse files
committed
fix(sandbox): parse bracketed IPv6 authorities in client CONNECT targets
parse_target split the CONNECT authority at the first colon, so an IPv6-literal target like [2001:db8::1]:443 always failed port parsing and IPv6-literal clients could never reach policy evaluation; a regression test even locked in that failure. Parse the RFC 3986 bracketed form and return the host bracket-free, matching what DNS resolution, SSRF validation, NO_PROXY matching, and the upstream CONNECT builder expect. Unclosed brackets, a missing or empty port after the bracket, and non-numeric ports are rejected; unbracketed behavior is unchanged. Replaces the failure-locking test with success coverage for bracketed targets and adds malformed-bracket rejection cases. Signed-off-by: Philippe Martin <[email protected]>
1 parent 5be9905 commit 3192ee8

1 file changed

Lines changed: 43 additions & 8 deletions

File tree

  • crates/openshell-supervisor-network/src

‎crates/openshell-supervisor-network/src/proxy.rs‎

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4948,10 +4948,26 @@ async fn handle_forward_proxy(
49484948
Ok(())
49494949
}
49504950

4951+
/// Parse a CONNECT authority into `(host, port)`.
4952+
///
4953+
/// IPv6 literals use the RFC 3986 bracketed form (`[::1]:443`); the returned
4954+
/// host is bracket-free, matching what DNS resolution, SSRF validation, and
4955+
/// `NO_PROXY` matching expect. Host content is otherwise passed through
4956+
/// unvalidated — policy and resolution decide what it means.
49514957
fn parse_target(target: &str) -> Result<(String, u16)> {
4952-
let (host, port_str) = target
4953-
.split_once(':')
4954-
.ok_or_else(|| miette::miette!("CONNECT target missing port: {target}"))?;
4958+
let (host, port_str) = if let Some(rest) = target.strip_prefix('[') {
4959+
let (host, after) = rest
4960+
.split_once(']')
4961+
.ok_or_else(|| miette::miette!("CONNECT target has unclosed '[': {target}"))?;
4962+
let port_str = after
4963+
.strip_prefix(':')
4964+
.ok_or_else(|| miette::miette!("CONNECT target missing port: {target}"))?;
4965+
(host, port_str)
4966+
} else {
4967+
target
4968+
.split_once(':')
4969+
.ok_or_else(|| miette::miette!("CONNECT target missing port: {target}"))?
4970+
};
49554971
let port: u16 = port_str
49564972
.parse()
49574973
.map_err(|_| miette::miette!("Invalid port in CONNECT target: {target}"))?;
@@ -8337,11 +8353,30 @@ network_policies:
83378353
}
83388354

83398355
#[test]
8340-
fn test_parse_target_ipv6_bracket_notation_fails() {
8341-
assert!(
8342-
parse_target("[::1]:443").is_err(),
8343-
"split_once splits at first colon inside brackets — port parse fails"
8344-
);
8356+
fn test_parse_target_ipv6_bracket_notation() {
8357+
let (host, port) = parse_target("[::1]:443").unwrap();
8358+
assert_eq!(host, "::1", "brackets are stripped from the parsed host");
8359+
assert_eq!(port, 443);
8360+
8361+
let (host, port) = parse_target("[2001:db8::1]:8443").unwrap();
8362+
assert_eq!(host, "2001:db8::1");
8363+
assert_eq!(port, 8443);
8364+
}
8365+
8366+
#[test]
8367+
fn test_parse_target_rejects_malformed_ipv6_brackets() {
8368+
for target in [
8369+
// Unclosed bracket.
8370+
"[::1:443",
8371+
// No port after the bracket.
8372+
"[::1]",
8373+
"[::1]443",
8374+
// Empty or non-numeric port.
8375+
"[::1]:",
8376+
"[::1]:notaport",
8377+
] {
8378+
assert!(parse_target(target).is_err(), "{target} should be rejected");
8379+
}
83458380
}
83468381

83478382
// -- parse_proxy_uri: hostname parser regression tests --

0 commit comments

Comments
 (0)