C: review round 2 proxy query unit rule and shared status classifier

Proxied journal/unit values now use the local unit-name helper instead of a
second, stricter check, so escaped units pass through as text, %5C or %5c.
The peer status table is one function shared with the transport, and the
segment/encoding cases left untested get allowlist and rejection cases.
This commit is contained in:
ari committed 2026-10-19 22:35:51 -04:00
1 parent da2e8d7be2
commit 8b7569363c
4 files changed
+210 -16

No files matched your search

+54
View File
@@ -518,6 +518,7 @@ async fn proxy_forwards_unit_names_the_local_route_accepts() {
"/peers/beta/systemd/units/-.slice",
"/peers/beta/systemd/units/mnt-data%5Cx2dbackup.mount",
"/peers/beta/systemd/units/foo%5cx2db.mount",
"/peers/beta/systemd/units/foo\\x2db.mount",
] {
let (status, headers, body) = call(&app, "GET", uri, &[]).await;
assert_eq!(status, StatusCode::OK, "{uri}: {body}");
@@ -531,10 +532,63 @@ async fn proxy_forwards_unit_names_the_local_route_accepts() {
"/systemd/units/-.slice",
"/systemd/units/mnt-data%5Cx2dbackup.mount",
"/systemd/units/foo%5Cx2db.mount",
"/systemd/units/foo%5Cx2db.mount",
]
);
}
/// The journal `unit` filter is a unit name, so the proxy must accept the
/// escaped forms the local routes accept, whether the caller sends `%5C` or a
/// literal backslash.
#[tokio::test]
async fn proxy_forwards_journal_unit_values_the_local_route_accepts() {
let fake = FakeTransport::new();
fake.peer("beta")
.respond("/journal/follow", 200, "text/event-stream", "")
.respond("/journal", 200, "application/json", "[]");
let app = proxy_app(fake.clone());
for uri in [
"/peers/beta/journal?unit=mnt-data%5Cx2dbackup.mount",
"/peers/beta/journal?unit=mnt-data%5cx2dbackup.mount",
"/peers/beta/journal?unit=mnt-data\\x2dbackup.mount",
"/peers/beta/journal?unit=dev-disk-by%5Cx2did.device&priority=err",
"/peers/beta/journal/follow?unit=user@100.service",
] {
let (status, headers, body) = call(&app, "GET", uri, &[]).await;
assert_eq!(status, StatusCode::OK, "{uri}: {body}");
assert_eq!(peer_header(&headers), Some("beta"), "{uri}");
}
let queries: Vec<_> = fake.calls().into_iter().map(|(_, req)| req.query).collect();
assert_eq!(
queries,
vec![
vec![("unit".into(), "mnt-data\\x2dbackup.mount".into())],
vec![("unit".into(), "mnt-data\\x2dbackup.mount".into())],
vec![("unit".into(), "mnt-data\\x2dbackup.mount".into())],
vec![
("unit".into(), "dev-disk-by\\x2did.device".into()),
("priority".into(), "err".into()),
],
vec![("unit".into(), "user@100.service".into())],
]
);
// Still filtered: traversal text in the value and an unknown key.
for uri in [
"/peers/beta/journal?unit=../x",
"/peers/beta/journal?unit=nginx.service&token=abc",
] {
let (status, _, body) = call(&app, "GET", uri, &[]).await;
assert_eq!(status, StatusCode::BAD_REQUEST, "{uri}: {body}");
assert_eq!(
json(&body)["error"]["message"],
"peer beta: unsupported query parameter",
"{uri}"
);
}
assert_eq!(fake.calls().len(), 5);
}
/// The contract promises the peer's status only for 2xx; a transport that
/// returns anything else must not turn into a redirect or a peer error body.
#[tokio::test]
+148 -15
View File
@@ -116,6 +116,16 @@ pub(crate) struct PreparedResponse {
pub body: PreparedBody,
}
/// One table for peer statuses: the transport classifies a live response with
/// it and [`prepare_response`] checks again, so neither side can drift.
pub(crate) fn classify_status(status: u16) -> PeerError {
match status {
404 => PeerError::NotFound,
401 | 403 => PeerError::Rejected,
code => PeerError::Http(code),
}
}
/// The contract promises pass-through only for 2xx, so anything else is
/// classified here rather than relayed: a transport that forgot to map a
/// redirect or the peer's own error body must not reach the caller.
@@ -128,14 +138,7 @@ pub(crate) fn prepare_response(
) -> Result<PreparedResponse, AppError> {
let status = match StatusCode::from_u16(resp.status) {
Ok(status) if status.is_success() => status,
_ => {
let class = match resp.status {
404 => PeerError::NotFound,
401 | 403 => PeerError::Rejected,
code => PeerError::Http(code),
};
return Err(peer_error_to_app(peer, &class));
}
_ => return Err(peer_error_to_app(peer, &classify_status(resp.status))),
};
let content_type = content_type(peer, resp.content_type.as_deref())?;
let body = prepare_body(peer, resp.body, max_bytes, deadline, shutdown)?;
@@ -367,7 +370,11 @@ fn allow_query(
let mut out = Vec::with_capacity(query.len());
for (key, value) in query {
let known = allowed.iter().any(|name| *name == key);
if !known || !query_key_ok(key) || !query_value_ok(value) || seen.contains(&key.as_str()) {
if !known
|| !query_key_ok(key)
|| !query_value_ok(key, value)
|| seen.contains(&key.as_str())
{
return Err(query_rejected(peer.as_str()));
}
seen.push(key.as_str());
@@ -384,13 +391,21 @@ fn query_key_ok(key: &str) -> bool {
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')
}
fn query_value_ok(value: &str) -> bool {
value.len() <= 1024
&& !value
.chars()
.any(|c| c.is_control() || c == '\\' || c == '/')
/// Values arrive decoded and leave as encoded query pairs, so only control
/// characters and a second encoding layer are unsafe here. The journal `unit`
/// filter is a unit name, so it gets the local route's unit check verbatim:
/// that is where a backslash (systemd's escape) is legal.
fn query_value_ok(key: &str, value: &str) -> bool {
if value.len() > 1024 || encoding_forbidden(value) {
return false;
}
if key == "unit" {
return validate_unit_name(value).is_ok();
}
!value
.chars()
.any(|c| c.is_control() || c == '\\' || c == '/')
&& !value.contains("..")
&& !encoding_forbidden(value)
}
fn route_is_follow(route: &str, query: &[(String, String)]) -> bool {
@@ -506,6 +521,10 @@ mod tests {
"systemd/units/foo\\x2dbar.mount",
"systemd/units/mnt-data%5Cx2dbackup.mount",
"systemd/units/dev-disk-by%5cx2did.device",
// A `..` inside a name is not traversal: segments cannot contain
// a slash and the forwarded path is re-encoded from them.
"systemd/units/foo..bar.mount",
"/docker/containers/foo..bar",
];
for path in ok {
get_with(fake.as_ref(), &peer, path, &[]).await.unwrap();
@@ -528,6 +547,8 @@ mod tests {
"/systemd/units/foo%5Cx2dbar.mount",
"/systemd/units/mnt-data%5Cx2dbackup.mount",
"/systemd/units/dev-disk-by%5Cx2did.device",
"/systemd/units/foo..bar.mount",
"/docker/containers/foo..bar",
]
);
}
@@ -640,6 +661,9 @@ mod tests {
"systemd/units/nginx%0d.service",
"systemd/units/foo%25.service",
"systemd/units/foo%zz.service",
// An encoded `..` anywhere is a second encoding layer, even when
// it sits inside one segment rather than being the segment.
"systemd/units/foo%2e%2ebar.mount",
// A backslash is only a legal character inside a unit name.
"systemd%5cunits",
"journal%5cfollow",
@@ -721,6 +745,115 @@ mod tests {
assert!(calls[2].1.accept.is_none());
}
#[tokio::test]
async fn journal_unit_values_are_forwarded_like_the_local_route() {
let fake = FakeTransport::new();
let peer = peer_cfg();
// Raw text as an MCP caller would pass it, and the same values in a
// request target: `%5C` and a literal backslash both decode to one
// backslash, which is what systemd uses to escape a unit name.
let raw_queries = [
"unit=mnt-data%5Cx2dbackup.mount",
"unit=mnt-data%5cx2dbackup.mount",
"unit=mnt-data\\x2dbackup.mount",
"unit=dev-disk-by%5Cx2did.device&priority=err",
"unit=-.mount",
"unit=user@100.service",
];
for raw in raw_queries {
let query = query_from_raw("beta", Some(raw)).unwrap();
get_with(fake.as_ref(), &peer, "/journal", &query)
.await
.unwrap();
}
get_with(
fake.as_ref(),
&peer,
"/journal/follow",
&[("unit".into(), "mnt-data\\x2dbackup.mount".into())],
)
.await
.unwrap();
assert_eq!(
fake.calls()
.into_iter()
.map(|(_, req)| req.query)
.collect::<Vec<_>>(),
vec![
vec![("unit".into(), "mnt-data\\x2dbackup.mount".into())],
vec![("unit".into(), "mnt-data\\x2dbackup.mount".into())],
vec![("unit".into(), "mnt-data\\x2dbackup.mount".into())],
vec![
("unit".into(), "dev-disk-by\\x2did.device".into()),
("priority".into(), "err".into()),
],
vec![("unit".into(), "-.mount".into())],
vec![("unit".into(), "user@100.service".into())],
vec![("unit".into(), "mnt-data\\x2dbackup.mount".into())],
]
);
}
#[tokio::test]
async fn journal_unit_values_still_reject_unsafe_text() {
let fake = FakeTransport::new();
let peer = peer_cfg();
let bad = [
// Not a unit name, so the local unit rule rejects it.
"unit=../x",
"unit=a/b",
"unit=nginx",
"unit=nginx.service%2fx",
"unit=mnt-data%5Cx2d.mount%2e",
"unit=mnt-data%0ax2d.mount",
"unit=a%255cb.mount",
];
for raw in bad {
// Some values are stopped while the raw query is parsed and the
// rest by the allowlist; both are the same 400.
let err = match query_from_raw("beta", Some(raw)) {
Ok(query) => get_with(fake.as_ref(), &peer, "/journal", &query)
.await
.unwrap_err(),
Err(err) => err,
};
assert_eq!(err.code(), "bad_request", "{raw}");
assert_eq!(
err.message(),
"peer beta: unsupported query parameter",
"{raw}"
);
}
// Values that reach the check already decoded (the MCP path) are
// rejected there rather than by the raw parser.
for value in [
"mnt-data\u{0}x2d.mount",
"mnt-data\nx2d.mount",
"mnt-data\rx2d.mount",
"mnt-data\x2d.mount/../../etc/passwd",
] {
let err = get_with(
fake.as_ref(),
&peer,
"/journal",
&[("unit".into(), value.into())],
)
.await
.unwrap_err();
assert_eq!(err.code(), "bad_request", "{value:?}");
}
assert!(fake.calls().is_empty());
}
#[test]
fn status_classification_is_one_table() {
assert!(matches!(classify_status(404), PeerError::NotFound));
assert!(matches!(classify_status(401), PeerError::Rejected));
assert!(matches!(classify_status(403), PeerError::Rejected));
assert!(matches!(classify_status(302), PeerError::Http(302)));
assert!(matches!(classify_status(200), PeerError::Http(200)));
}
#[tokio::test]
async fn slow_peer_is_a_timeout() {
let fake = FakeTransport::new();