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
101 changes: 101 additions & 0 deletions pkgs/fleet/crates/fleet-proxy/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,43 @@ impl From<std::io::Error> for HttpError {
}
}

/// Whether `method` is a valid HTTP method token (RFC 9110
/// `token`): non-empty, `tchar`s only. Anything else — whitespace,
/// a control character, a separator — cannot appear in a request
/// line this client emits (#5750).
pub(crate) fn valid_method(method: &str) -> bool {
!method.is_empty()
&& method.bytes().all(|b| {
b.is_ascii_alphanumeric()
|| matches!(
b,
b'!' | b'#'
| b'$'
| b'%'
| b'&'
| b'\''
| b'*'
| b'+'
| b'-'
| b'.'
| b'^'
| b'_'
| b'`'
| b'|'
| b'~'
)
})
}

/// Whether `path` may appear in a request line: non-empty,
/// origin-form (starts with `/`), and free of spaces, control
/// characters, and DEL — the bytes that would break the line or
/// forge a header (#5750). Query strings and percent-encoded
/// bytes pass unchanged.
pub(crate) fn valid_path(path: &str) -> bool {
path.starts_with('/') && path.bytes().all(|b| b > 0x20 && b != 0x7f)
}

/// One HTTP connection to a laptop service, over a tunnel stream.
pub struct HttpConn {
reader: BufReader<TcpStream>,
Expand Down Expand Up @@ -129,6 +166,18 @@ impl HttpConn {
token: &str,
body: Option<&[u8]>,
) -> Result<(u16, Vec<u8>), HttpError> {
// The method and path are interpolated into the request
// line below: a control character in either is a CRLF
// injection that forges headers or smuggles a whole second
// request onto this pooled connection (#5750). Reject
// before a single byte is written. (The Python broker got
// this from http.client, which validates both.)
if !valid_method(method) || !valid_path(path) {
self.reusable = false;
return Err(HttpError::Malformed(format!(
"invalid request method or path (method {method:?})"
)));
}
let mut head = format!(
"{method} {path} HTTP/1.1\r\n\
Host: {host_header}\r\n\
Expand Down Expand Up @@ -350,6 +399,58 @@ mod tests {
conn.request("GET", "/x", "127.0.0.1", "token", None)
}

#[test]
fn crlf_in_path_is_malformed_before_any_write() {
// #5750: the crafted path that smuggled a second request
// in the probe must now fail at validation; the canned
// peer never sees a request (no response is queued).
let (mut conn, _server) = canned_conn(vec![]);
let err = conn
.request(
"GET",
"/health\r\nX-Injected: yes\r\nContent-Length: 0\r\n\r\nGET /admin HTTP/1.1",
"127.0.0.1",
"token",
None,
)
.unwrap_err();
assert!(matches!(err, HttpError::Malformed(_)), "{err}");
assert!(!conn.reusable());
}

#[test]
fn crlf_in_method_and_space_in_path_are_malformed() {
let (mut conn, _server) = canned_conn(vec![]);
let err = conn
.request("GET\r\nX-Injected: 1", "/x", "127.0.0.1", "token", None)
.unwrap_err();
assert!(matches!(err, HttpError::Malformed(_)), "{err}");
let (mut conn, _server) = canned_conn(vec![]);
let err = conn
.request("GET", "/x y", "127.0.0.1", "token", None)
.unwrap_err();
assert!(matches!(err, HttpError::Malformed(_)), "{err}");
let (mut conn, _server) = canned_conn(vec![]);
let err = conn
.request("GET", "", "127.0.0.1", "token", None)
.unwrap_err();
assert!(matches!(err, HttpError::Malformed(_)), "{err}");
}

#[test]
fn method_and_path_validators_accept_ordinary_traffic() {
assert!(valid_method("GET"));
assert!(valid_method("PROPFIND"));
assert!(!valid_method(""));
assert!(!valid_method("GE T"));
assert!(valid_path("/health"));
assert!(valid_path("/queue/list?state=queued&n=5"));
assert!(valid_path("/files/a%20b"));
assert!(!valid_path(""));
assert!(!valid_path("health"));
assert!(!valid_path("/x\u{7f}"));
}

#[test]
fn well_formed_content_length_round_trips_and_stays_reusable() {
let (mut conn, server) = canned_conn(vec![
Expand Down
14 changes: 14 additions & 0 deletions pkgs/fleet/crates/fleet-proxy/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,20 @@ fn parse_request(frame: &serde_json::Value) -> Result<Request, String> {
.and_then(|v| v.as_str())
.ok_or("request is missing \"path\"")?
.to_string();
// The method/path are interpolated into an HTTP request line
// by HttpConn::request: reject the shapes that would inject
// headers or smuggle a second request here, at the frame
// boundary, before any pool checkout happens (#5750).
if !crate::http::valid_method(&method) {
return Err(format!(
"request \"method\" {method:?} is not an HTTP token"
));
}
if !crate::http::valid_path(&path) {
return Err(format!(
"request \"path\" {path:?} is not a clean origin-form path"
));
}
let body = frame.get("body").filter(|b| !b.is_null()).cloned();
let port = frame
.get("port")
Expand Down