use std::time::{SystemTime, UNIX_EPOCH}; use axum::{ Json, body::{Body, HttpBody as _, to_bytes}, extract::{OriginalUri, Request, State}, http::{HeaderValue, Method, StatusCode}, middleware::Next, response::{IntoResponse, Response}, }; use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64_STANDARD}; use deployment::Deployment; use relay_client::RELAY_HEADER; use relay_control::signing::{ NONCE_HEADER, REQUEST_SIGNATURE_HEADER, RESPONSE_NONCE_HEADER, RESPONSE_SIGNATURE_HEADER, RESPONSE_TIMESTAMP_HEADER, RelaySigningService, RequestSignature, SIGNING_SESSION_HEADER, TIMESTAMP_HEADER, build_response_signing_message, }; use relay_webrtc::WEBRTC_PEER_HEADER; use url::form_urlencoded; use utils::response::ApiResponse; use uuid::Uuid; use crate::{DeploymentImpl, error::ApiError}; pub type RelayRequestSignatureContext = RequestSignature; /// Maximum body size (50 MiB) buffered for relay signing. /// /// Request bodies are always buffered: the request signature covers a hash of /// the body, so it has to be checked before the handler sees any of it. Larger /// requests get 413. Every upload route in the app is limited to 20 MiB. /// /// Response bodies are buffered and signed only when their length is already /// known and within this cap (see [`sign_response`]). const RELAY_SIGNED_BODY_MAX_BYTES: usize = 50 * 1024 * 1024; /// How a relayed request to an `/api` route is handled. Paths are the ones the /// `/api` router matches on, so without the `/api` prefix. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum RelayRoutePolicy { /// Must work before a device is paired. Passed through without a /// signature, and the response is not signed. Unsigned, /// Requires a valid signature from a paired device; the response is signed. Signed, /// Requires a valid signature, then is refused with 404: only the local /// machine may use it. Also refused for requests from a WebRTC peer. LocalOnly, } /// Routes a relayed request may reach without a signature. Each one does its /// own authentication: the SPAKE2 steps need the one-time enrollment code, and /// the refresh verifies an Ed25519 signature from a trusted client. /// /// Matching is exact on the raw path. A trailing slash, a doubled slash or a /// percent-encoded variant is not on this list, so it falls back to `Signed`. const UNSIGNED_RELAY_ROUTES: &[(Method, &str)] = &[ (Method::POST, "/relay-auth/server/spake2/start"), (Method::POST, "/relay-auth/server/spake2/finish"), (Method::POST, "/relay-auth/server/signing-session/refresh"), ]; /// Decide how a relayed request is handled. Anything not listed is `Signed`. pub fn relay_route_policy(method: &Method, path: &str) -> RelayRoutePolicy { if UNSIGNED_RELAY_ROUTES .iter() .any(|(allowed_method, allowed_path)| allowed_method == method && *allowed_path == path) { return RelayRoutePolicy::Unsigned; } if is_local_only_path(path) { return RelayRoutePolicy::LocalOnly; } RelayRoutePolicy::Signed } /// Routes only this machine may use: managing this host's paired devices and /// the enrollment code, pairing this machine with other hosts, and anything /// that contacts another host with this host's keys (`/host/*`, /// `/open-remote-editor/*`, which also opens an editor here). /// /// The frontend's `LOCAL_ONLY_API_PREFIXES` (`localApiTransport.ts`) lists the /// routes it never sends to another host. Every route there is either listed /// here or is one of the pairing routes in `UNSIGNED_RELAY_ROUTES`; keep the /// two in step. fn is_local_only_path(path: &str) -> bool { path == "/relay-auth/server/enrollment-code" || path == "/relay-auth/server/clients" || path.starts_with("/relay-auth/server/clients/") || path.starts_with("/relay-auth/client/") || path.starts_with("/host/") || path.starts_with("/open-remote-editor/") } /// Guard for every `/api` route. Relayed requests are handled by /// [`relay_route_policy`], so a new route requires a signature by default. /// Requests from a WebRTC peer are refused on local-only routes. All other /// requests pass through untouched. pub async fn relay_request_guard( State(deployment): State, request: Request, next: Next, ) -> Result { guard_relay_request(deployment.relay_signing(), request, next).await } pub(crate) async fn guard_relay_request( signing: &RelaySigningService, request: Request, next: Next, ) -> Result { let policy = relay_route_policy(request.method(), request.uri().path()); if is_relay_request(&request) { if policy == RelayRoutePolicy::Unsigned { return Ok(next.run(request).await); } let (request, request_signature, path_and_query) = verify_relay_request(signing, request).await?; let response = if policy == RelayRoutePolicy::LocalOnly { local_only_response() } else { next.run(request).await }; return sign_response(signing, response, &request_signature, &path_and_query).await; } // A WebRTC data channel can only be opened by a paired device (the offer // goes through `/webrtc/offer`, which needs a signature), so requests over // it carry no signature of their own. They still come from another device. if is_webrtc_peer_request(&request) && policy == RelayRoutePolicy::LocalOnly { return Ok(local_only_response()); } Ok(next.run(request).await) } /// 404 rather than 401/403: relay clients read those as an expired signing /// session and re-handshake, and the remote web app's settings still request /// `/relay-auth/client/hosts` from the host it is viewing. fn local_only_response() -> Response { ( StatusCode::NOT_FOUND, Json(ApiResponse::<()>::error( "This route is only available on the host machine.", )), ) .into_response() } async fn verify_relay_request( signing: &RelaySigningService, request: Request, ) -> Result<(Request, RequestSignature, String), ApiError> { let (request_signature, path_and_query) = extract_request_signature(&request)?; let (parts, body) = request.into_parts(); let body_bytes = to_bytes(body, RELAY_SIGNED_BODY_MAX_BYTES) .await .map_err(|_| ApiError::PayloadTooLarge)?; if let Err(error) = signing .verify_request( &request_signature, parts.method.as_str(), &path_and_query, &body_bytes, ) .await { tracing::warn!( signing_session_id = %request_signature.signing_session_id, path = %path_and_query, reason = %error.as_str(), "Rejecting relay request with invalid signature" ); return Err(ApiError::Unauthorized); } let mut request = Request::from_parts(parts, Body::from(body_bytes)); request.extensions_mut().insert(request_signature.clone()); Ok((request, request_signature, path_and_query)) } /// Add `x-vk-resp-*` signature headers to a relayed response. /// /// Only bodies whose length is already known and within /// [`RELAY_SIGNED_BODY_MAX_BYTES`] are signed: handler responses built from /// bytes, strings or JSON, error responses, and WebSocket upgrades. Any other /// body is a stream (SSE, the preview proxy, file downloads). It is passed /// through without signature headers, so it reaches the client as it is /// produced instead of being held until it ends, and has no size cap. /// /// No shipped client checks response signatures today. A client that starts /// checking them must treat a response without these headers as an unsigned /// stream, or a signature format for streams has to be added first. async fn sign_response( signing: &RelaySigningService, response: Response, request_signature: &RequestSignature, path_and_query: &str, ) -> Result { let known_len = response.body().size_hint().exact(); if !known_len.is_some_and(|len| len <= RELAY_SIGNED_BODY_MAX_BYTES as u64) { return Ok(response); } let (mut parts, body) = response.into_parts(); let body_bytes = to_bytes(body, RELAY_SIGNED_BODY_MAX_BYTES) .await .map_err(|_| ApiError::PayloadTooLarge)?; let response_timestamp = unix_timestamp_now().map_err(|_| ApiError::Unauthorized)?; let response_nonce = Uuid::new_v4(); let status = parts.status.as_u16(); let message = build_response_signing_message( response_timestamp, status, path_and_query, request_signature.signing_session_id, request_signature.nonce, response_nonce, &body_bytes, ); let signature = signing.sign_bytes(message.as_bytes()); let response_signature = BASE64_STANDARD.encode(signature.to_bytes()); insert_header( &mut parts, RESPONSE_TIMESTAMP_HEADER, &response_timestamp.to_string(), ); insert_header( &mut parts, RESPONSE_NONCE_HEADER, &response_nonce.to_string(), ); insert_header(&mut parts, RESPONSE_SIGNATURE_HEADER, &response_signature); Ok(Response::from_parts(parts, Body::from(body_bytes))) } #[allow(clippy::result_large_err)] fn extract_request_signature(request: &Request) -> Result<(RequestSignature, String), ApiError> { if let Some(result) = try_parse_signature_from_headers(request)? { return Ok(result); } if let Some(result) = try_parse_signature_from_query(request)? { return Ok(result); } Err(ApiError::Unauthorized) } #[allow(clippy::result_large_err)] fn try_parse_signature_from_headers( request: &Request, ) -> Result, ApiError> { let signing_session = parse_header_optional::(request, SIGNING_SESSION_HEADER); let timestamp = parse_header_optional::(request, TIMESTAMP_HEADER); let nonce = parse_header_optional::(request, NONCE_HEADER); let request_signature = parse_header_optional::(request, REQUEST_SIGNATURE_HEADER); let any_present = signing_session.is_some() || timestamp.is_some() || nonce.is_some() || request_signature.is_some(); let all_present = signing_session.is_some() && timestamp.is_some() && nonce.is_some() && request_signature.is_some(); if any_present && !all_present { return Err(ApiError::Unauthorized); } if !all_present { return Ok(None); } let signing_session_id = signing_session .and_then(|value| value.parse::().ok()) .ok_or(ApiError::Unauthorized)?; let timestamp = timestamp .and_then(|value| value.parse::().ok()) .ok_or(ApiError::Unauthorized)?; let nonce = nonce .and_then(|value| value.parse::().ok()) .ok_or(ApiError::Unauthorized)?; let signature_b64 = request_signature.ok_or(ApiError::Unauthorized)?; Ok(Some(( RequestSignature { signing_session_id, timestamp, nonce, signature_b64, }, relay_path_and_query(request)?, ))) } #[allow(clippy::result_large_err)] fn try_parse_signature_from_query( request: &Request, ) -> Result, ApiError> { let Some(original_uri) = request.extensions().get::() else { tracing::warn!("Rejecting relay request without OriginalUri extension"); return Err(ApiError::Unauthorized); }; let path = original_uri.0.path().to_string(); let query = original_uri.0.query().unwrap_or_default(); if query.is_empty() { return Ok(None); } let mut filtered_query = form_urlencoded::Serializer::new(String::new()); let mut signing_session: Option = None; let mut timestamp: Option = None; let mut nonce: Option = None; let mut request_signature: Option = None; for (key, value) in form_urlencoded::parse(query.as_bytes()) { match key.as_ref() { SIGNING_SESSION_HEADER => signing_session = Some(value.into_owned()), TIMESTAMP_HEADER => timestamp = Some(value.into_owned()), NONCE_HEADER => nonce = Some(value.into_owned()), REQUEST_SIGNATURE_HEADER => request_signature = Some(value.into_owned()), _ => { filtered_query.append_pair(&key, &value); } } } let any_present = signing_session.is_some() || timestamp.is_some() || nonce.is_some() || request_signature.is_some(); let all_present = signing_session.is_some() && timestamp.is_some() && nonce.is_some() && request_signature.is_some(); if any_present && !all_present { return Err(ApiError::Unauthorized); } if !any_present { return Ok(None); } let signing_session_id = signing_session .and_then(|value| value.parse::().ok()) .ok_or(ApiError::Unauthorized)?; let timestamp = timestamp .and_then(|value| value.parse::().ok()) .ok_or(ApiError::Unauthorized)?; let nonce = nonce .and_then(|value| value.parse::().ok()) .ok_or(ApiError::Unauthorized)?; let signature_b64 = request_signature.ok_or(ApiError::Unauthorized)?; let filtered = filtered_query.finish(); let path_and_query = if filtered.is_empty() { path } else { format!("{path}?{filtered}") }; Ok(Some(( RequestSignature { signing_session_id, timestamp, nonce, signature_b64, }, path_and_query, ))) } #[allow(clippy::result_large_err)] fn relay_path_and_query(request: &Request) -> Result { let Some(original_uri) = request.extensions().get::() else { tracing::warn!("Rejecting relay request without OriginalUri extension"); return Err(ApiError::Unauthorized); }; Ok(original_uri .0 .path_and_query() .map(|path_and_query| path_and_query.as_str().to_string()) .unwrap_or_else(|| original_uri.0.path().to_string())) } fn parse_header_optional(request: &Request, name: &'static str) -> Option { request .headers() .get(name) .and_then(|v| v.to_str().ok()) .and_then(|value| value.parse::().ok()) } fn insert_header(parts: &mut axum::http::response::Parts, name: &'static str, value: &str) { if let Ok(value) = HeaderValue::from_str(value) { parts.headers.insert(name, value); } } fn unix_timestamp_now() -> Result { let duration = SystemTime::now() .duration_since(UNIX_EPOCH) .map_err(|_| ())?; i64::try_from(duration.as_secs()).map_err(|_| ()) } fn is_webrtc_peer_request(request: &Request) -> bool { request.headers().contains_key(WEBRTC_PEER_HEADER) } fn is_relay_request(request: &Request) -> bool { request .headers() .get(RELAY_HEADER) .and_then(|value| value.to_str().ok()) .is_some_and(|value| value.trim() == "1") } #[cfg(test)] mod tests { use axum::{ Router, body::{Body, to_bytes}, extract::{Request, State}, http::{Method, StatusCode}, middleware::{Next, from_fn_with_state}, response::Response, routing::{get, post}, }; use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64_STANDARD}; use ed25519_dalek::{Signature, SigningKey, Verifier}; use relay_client::RELAY_HEADER; use relay_control::signing::{ NONCE_HEADER, REQUEST_SIGNATURE_HEADER, RESPONSE_NONCE_HEADER, RESPONSE_SIGNATURE_HEADER, RESPONSE_TIMESTAMP_HEADER, RelaySigningService, SIGNING_SESSION_HEADER, TIMESTAMP_HEADER, build_response_signing_message, }; use relay_webrtc::WEBRTC_PEER_HEADER; use tower::ServiceExt; use uuid::Uuid; use super::{RelayRoutePolicy, guard_relay_request, relay_route_policy}; use crate::error::ApiError; const HOST_ID: &str = "3f2b8c1e-7d4a-4f6b-9a1e-2c5d8e9f0a1b"; #[test] fn pairing_routes_are_unsigned() { for path in [ "/relay-auth/server/spake2/start", "/relay-auth/server/spake2/finish", "/relay-auth/server/signing-session/refresh", ] { assert_eq!( relay_route_policy(&Method::POST, path), RelayRoutePolicy::Unsigned, "{path}" ); } } #[test] fn other_routes_are_signed() { for (method, path) in [ (Method::GET, "/health"), (Method::GET, "/terminal/ws"), (Method::POST, "/webrtc/offer"), (Method::GET, "/does-not-exist"), (Method::GET, "/"), (Method::GET, ""), ] { assert_eq!( relay_route_policy(&method, path), RelayRoutePolicy::Signed, "{method} {path}" ); } } #[test] fn device_management_and_onward_proxying_are_local_only() { let host_path = format!("/host/{HOST_ID}/api/health"); for (method, path) in [ (Method::GET, "/relay-auth/server/clients"), (Method::DELETE, "/relay-auth/server/clients/abc"), (Method::POST, "/relay-auth/server/enrollment-code"), (Method::POST, "/relay-auth/client/pair"), (Method::GET, "/relay-auth/client/hosts"), (Method::DELETE, "/relay-auth/client/hosts/abc"), (Method::POST, "/open-remote-editor/workspace"), (Method::GET, host_path.as_str()), (Method::POST, host_path.as_str()), ] { assert_eq!( relay_route_policy(&method, path), RelayRoutePolicy::LocalOnly, "{method} {path}" ); } } #[test] fn variants_of_pairing_routes_are_not_unsigned() { for path in [ "/relay-auth/server/spake2/start/", "//relay-auth/server/spake2/start", "/relay-auth/server//spake2/start", "/relay-auth/server/spake2%2Fstart", "/relay-auth/server/spake2/%73tart", "/relay-auth/server/SPAKE2/start", "/relay-auth/server/spake2/start/../../clients", "/api/relay-auth/server/spake2/start", ] { assert_ne!( relay_route_policy(&Method::POST, path), RelayRoutePolicy::Unsigned, "{path}" ); } } #[test] fn wrong_method_on_pairing_route_is_signed() { for method in [Method::GET, Method::PUT, Method::DELETE, Method::HEAD] { assert_eq!( relay_route_policy(&method, "/relay-auth/server/spake2/start"), RelayRoutePolicy::Signed, "{method}" ); } } struct TestPeer { server: RelaySigningService, client: RelaySigningService, session_id: Uuid, } async fn paired_peer() -> TestPeer { let server = RelaySigningService::new(SigningKey::from_bytes(&[1; 32])); let client_key = SigningKey::from_bytes(&[2; 32]); let session_id = server.create_session(client_key.verifying_key()).await; TestPeer { server, client: RelaySigningService::new(client_key), session_id, } } async fn test_guard( State(signing): State, request: Request, next: Next, ) -> Result { guard_relay_request(&signing, request, next).await } fn app(signing: RelaySigningService) -> Router { let api = Router::new() .route("/health", get(|| async { "ok" })) .route( "/relay-auth/server/spake2/start", post(|| async { "start" }), ) .route("/relay-auth/server/clients", get(|| async { "clients" })) .route("/relay-auth/client/hosts", get(|| async { "hosts" })) .route("/host/{host_id}/{*tail}", get(|| async { "proxied" })) .layer(from_fn_with_state(signing, test_guard)); Router::new().nest("/api", api) } fn request(method: Method, uri: &str, relayed: bool) -> Request { let mut builder = Request::builder().method(method).uri(uri); if relayed { builder = builder.header(RELAY_HEADER, "1"); } builder.body(Body::empty()).unwrap() } fn signed_request(peer: &TestPeer, method: Method, uri: &str) -> Request { let signature = peer .client .sign_request(peer.session_id, method.as_str(), uri, &[]); Request::builder() .method(method) .uri(uri) .header(RELAY_HEADER, "1") .header( SIGNING_SESSION_HEADER, signature.signing_session_id.to_string(), ) .header(TIMESTAMP_HEADER, signature.timestamp.to_string()) .header(NONCE_HEADER, signature.nonce.to_string()) .header(REQUEST_SIGNATURE_HEADER, signature.signature_b64) .body(Body::empty()) .unwrap() } async fn status(app: &Router, request: Request) -> StatusCode { app.clone().oneshot(request).await.unwrap().status() } #[tokio::test] async fn local_requests_pass_through_unchanged() { let peer = paired_peer().await; let app = app(peer.server.clone()); for uri in [ "/api/health", "/api/relay-auth/server/clients", "/api/relay-auth/client/hosts", &format!("/api/host/{HOST_ID}/api/health"), ] { let response = app .clone() .oneshot(request(Method::GET, uri, false)) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK, "{uri}"); assert!(response.headers().get(RESPONSE_SIGNATURE_HEADER).is_none()); } } #[tokio::test] async fn unsigned_relayed_requests_are_rejected() { let peer = paired_peer().await; let app = app(peer.server.clone()); for (method, uri) in [ (Method::GET, "/api/health".to_string()), (Method::GET, "/api/relay-auth/server/clients".to_string()), (Method::GET, format!("/api/host/{HOST_ID}/api/health")), ] { assert_eq!( status(&app, request(method.clone(), &uri, true)).await, StatusCode::UNAUTHORIZED, "{method} {uri}" ); } } #[tokio::test] async fn unmatched_relayed_paths_reach_no_handler() { // axum answers unmatched paths with its own 404 before any layer runs. let peer = paired_peer().await; let app = app(peer.server.clone()); for uri in [ "/api/does-not-exist", "/api/relay-auth/server/spake2/start/", "/api/relay-auth/server/spake2%2Fstart", "/api//relay-auth/server/spake2/start", ] { assert_eq!( status(&app, request(Method::POST, uri, true)).await, StatusCode::NOT_FOUND, "{uri}" ); } } #[tokio::test] async fn pairing_route_works_unsigned_over_relay() { let peer = paired_peer().await; let app = app(peer.server.clone()); let response = app .oneshot(request( Method::POST, "/api/relay-auth/server/spake2/start", true, )) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK); assert!(response.headers().get(RESPONSE_SIGNATURE_HEADER).is_none()); } #[tokio::test] async fn signed_relayed_request_passes_and_response_is_signed() { let peer = paired_peer().await; let app = app(peer.server.clone()); let request = signed_request(&peer, Method::GET, "/api/health"); let request_nonce: Uuid = request.headers()[NONCE_HEADER] .to_str() .unwrap() .parse() .unwrap(); let response = app.oneshot(request).await.unwrap(); assert_eq!(response.status(), StatusCode::OK); let header = |name: &str| response.headers()[name].to_str().unwrap().to_string(); let timestamp: i64 = header(RESPONSE_TIMESTAMP_HEADER).parse().unwrap(); let response_nonce: Uuid = header(RESPONSE_NONCE_HEADER).parse().unwrap(); let signature_bytes: [u8; 64] = BASE64_STANDARD .decode(header(RESPONSE_SIGNATURE_HEADER)) .unwrap() .try_into() .unwrap(); let body = to_bytes(response.into_body(), usize::MAX).await.unwrap(); assert_eq!(&body[..], b"ok"); let message = build_response_signing_message( timestamp, 200, "/api/health", peer.session_id, request_nonce, response_nonce, &body, ); peer.server .server_public_key() .verify(message.as_bytes(), &Signature::from_bytes(&signature_bytes)) .expect("response signature verifies"); } #[tokio::test] async fn signature_in_query_is_accepted() { // WebSocket upgrades carry the signature in the query string. let peer = paired_peer().await; let app = app(peer.server.clone()); let signature = peer .client .sign_request(peer.session_id, "GET", "/api/health?x=1", &[]); let uri = format!( "/api/health?x=1&{SIGNING_SESSION_HEADER}={}&{TIMESTAMP_HEADER}={}&{NONCE_HEADER}={}&{REQUEST_SIGNATURE_HEADER}={}", signature.signing_session_id, signature.timestamp, signature.nonce, url::form_urlencoded::byte_serialize(signature.signature_b64.as_bytes()) .collect::(), ); assert_eq!( status(&app, request(Method::GET, &uri, true)).await, StatusCode::OK ); } #[tokio::test] async fn invalid_or_replayed_signatures_are_rejected() { let peer = paired_peer().await; let app = app(peer.server.clone()); // Signed for a different path. let mut wrong_path = signed_request(&peer, Method::GET, "/api/other"); *wrong_path.uri_mut() = "/api/health".parse().unwrap(); assert_eq!(status(&app, wrong_path).await, StatusCode::UNAUTHORIZED); // Unknown signing session. let stranger = RelaySigningService::new(SigningKey::from_bytes(&[3; 32])); let unknown = TestPeer { server: peer.server.clone(), client: stranger, session_id: Uuid::new_v4(), }; assert_eq!( status(&app, signed_request(&unknown, Method::GET, "/api/health")).await, StatusCode::UNAUTHORIZED ); // Replay of a request that was accepted once. let first = signed_request(&peer, Method::GET, "/api/health"); let mut replay = request(Method::GET, "/api/health", false); *replay.headers_mut() = first.headers().clone(); assert_eq!(status(&app, first).await, StatusCode::OK); assert_eq!(status(&app, replay).await, StatusCode::UNAUTHORIZED); } #[tokio::test] async fn local_only_routes_are_refused_even_when_signed() { let peer = paired_peer().await; let app = app(peer.server.clone()); for uri in [ "/api/relay-auth/server/clients".to_string(), "/api/relay-auth/client/hosts".to_string(), format!("/api/host/{HOST_ID}/api/health"), ] { let response = app .clone() .oneshot(signed_request(&peer, Method::GET, &uri)) .await .unwrap(); assert_eq!(response.status(), StatusCode::NOT_FOUND, "{uri}"); assert!(response.headers().get(RESPONSE_SIGNATURE_HEADER).is_some()); } } fn webrtc_peer_request(uri: &str) -> Request { Request::builder() .method(Method::GET) .uri(uri) .header(WEBRTC_PEER_HEADER, "1") .body(Body::empty()) .unwrap() } #[tokio::test] async fn webrtc_peer_requests_skip_signatures_but_not_local_only_routes() { let peer = paired_peer().await; let app = app(peer.server.clone()); let response = app .clone() .oneshot(webrtc_peer_request("/api/health")) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK); assert!(response.headers().get(RESPONSE_SIGNATURE_HEADER).is_none()); for uri in [ "/api/relay-auth/server/clients".to_string(), "/api/relay-auth/client/hosts".to_string(), format!("/api/host/{HOST_ID}/api/health"), ] { assert_eq!( status(&app, webrtc_peer_request(&uri)).await, StatusCode::NOT_FOUND, "{uri}" ); } } #[tokio::test] async fn relayed_and_webrtc_markers_together_still_need_a_signature() { let peer = paired_peer().await; let app = app(peer.server.clone()); let mut request = request(Method::GET, "/api/health", true); request .headers_mut() .insert(WEBRTC_PEER_HEADER, "1".parse().unwrap()); assert_eq!(status(&app, request).await, StatusCode::UNAUTHORIZED); } } #[cfg(test)] mod response_streaming_tests { use std::{convert::Infallible, time::Duration}; use axum::{ body::{Body, Bytes, to_bytes}, http::StatusCode, response::{ IntoResponse, Response, sse::{Event, Sse}, }, }; use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64_STANDARD}; use ed25519_dalek::{Signature, SigningKey, Verifier}; use futures_util::{StreamExt, stream}; use relay_control::signing::{ RESPONSE_NONCE_HEADER, RESPONSE_SIGNATURE_HEADER, RESPONSE_TIMESTAMP_HEADER, RelaySigningService, RequestSignature, build_response_signing_message, }; use tokio::{sync::mpsc, time::timeout}; use uuid::Uuid; use super::{RELAY_SIGNED_BODY_MAX_BYTES, sign_response}; const PATH: &str = "/api/preview/3000/stream?x=1"; const WAIT: Duration = Duration::from_secs(5); fn request_signature() -> RequestSignature { RequestSignature { signing_session_id: Uuid::new_v4(), timestamp: 0, nonce: Uuid::new_v4(), signature_b64: String::new(), } } fn signing() -> RelaySigningService { RelaySigningService::new(SigningKey::from_bytes(&[1; 32])) } fn assert_unsigned(response: &Response) { for name in [ RESPONSE_TIMESTAMP_HEADER, RESPONSE_NONCE_HEADER, RESPONSE_SIGNATURE_HEADER, ] { assert!(response.headers().get(name).is_none(), "{name} was set"); } } fn header<'a>(response: &'a Response, name: &str) -> &'a str { response .headers() .get(name) .unwrap_or_else(|| panic!("missing {name}")) .to_str() .unwrap() } #[tokio::test] async fn known_length_response_is_signed() { let signing = signing(); let request_signature = request_signature(); let response = (StatusCode::CREATED, "hello").into_response(); let response = sign_response(&signing, response, &request_signature, PATH) .await .unwrap(); assert_eq!(response.status(), StatusCode::CREATED); let timestamp: i64 = header(&response, RESPONSE_TIMESTAMP_HEADER) .parse() .unwrap(); let response_nonce: Uuid = header(&response, RESPONSE_NONCE_HEADER).parse().unwrap(); let signature_bytes = BASE64_STANDARD .decode(header(&response, RESPONSE_SIGNATURE_HEADER)) .unwrap(); let signature = Signature::from_slice(&signature_bytes).unwrap(); let body = to_bytes(response.into_body(), usize::MAX).await.unwrap(); assert_eq!(&body[..], b"hello"); let message = build_response_signing_message( timestamp, StatusCode::CREATED.as_u16(), PATH, request_signature.signing_session_id, request_signature.nonce, response_nonce, &body, ); signing .server_public_key() .verify(message.as_bytes(), &signature) .expect("response signature verifies"); } #[tokio::test] async fn streaming_response_is_passed_through_unsigned() { let (sender, receiver) = mpsc::channel::(1); let chunks = stream::unfold(receiver, |mut receiver| async move { receiver .recv() .await .map(|chunk| (Ok::<_, Infallible>(chunk), receiver)) }); let response = Response::new(Body::from_stream(chunks)); // Buffering would wait for the stream to end, which never happens here. let response = timeout( WAIT, sign_response(&signing(), response, &request_signature(), PATH), ) .await .expect("response is not buffered") .unwrap(); assert_unsigned(&response); let mut body = response.into_body().into_data_stream(); sender.send(Bytes::from_static(b"first")).await.unwrap(); let first = timeout(WAIT, body.next()) .await .expect("first chunk arrives before the stream ends") .unwrap() .unwrap(); assert_eq!(&first[..], b"first"); drop(sender); assert!(timeout(WAIT, body.next()).await.unwrap().is_none()); } #[tokio::test] async fn sse_response_is_passed_through_unsigned() { let events = stream::pending::>(); let response = Sse::new(events).into_response(); let response = timeout( WAIT, sign_response(&signing(), response, &request_signature(), PATH), ) .await .expect("SSE response is not buffered") .unwrap(); assert_eq!(response.status(), StatusCode::OK); assert_unsigned(&response); } #[tokio::test] async fn known_length_body_over_cap_is_passed_through_unsigned() { let len = RELAY_SIGNED_BODY_MAX_BYTES + 1; let response = Response::new(Body::from(vec![0u8; len])); let response = sign_response(&signing(), response, &request_signature(), PATH) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK); assert_unsigned(&response); let body = to_bytes(response.into_body(), usize::MAX).await.unwrap(); assert_eq!(body.len(), len); } }