C: C2 GET-only pass-through proxy with allowlist, hop guard and query filter
This commit is contained in:
1 parent
bec0ab5736
commit
bab6959a94
4 files changed
+962
-36
No files matched your search
+542
-32
@@ -1,24 +1,70 @@
|
||||
//! Proxy one GET to a peer.
|
||||
//!
|
||||
//! The path allowlist, rejection of `..` and encoded slashes, and the query
|
||||
//! allowlist belong in this module. They are not implemented yet: this stub
|
||||
//! forwards the request and maps peer failures. REST and MCP share this
|
||||
//! function, so those checks must not grow a second copy. Caller credentials
|
||||
//! are never forwarded.
|
||||
//! REST and MCP both call [`get`]. The allowlist is the peer's documented
|
||||
//! read routes; `ping` and `peers` are excluded so this cannot chain or
|
||||
//! become an open proxy. Caller credentials are never copied: the transport
|
||||
//! attaches the peer token itself.
|
||||
//!
|
||||
//! Paths are the route under `/api/v1` (`/systemd/units`). A caller that
|
||||
//! includes the `/api/v1` prefix is accepted and that prefix is stripped, so
|
||||
//! the transport always sees one form.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use axum::http::{HeaderName, HeaderValue};
|
||||
use tokio::time::Instant;
|
||||
|
||||
use super::error::peer_error_to_app;
|
||||
use axum::http::{HeaderMap, HeaderName, HeaderValue};
|
||||
use bytes::Bytes;
|
||||
use futures::stream::{BoxStream, Stream, StreamExt};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use super::error::{peer_error_to_app, PeerError};
|
||||
use super::registry::ProxyOpts;
|
||||
use super::transport::{PeerRequest, PeerResponse, PeerTransport};
|
||||
use super::types::PeerConfig;
|
||||
use super::transport::{PeerBody, PeerRequest, PeerResponse, PeerTransport};
|
||||
use super::types::{PeerConfig, PeerName};
|
||||
use crate::error::AppError;
|
||||
|
||||
const HOP_HEADER: &str = "x-slinuxm-hop";
|
||||
const TRACEPARENT: &str = "traceparent";
|
||||
|
||||
/// Reject an inbound hop marker. Any value counts: a consolidator must not
|
||||
/// forward a request that has already been proxied.
|
||||
pub(crate) fn reject_inbound_hop(peer: &str, headers: &HeaderMap) -> Result<(), AppError> {
|
||||
if headers.contains_key(HOP_HEADER) {
|
||||
return Err(chained(peer));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn chained(peer: &str) -> AppError {
|
||||
AppError::BadRequest(format!("peer {peer}: chained requests are not allowed"))
|
||||
}
|
||||
|
||||
fn path_rejected(peer: &PeerName) -> AppError {
|
||||
AppError::BadRequest(format!("peer {peer}: path not allowed"))
|
||||
}
|
||||
|
||||
fn query_rejected(peer: &str) -> AppError {
|
||||
AppError::BadRequest(format!("peer {peer}: unsupported query parameter"))
|
||||
}
|
||||
|
||||
/// Parse a raw query string. Bad encodings are rejected before the pairs are
|
||||
/// handed to [`get`], which checks the allowlist again.
|
||||
pub(crate) fn query_from_raw(
|
||||
peer: &str,
|
||||
raw: Option<&str>,
|
||||
) -> Result<Vec<(String, String)>, AppError> {
|
||||
let Some(raw) = raw.filter(|s| !s.is_empty()) else {
|
||||
return Ok(Vec::new());
|
||||
};
|
||||
if raw.len() > 4096 || encoding_forbidden(raw) || raw.chars().any(|c| c.is_control()) {
|
||||
return Err(query_rejected(peer));
|
||||
}
|
||||
Ok(url::form_urlencoded::parse(raw.as_bytes())
|
||||
.map(|(k, v)| (k.into_owned(), v.into_owned()))
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn header(name: &'static str, value: &str) -> (HeaderName, HeaderValue) {
|
||||
let name = HeaderName::from_static(name);
|
||||
let value = HeaderValue::from_str(value).expect("static header value");
|
||||
@@ -33,41 +79,275 @@ pub(crate) async fn get(
|
||||
opts: ProxyOpts,
|
||||
timeout: Duration,
|
||||
) -> Result<PeerResponse, AppError> {
|
||||
let Validated { path, query, sse } = validate(&peer.name, path, query, opts.accept_sse)?;
|
||||
// Marks the hop so a peer refuses chained consolidation.
|
||||
let mut headers = vec![header(HOP_HEADER, "1")];
|
||||
if let Some(traceparent) = opts.traceparent.as_deref() {
|
||||
if let Ok(value) = HeaderValue::from_str(traceparent) {
|
||||
headers.push((HeaderName::from_static(TRACEPARENT), value));
|
||||
if !value.as_bytes().contains(&b' ') {
|
||||
headers.push((HeaderName::from_static(TRACEPARENT), value));
|
||||
}
|
||||
}
|
||||
}
|
||||
let req = PeerRequest {
|
||||
path: path.to_owned(),
|
||||
query: query.to_vec(),
|
||||
path,
|
||||
query,
|
||||
headers,
|
||||
accept: opts.accept_sse.then(|| "text/event-stream".to_owned()),
|
||||
accept: sse.then(|| "text/event-stream".to_owned()),
|
||||
};
|
||||
transport
|
||||
.get(peer, req, timeout)
|
||||
.await
|
||||
.map_err(|e| peer_error_to_app(&peer.name, &e))
|
||||
let result = tokio::time::timeout(timeout, transport.get(peer, req, timeout)).await;
|
||||
match result {
|
||||
Ok(Ok(resp)) => Ok(resp),
|
||||
Ok(Err(e)) => Err(peer_error_to_app(&peer.name, &e)),
|
||||
Err(_elapsed) => Err(peer_error_to_app(&peer.name, &PeerError::Timeout)),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub(crate) fn prepare_body(
|
||||
peer: &PeerName,
|
||||
body: PeerBody,
|
||||
max_bytes: u64,
|
||||
deadline: Instant,
|
||||
shutdown: CancellationToken,
|
||||
) -> Result<PreparedBody, AppError> {
|
||||
match body {
|
||||
PeerBody::Bytes(bytes) if bytes.len() as u64 > max_bytes => {
|
||||
Err(peer_error_to_app(peer, &PeerError::Oversize))
|
||||
}
|
||||
PeerBody::Bytes(bytes) => Ok(PreparedBody::Bytes(bytes)),
|
||||
PeerBody::Stream(stream) => Ok(PreparedBody::Stream(Box::pin(bound_stream(
|
||||
stream, max_bytes, deadline, shutdown,
|
||||
)))),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) enum PreparedBody {
|
||||
Bytes(Bytes),
|
||||
Stream(BoxStream<'static, Result<Bytes, std::io::Error>>),
|
||||
}
|
||||
|
||||
fn bound_stream(
|
||||
stream: BoxStream<'static, Result<Bytes, PeerError>>,
|
||||
max_bytes: u64,
|
||||
deadline: Instant,
|
||||
shutdown: CancellationToken,
|
||||
) -> impl Stream<Item = Result<Bytes, std::io::Error>> + Send {
|
||||
futures::stream::unfold((stream, 0u64), move |(mut stream, sent)| {
|
||||
let shutdown = shutdown.clone();
|
||||
async move {
|
||||
let next = tokio::select! {
|
||||
biased;
|
||||
() = shutdown.cancelled() => return None,
|
||||
() = tokio::time::sleep_until(deadline) => return None,
|
||||
item = stream.next() => item,
|
||||
};
|
||||
let bytes = match next? {
|
||||
Ok(bytes) => bytes,
|
||||
// Headers are already sent. Dropping the chunk keeps a
|
||||
// token-file path or peer URL out of the body.
|
||||
Err(_) => return None,
|
||||
};
|
||||
if sent.saturating_add(bytes.len() as u64) > max_bytes {
|
||||
return None;
|
||||
}
|
||||
let sent = sent + bytes.len() as u64;
|
||||
Some((Ok(bytes), (stream, sent)))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
struct Validated {
|
||||
path: String,
|
||||
query: Vec<(String, String)>,
|
||||
sse: bool,
|
||||
}
|
||||
|
||||
fn validate(
|
||||
peer: &PeerName,
|
||||
path: &str,
|
||||
query: &[(String, String)],
|
||||
accept_sse: bool,
|
||||
) -> Result<Validated, AppError> {
|
||||
let route = canonical_route(path).ok_or_else(|| path_rejected(peer))?;
|
||||
let query = allow_query(peer, &route, query)?;
|
||||
let sse = accept_sse || route_is_follow(&route, &query);
|
||||
Ok(Validated {
|
||||
path: format!("/{route}"),
|
||||
query,
|
||||
sse,
|
||||
})
|
||||
}
|
||||
|
||||
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('/');
|
||||
if let Some(rest) = rel.strip_prefix("api/v1/") {
|
||||
rel = rest;
|
||||
}
|
||||
if rel.is_empty() || rel.split('/').any(|s| !segment_chars_ok(s)) {
|
||||
return None;
|
||||
}
|
||||
if !route_allowed(rel) {
|
||||
return None;
|
||||
}
|
||||
Some(rel.to_owned())
|
||||
}
|
||||
|
||||
fn segment_chars_ok(segment: &str) -> bool {
|
||||
!segment.is_empty() && segment != "." && segment != ".." && !segment.contains("..")
|
||||
}
|
||||
|
||||
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.
|
||||
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))
|
||||
}
|
||||
|
||||
fn route_allowed(route: &str) -> bool {
|
||||
matches!(
|
||||
route,
|
||||
"systemd/units" | "systemd/timers" | "journal" | "journal/follow" | "docker/ps"
|
||||
) || unit_path(route)
|
||||
|| container_path(route)
|
||||
}
|
||||
|
||||
fn unit_path(route: &str) -> bool {
|
||||
route
|
||||
.strip_prefix("systemd/units/")
|
||||
.is_some_and(resource_segment)
|
||||
}
|
||||
|
||||
fn container_path(route: &str) -> bool {
|
||||
let Some(rest) = route.strip_prefix("docker/containers/") else {
|
||||
return false;
|
||||
};
|
||||
match rest.strip_suffix("/logs") {
|
||||
Some(name) => resource_segment(name),
|
||||
None => resource_segment(rest),
|
||||
}
|
||||
}
|
||||
|
||||
fn resource_segment(name: &str) -> bool {
|
||||
let mut chars = name.chars();
|
||||
let Some(first) = chars.next() else {
|
||||
return false;
|
||||
};
|
||||
first.is_ascii_alphanumeric()
|
||||
&& name
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-' | '@' | ':'))
|
||||
}
|
||||
|
||||
fn allowed_keys(route: &str) -> &'static [&'static str] {
|
||||
match route {
|
||||
"systemd/units" => &["state"],
|
||||
"journal" | "journal/follow" => &["unit", "priority", "lines", "since"],
|
||||
"docker/ps" => &["all"],
|
||||
logs if logs.starts_with("docker/containers/") && logs.ends_with("/logs") => {
|
||||
&["tail", "since", "follow"]
|
||||
}
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
|
||||
fn allow_query(
|
||||
peer: &PeerName,
|
||||
route: &str,
|
||||
query: &[(String, String)],
|
||||
) -> Result<Vec<(String, String)>, AppError> {
|
||||
if query.len() > 8 {
|
||||
return Err(query_rejected(peer.as_str()));
|
||||
}
|
||||
let allowed = allowed_keys(route);
|
||||
let mut seen = Vec::new();
|
||||
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()) {
|
||||
return Err(query_rejected(peer.as_str()));
|
||||
}
|
||||
seen.push(key.as_str());
|
||||
out.push((key.clone(), value.clone()));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn query_key_ok(key: &str) -> bool {
|
||||
!key.is_empty()
|
||||
&& key.len() <= 32
|
||||
&& key
|
||||
.chars()
|
||||
.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 == '/')
|
||||
&& !value.contains("..")
|
||||
&& !encoding_forbidden(value)
|
||||
}
|
||||
|
||||
fn route_is_follow(route: &str, query: &[(String, String)]) -> bool {
|
||||
if route == "journal/follow" {
|
||||
return true;
|
||||
}
|
||||
route.ends_with("/logs")
|
||||
&& query
|
||||
.iter()
|
||||
.any(|(key, value)| key == "follow" && matches!(value.as_str(), "1" | "true"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::peers::testing::{fake_peers_config, FakeTransport};
|
||||
use futures::StreamExt;
|
||||
|
||||
fn peer_cfg() -> PeerConfig {
|
||||
fake_peers_config(&["beta"]).peer.remove(0)
|
||||
}
|
||||
|
||||
fn header_of<'a>(req: &'a PeerRequest, name: &str) -> Option<&'a str> {
|
||||
req.headers
|
||||
.iter()
|
||||
.find(|(k, _)| k.as_str() == name)
|
||||
.map(|(_, v)| v.to_str().unwrap())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn adds_hop_and_trace_context_but_no_credentials() {
|
||||
let fake = FakeTransport::new();
|
||||
let cfg = fake_peers_config(&["beta"]);
|
||||
let peer = peer_cfg();
|
||||
let opts = ProxyOpts {
|
||||
traceparent: Some("00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01".into()),
|
||||
accept_sse: true,
|
||||
};
|
||||
get(
|
||||
fake.as_ref(),
|
||||
&cfg.peer[0],
|
||||
&peer,
|
||||
"/journal/follow",
|
||||
&[("lines".into(), "10".into())],
|
||||
opts,
|
||||
@@ -82,26 +362,20 @@ mod tests {
|
||||
assert_eq!(req.path, "/journal/follow");
|
||||
assert_eq!(req.query, vec![("lines".to_owned(), "10".to_owned())]);
|
||||
assert_eq!(req.accept.as_deref(), Some("text/event-stream"));
|
||||
let header = |name: &str| {
|
||||
req.headers
|
||||
.iter()
|
||||
.find(|(k, _)| k.as_str() == name)
|
||||
.map(|(_, v)| v.to_str().unwrap())
|
||||
};
|
||||
assert_eq!(header("x-slinuxm-hop"), Some("1"));
|
||||
assert!(header("traceparent").unwrap().starts_with("00-"));
|
||||
assert_eq!(header("authorization"), None);
|
||||
assert_eq!(header_of(req, "x-slinuxm-hop"), Some("1"));
|
||||
assert!(header_of(req, "traceparent").unwrap().starts_with("00-"));
|
||||
assert_eq!(header_of(req, "authorization"), None);
|
||||
assert_eq!(header_of(req, "cookie"), None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn maps_rejected_with_peer_name() {
|
||||
let fake = FakeTransport::new();
|
||||
fake.peer("beta")
|
||||
.respond_err("/systemd/units", crate::peers::error::PeerError::Rejected);
|
||||
let cfg = fake_peers_config(&["beta"]);
|
||||
.respond_err("/systemd/units", PeerError::Rejected);
|
||||
let err = get(
|
||||
fake.as_ref(),
|
||||
&cfg.peer[0],
|
||||
&peer_cfg(),
|
||||
"/systemd/units",
|
||||
&[],
|
||||
ProxyOpts::default(),
|
||||
@@ -110,7 +384,243 @@ mod tests {
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), "unavailable");
|
||||
assert!(err.message().contains("beta"));
|
||||
assert_eq!(err.message(), "peer beta: peer rejected credentials");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn allowlist_forwards_documented_routes_only() {
|
||||
let fake = FakeTransport::new();
|
||||
let peer = peer_cfg();
|
||||
let ok = [
|
||||
"/systemd/units",
|
||||
"systemd/units/nginx.service",
|
||||
"/systemd/timers",
|
||||
"/journal",
|
||||
"/journal/follow",
|
||||
"/docker/ps",
|
||||
"/docker/containers/web",
|
||||
"/docker/containers/web/logs",
|
||||
"/api/v1/systemd/units",
|
||||
"systemd/units/user@100.service",
|
||||
];
|
||||
for path in ok {
|
||||
get(
|
||||
fake.as_ref(),
|
||||
&peer,
|
||||
path,
|
||||
&[],
|
||||
ProxyOpts::default(),
|
||||
Duration::from_secs(1),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
let paths: Vec<_> = fake.calls().into_iter().map(|(_, req)| req.path).collect();
|
||||
assert_eq!(
|
||||
paths,
|
||||
vec![
|
||||
"/systemd/units",
|
||||
"/systemd/units/nginx.service",
|
||||
"/systemd/timers",
|
||||
"/journal",
|
||||
"/journal/follow",
|
||||
"/docker/ps",
|
||||
"/docker/containers/web",
|
||||
"/docker/containers/web/logs",
|
||||
"/systemd/units",
|
||||
"/systemd/units/user@100.service",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_traversal_excluded_routes_and_bad_query_without_calling() {
|
||||
let fake = FakeTransport::new();
|
||||
fake.peer("beta")
|
||||
.respond("/systemd/units", 200, "application/json", "{}")
|
||||
.respond("/journal/follow", 200, "application/json", "{}");
|
||||
let peer = peer_cfg();
|
||||
let bad_paths = [
|
||||
"/ping",
|
||||
"/api/v1/ping",
|
||||
"/peers/gamma/systemd/units",
|
||||
"/healthz",
|
||||
"/systemd",
|
||||
"/systemd/units/../timers",
|
||||
"/systemd/../docker/ps",
|
||||
"systemd/units/%2e%2e",
|
||||
"journal%2ffollow",
|
||||
"systemd/units/foo%2Fbar",
|
||||
"/systemd/units//nginx.service",
|
||||
"/docker/containers/a/b",
|
||||
"/journal/follow/extra",
|
||||
"",
|
||||
"/",
|
||||
"/systemd/units/%2e.",
|
||||
"systemd\\units",
|
||||
];
|
||||
for path in bad_paths {
|
||||
let err = get(
|
||||
fake.as_ref(),
|
||||
&peer,
|
||||
path,
|
||||
&[],
|
||||
ProxyOpts::default(),
|
||||
Duration::from_secs(1),
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), "bad_request", "{path}");
|
||||
assert_eq!(err.message(), "peer beta: path not allowed", "{path}");
|
||||
}
|
||||
let bad_queries = [
|
||||
vec![("token".into(), "abc".into())],
|
||||
vec![
|
||||
("state".into(), "failed".into()),
|
||||
("lines".into(), "1".into()),
|
||||
],
|
||||
vec![
|
||||
("state".into(), "failed".into()),
|
||||
("state".into(), "active".into()),
|
||||
],
|
||||
vec![("State".into(), "failed".into())],
|
||||
vec![("state".into(), "a/b".into())],
|
||||
vec![("state".into(), "a%2fb".into())],
|
||||
vec![("state".into(), "..".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();
|
||||
assert_eq!(err.code(), "bad_request", "{query:?}");
|
||||
assert_eq!(err.message(), "peer beta: unsupported query parameter");
|
||||
}
|
||||
assert!(fake.calls().is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn forwards_only_allowlisted_query_and_follow_accept() {
|
||||
let fake = FakeTransport::new();
|
||||
let peer = peer_cfg();
|
||||
get(
|
||||
fake.as_ref(),
|
||||
&peer,
|
||||
"/systemd/units",
|
||||
&[("state".into(), "failed".into())],
|
||||
ProxyOpts::default(),
|
||||
Duration::from_secs(1),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
get(
|
||||
fake.as_ref(),
|
||||
&peer,
|
||||
"/docker/containers/web/logs",
|
||||
&[("tail".into(), "10".into()), ("follow".into(), "1".into())],
|
||||
ProxyOpts::default(),
|
||||
Duration::from_secs(1),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
get(
|
||||
fake.as_ref(),
|
||||
&peer,
|
||||
"/docker/containers/web/logs",
|
||||
&[("follow".into(), "0".into())],
|
||||
ProxyOpts::default(),
|
||||
Duration::from_secs(1),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let calls = fake.calls();
|
||||
assert_eq!(calls[0].1.query, vec![("state".into(), "failed".into())]);
|
||||
assert!(calls[0].1.accept.is_none());
|
||||
assert_eq!(calls[1].1.accept.as_deref(), Some("text/event-stream"));
|
||||
assert!(calls[2].1.accept.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn slow_peer_is_a_timeout() {
|
||||
let fake = FakeTransport::new();
|
||||
fake.peer("beta").delay(Duration::from_secs(30));
|
||||
let err = get(
|
||||
fake.as_ref(),
|
||||
&peer_cfg(),
|
||||
"/systemd/units",
|
||||
&[],
|
||||
ProxyOpts::default(),
|
||||
Duration::from_millis(20),
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(err.message(), "peer beta: timeout");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn inbound_hop_is_rejected() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(HOP_HEADER, HeaderValue::from_static("1"));
|
||||
let err = reject_inbound_hop("beta", &headers).unwrap_err();
|
||||
assert_eq!(err.code(), "bad_request");
|
||||
assert_eq!(err.message(), "peer beta: chained requests are not allowed");
|
||||
headers.insert(HOP_HEADER, HeaderValue::from_static("0"));
|
||||
assert!(reject_inbound_hop("beta", &headers).is_err());
|
||||
assert!(reject_inbound_hop("beta", &HeaderMap::new()).is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn query_raw_rejects_encoded_slash() {
|
||||
let err = query_from_raw("beta", Some("state=failed&unit=a%2Fb")).unwrap_err();
|
||||
assert_eq!(err.message(), "peer beta: unsupported query parameter");
|
||||
let pairs = query_from_raw("beta", Some("state=failed")).unwrap();
|
||||
assert_eq!(pairs, vec![("state".into(), "failed".into())]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn body_cap_and_stream_stop_on_shutdown() {
|
||||
let name = PeerName::parse("beta").unwrap();
|
||||
let err = match prepare_body(
|
||||
&name,
|
||||
PeerBody::Bytes(Bytes::from_static(b"0123456789")),
|
||||
4,
|
||||
Instant::now() + Duration::from_secs(5),
|
||||
CancellationToken::new(),
|
||||
) {
|
||||
Err(err) => err,
|
||||
Ok(_) => panic!("expected an oversize error"),
|
||||
};
|
||||
assert_eq!(err.message(), "peer beta: oversize");
|
||||
|
||||
let shutdown = CancellationToken::new();
|
||||
let stream = futures::stream::unfold(0u32, |n| async move {
|
||||
if n > 0 {
|
||||
tokio::time::sleep(Duration::from_secs(30)).await;
|
||||
}
|
||||
Some((
|
||||
Ok::<Bytes, PeerError>(Bytes::from(format!("data: {n}\n\n"))),
|
||||
n + 1,
|
||||
))
|
||||
});
|
||||
let PreparedBody::Stream(mut body) = prepare_body(
|
||||
&name,
|
||||
PeerBody::Stream(Box::pin(stream)),
|
||||
1024,
|
||||
Instant::now() + Duration::from_secs(30),
|
||||
shutdown.clone(),
|
||||
)
|
||||
.unwrap() else {
|
||||
panic!("expected a stream");
|
||||
};
|
||||
let first = body.next().await.unwrap().unwrap();
|
||||
assert_eq!(&first[..], b"data: 0\n\n");
|
||||
shutdown.cancel();
|
||||
assert!(body.next().await.is_none());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user