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
@@ -496,10 +496,86 @@ async fn proxy_caps_buffered_bodies_and_passes_sse_bytes() {
|
||||
.and_then(|v| v.to_str().ok()),
|
||||
Some("text/event-stream")
|
||||
);
|
||||
// Same cache policy as the local follow routes: no intermediary buffers it.
|
||||
assert_eq!(
|
||||
headers
|
||||
.get(axum::http::header::CACHE_CONTROL)
|
||||
.and_then(|v| v.to_str().ok()),
|
||||
Some("no-cache")
|
||||
);
|
||||
// The cap stops the stream once four bytes have been forwarded.
|
||||
assert_eq!(body, "abcd");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn proxy_forwards_unit_names_the_local_route_accepts() {
|
||||
let fake = FakeTransport::new();
|
||||
fake.peer("beta")
|
||||
.respond("/systemd/units/", 200, "application/json", "{}");
|
||||
let app = proxy_app(fake.clone());
|
||||
for uri in [
|
||||
"/peers/beta/systemd/units/-.mount",
|
||||
"/peers/beta/systemd/units/-.slice",
|
||||
"/peers/beta/systemd/units/mnt-data%5Cx2dbackup.mount",
|
||||
"/peers/beta/systemd/units/foo%5cx2db.mount",
|
||||
] {
|
||||
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 paths: Vec<_> = fake.calls().into_iter().map(|(_, req)| req.path).collect();
|
||||
assert_eq!(
|
||||
paths,
|
||||
vec![
|
||||
"/systemd/units/-.mount",
|
||||
"/systemd/units/-.slice",
|
||||
"/systemd/units/mnt-data%5Cx2dbackup.mount",
|
||||
"/systemd/units/foo%5Cx2db.mount",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/// 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]
|
||||
async fn proxy_maps_non_success_status_instead_of_relaying_it() {
|
||||
let rows = [
|
||||
(404, 404, "not_found", "peer beta: not found on peer"),
|
||||
(
|
||||
401,
|
||||
503,
|
||||
"unavailable",
|
||||
"peer beta: peer rejected credentials",
|
||||
),
|
||||
(
|
||||
403,
|
||||
503,
|
||||
"unavailable",
|
||||
"peer beta: peer rejected credentials",
|
||||
),
|
||||
(500, 503, "unavailable", "peer beta: http_500"),
|
||||
(302, 503, "unavailable", "peer beta: http_302"),
|
||||
];
|
||||
for (peer_status, expected, code, message) in rows {
|
||||
let fake = FakeTransport::new();
|
||||
fake.peer("beta").respond(
|
||||
"/systemd/units",
|
||||
peer_status,
|
||||
"application/json",
|
||||
r#"{"error":{"code":"peer_body"}}"#,
|
||||
);
|
||||
let app = proxy_app(fake);
|
||||
let (status, headers, body) = call(&app, "GET", "/peers/beta/systemd/units", &[]).await;
|
||||
assert_eq!(status.as_u16(), expected, "{peer_status}");
|
||||
let v = json(&body);
|
||||
assert_eq!(v["error"]["code"], code, "{peer_status}");
|
||||
assert_eq!(v["error"]["message"], message, "{peer_status}");
|
||||
assert!(!body.contains("peer_body"), "{peer_status}");
|
||||
assert!(headers.get(axum::http::header::LOCATION).is_none());
|
||||
assert_eq!(peer_header(&headers), Some("beta"), "{peer_status}");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn proxy_unknown_peer_and_smuggled_content_type() {
|
||||
let fake = FakeTransport::new();
|
||||
@@ -509,11 +585,20 @@ async fn proxy_unknown_peer_and_smuggled_content_type() {
|
||||
"application/json\r\nX-Injected: 1",
|
||||
"{}",
|
||||
);
|
||||
let app = proxy_app(fake);
|
||||
let (status, headers, body) = call(&app, "GET", "/peers/nope/systemd/units", &[]).await;
|
||||
assert_eq!(status, StatusCode::NOT_FOUND);
|
||||
assert_eq!(json(&body)["error"]["message"], "peer nope: unknown peer");
|
||||
assert_eq!(peer_header(&headers), Some("nope"));
|
||||
let app = proxy_app(fake.clone());
|
||||
// No peer header names a peer that does not exist, and no call is made.
|
||||
for peer in ["nope", "Beta", "-beta"] {
|
||||
let uri = format!("/peers/{peer}/systemd/units");
|
||||
let (status, headers, body) = call(&app, "GET", &uri, &[]).await;
|
||||
assert_eq!(status, StatusCode::NOT_FOUND, "{peer}");
|
||||
assert_eq!(
|
||||
json(&body)["error"]["message"],
|
||||
format!("peer {peer}: unknown peer"),
|
||||
"{peer}"
|
||||
);
|
||||
assert_eq!(peer_header(&headers), None, "{peer}");
|
||||
}
|
||||
assert!(fake.calls().is_empty());
|
||||
let (status, headers, body) = call(&app, "GET", "/peers/beta/systemd/units", &[]).await;
|
||||
assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
|
||||
assert_eq!(
|
||||
|
||||
+75
-80
@@ -8,18 +8,16 @@ use std::time::Duration;
|
||||
|
||||
use axum::body::Body;
|
||||
use axum::extract::{Path, Request, State};
|
||||
use axum::http::{header, HeaderName, HeaderValue, StatusCode};
|
||||
use axum::http::{header, HeaderName, HeaderValue};
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::routing::get;
|
||||
use axum::{Json, Router};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::peers::error::peer_error_to_app;
|
||||
use crate::peers::proxy::{self, PreparedBody};
|
||||
use crate::peers::transport::PeerResponse;
|
||||
use crate::peers::proxy::{self, PreparedBody, PreparedResponse};
|
||||
use crate::peers::types::{PeerName, PeerStatus};
|
||||
use crate::peers::ProxyOpts;
|
||||
use crate::peers::{PeerRegistry, ProxyOpts};
|
||||
use crate::state::AppState;
|
||||
|
||||
pub fn routes() -> Router<AppState> {
|
||||
@@ -34,22 +32,25 @@ struct PeerList {
|
||||
peers: Vec<PeerStatus>,
|
||||
}
|
||||
|
||||
fn registry(st: &AppState) -> Result<&crate::peers::PeerRegistry, AppError> {
|
||||
/// Only reachable with a registry: `api::router` merges these routes on
|
||||
/// `state.peers.is_some()` and the field is fixed for the process lifetime.
|
||||
fn registry(st: &AppState) -> &PeerRegistry {
|
||||
st.peers
|
||||
.as_deref()
|
||||
.ok_or_else(|| AppError::NotFound("no such route".into()))
|
||||
.expect("peer routes are mounted only with a peer registry")
|
||||
}
|
||||
|
||||
async fn list(State(st): State<AppState>) -> Result<Json<PeerList>, AppError> {
|
||||
let peers = registry(&st)?.snapshot();
|
||||
Ok(Json(PeerList { peers }))
|
||||
async fn list(State(st): State<AppState>) -> Json<PeerList> {
|
||||
Json(PeerList {
|
||||
peers: registry(&st).snapshot(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn one(
|
||||
State(st): State<AppState>,
|
||||
Path(peer): Path<String>,
|
||||
) -> Result<Json<PeerStatus>, AppError> {
|
||||
Ok(Json(registry(&st)?.status(&peer)?))
|
||||
Ok(Json(registry(&st).status(&peer)?))
|
||||
}
|
||||
|
||||
async fn pass(
|
||||
@@ -57,31 +58,40 @@ async fn pass(
|
||||
Path((peer, _path)): Path<(String, String)>,
|
||||
request: Request,
|
||||
) -> Response {
|
||||
with_peer_header(&peer, pass_through(&st, &peer, request).await)
|
||||
let reg = registry(&st);
|
||||
// The registry parses and looks the name up in one call, so an unknown or
|
||||
// malformed name gets the contract's 404 and no peer header: the response
|
||||
// does not come from a peer.
|
||||
let name = match reg.status(&peer) {
|
||||
Ok(status) => status.name,
|
||||
Err(err) => return err.into_response(),
|
||||
};
|
||||
with_peer_header(&name, pass_through(reg, &name, &st, request).await)
|
||||
}
|
||||
|
||||
/// Every outcome for a proxy call names the peer, including consolidator-side
|
||||
/// errors (bad path, dead peer), so a client can tell which peer failed.
|
||||
fn with_peer_header(peer: &str, mut response: Response) -> Response {
|
||||
if let Ok(value) = HeaderValue::from_str(peer) {
|
||||
response
|
||||
.headers_mut()
|
||||
.insert(HeaderName::from_static(proxy::PEER_HEADER), value);
|
||||
}
|
||||
fn with_peer_header(name: &PeerName, mut response: Response) -> Response {
|
||||
let value = HeaderValue::from_str(name.as_str()).expect("validated peer name");
|
||||
response
|
||||
.headers_mut()
|
||||
.insert(HeaderName::from_static(proxy::PEER_HEADER), value);
|
||||
response
|
||||
}
|
||||
|
||||
async fn pass_through(st: &AppState, peer: &str, request: Request) -> Response {
|
||||
let Some(reg) = st.peers.clone() else {
|
||||
return AppError::NotFound("no such route".into()).into_response();
|
||||
};
|
||||
if let Err(err) = proxy::reject_inbound_hop(peer, request.headers()) {
|
||||
async fn pass_through(
|
||||
reg: &PeerRegistry,
|
||||
name: &PeerName,
|
||||
st: &AppState,
|
||||
request: Request,
|
||||
) -> Response {
|
||||
if let Err(err) = proxy::reject_inbound_hop(name.as_str(), request.headers()) {
|
||||
return err.into_response();
|
||||
}
|
||||
let Some(raw_path) = raw_peer_path(request.uri().path()) else {
|
||||
return AppError::BadRequest(format!("peer {peer}: path not allowed")).into_response();
|
||||
return AppError::BadRequest(format!("peer {name}: path not allowed")).into_response();
|
||||
};
|
||||
let query = match proxy::query_from_raw(peer, request.uri().query()) {
|
||||
let query = match proxy::query_from_raw(name.as_str(), request.uri().query()) {
|
||||
Ok(query) => query,
|
||||
Err(err) => return err.into_response(),
|
||||
};
|
||||
@@ -89,12 +99,50 @@ async fn pass_through(st: &AppState, peer: &str, request: Request) -> Response {
|
||||
traceparent: inbound_traceparent(request.headers()),
|
||||
accept_sse: accepts_event_stream(request.headers()),
|
||||
};
|
||||
match reg.get(peer, raw_path, &query, opts).await {
|
||||
Ok(resp) => proxy_response(st, peer, resp),
|
||||
let resp = match reg.get(name.as_str(), raw_path, &query, opts).await {
|
||||
Ok(resp) => resp,
|
||||
Err(err) => return err.into_response(),
|
||||
};
|
||||
let deadline =
|
||||
tokio::time::Instant::now() + Duration::from_secs(st.config.limits.max_follow_secs);
|
||||
match proxy::prepare_response(
|
||||
name,
|
||||
resp,
|
||||
reg.config().max_response_bytes,
|
||||
deadline,
|
||||
st.shutdown.clone(),
|
||||
) {
|
||||
Ok(prepared) => prepared_response(prepared),
|
||||
Err(err) => err.into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
fn prepared_response(prepared: PreparedResponse) -> Response {
|
||||
let sse = prepared
|
||||
.content_type
|
||||
.as_ref()
|
||||
.is_some_and(|value| value.as_bytes() == b"text/event-stream");
|
||||
let body = match prepared.body {
|
||||
PreparedBody::Bytes(bytes) => Body::from(bytes),
|
||||
PreparedBody::Stream(stream) => Body::from_stream(stream),
|
||||
};
|
||||
let mut response = Response::new(body);
|
||||
*response.status_mut() = prepared.status;
|
||||
if let Some(content_type) = prepared.content_type {
|
||||
response
|
||||
.headers_mut()
|
||||
.insert(header::CONTENT_TYPE, content_type);
|
||||
}
|
||||
// A follow must not be buffered or cached by an intermediary; the local
|
||||
// routes get this from axum's `Sse`.
|
||||
if sse {
|
||||
response
|
||||
.headers_mut()
|
||||
.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache"));
|
||||
}
|
||||
response
|
||||
}
|
||||
|
||||
/// Path after `/peers/{name}/`, still percent-encoded. Nesting strips
|
||||
/// `/api/v1` before this runs, so both the mounted server and the bare API
|
||||
/// router yield `/peers/...`.
|
||||
@@ -130,59 +178,6 @@ fn accepts_event_stream(headers: &axum::http::HeaderMap) -> bool {
|
||||
})
|
||||
}
|
||||
|
||||
fn proxy_response(st: &AppState, peer: &str, resp: PeerResponse) -> Response {
|
||||
let name = match PeerName::parse(peer) {
|
||||
Ok(name) => name,
|
||||
Err(_) => {
|
||||
return AppError::NotFound(format!("peer {peer}: unknown peer")).into_response();
|
||||
}
|
||||
};
|
||||
let max_bytes = st
|
||||
.peers
|
||||
.as_ref()
|
||||
.map(|reg| reg.config().max_response_bytes)
|
||||
.unwrap_or(0);
|
||||
let deadline =
|
||||
tokio::time::Instant::now() + Duration::from_secs(st.config.limits.max_follow_secs);
|
||||
let status = match StatusCode::from_u16(resp.status) {
|
||||
Ok(status) => status,
|
||||
Err(_) => {
|
||||
return peer_error_to_app(&name, &crate::peers::PeerError::Http(resp.status))
|
||||
.into_response();
|
||||
}
|
||||
};
|
||||
let content_type = match content_type_header(&name, resp.content_type.as_deref()) {
|
||||
Ok(value) => value,
|
||||
Err(err) => return err.into_response(),
|
||||
};
|
||||
let body = match proxy::prepare_body(&name, resp.body, max_bytes, deadline, st.shutdown.clone())
|
||||
{
|
||||
Ok(PreparedBody::Bytes(bytes)) => Body::from(bytes),
|
||||
Ok(PreparedBody::Stream(stream)) => Body::from_stream(stream),
|
||||
Err(err) => return err.into_response(),
|
||||
};
|
||||
let mut response = Response::new(body);
|
||||
*response.status_mut() = status;
|
||||
if let Some(content_type) = content_type {
|
||||
response
|
||||
.headers_mut()
|
||||
.insert(header::CONTENT_TYPE, content_type);
|
||||
}
|
||||
response
|
||||
}
|
||||
|
||||
fn content_type_header(
|
||||
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, &crate::peers::PeerError::Malformed(String::new()))
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use axum::http::StatusCode;
|
||||
|
||||
+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