C: review round 1 proxy fixes
Unit names go through the local route's validation, so escaped and dash-rooted units work through the proxy; backslashes stay legal only there. Response preparation (status classification, content-type check, size/stream bounds) moves into proxy.rs for REST and MCP to share. x-slinuxm-peer is only set for configured peers, proxied SSE is no-cache, and the unreachable handler fallbacks are gone.
This commit is contained in:
1 parent
9be74b9dd8
commit
7a4d64c051
3 files changed
+405
-153
No files matched your search
+240
-68
@@ -13,7 +13,7 @@ use std::time::Duration;
|
||||
|
||||
use tokio::time::Instant;
|
||||
|
||||
use axum::http::{HeaderMap, HeaderName, HeaderValue};
|
||||
use axum::http::{HeaderMap, HeaderName, HeaderValue, StatusCode};
|
||||
use bytes::Bytes;
|
||||
use futures::stream::{BoxStream, Stream, StreamExt};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
@@ -23,6 +23,7 @@ use super::registry::ProxyOpts;
|
||||
use super::transport::{PeerBody, PeerRequest, PeerResponse, PeerTransport};
|
||||
use super::types::{PeerConfig, PeerName};
|
||||
use crate::error::AppError;
|
||||
use crate::sources::systemd::validate_unit_name;
|
||||
|
||||
const HOP_HEADER: &str = "x-slinuxm-hop";
|
||||
const TRACEPARENT: &str = "traceparent";
|
||||
@@ -106,6 +107,54 @@ pub(crate) async fn get(
|
||||
}
|
||||
}
|
||||
|
||||
/// A peer response shaped for a surface: status already classified, the
|
||||
/// peer's content type checked, body bounded. REST and MCP both build their
|
||||
/// response from this so neither owns a second copy of the limits.
|
||||
pub(crate) struct PreparedResponse {
|
||||
pub status: StatusCode,
|
||||
pub content_type: Option<HeaderValue>,
|
||||
pub body: PreparedBody,
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub(crate) fn prepare_response(
|
||||
peer: &PeerName,
|
||||
resp: PeerResponse,
|
||||
max_bytes: u64,
|
||||
deadline: Instant,
|
||||
shutdown: CancellationToken,
|
||||
) -> 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));
|
||||
}
|
||||
};
|
||||
let content_type = content_type(peer, resp.content_type.as_deref())?;
|
||||
let body = prepare_body(peer, resp.body, max_bytes, deadline, shutdown)?;
|
||||
Ok(PreparedResponse {
|
||||
status,
|
||||
content_type,
|
||||
body,
|
||||
})
|
||||
}
|
||||
|
||||
fn content_type(peer: &PeerName, value: Option<&str>) -> Result<Option<HeaderValue>, AppError> {
|
||||
match value {
|
||||
None => Ok(None),
|
||||
Some(value) => HeaderValue::from_str(value)
|
||||
.map(Some)
|
||||
.map_err(|_| peer_error_to_app(peer, &PeerError::Malformed(String::new()))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Buffered bodies over the cap become `Oversize`. Streams are cut off at the
|
||||
/// cap, at `deadline`, or when `shutdown` fires, because a follow holds a
|
||||
/// connection until the peer closes it.
|
||||
@@ -188,43 +237,73 @@ fn canonical_route(path: &str) -> Option<String> {
|
||||
if path.len() > 512 || encoding_forbidden(path) {
|
||||
return None;
|
||||
}
|
||||
let decoded = percent_encoding::percent_decode_str(path)
|
||||
.decode_utf8()
|
||||
.ok()?;
|
||||
if !decoded_chars_ok(&decoded) {
|
||||
return None;
|
||||
}
|
||||
let mut rel = decoded.trim_start_matches('/');
|
||||
let mut rel = path.trim_start_matches('/');
|
||||
if let Some(rest) = rel.strip_prefix("api/v1/") {
|
||||
rel = rest;
|
||||
}
|
||||
if rel.is_empty() || rel.split('/').any(|s| !segment_chars_ok(s)) {
|
||||
if rel.is_empty() {
|
||||
return None;
|
||||
}
|
||||
if !route_allowed(rel) {
|
||||
let mut decoded = Vec::new();
|
||||
let mut forward = Vec::new();
|
||||
for raw in rel.split('/') {
|
||||
let segment = percent_encoding::percent_decode_str(raw)
|
||||
.decode_utf8()
|
||||
.ok()?;
|
||||
if !segment_ok(&segment) {
|
||||
return None;
|
||||
}
|
||||
forward.push(percent_encoding::utf8_percent_encode(&segment, SEGMENT_SET).to_string());
|
||||
decoded.push(segment.into_owned());
|
||||
}
|
||||
if !route_allowed(&decoded.join("/")) {
|
||||
return None;
|
||||
}
|
||||
Some(rel.to_owned())
|
||||
// The forward path is re-encoded rather than the caller's text reused so
|
||||
// a backslash from an escaped unit name cannot be read as a separator by
|
||||
// the HTTP client.
|
||||
Some(forward.join("/"))
|
||||
}
|
||||
|
||||
fn segment_chars_ok(segment: &str) -> bool {
|
||||
!segment.is_empty() && segment != "." && segment != ".." && !segment.contains("..")
|
||||
/// Characters that must stay percent-encoded in a forwarded segment.
|
||||
const SEGMENT_SET: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS
|
||||
.add(b' ')
|
||||
.add(b'"')
|
||||
.add(b'#')
|
||||
.add(b'%')
|
||||
.add(b'/')
|
||||
.add(b'<')
|
||||
.add(b'>')
|
||||
.add(b'?')
|
||||
.add(b'[')
|
||||
.add(b'\\')
|
||||
.add(b']')
|
||||
.add(b'^')
|
||||
.add(b'`')
|
||||
.add(b'{')
|
||||
.add(b'|')
|
||||
.add(b'}');
|
||||
|
||||
/// Segment-level rules only; the allowlist below decides which characters a
|
||||
/// segment may carry for its route.
|
||||
fn segment_ok(segment: &str) -> bool {
|
||||
!segment.is_empty()
|
||||
&& segment != "."
|
||||
&& segment != ".."
|
||||
&& !segment
|
||||
.chars()
|
||||
.any(|c| c.is_control() || matches!(c, '%' | '?' | '#' | '/'))
|
||||
}
|
||||
|
||||
fn decoded_chars_ok(s: &str) -> bool {
|
||||
!s.chars()
|
||||
.any(|c| c.is_control() || c == '\\' || c == '%' || c == '?' || c == '#')
|
||||
}
|
||||
|
||||
/// Encoded slash, backslash, dot-dot, NUL and CRLF, including a second layer
|
||||
/// of encoding (`%252f`). Checked case-insensitively on the raw text.
|
||||
/// Encoded slash, dot-dot, NUL and CRLF, including a second layer of encoding
|
||||
/// (`%252f`, `%255c`). A single `%5c` is not listed: systemd escapes names
|
||||
/// such as `dev-disk-by\x2did.device`, and the local route accepts them, so
|
||||
/// the allowlist decides where a backslash is legal.
|
||||
fn encoding_forbidden(raw: &str) -> bool {
|
||||
let lower = raw.to_ascii_lowercase();
|
||||
[
|
||||
"%2f", "%5c", "%00", "%0a", "%0d", "%2e%2e", "%252f", "%255c",
|
||||
]
|
||||
.iter()
|
||||
.any(|needle| lower.contains(needle))
|
||||
["%2f", "%00", "%0a", "%0d", "%2e%2e", "%252f", "%255c"]
|
||||
.iter()
|
||||
.any(|needle| lower.contains(needle))
|
||||
}
|
||||
|
||||
fn route_allowed(route: &str) -> bool {
|
||||
@@ -235,10 +314,11 @@ fn route_allowed(route: &str) -> bool {
|
||||
|| container_path(route)
|
||||
}
|
||||
|
||||
/// Unit names go through the same validation as the local route.
|
||||
fn unit_path(route: &str) -> bool {
|
||||
route
|
||||
.strip_prefix("systemd/units/")
|
||||
.is_some_and(resource_segment)
|
||||
.is_some_and(|name| validate_unit_name(name).is_ok())
|
||||
}
|
||||
|
||||
fn container_path(route: &str) -> bool {
|
||||
@@ -340,6 +420,27 @@ mod tests {
|
||||
.map(|(_, v)| v.to_str().unwrap())
|
||||
}
|
||||
|
||||
async fn get_with(
|
||||
fake: &FakeTransport,
|
||||
peer: &PeerConfig,
|
||||
path: &str,
|
||||
query: &[(String, String)],
|
||||
) -> Result<PeerResponse, AppError> {
|
||||
get(
|
||||
fake,
|
||||
peer,
|
||||
path,
|
||||
query,
|
||||
ProxyOpts::default(),
|
||||
Duration::from_secs(1),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
fn forwarded(fake: &FakeTransport) -> Vec<String> {
|
||||
fake.calls().into_iter().map(|(_, req)| req.path).collect()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn adds_hop_and_trace_context_but_no_credentials() {
|
||||
let fake = FakeTransport::new();
|
||||
@@ -376,16 +477,9 @@ mod tests {
|
||||
let fake = FakeTransport::new();
|
||||
fake.peer("beta")
|
||||
.respond_err("/systemd/units", PeerError::Rejected);
|
||||
let err = get(
|
||||
fake.as_ref(),
|
||||
&peer_cfg(),
|
||||
"/systemd/units",
|
||||
&[],
|
||||
ProxyOpts::default(),
|
||||
Duration::from_secs(1),
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
let err = get_with(fake.as_ref(), &peer_cfg(), "/systemd/units", &[])
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), "unavailable");
|
||||
assert_eq!(err.message(), "peer beta: peer rejected credentials");
|
||||
}
|
||||
@@ -405,22 +499,19 @@ mod tests {
|
||||
"/docker/containers/web/logs",
|
||||
"/api/v1/systemd/units",
|
||||
"systemd/units/user@100.service",
|
||||
// Names the local route accepts must pass here too: `-` roots and
|
||||
// escaped unit names, as text and percent-encoded.
|
||||
"systemd/units/-.mount",
|
||||
"/systemd/units/-.slice",
|
||||
"systemd/units/foo\\x2dbar.mount",
|
||||
"systemd/units/mnt-data%5Cx2dbackup.mount",
|
||||
"systemd/units/dev-disk-by%5cx2did.device",
|
||||
];
|
||||
for path in ok {
|
||||
get(
|
||||
fake.as_ref(),
|
||||
&peer,
|
||||
path,
|
||||
&[],
|
||||
ProxyOpts::default(),
|
||||
Duration::from_secs(1),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
get_with(fake.as_ref(), &peer, path, &[]).await.unwrap();
|
||||
}
|
||||
let paths: Vec<_> = fake.calls().into_iter().map(|(_, req)| req.path).collect();
|
||||
assert_eq!(
|
||||
paths,
|
||||
forwarded(&fake),
|
||||
vec![
|
||||
"/systemd/units",
|
||||
"/systemd/units/nginx.service",
|
||||
@@ -432,10 +523,90 @@ mod tests {
|
||||
"/docker/containers/web/logs",
|
||||
"/systemd/units",
|
||||
"/systemd/units/user@100.service",
|
||||
"/systemd/units/-.mount",
|
||||
"/systemd/units/-.slice",
|
||||
"/systemd/units/foo%5Cx2dbar.mount",
|
||||
"/systemd/units/mnt-data%5Cx2dbackup.mount",
|
||||
"/systemd/units/dev-disk-by%5Cx2did.device",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unit_names_are_forwarded_in_one_normalised_form() {
|
||||
let fake = FakeTransport::new();
|
||||
let peer = peer_cfg();
|
||||
for path in [
|
||||
"systemd/units/-.mount",
|
||||
"systemd/units/foo\\x2dbar.mount",
|
||||
"systemd/units/foo%5cx2dbar.mount",
|
||||
"/api/v1/systemd/units/mnt-data%5Cx2dbackup.mount",
|
||||
] {
|
||||
get_with(fake.as_ref(), &peer, path, &[]).await.unwrap();
|
||||
}
|
||||
assert_eq!(
|
||||
forwarded(&fake),
|
||||
vec![
|
||||
"/systemd/units/-.mount",
|
||||
"/systemd/units/foo%5Cx2dbar.mount",
|
||||
"/systemd/units/foo%5Cx2dbar.mount",
|
||||
"/systemd/units/mnt-data%5Cx2dbackup.mount",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prepared_response_maps_status_and_checks_the_body() {
|
||||
let name = PeerName::parse("beta").unwrap();
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
let prepare = |status: u16, content_type: Option<&str>, body: &'static [u8]| {
|
||||
prepare_response(
|
||||
&name,
|
||||
PeerResponse {
|
||||
status,
|
||||
content_type: content_type.map(str::to_owned),
|
||||
body: PeerBody::Bytes(Bytes::from_static(body)),
|
||||
},
|
||||
4,
|
||||
deadline,
|
||||
CancellationToken::new(),
|
||||
)
|
||||
};
|
||||
|
||||
let ok = prepare(200, Some("application/json"), b"{}").unwrap();
|
||||
assert_eq!(ok.status, StatusCode::OK);
|
||||
assert_eq!(ok.content_type.unwrap(), "application/json");
|
||||
|
||||
let rows = [
|
||||
(404, "not_found", "peer beta: not found on peer"),
|
||||
(401, "unavailable", "peer beta: peer rejected credentials"),
|
||||
(403, "unavailable", "peer beta: peer rejected credentials"),
|
||||
(500, "unavailable", "peer beta: http_500"),
|
||||
(302, "unavailable", "peer beta: http_302"),
|
||||
(999, "unavailable", "peer beta: http_999"),
|
||||
];
|
||||
for (status, code, message) in rows {
|
||||
let err = match prepare(status, None, b"") {
|
||||
Err(err) => err,
|
||||
Ok(_) => panic!("status {status} must not pass through"),
|
||||
};
|
||||
assert_eq!(err.code(), code, "{status}");
|
||||
assert_eq!(err.message(), message, "{status}");
|
||||
}
|
||||
|
||||
let err = match prepare(200, None, b"0123456789") {
|
||||
Err(err) => err,
|
||||
Ok(_) => panic!("oversize body must not pass through"),
|
||||
};
|
||||
assert_eq!(err.message(), "peer beta: oversize");
|
||||
|
||||
let err = match prepare(200, Some("application/json\r\nX-Injected: 1"), b"{}") {
|
||||
Err(err) => err,
|
||||
Ok(_) => panic!("smuggled content type must not pass through"),
|
||||
};
|
||||
assert_eq!(err.message(), "peer beta: malformed response");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_traversal_excluded_routes_and_bad_query_without_calling() {
|
||||
let fake = FakeTransport::new();
|
||||
@@ -461,18 +632,21 @@ mod tests {
|
||||
"/",
|
||||
"/systemd/units/%2e.",
|
||||
"systemd\\units",
|
||||
// A second encoding layer and control characters.
|
||||
"systemd%252ffollow",
|
||||
"systemd/units/foo%255cbar.mount",
|
||||
"systemd/units/nginx.service%00",
|
||||
"systemd/units/nginx.service%0a",
|
||||
"systemd/units/nginx%0d.service",
|
||||
"systemd/units/foo%25.service",
|
||||
"systemd/units/foo%zz.service",
|
||||
// A backslash is only a legal character inside a unit name.
|
||||
"systemd%5cunits",
|
||||
"journal%5cfollow",
|
||||
"docker/containers/web%5cx2d1",
|
||||
];
|
||||
for path in bad_paths {
|
||||
let err = get(
|
||||
fake.as_ref(),
|
||||
&peer,
|
||||
path,
|
||||
&[],
|
||||
ProxyOpts::default(),
|
||||
Duration::from_secs(1),
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
let err = get_with(fake.as_ref(), &peer, path, &[]).await.unwrap_err();
|
||||
assert_eq!(err.code(), "bad_request", "{path}");
|
||||
assert_eq!(err.message(), "peer beta: path not allowed", "{path}");
|
||||
}
|
||||
@@ -490,18 +664,16 @@ mod tests {
|
||||
vec![("state".into(), "a/b".into())],
|
||||
vec![("state".into(), "a%2fb".into())],
|
||||
vec![("state".into(), "..".into())],
|
||||
vec![("state".into(), "a%252fb".into())],
|
||||
vec![("state".into(), "a%255cb".into())],
|
||||
vec![("state".into(), "a%00b".into())],
|
||||
vec![("state".into(), "a%0ab".into())],
|
||||
vec![("state".into(), "a%0db".into())],
|
||||
];
|
||||
for query in bad_queries {
|
||||
let err = get(
|
||||
fake.as_ref(),
|
||||
&peer,
|
||||
"/systemd/units",
|
||||
&query,
|
||||
ProxyOpts::default(),
|
||||
Duration::from_secs(1),
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
let err = get_with(fake.as_ref(), &peer, "/systemd/units", &query)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), "bad_request", "{query:?}");
|
||||
assert_eq!(err.message(), "peer beta: unsupported query parameter");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user