From b2ee8f495fcdca6466da232d3bd442248bd47800 Mon Sep 17 00:00:00 2001 From: Piotr Mlocek Date: Fri, 18 Sep 2026 01:04:59 -0700 Subject: [PATCH] feat(middleware)!: add streaming request hooks Replace the unary HTTP request evaluator with a bidirectional streaming protocol and move SigV4 signing into the built-in middleware stage. BREAKING CHANGE: replace the unary HTTP request middleware RPC with the streaming HttpRequestPreCredentials Evaluate contract. Signed-off-by: Piotr Mlocek --- Cargo.lock | 6 +- architecture/sandbox-limits.md | 44 +- architecture/sandbox.md | 42 +- crates/openshell-core/src/middleware.rs | 149 +- .../Cargo.toml | 3 + .../src/lib.rs | 193 +- .../src/regex.rs | 38 +- .../src/sigv4.rs | 216 +- .../src/headers.rs | 15 +- .../src/lib.rs | 2529 +++++++++------- .../src/remote.rs | 48 +- .../src/request.rs | 1917 +++++++++++++ .../src/response.rs | 44 +- .../openshell-supervisor-network/Cargo.toml | 5 +- .../src/l7/middleware.rs | 806 +++++- .../src/l7/mod.rs | 1 + .../src/l7/post_credentials.rs | 371 +++ .../src/l7/relay.rs | 987 ++++++- .../src/l7/rest.rs | 1567 +++++++--- .../src/l7/websocket.rs | 8 - .../openshell-supervisor-network/src/lib.rs | 1 - .../openshell-supervisor-network/src/proxy.rs | 164 +- .../tests/sigv4_localstack.rs | 4 +- docs/extensibility/supervisor-middleware.mdx | 60 +- docs/providers/aws-sigv4.mdx | 4 +- docs/reference/gateway-config.mdx | 4 +- .../src/main.rs | 329 ++- .../Cargo.lock | 2555 +++++++++++++++++ .../Cargo.toml | 28 + .../README.md | 100 + .../policy.yaml | 48 + .../src/main.rs | 1022 +++++++ .../src/signer.rs | 1180 ++++++++ proto/supervisor_middleware.proto | 272 +- rfc/0009-supervisor-middleware/README.md | 174 +- .../appendices/extension-authentication.md | 2 +- .../appendices/protocol-extensions.md | 52 +- .../references/supervisor-middleware.md | 7 +- skills/generate-sandbox-policy/SKILL.md | 4 + skills/openshell-cli/SKILL.md | 2 + tasks/rust.toml | 3 + tasks/test.toml | 1 + 42 files changed, 12800 insertions(+), 2205 deletions(-) rename crates/{openshell-supervisor-network => openshell-supervisor-middleware-builtins}/src/sigv4.rs (74%) create mode 100644 crates/openshell-supervisor-middleware/src/request.rs create mode 100644 crates/openshell-supervisor-network/src/l7/post_credentials.rs create mode 100644 examples/supervisor-middleware-git-signing/Cargo.lock create mode 100644 examples/supervisor-middleware-git-signing/Cargo.toml create mode 100644 examples/supervisor-middleware-git-signing/README.md create mode 100644 examples/supervisor-middleware-git-signing/policy.yaml create mode 100644 examples/supervisor-middleware-git-signing/src/main.rs create mode 100644 examples/supervisor-middleware-git-signing/src/signer.rs diff --git a/Cargo.lock b/Cargo.lock index 109e6df8d..861c14225 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4764,6 +4764,9 @@ name = "openshell-supervisor-middleware-builtins" version = "0.0.0" dependencies = [ "async-trait", + "aws-credential-types", + "aws-sigv4", + "aws-smithy-runtime-api", "miette", "openshell-core", "prost-types", @@ -4781,9 +4784,6 @@ version = "0.0.0" dependencies = [ "apollo-parser", "async-trait", - "aws-credential-types", - "aws-sigv4", - "aws-smithy-runtime-api", "base64", "bytes", "flate2", diff --git a/architecture/sandbox-limits.md b/architecture/sandbox-limits.md index 9045ddc35..2cafb7566 100644 --- a/architecture/sandbox-limits.md +++ b/architecture/sandbox-limits.md @@ -60,12 +60,14 @@ budgets as new activity. |---|---:|---| | Concurrent buffered work | 32 | Shared by HTTP requests, WebSocket messages, and WebSocket preflight. One permit covers one complete unit of work. | | Admission waiters | 64 | Additional work is shed when both the active budget and waiter budget are full. HTTP receives a complete 503 response before its body is buffered. | -| Persistent middleware sessions | 32 | Shared process-wide session budget for streaming middleware protocols. WebSocket preflight uses immediate admission before opening streams and retains one permit while any stage remains active. | -| HTTP body or WebSocket text message | 4 MiB | Platform maximum for input and replacement payloads. Service, operator, and stage limits may narrow it. | +| Persistent middleware sessions | 32 | Shared process-wide session budget for HTTP request and WebSocket streams. Admission is retained while any stage remains active. | +| Middleware payload or unit | 4 MiB | Platform maximum for a complete whole-body payload, WebSocket text message, or advertised stream unit. Request stream units are further capped at 64 KiB. | +| Owned request representation | 1 GiB input and 1 GiB output | Logical deferred-storage limit for fail-closed owned streams. Implementations spool rather than retain this representation in memory. | | Middleware configs and stages | 10 | At most 10 configs in policy and 10 selected stages in one chain. | | Selector patterns | 32 | Combined include and exclude patterns per middleware config. | | Per-stage RPC | 500 ms default, 10 ms–30 s | An operator timeout caps a binding timeout. | -| Complete message chain | 30 s | Starts after work admission; admission backpressure does not consume the chain budget. | +| Middleware unit or message chain | 30 s | Bounds one HTTP body-unit pass, one HTTP finalization pass, or one WebSocket message across all selected stages. It is not an accepted-stream lifetime. | +| HTTP request body processing | 2 min total | Bounds request-body receipt, middleware processing, and output delivery. Pure lockstep chains may forward approved units during this interval; withholding modes spool first. Timeout cancels the session and terminates the request. | | WebSocket preflight | 1 s maximum | Caps handshake delay independently of the message RPC timeout. | | Remote service connect | 5 s | Applies while establishing a middleware gRPC channel. | @@ -75,19 +77,19 @@ request headers totaling 64 KiB, 64 header mutations, 32 findings per stage, and 64 metadata entries. The detailed external contract lives in [Supervisor Middleware](../docs/extensibility/supervisor-middleware.mdx). -The work semaphore bounds aggregate buffered middleware input to approximately -`32 × 4 MiB`, plus bounded envelope and parser overhead. It is a concurrency -safety valve, not rate limiting or a promise that 32 simultaneous maximum-size -messages are inexpensive. +The work semaphore bounds concurrent middleware progress and caps aggregate +in-memory whole-body or WebSocket input at approximately `32 × 4 MiB`, plus +bounded envelope and parser overhead. Streaming HTTP bodies use bounded units +and storage-backed output instead. This is a concurrency safety valve, not rate +limiting or a promise that maximum-sized work is inexpensive. The persistent session semaphore is independent from the work semaphore. One -WebSocket middleware session consumes one permit regardless of its active-stage -fan-out, which is separately capped at 10 stages. All-skip preflight releases -the permit immediately. A retained session releases it at connection end or as -soon as its last active stage is disabled. Session admission does not wait: -capacity exhaustion follows each selected config's `on_error` behavior before -any stream opens. The protocol-neutral registry ownership allows future -streaming HTTP middleware to use the same process-wide budget. +HTTP request or WebSocket middleware session consumes one permit regardless of +its active-stage fan-out, which is separately capped at 10 stages. All-skip +preflight releases the permit immediately. A retained session releases it at +request or connection end, or as soon as its last active stage is disabled. +WebSocket session admission does not wait; capacity exhaustion follows each +selected config's `on_error` behavior before any stream opens. ## Egress Framing and Inspection @@ -114,12 +116,14 @@ buffer only when it owns an explicit bound. Every parsed WebSocket text message acquires network-owned assembly capacity before payload allocation or reading, including relays used only for native policy, credential rewriting, compression, or a disabled fail-open middleware session. The process-lifetime budget survives policy reloads, and the assembly retains its permit through decompression, policy and middleware evaluation, credential rewriting, and upstream forwarding. Active middleware sessions additionally acquire shared middleware work before buffering. Input progress resets only the idle deadline. Forwarding uses one total deadline across the complete frame header, payload, and flush. Every timeout and terminal parser error releases both permits through ordinary ownership. Queue exhaustion emits a payload-free network denial event. -The operator middleware `max_payload_bytes` ceiling applies to payloads exposed -through HTTP-body and WebSocket text-message bindings. It does not replace the -raw binary frame safety bound because binary messages are never delivered to V1 -middleware. A passed binary logical message still advances the active -middleware session sequence and emits coverage telemetry, so a later text RPC -can contain a valid sequence gap. +The operator middleware `max_payload_bytes` ceiling applies to complete +whole-body payloads, stream units, and WebSocket text messages. The HTTP request +runtime further caps units at 64 KiB and advertises the separate owned-stream +deferred limit during preflight. Neither limit replaces the raw binary frame +safety bound because binary messages are never delivered to V1 middleware. A +passed binary logical message still advances the active middleware session +sequence and emits coverage telemetry, so a later text RPC can contain a valid +sequence gap. ## Network and Upstream Proxying diff --git a/architecture/sandbox.md b/architecture/sandbox.md index 74bbfb4ae..85e702079 100644 --- a/architecture/sandbox.md +++ b/architecture/sandbox.md @@ -283,12 +283,27 @@ host selectors choose the chain independently of the network rule that admitted the request. Policy-local map keys identify configs, while built-in names or operator-owned registration names identify implementations. -Built-ins run in-process against a borrowed view of the chain's current HTTP -request state. Operator services retain the bounded protobuf/gRPC contract, and -the remote adapter materializes an owned HTTP evaluation only when a request -crosses that transport boundary. Both paths support bounded bidirectional -WebSocket sessions, so a manifest advertises capabilities independently of -transport. +Built-ins and operator services use the same event-oriented request contract. +Each selected `HTTP_REQUEST/PRE_CREDENTIALS` stage opens a bidirectional stream, +receives preflight, and selects header-only, whole-body, lockstep stream, or +owned-stream processing. The HTTP/1 relay normalizes fixed and chunked bodies +into bounded units. A chain made only of lockstep stream stages can forward each +approved unit upstream immediately with bounded channel and socket backpressure. +The relay switches the upstream request to chunked framing when transformations +can change unit sizes and concurrently watches for an early upstream response. +It cancels the middleware session and never replays the request if the upstream +responds before upload completes. Chains containing whole-body or owned stages, +body-aware policy re-evaluation, request-body credential rewriting, or signing +that needs the complete payload retain a hold barrier and spool output first. +Whole-body stages receive one data-bearing final unit; streaming and owned +stages receive nonempty data units followed by one empty terminal unit. Body +receipt, middleware processing, and output delivery share a two-minute +wall-clock deadline. +Owned streams transfer replay responsibility to a fail-closed stage, allowing +whole-request transformations larger than the per-message protobuf limit while +keeping input, output, and backpressure bounded. Body-aware GraphQL, JSON-RPC, +and MCP paths retain a hold barrier so policy can re-evaluate every accepted +replacement before later stages or upstream delivery. When a stage ends, the remote adapter sends its terminal event, half-closes the request stream, and briefly drains the response stream before releasing the transport. This keeps a queued terminal event from being canceled with the @@ -305,6 +320,12 @@ middleware registry validates implementation-owned config. The generic registry and chain runner live in `openshell-supervisor-middleware`; first-party implementations live in `openshell-supervisor-middleware-builtins`. +The restricted `HTTP_REQUEST/POST_CREDENTIALS` phase is available only to +trusted in-process built-ins. External manifests advertising that phase are +rejected because it can observe resolved credentials. `openshell/sigv4` owns +AWS request signing at this phase while existing endpoint policy fields select +its signing mode and target. + The selected middleware chain can also inspect the final HTTP response before it returns to the workload. Stages select header-only, whole-body, or streaming inspection independently. The relay owns response framing when body bytes can @@ -530,10 +551,11 @@ subject, and gateway SPIFFE subject, and their cache lifetime is capped by the intermediate token response, stored subject-token expiry, and supervisor SVID expiry. -For AWS endpoints that require request-level signing, the proxy supports SigV4 -re-signing. When `credential_signing: sigv4` is set on an L7 endpoint, the proxy -strips the client's placeholder-based AWS auth headers, re-signs with real -credentials from the provider, and forwards the request upstream. The signing +For AWS endpoints that require request-level signing, the restricted in-process +`openshell/sigv4` middleware performs SigV4 re-signing after provider credential +resolution. When `credential_signing: sigv4` is set on an L7 endpoint, it strips +the client's placeholder-based AWS auth headers, re-signs with real credentials +from the provider, and forwards the request upstream. The signing endpoint must have a credential source before the policy generation activates: an attached endpoint-bearing AWS profile whose boundary covers the endpoint, or an attached endpointless AWS profile explicitly named by the endpoint's diff --git a/crates/openshell-core/src/middleware.rs b/crates/openshell-core/src/middleware.rs index a4023bfb1..e0cd2f2bc 100644 --- a/crates/openshell-core/src/middleware.rs +++ b/crates/openshell-core/src/middleware.rs @@ -11,12 +11,16 @@ use tokio::sync::mpsc; use tonic::{Request, Response, Status}; use crate::proto::{ - HttpHeader, HttpRequestEvaluation, HttpRequestResult, HttpRequestTarget, HttpResponseEvent, - HttpResponseEventResult, MiddlewareManifest, RequestContext, SupervisorMiddlewarePhase, - ValidateConfigRequest, ValidateConfigResponse, WebSocketSessionEvent, + HttpRequestEvent, HttpRequestEventResult, HttpResponseEvent, HttpResponseEventResult, + MiddlewareManifest, ValidateConfigRequest, ValidateConfigResponse, WebSocketSessionEvent, WebSocketSessionEventResult, }; +/// Transport-neutral result stream for one HTTP request middleware stage. +pub type HttpRequestResultStream = Pin< + Box> + Send + 'static>, +>; + /// Transport-neutral result stream for one HTTP response middleware stage. pub type HttpResponseResultStream = Pin< Box> + Send + 'static>, @@ -44,15 +48,23 @@ pub trait SupervisorMiddlewareEndpoint: Send + Sync { request: Request, ) -> Result, Status>; - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - request: Request, - ) -> Result, Status>; + _requests: mpsc::Receiver, + ) -> Result { + Err(Status::unimplemented( + "middleware does not implement HTTP request pre-credentials evaluation", + )) + } async fn open_websocket_session( &self, - requests: mpsc::Receiver, - ) -> Result; + _requests: mpsc::Receiver, + ) -> Result { + Err(Status::unimplemented( + "middleware does not implement WebSocket sessions", + )) + } async fn open_http_response_pre_return( &self, @@ -64,95 +76,11 @@ pub trait SupervisorMiddlewareEndpoint: Send + Sync { } } -/// Borrowed request state exposed to one in-process middleware invocation. -/// -/// The view reflects every transformation applied by earlier stages. It is valid -/// only for the current invocation and cannot be retained by the middleware. -#[derive(Clone, Copy)] -pub struct HttpRequestView<'a> { - phase: SupervisorMiddlewarePhase, - context: &'a RequestContext, - config: &'a prost_types::Struct, - target: &'a HttpRequestTarget, - headers: &'a [HttpHeader], - body: &'a [u8], - middleware_name: &'a str, -} - -impl<'a> HttpRequestView<'a> { - /// Create a view over the chain's current request state for one stage. - #[must_use] - pub fn new( - phase: SupervisorMiddlewarePhase, - context: &'a RequestContext, - config: &'a prost_types::Struct, - target: &'a HttpRequestTarget, - headers: &'a [HttpHeader], - body: &'a [u8], - middleware_name: &'a str, - ) -> Self { - Self { - phase, - context, - config, - target, - headers, - body, - middleware_name, - } - } - - /// Return the typed middleware phase selected for this invocation. - #[must_use] - pub fn phase(self) -> SupervisorMiddlewarePhase { - self.phase - } - - /// Return the request and sandbox identity shared by every chain stage. - #[must_use] - pub fn context(self) -> &'a RequestContext { - self.context - } - - /// Return the validated configuration for this policy-selected stage. - #[must_use] - pub fn config(self) -> &'a prost_types::Struct { - self.config - } - - /// Return the admitted destination and HTTP request target. - #[must_use] - pub fn target(self) -> &'a HttpRequestTarget { - self.target - } - - /// Return visible request headers in wire order, including repeated names. - #[must_use] - pub fn headers(self) -> &'a [HttpHeader] { - self.headers - } - - /// Return the current body, including replacements made by earlier stages. - #[must_use] - pub fn body(self) -> &'a [u8] { - self.body - } - - /// Return the in-process middleware manifest or attachment name selected by - /// policy, including custom implementation names. - #[must_use] - pub fn middleware_name(self) -> &'a str { - self.middleware_name - } -} - /// Asynchronous contract for supervisor middleware that runs in-process. /// /// Remote services use the protobuf `SupervisorMiddleware` contract instead. -/// The borrowed view remains valid for the evaluation future, so implementations -/// can yield without constructing an owned protobuf request envelope. -/// WebSocket sessions already use bounded channel and stream ownership, so that -/// operation is shared with the transport-neutral endpoint contract. +/// HTTP and WebSocket operations use bounded channels and streams shared with +/// the transport-neutral endpoint contract. /// /// Downstream implementations must apply `#[async_trait::async_trait]` to each /// `impl InProcessMiddleware` block. The macro's default expansion creates @@ -171,10 +99,10 @@ impl<'a> HttpRequestView<'a> { /// use std::sync::Arc; /// /// use miette::Result; -/// use openshell_core::middleware::{HttpRequestView, InProcessMiddleware}; +/// use openshell_core::middleware::InProcessMiddleware; /// use openshell_core::proto::{ -/// Decision, HttpRequestResult, MiddlewareBinding, MiddlewareManifest, -/// SupervisorMiddlewareOperation, SupervisorMiddlewarePhase, +/// MiddlewareBinding, MiddlewareManifest, SupervisorMiddlewareOperation, +/// SupervisorMiddlewarePhase, /// }; /// use prost_types::Struct; /// @@ -203,16 +131,6 @@ impl<'a> HttpRequestView<'a> { /// ) -> Result<()> { /// Ok(()) /// } -/// -/// async fn evaluate_http_request( -/// &self, -/// _request: HttpRequestView<'_>, -/// ) -> Result { -/// Ok(HttpRequestResult { -/// decision: Decision::Allow as i32, -/// ..Default::default() -/// }) -/// } /// } /// /// let service: Arc = Arc::new(Service); @@ -235,16 +153,15 @@ pub trait InProcessMiddleware: Send + Sync { config: &prost_types::Struct, ) -> Result<()>; - /// Evaluate one request using borrowed chain state. - /// - /// # Errors - /// - /// Returns an error when the selected implementation cannot evaluate the - /// request or its validated configuration. - async fn evaluate_http_request( + /// Open one HTTP request pre-credentials stream. + async fn open_http_request_pre_credentials( &self, - request: HttpRequestView<'_>, - ) -> Result; + _requests: mpsc::Receiver, + ) -> std::result::Result { + Err(Status::unimplemented( + "middleware does not implement HTTP request pre-credentials evaluation", + )) + } /// Open one persistent WebSocket middleware session. /// diff --git a/crates/openshell-supervisor-middleware-builtins/Cargo.toml b/crates/openshell-supervisor-middleware-builtins/Cargo.toml index 8e38a05ad..9142fe13d 100644 --- a/crates/openshell-supervisor-middleware-builtins/Cargo.toml +++ b/crates/openshell-supervisor-middleware-builtins/Cargo.toml @@ -14,6 +14,9 @@ rust-version.workspace = true openshell-core = { path = "../openshell-core", default-features = false } async-trait = "0.1" +aws-credential-types = { version = "1", features = ["hardcoded-credentials"] } +aws-sigv4 = { version = "1", features = ["sign-http", "http1"] } +aws-smithy-runtime-api = { version = "1", features = ["client"] } miette = { workspace = true } prost-types = { workspace = true } regex = { workspace = true } diff --git a/crates/openshell-supervisor-middleware-builtins/src/lib.rs b/crates/openshell-supervisor-middleware-builtins/src/lib.rs index f87afafc3..9ee70a9db 100644 --- a/crates/openshell-supervisor-middleware-builtins/src/lib.rs +++ b/crates/openshell-supervisor-middleware-builtins/src/lib.rs @@ -4,20 +4,29 @@ //! First-party in-process supervisor middleware implementations. mod regex; +pub mod sigv4; use std::sync::Arc; use miette::{Result, miette}; -use openshell_core::middleware::{HttpRequestView, InProcessMiddleware, WebSocketResponseStream}; +use openshell_core::middleware::{ + HttpRequestResultStream, InProcessMiddleware, WebSocketResponseStream, +}; use openshell_core::proto::{ - HttpRequestResult, MiddlewareManifest, SupervisorMiddlewarePhase, WebSocketPreflightAction, + HttpRequestBodyMode, HttpRequestBodyPassThrough, HttpRequestBodyResult, + HttpRequestBodyTransform, HttpRequestEvent, HttpRequestEventResult, + HttpRequestPreflightInspect, HttpRequestPreflightResult, HttpRequestTrailersResult, + MiddlewareManifest, SupervisorMiddlewarePhase, WebSocketPreflightAction, WebSocketPreflightDecision, WebSocketSessionEvent, WebSocketSessionEventResult, + http_request_body_result, http_request_body_transform, http_request_body_unit, + http_request_event, http_request_event_result, http_request_preflight_result, web_socket_message, web_socket_session_event, web_socket_session_event_result, }; use tokio_stream::{Stream, StreamExt}; use tonic::Status; pub use regex::{NAME as BUILTIN_REGEX, RegexConfig, RegexMode}; +pub use sigv4::NAME as BUILTIN_SIGV4; /// Return the first-party services that the gateway and supervisor install. pub fn services() -> Vec> { @@ -35,20 +44,144 @@ pub fn validate_config(implementation: &str, config: &prost_types::Struct) -> Re } } -fn evaluate_http_request(request: HttpRequestView<'_>) -> Result { - match request.middleware_name() { - BUILTIN_REGEX => regex::evaluate_http_request(request.config(), request.body()), - other => Err(miette!( - "middleware implementation '{other}' is not a registered OpenShell built-in" - )), - } -} - -/// Aggregate service exposing first-party middleware through the borrowed in-process contract. +/// Aggregate service exposing first-party middleware through the in-process contract. #[derive(Debug, Default)] pub struct BuiltinMiddlewareService; impl BuiltinMiddlewareService { + fn request_stream( + mut requests: tokio::sync::mpsc::Receiver, + ) -> HttpRequestResultStream { + let (responses_tx, responses_rx) = tokio::sync::mpsc::channel(4); + tokio::spawn(async move { + let mut config = None; + let mut inspected_body = false; + while let Some(request) = requests.recv().await { + let Some(event) = request.event else { + let _ = responses_tx + .send(Err(Status::invalid_argument("empty request event"))) + .await; + break; + }; + let result = match event { + http_request_event::Event::Preflight(preflight) if config.is_none() => { + if preflight.middleware_name != BUILTIN_REGEX { + Err(Status::invalid_argument(format!( + "middleware implementation '{}' is not a registered OpenShell built-in", + preflight.middleware_name + ))) + } else if !preflight + .permitted_body_modes + .contains(&(HttpRequestBodyMode::WholeBodyBytes as i32)) + { + Err(Status::failed_precondition( + "openshell/regex requires whole-body request inspection", + )) + } else { + let selected = preflight.config.unwrap_or_default(); + match regex::validate_config(&selected) { + Ok(()) => { + config = Some(selected); + Ok(HttpRequestEventResult { + result: Some( + http_request_event_result::Result::PreflightResult( + HttpRequestPreflightResult { + action: Some( + http_request_preflight_result::Action::Inspect( + HttpRequestPreflightInspect { + body_mode: HttpRequestBodyMode::WholeBodyBytes as i32, + header_mutations: Vec::new(), + }, + ), + ), + ..Default::default() + }, + ), + ), + }) + } + Err(error) => Err(Status::invalid_argument(error.to_string())), + } + } + } + http_request_event::Event::Body(body) + if config.is_some() && !inspected_body && body.end_of_stream => + { + let Some(http_request_body_unit::Payload::Data(data)) = body.payload else { + let _ = responses_tx + .send(Err(Status::invalid_argument( + "missing request body payload", + ))) + .await; + break; + }; + inspected_body = true; + match regex::evaluate_http_body( + config.as_ref().expect("validated request config"), + &data, + ) { + Ok(evaluation) => { + let action = evaluation.replacement.map_or_else( + || { + http_request_body_result::Action::PassThrough( + HttpRequestBodyPassThrough {}, + ) + }, + |replacement| { + http_request_body_result::Action::Transform( + HttpRequestBodyTransform { + replacement: Some( + http_request_body_transform::Replacement::Data( + replacement, + ), + ), + }, + ) + }, + ); + Ok(HttpRequestEventResult { + result: Some(http_request_event_result::Result::BodyResult( + HttpRequestBodyResult { + sequence: body.sequence, + action: Some(action), + findings: evaluation.findings, + metadata: evaluation.metadata, + ..Default::default() + }, + )), + }) + } + Err(error) => Err(Status::invalid_argument(error.to_string())), + } + } + http_request_event::Event::Trailers(_) if inspected_body => { + Ok(HttpRequestEventResult { + result: Some(http_request_event_result::Result::TrailersResult( + HttpRequestTrailersResult::default(), + )), + }) + } + http_request_event::Event::SessionEnd(_) if config.is_some() => break, + _ => Err(Status::failed_precondition( + "invalid built-in HTTP request lifecycle", + )), + }; + match result { + Ok(result) => { + if responses_tx.send(Ok(result)).await.is_err() { + break; + } + } + Err(error) => { + let _ = responses_tx.send(Err(error)).await; + break; + } + } + } + }); + Box::pin(tokio_stream::wrappers::ReceiverStream::new(responses_rx)) + } + fn websocket_stream(mut requests: S) -> WebSocketResponseStream where S: Stream> @@ -204,11 +337,11 @@ impl InProcessMiddleware for BuiltinMiddlewareService { validate_config(middleware_name, config) } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - request: HttpRequestView<'_>, - ) -> Result { - evaluate_http_request(request) + requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { + Ok(Self::request_stream(requests)) } async fn open_websocket_session( @@ -225,8 +358,7 @@ impl InProcessMiddleware for BuiltinMiddlewareService { mod tests { use super::*; use openshell_core::proto::{ - Decision, HttpRequestTarget, RequestContext, SupervisorMiddlewareOperation, - SupervisorMiddlewarePhase, WebSocketPreflight, + Decision, SupervisorMiddlewareOperation, SupervisorMiddlewarePhase, WebSocketPreflight, }; fn string_config(key: &str, value: &str) -> prost_types::Struct { @@ -241,18 +373,11 @@ mod tests { } } - fn evaluate_body(body: &[u8], config: &prost_types::Struct) -> Result { - let context = RequestContext::default(); - let target = HttpRequestTarget::default(); - evaluate_http_request(HttpRequestView::new( - SupervisorMiddlewarePhase::PreCredentials, - &context, - config, - &target, - &[], - body, - BUILTIN_REGEX, - )) + fn evaluate_body( + body: &[u8], + config: &prost_types::Struct, + ) -> Result { + regex::evaluate_http_body(config, body) } #[tokio::test] @@ -329,9 +454,7 @@ mod tests { ) .expect("evaluate regex binding"); - assert_eq!(result.decision, Decision::Allow as i32); - assert!(result.has_body); - let body = String::from_utf8(result.body).unwrap(); + let body = String::from_utf8(result.replacement.expect("replacement")).unwrap(); assert!(body.contains("top-secret")); assert!(!body.contains("sk-ABCDEFGHIJKLMNOP")); assert!( @@ -389,9 +512,7 @@ mod tests { let result = evaluate_body(body.as_bytes(), &prost_types::Struct::default()) .expect("evaluate regex binding"); - assert_eq!(result.decision, Decision::Allow as i32); - assert!(!result.has_body); - assert!(result.body.is_empty()); + assert!(result.replacement.is_none()); assert!(result.findings.is_empty()); } diff --git a/crates/openshell-supervisor-middleware-builtins/src/regex.rs b/crates/openshell-supervisor-middleware-builtins/src/regex.rs index 3b1652456..99176c712 100644 --- a/crates/openshell-supervisor-middleware-builtins/src/regex.rs +++ b/crates/openshell-supervisor-middleware-builtins/src/regex.rs @@ -14,8 +14,8 @@ use std::sync::LazyLock; use miette::{Result, miette}; use openshell_core::proto::{ - Decision, Finding, HttpRequestResult, MiddlewareBinding, SupervisorMiddlewareOperation, - SupervisorMiddlewarePhase, WebSocketMessageResult, web_socket_message_result, + Decision, Finding, MiddlewareBinding, SupervisorMiddlewareOperation, SupervisorMiddlewarePhase, + WebSocketMessageResult, web_socket_message_result, }; use regex::Regex; use serde::Deserialize; @@ -91,31 +91,29 @@ pub fn validate_config(config: &prost_types::Struct) -> Result<()> { RegexConfig::from_struct(config).map(|_| ()) } -/// Evaluate a borrowed HTTP body and return a replacement only when a pattern matches. -pub fn evaluate_http_request( - config: &prost_types::Struct, - body: &[u8], -) -> Result { +#[derive(Debug)] +pub struct HttpBodyEvaluation { + pub replacement: Option>, + pub findings: Vec, + pub metadata: HashMap, +} + +/// Evaluate a complete HTTP body and return a replacement only when a pattern matches. +pub fn evaluate_http_body(config: &prost_types::Struct, body: &[u8]) -> Result { validate_config(config)?; let text = std::str::from_utf8(body).map_err(|_| miette!("{NAME} requires UTF-8 request bodies"))?; let (body, matches) = apply_replacements(text); let (findings, metadata) = findings_and_metadata(&matches); - let has_body = !matches.is_empty(); - let result = HttpRequestResult { - decision: Decision::Allow as i32, - reason: String::new(), - body: match body { - Cow::Borrowed(_) => Vec::new(), - Cow::Owned(body) => body.into_bytes(), - }, - has_body, - header_mutations: Vec::new(), + let replacement = match body { + Cow::Borrowed(_) => None, + Cow::Owned(body) => Some(body.into_bytes()), + }; + Ok(HttpBodyEvaluation { + replacement, findings, metadata, - reason_code: String::new(), - }; - Ok(result) + }) } pub fn evaluate_websocket_text( diff --git a/crates/openshell-supervisor-network/src/sigv4.rs b/crates/openshell-supervisor-middleware-builtins/src/sigv4.rs similarity index 74% rename from crates/openshell-supervisor-network/src/sigv4.rs rename to crates/openshell-supervisor-middleware-builtins/src/sigv4.rs index 79e4c32e1..eccf320a0 100644 --- a/crates/openshell-supervisor-network/src/sigv4.rs +++ b/crates/openshell-supervisor-middleware-builtins/src/sigv4.rs @@ -8,8 +8,161 @@ use aws_sigv4::http_request::{ use aws_sigv4::sign::v4; use aws_smithy_runtime_api::client::identity::Identity; use miette::{Result, miette}; +use std::fmt; use std::time::SystemTime; +/// Built-in name used for the trusted post-credential signing stage. +pub const NAME: &str = "openshell/sigv4"; + +/// Maximum body retained by the built-in when the AWS signature covers the +/// complete payload. +pub const MAX_BODY_BYTES: usize = 10 * 1024 * 1024; + +/// Endpoint-selected signing behavior. This mirrors the existing policy +/// values without exposing network-policy types to the built-in crate. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum RequestedPayloadMode { + /// Preserve the payload mode selected by the AWS client when possible. + Auto, + /// Hash and sign the complete request body. + SignBody, + /// Sign only the request head with `UNSIGNED-PAYLOAD`. + UnsignedPayload, +} + +/// Normalized request framing relevant to payload-mode selection. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BodyFraming { + None, + ContentLength, + Chunked, +} + +/// Payload representation covered by the generated signature. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum PayloadMode { + /// Hash the complete request body. + SignBody, + /// Sign headers with the AWS `UNSIGNED-PAYLOAD` sentinel. + UnsignedPayload, + /// Sign an `aws-chunked` stream that carries an unsigned trailer. + StreamingUnsignedTrailer, +} + +impl fmt::Display for PayloadMode { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::SignBody => formatter.write_str("sign_body"), + Self::UnsignedPayload => formatter.write_str("unsigned_payload"), + Self::StreamingUnsignedTrailer => formatter.write_str("streaming_unsigned_trailer"), + } + } +} + +/// Resolve the concrete AWS payload mode from endpoint policy and the +/// caller's original request head. +pub fn resolve_payload_mode( + requested: RequestedPayloadMode, + original_headers: &str, + framing: BodyFraming, +) -> Result { + match requested { + RequestedPayloadMode::SignBody => return Ok(PayloadMode::SignBody), + RequestedPayloadMode::UnsignedPayload => return Ok(PayloadMode::UnsignedPayload), + RequestedPayloadMode::Auto => {} + } + + for line in original_headers.lines().skip(1) { + let Some((name, value)) = line.split_once(':') else { + continue; + }; + if !name.eq_ignore_ascii_case("x-amz-content-sha256") { + continue; + } + let value = value.trim().to_ascii_lowercase(); + return match value.as_str() { + "streaming-unsigned-payload-trailer" => Ok(PayloadMode::StreamingUnsignedTrailer), + "unsigned-payload" => Ok(PayloadMode::UnsignedPayload), + value if value.starts_with("streaming-") => Err(miette!( + "SigV4 auto-detect does not support chunk-signed streaming mode \ + '{value}'; use credential_signing: sigv4:no_body to stream \ + with UNSIGNED-PAYLOAD instead" + )), + _ => Ok(PayloadMode::SignBody), + }; + } + + Ok(if framing == BodyFraming::ContentLength { + PayloadMode::SignBody + } else { + PayloadMode::UnsignedPayload + }) +} + +/// Credential values kept inside the trusted built-in boundary. +#[derive(Debug, Clone, Copy)] +pub struct SigningCredentials<'a> { + pub access_key: &'a str, + pub secret_key: &'a str, + pub session_token: Option<&'a str>, +} + +/// Non-secret signing target selected from the admitted endpoint policy. +#[derive(Debug, Clone, Copy)] +pub struct SigningTarget<'a> { + pub host: &'a str, + pub region: &'a str, + pub service: &'a str, +} + +/// Restricted in-process signer invoked only after credential resolution. +#[derive(Debug, Default)] +pub struct SigV4Middleware; + +impl SigV4Middleware { + /// Remove caller-provided AWS authorization fields before credential + /// placeholder rewriting and trusted re-signing. + pub fn strip_existing_auth(raw: &[u8]) -> Result> { + strip_aws_headers(raw) + } + + /// Sign a complete request, including its body hash. + pub fn sign_body( + raw: &[u8], + target: SigningTarget<'_>, + credentials: SigningCredentials<'_>, + ) -> Result> { + apply_sigv4_to_request( + raw, + target.host, + target.region, + target.service, + credentials.access_key, + credentials.secret_key, + credentials.session_token, + ) + } + + /// Sign a request head without retaining the request body. + pub fn sign_headers( + raw_headers: &[u8], + target: SigningTarget<'_>, + credentials: SigningCredentials<'_>, + payload_mode: PayloadMode, + ) -> Result> { + apply_sigv4_headers_only_with_body( + raw_headers, + target.host, + target.region, + target.service, + credentials.access_key, + credentials.secret_key, + credentials.session_token, + payload_mode, + ) + } +} + /// AWS regions contain a hyphen followed by a digit (e.g., `us-east-1`). /// Service names like `s3` or `bedrock-runtime` do not. fn looks_like_region(s: &str) -> bool { @@ -326,7 +479,7 @@ pub fn apply_sigv4_headers_only( access_key, secret_key, session_token, - SignableBody::UnsignedPayload, + PayloadMode::UnsignedPayload, ) } @@ -344,7 +497,7 @@ pub fn apply_sigv4_headers_only_with_body( access_key: &str, secret_key: &str, session_token: Option<&str>, - body: SignableBody<'_>, + body: PayloadMode, ) -> Result> { let header_str = std::str::from_utf8(raw_headers) .map_err(|e| miette!("SigV4 signing: request headers are not valid UTF-8: {e}"))?; @@ -353,6 +506,15 @@ pub fn apply_sigv4_headers_only_with_body( let identity = build_identity(access_key, secret_key, session_token); let signing_params = build_signing_params(&identity, region, service)?; + let body = match body { + PayloadMode::SignBody => { + return Err(miette!( + "headers-only SigV4 signing cannot select full body hashing" + )); + } + PayloadMode::UnsignedPayload => SignableBody::UnsignedPayload, + PayloadMode::StreamingUnsignedTrailer => SignableBody::StreamingUnsignedPayloadTrailer, + }; let signable_request = SignableRequest::new( parts.method, &uri, @@ -375,6 +537,56 @@ pub fn apply_sigv4_headers_only_with_body( mod tests { use super::*; + #[test] + fn auto_payload_mode_preserves_unsigned_payload() { + assert_eq!( + resolve_payload_mode( + RequestedPayloadMode::Auto, + "PUT / HTTP/1.1\r\nX-Amz-Content-Sha256: UNSIGNED-PAYLOAD\r\n\r\n", + BodyFraming::ContentLength, + ) + .unwrap(), + PayloadMode::UnsignedPayload + ); + } + + #[test] + fn auto_payload_mode_preserves_streaming_unsigned_trailer() { + assert_eq!( + resolve_payload_mode( + RequestedPayloadMode::Auto, + "PUT / HTTP/1.1\r\nX-Amz-Content-Sha256: STREAMING-UNSIGNED-PAYLOAD-TRAILER\r\n\r\n", + BodyFraming::Chunked, + ) + .unwrap(), + PayloadMode::StreamingUnsignedTrailer + ); + } + + #[test] + fn auto_payload_mode_uses_body_hash_for_content_length() { + assert_eq!( + resolve_payload_mode( + RequestedPayloadMode::Auto, + "POST / HTTP/1.1\r\nContent-Length: 2\r\n\r\n", + BodyFraming::ContentLength, + ) + .unwrap(), + PayloadMode::SignBody + ); + } + + #[test] + fn auto_payload_mode_rejects_chunk_signed_streams() { + let error = resolve_payload_mode( + RequestedPayloadMode::Auto, + "PUT / HTTP/1.1\r\nX-Amz-Content-Sha256: STREAMING-AWS4-HMAC-SHA256-PAYLOAD\r\n\r\n", + BodyFraming::Chunked, + ) + .unwrap_err(); + assert!(error.to_string().contains("chunk-signed streaming mode")); + } + #[test] fn extract_region_from_hostname() { let region = extract_aws_region("bedrock-runtime.us-east-2.amazonaws.com").unwrap(); diff --git a/crates/openshell-supervisor-middleware/src/headers.rs b/crates/openshell-supervisor-middleware/src/headers.rs index 056ad32cf..654498dab 100644 --- a/crates/openshell-supervisor-middleware/src/headers.rs +++ b/crates/openshell-supervisor-middleware/src/headers.rs @@ -15,6 +15,7 @@ pub const MAX_HEADER_MUTATION_BYTES: usize = 32 * 1024; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum HeaderAuthority { Request, + RequestTrailers, Response, ResponseTrailers, } @@ -140,10 +141,12 @@ pub fn apply( Some(header_mutation::Operation::Write(write)) => { let name = validate_name(&write.name)?; validate_authority(authority, MutationKind::Write, &write.name, &name)?; - if authority == HeaderAuthority::ResponseTrailers - && !existing_headers - .iter() - .any(|existing| existing.name.eq_ignore_ascii_case(&name)) + if matches!( + authority, + HeaderAuthority::RequestTrailers | HeaderAuthority::ResponseTrailers + ) && !existing_headers + .iter() + .any(|existing| existing.name.eq_ignore_ascii_case(&name)) { return Err(HeaderMutationError::AbsentTrailerName { name: write.name.clone(), @@ -240,7 +243,9 @@ fn validate_authority( normalized_name: &str, ) -> Result<(), HeaderMutationError> { let protected = match authority { - HeaderAuthority::Request => is_request_protected(normalized_name), + HeaderAuthority::Request | HeaderAuthority::RequestTrailers => { + is_request_protected(normalized_name) + } HeaderAuthority::Response => { is_response_protected(normalized_name) || (kind == MutationKind::Write && is_response_remove_only(normalized_name)) diff --git a/crates/openshell-supervisor-middleware/src/lib.rs b/crates/openshell-supervisor-middleware/src/lib.rs index d9926678c..17d0d99f5 100644 --- a/crates/openshell-supervisor-middleware/src/lib.rs +++ b/crates/openshell-supervisor-middleware/src/lib.rs @@ -5,9 +5,16 @@ pub mod headers; mod remote; +mod request; mod response; mod websocket; +pub use request::{ + HttpRequestDiagnostics, HttpRequestFinish, HttpRequestInvocation, HttpRequestInvocationOutcome, + HttpRequestMiddlewareFailure, HttpRequestPreflightInput, HttpRequestPreflightOutcome, + HttpRequestSession, MAX_HTTP_REQUEST_DEFERRED_BYTES, MAX_HTTP_REQUEST_STREAM_UNIT_BYTES, +}; + pub use response::{ HttpResponseDiagnostics, HttpResponseFinish, HttpResponseInvocation, HttpResponseInvocationOutcome, HttpResponseMiddlewareFailure, HttpResponsePreflightInput, @@ -30,113 +37,19 @@ use std::time::Duration; use miette::{Result, miette}; use prost::Message; -use openshell_core::proto::middleware::v1::supervisor_middleware_server::SupervisorMiddleware; use openshell_core::proto::{ - Decision, Finding, HeaderMutation, HttpHeader, HttpRequestEvaluation, HttpRequestTarget, - MiddlewareBinding, MiddlewareManifest, NetworkMiddlewareConfig, RequestContext, SandboxPolicy, + Decision, Finding, HeaderMutation, HttpHeader, HttpRequestTarget, MiddlewareBinding, + MiddlewareManifest, NetworkMiddlewareConfig, RequestContext, SandboxPolicy, SupervisorMiddlewareOperation, SupervisorMiddlewarePhase, SupervisorMiddlewareService, ValidateConfigRequest, ValidateConfigResponse, }; use tokio::sync::{OnceCell, OwnedSemaphorePermit, Semaphore}; -use tonic::{Request, Response as TonicResponse, Status as TonicStatus}; +use tonic::Request; pub use openshell_core::middleware::{ - HttpRequestView, HttpResponseResultStream, InProcessMiddleware, SupervisorMiddlewareEndpoint, - WebSocketResponseStream, + HttpRequestResultStream, HttpResponseResultStream, InProcessMiddleware, + SupervisorMiddlewareEndpoint, WebSocketResponseStream, }; -pub type MiddlewareService = - dyn SupervisorMiddleware; - -struct GeneratedMiddlewareEndpoint { - service: Arc, -} - -#[tonic::async_trait] -impl SupervisorMiddlewareEndpoint for GeneratedMiddlewareEndpoint { - async fn describe( - &self, - request: Request<()>, - ) -> std::result::Result, TonicStatus> { - self.service.describe(request).await - } - - async fn validate_config( - &self, - request: Request, - ) -> std::result::Result, TonicStatus> { - self.service.validate_config(request).await - } - - async fn evaluate_http_request( - &self, - request: Request, - ) -> std::result::Result, TonicStatus> - { - self.service.evaluate_http_request(request).await - } - - async fn open_websocket_session( - &self, - _receiver: tokio::sync::mpsc::Receiver, - ) -> std::result::Result { - Err(TonicStatus::unimplemented( - "middleware service does not expose an in-process WebSocket stream", - )) - } -} - -#[tonic::async_trait] -impl InProcessMiddleware for GeneratedMiddlewareEndpoint { - async fn describe(&self) -> MiddlewareManifest { - self.service - .describe(Request::new(())) - .await - .expect("generated in-process Describe failed") - .into_inner() - } - - async fn validate_config( - &self, - middleware_name: &str, - config: &prost_types::Struct, - ) -> Result<()> { - let response = self - .service - .validate_config(Request::new(ValidateConfigRequest { - config: Some(config.clone()), - middleware_name: middleware_name.to_string(), - })) - .await - .map_err(|error| miette!("{error}"))? - .into_inner(); - if response.valid { - Ok(()) - } else { - Err(miette!("{}", response.reason)) - } - } - - async fn evaluate_http_request( - &self, - request: HttpRequestView<'_>, - ) -> Result { - self.service - .evaluate_http_request(Request::new(request_view_to_evaluation(request))) - .await - .map(tonic::Response::into_inner) - .map_err(|error| miette!("{error}")) - } -} - -/// Adapt a generated HTTP-only service to the borrowed in-process contract. -/// -/// This compatibility adapter is intended for tests and downstream HTTP-only -/// implementations. First-party built-ins implement [`InProcessMiddleware`] -/// directly so their HTTP path remains allocation-free. -pub fn http_only_endpoint(service: Arc) -> Arc { - Arc::new(GeneratedMiddlewareEndpoint { service }) -} - struct EndpointInProcessAdapter { endpoint: Arc, } @@ -172,15 +85,13 @@ impl InProcessMiddleware for EndpointInProcessAdapter { } } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - request: HttpRequestView<'_>, - ) -> Result { + requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { self.endpoint - .evaluate_http_request(Request::new(request_view_to_evaluation(request))) + .open_http_request_pre_credentials(requests) .await - .map(tonic::Response::into_inner) - .map_err(|error| miette!("{error}")) } async fn open_websocket_session( @@ -201,8 +112,8 @@ impl InProcessMiddleware for EndpointInProcessAdapter { /// Adapt a transport-neutral endpoint to the in-process registry contract. /// /// Prefer implementing [`InProcessMiddleware`] directly. This compatibility -/// path materializes an owned HTTP request, but preserves direct WebSocket -/// streams for endpoint implementations that predate the borrowed contract. +/// path preserves the request, response, and WebSocket event streams while +/// adapting configuration calls to the transport-neutral endpoint surface. pub fn in_process_endpoint( endpoint: Arc, ) -> Arc { @@ -262,8 +173,8 @@ impl MiddlewareWorkAdmissionOutcome { /// /// Protocol-specific session runners retain this guard while at least one /// streaming stage remains active. Registry replacement preserves the shared -/// admission state so future streaming HTTP middleware can use the same -/// process-wide bound. +/// admission state so HTTP and WebSocket streams use the same process-wide +/// bound across registry replacement. #[derive(Debug)] struct MiddlewareSessionPermit { _session: OwnedSemaphorePermit, @@ -334,6 +245,7 @@ const EXTERNAL_FINDING_LABEL: &str = "External middleware finding"; #[cfg(test)] const HTTP_REQUEST_OPERATION: SupervisorMiddlewareOperation = SupervisorMiddlewareOperation::HttpRequest; +#[cfg(test)] const PRE_CREDENTIALS_PHASE: SupervisorMiddlewarePhase = SupervisorMiddlewarePhase::PreCredentials; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum OnError { @@ -515,56 +427,6 @@ pub struct MiddlewareInvocation { pub failed: bool, } -enum OnErrorAction { - /// `fail_open`: skip this middleware, leaving the request unchanged. - FailOpen, - /// `fail_closed`: short-circuit the chain and deny with the given reason. - FailClosed(String), -} - -/// Apply a middleware entry's `on_error` policy after a failure (service error or -/// malformed response). Records a `failed` invocation for telemetry in both cases. -fn apply_on_error( - entry: &DescribedChainEntry, - reason: &str, - applied: &mut Vec, -) -> OnErrorAction { - match entry.entry.on_error { - OnError::FailOpen => { - applied.push(MiddlewareInvocation { - name: entry.entry.name.clone(), - implementation: entry.entry.implementation.clone(), - decision: Decision::Allow, - transformed: false, - failed: true, - }); - OnErrorAction::FailOpen - } - OnError::FailClosed => { - applied.push(MiddlewareInvocation { - name: entry.entry.name.clone(), - implementation: entry.entry.implementation.clone(), - decision: Decision::Deny, - transformed: false, - failed: true, - }); - OnErrorAction::FailClosed(format!("middleware_failed: {reason}")) - } - } -} - -fn request_view_to_evaluation(request: HttpRequestView<'_>) -> HttpRequestEvaluation { - HttpRequestEvaluation { - phase: request.phase() as i32, - context: Some(request.context().clone()), - config: Some(request.config().clone()), - target: Some(request.target().clone()), - headers: request.headers().to_vec(), - body: request.body().to_vec(), - middleware_name: request.middleware_name().to_string(), - } -} - #[derive(Clone)] pub struct ChainRunner { registry: Arc, @@ -610,18 +472,13 @@ impl MiddlewareDispatch { } } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - request: HttpRequestView<'_>, - ) -> std::result::Result, tonic::Status> - { + receiver: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { match self { - Self::InProcess(service) => service - .evaluate_http_request(request) - .await - .map(tonic::Response::new) - .map_err(|error| tonic::Status::invalid_argument(error.to_string())), - Self::Grpc(service) => service.evaluate_http_request(request).await, + Self::InProcess(service) => service.open_http_request_pre_credentials(receiver).await, + Self::Grpc(service) => service.open_http_request_pre_credentials(receiver).await, } } @@ -694,16 +551,6 @@ impl MiddlewareDiagnosticPolicy { } } - fn process_result( - self, - middleware_name: &str, - result: &mut openshell_core::proto::HttpRequestResult, - ) { - if self == Self::Normalize { - normalize_untrusted_diagnostics(middleware_name, result); - } - } - fn header_mutation_error_reason(self, error: &headers::HeaderMutationError) -> String { match self { Self::Preserve => safe_reason(&error.to_string()), @@ -854,6 +701,7 @@ fn validate_payload_limit(source: &str, binding: &MiddlewareBinding) -> Result Result Ok(SupportedBinding::HttpPreCredentials), + ( + Some(SupervisorMiddlewareOperation::HttpRequest), + Some(SupervisorMiddlewarePhase::PostCredentials), + ) => Ok(SupportedBinding::HttpPostCredentials), ( Some(SupervisorMiddlewareOperation::HttpResponse), Some(SupervisorMiddlewarePhase::PreReturn), @@ -948,6 +800,15 @@ fn validate_external_manifest( operator_max_payload_bytes: usize, authenticated: bool, ) -> Result<()> { + if manifest.bindings.iter().any(|binding| { + SupervisorMiddlewarePhase::try_from(binding.phase) + .is_ok_and(|phase| phase == SupervisorMiddlewarePhase::PostCredentials) + }) { + return Err(miette!( + "external middleware registration '{}' advertises POST_CREDENTIALS, which is reserved for trusted in-process built-ins", + registration.name + )); + } validate_manifest_bindings( &format!("external middleware registration '{}'", registration.name), manifest, @@ -985,113 +846,6 @@ fn validate_expected_audience( Ok(()) } -/// External diagnostic text is untrusted and may contain request data. Keep -/// only values derived from the validated, operator-owned registration name -/// and numeric finding counts; do not carry per-request free-form text into -/// logs. -fn normalize_untrusted_diagnostics( - middleware_name: &str, - result: &mut openshell_core::proto::HttpRequestResult, -) { - let reason_id: String = middleware_name - .chars() - .map(|character| { - if character.is_ascii_alphanumeric() || matches!(character, '_' | '-') { - character - } else { - '_' - } - }) - .collect(); - result.reason = format!("middleware_denied:{reason_id}"); - result.metadata.clear(); - for finding in &mut result.findings { - finding.r#type = format!("{middleware_name}.finding"); - finding.label = EXTERNAL_FINDING_LABEL.to_string(); - finding.confidence.clear(); - finding.severity = match finding.severity.as_str() { - "low" => "low", - "high" => "high", - _ => "medium", - } - .to_string(); - } -} - -fn validate_request_view(request: HttpRequestView<'_>) -> std::result::Result<(), &'static str> { - if request.body().len() > MAX_MIDDLEWARE_PAYLOAD_BYTES { - return Err("request_body_over_capacity"); - } - if request.config().encoded_len() > MAX_MIDDLEWARE_CONFIG_BYTES { - return Err("request_config_over_capacity"); - } - if request.context().encoded_len() > MAX_MIDDLEWARE_CONTEXT_BYTES { - return Err("request_context_over_capacity"); - } - if request.target().encoded_len() > MAX_MIDDLEWARE_TARGET_BYTES { - return Err("request_target_over_capacity"); - } - if request.headers().len() > MAX_MIDDLEWARE_HEADERS { - return Err("request_header_count_over_capacity"); - } - let header_bytes = request.headers().iter().fold(0usize, |total, header| { - total.saturating_add(header.encoded_len()) - }); - if header_bytes > MAX_MIDDLEWARE_HEADER_BYTES { - return Err("request_header_bytes_over_capacity"); - } - Ok(()) -} - -fn validate_response_envelope( - result: &openshell_core::proto::HttpRequestResult, -) -> std::result::Result<(), &'static str> { - if result.body.len() > MAX_MIDDLEWARE_PAYLOAD_BYTES { - return Err("response_body_over_capacity"); - } - if result.reason.len() > MAX_MIDDLEWARE_REASON_BYTES { - return Err("response_reason_over_capacity"); - } - if !result.reason_code.is_empty() && !is_stable_reason_code(&result.reason_code) { - return Err("response_reason_code_invalid"); - } - if result.header_mutations.len() > headers::MAX_HEADER_MUTATIONS { - return Err("header_mutation_count_over_capacity"); - } - let mutation_bytes = result - .header_mutations - .iter() - .fold(0usize, |total, mutation| { - total.saturating_add(mutation.encoded_len()) - }); - if mutation_bytes > MAX_MIDDLEWARE_HEADER_MUTATION_WIRE_BYTES { - return Err("header_mutation_bytes_over_capacity"); - } - if result.findings.len() > MAX_MIDDLEWARE_FINDINGS_PER_STAGE { - return Err("response_findings_over_capacity"); - } - if result - .findings - .iter() - .any(|finding| finding.encoded_len() > MAX_MIDDLEWARE_FINDING_BYTES) - { - return Err("response_finding_over_capacity"); - } - if result.metadata.len() > MAX_MIDDLEWARE_METADATA_ENTRIES { - return Err("response_metadata_count_over_capacity"); - } - let metadata_bytes = result.metadata.iter().fold(0usize, |total, (key, value)| { - total.saturating_add(key.len()).saturating_add(value.len()) - }); - if metadata_bytes > MAX_MIDDLEWARE_METADATA_BYTES { - return Err("response_metadata_bytes_over_capacity"); - } - if result.encoded_len() > MIDDLEWARE_GRPC_MESSAGE_BYTES { - return Err("response_envelope_over_capacity"); - } - Ok(()) -} - impl MiddlewareRegistry { /// Describe in-process services, then connect and validate every /// operator-provided service registration. @@ -1346,9 +1100,7 @@ impl ChainRunner { } #[cfg(test)] - fn new_protobuf_for_tests(service: Arc) -> Self { - let endpoint: Arc = - Arc::new(GeneratedMiddlewareEndpoint { service }); + fn new_protobuf_for_tests(endpoint: Arc) -> Self { Self::from_service(MiddlewareDispatch::Grpc( remote::GrpcMiddlewareService::from_service(endpoint), )) @@ -1663,9 +1415,6 @@ impl ChainRunner { connection_nominated_headers, body, } = input; - // The request envelope is moved into one stable chain state. Built-ins - // borrow these values for every stage; only the gRPC adapter clones them - // when an operator service requires an owned protobuf message. let context = RequestContext { request_id, sandbox_id, @@ -1681,305 +1430,143 @@ impl ChainRunner { path, query, }; - let mut headers: Vec = headers + let mut headers = headers .into_iter() .map(|(name, value)| HttpHeader { name, value }) - .collect(); + .collect::>(); let mut body = body; let mut header_mutations = Vec::new(); let mut findings = Vec::new(); let mut metadata = BTreeMap::new(); let mut applied = Vec::new(); - let _admission = admission; - let chain_deadline = tokio::time::Instant::now() + MAX_MIDDLEWARE_CHAIN_TIMEOUT; + // The request session budget now bounds the streaming lifecycle, so a + // compatibility caller's pre-buffer admission can be released before + // opening the stage stream. + drop(admission); + // The compatibility collector evaluates one stage at a time so + // body-aware protocols can re-run policy after every accepted + // replacement. The HTTP relay uses the streaming session API directly + // and does not collect a complete request in memory. for entry in entries { - let Some(_binding) = entry.binding.as_ref() else { - match apply_on_error(entry, "binding_not_described", &mut applied) { - OnErrorAction::FailOpen => continue, - OnErrorAction::FailClosed(reason) => { - return Ok(ChainOutcome { - allowed: false, - reason, - body, - header_mutations, - findings, - metadata, - applied, - denial: None, - }); - } - } - }; - if body.len() > entry.max_payload_bytes { - match apply_on_error(entry, "request_body_over_capacity", &mut applied) { - OnErrorAction::FailOpen => continue, - OnErrorAction::FailClosed(reason) => { - return Ok(ChainOutcome { - allowed: false, - reason, - body, - header_mutations, - findings, - metadata, - applied, - denial: None, - }); - } - } - } - let request = HttpRequestView::new( - PRE_CREDENTIALS_PHASE, - &context, - &entry.entry.config, - &target, - &headers, - &body, - &entry.entry.implementation, - ); - if let Err(reason) = validate_request_view(request) { - match apply_on_error(entry, reason, &mut applied) { - OnErrorAction::FailOpen => continue, - OnErrorAction::FailClosed(reason) => { - return Ok(ChainOutcome { - allowed: false, - reason, - body, - header_mutations, - findings, - metadata, - applied, - denial: None, - }); - } - } - } - let Some(service) = entry.service.as_ref() else { - unreachable!("described binding always has a service") - }; - let remaining = chain_deadline.saturating_duration_since(tokio::time::Instant::now()); - if remaining.is_zero() { - match apply_on_error(entry, "middleware_chain_timeout", &mut applied) { - OnErrorAction::FailOpen => continue, - OnErrorAction::FailClosed(reason) => { - return Ok(ChainOutcome { - allowed: false, - reason, - body, - header_mutations, - findings, - metadata, - applied, - denial: None, - }); - } - } - } - let mut result = match call_with_timeout( - entry.timeout.min(remaining), - "EvaluateHttpRequest", - service.service.evaluate_http_request(request), - ) - .await - { - Ok(result) => result.into_inner(), - Err(err) => { - let reason = if err.code() == tonic::Code::DeadlineExceeded { - "middleware_timeout".to_string() - } else { - service.diagnostic_policy.error_reason(&err) - }; - match apply_on_error(entry, &reason, &mut applied) { - OnErrorAction::FailOpen => continue, - OnErrorAction::FailClosed(reason) => { - return Ok(ChainOutcome { - allowed: false, - reason, - body, - header_mutations, - findings, - metadata, - applied, - denial: None, - }); - } - } - } - }; + let preflight = self + .preflight_described_http_request_with_owned( + vec![entry.clone()], + HttpRequestPreflightInput { + context: context.clone(), + target: target.clone(), + declared_body_length: Some(body.len() as u64), + headers: headers.clone(), + connection_nominated_headers: connection_nominated_headers.clone(), + }, + false, + ) + .await?; - if let Err(reason) = validate_response_envelope(&result) { - match apply_on_error(entry, reason, &mut applied) { - OnErrorAction::FailOpen => continue, - OnErrorAction::FailClosed(reason) => { - return Ok(ChainOutcome { - allowed: false, - reason, - body, - header_mutations, - findings, - metadata, - applied, - denial: None, - }); - } - } - } + findings.extend(preflight.findings.clone()); + metadata.extend(preflight.metadata.clone()); + let mut stage_failed = preflight.invocations.iter().any(|item| item.failed); + let headers_transformed = preflight.headers != headers; + headers = preflight.headers; + header_mutations.extend(preflight.header_mutations); - service - .diagnostic_policy - .process_result(&entry.entry.implementation, &mut result); - - let decision = match Decision::try_from(result.decision) { - Ok(decision @ (Decision::Allow | Decision::Deny)) => decision, - Ok(Decision::Unspecified) | Err(_) => { - match apply_on_error(entry, "invalid_response_decision", &mut applied) { - OnErrorAction::FailOpen => continue, - OnErrorAction::FailClosed(reason) => { - return Ok(ChainOutcome { - allowed: false, - reason, - body, - header_mutations, - findings, - metadata, - applied, - denial: None, - }); - } - } - } - }; - - if decision == Decision::Deny { - let reason_code = - (!result.reason_code.is_empty()).then(|| result.reason_code.clone()); - let denial = MiddlewareDenial { - config_name: entry.entry.name.clone(), - reason_code, - }; - for finding in result.findings { - findings.push(NamespacedFinding { - middleware: entry.entry.name.clone(), - finding, - }); - } - if !result.metadata.is_empty() { - metadata.insert( - entry.entry.name.clone(), - result.metadata.into_iter().collect(), - ); - } + if !preflight.allowed { applied.push(MiddlewareInvocation { name: entry.entry.name.clone(), implementation: entry.entry.implementation.clone(), - decision, + decision: Decision::Deny, transformed: false, - failed: false, + failed: preflight.denial.is_none(), }); return Ok(ChainOutcome { allowed: false, - reason: middleware_denial_reason( - &denial.config_name, - denial.reason_code.as_deref(), - ), + reason: preflight.reason, body, header_mutations, findings, metadata, applied, - denial: Some(denial), + denial: preflight.denial, }); } - if result.has_body && result.body.len() > entry.max_payload_bytes { - match apply_on_error(entry, "response_body_over_capacity", &mut applied) { - OnErrorAction::FailOpen => continue, - OnErrorAction::FailClosed(reason) => { + let mut body_transformed = false; + if let Some(mut session) = preflight.session { + let original_body = body.clone(); + let mut output = Vec::new(); + let unit_limit = session.stream_unit_limit(); + let mut failed = None; + for chunk in body.chunks(unit_limit) { + match session.push_body(chunk.to_vec()).await { + Ok(units) => output.extend(units), + Err(error) => { + failed = Some(error); + break; + } + } + } + if let Some(error) = failed { + findings.extend(error.diagnostics.findings); + metadata.extend(error.diagnostics.metadata); + applied.push(MiddlewareInvocation { + name: entry.entry.name.clone(), + implementation: entry.entry.implementation.clone(), + decision: Decision::Deny, + transformed: false, + failed: error.denial.is_none(), + }); + return Ok(ChainOutcome { + allowed: false, + reason: error.reason, + body: original_body, + header_mutations, + findings, + metadata, + applied, + denial: error.denial, + }); + } + match session.finish(Vec::new()).await { + Ok(finish) => { + output.extend(finish.body_units); + body_transformed = finish.body_transformed; + stage_failed |= finish.invocations.iter().any(|item| item.failed); + findings.extend(finish.findings); + metadata.extend(finish.metadata); + body = output.concat(); + } + Err(error) => { + findings.extend(error.diagnostics.findings); + metadata.extend(error.diagnostics.metadata); + applied.push(MiddlewareInvocation { + name: entry.entry.name.clone(), + implementation: entry.entry.implementation.clone(), + decision: Decision::Deny, + transformed: false, + failed: error.denial.is_none(), + }); return Ok(ChainOutcome { allowed: false, - reason, - body, + reason: error.reason, + body: original_body, header_mutations, findings, metadata, applied, - denial: None, + denial: error.denial, }); } } } - // Validate and apply the entire stage atomically. Under fail-open, - // one malformed mutation must not leave earlier mutations from the - // same response visible to later middleware. - let updated_headers = if result.header_mutations.is_empty() { - None - } else { - match headers::apply( - headers::HeaderAuthority::Request, - &headers, - &connection_nominated_headers, - &result.header_mutations, - ) { - Ok(updated) => Some(updated), - Err(error) => { - let reason = service - .diagnostic_policy - .header_mutation_error_reason(&error); - match apply_on_error(entry, &reason, &mut applied) { - OnErrorAction::FailOpen => continue, - OnErrorAction::FailClosed(reason) => { - return Ok(ChainOutcome { - allowed: false, - reason, - body, - header_mutations, - findings, - metadata, - applied, - denial: None, - }); - } - } - } - } - }; - let headers_transformed = updated_headers - .as_ref() - .is_some_and(|updated| updated != &headers); - if let Some(updated) = updated_headers { - headers = updated; - } - header_mutations.extend(std::mem::take(&mut result.header_mutations)); - - let body_transformed = result.has_body; - if body_transformed { - body = std::mem::take(&mut result.body); - } - for finding in result.findings { - findings.push(NamespacedFinding { - middleware: entry.entry.name.clone(), - finding, - }); - } - if !result.metadata.is_empty() { - metadata.insert( - entry.entry.name.clone(), - result.metadata.into_iter().collect(), - ); - } applied.push(MiddlewareInvocation { name: entry.entry.name.clone(), implementation: entry.entry.implementation.clone(), - decision, + decision: Decision::Allow, transformed: body_transformed || headers_transformed, - failed: false, + failed: stage_failed, }); - // The stage ran successfully but its output must still satisfy the - // sandbox policy the original body was admitted under. Re-check now, - // before the next stage or the upstream sees the replaced body. A - // policy deny here is a hard deny, independent of `on_error`. if body_transformed && let TransformedBodyPolicy::Reevaluate(validate) = transformed_body_policy { @@ -2058,13 +1645,200 @@ pub(crate) fn safe_reason(reason: &str) -> String { mod tests { use super::*; use futures::{FutureExt, Stream, StreamExt}; + use openshell_core::proto::middleware::v1::http_request_pre_credentials_server::{ + HttpRequestPreCredentials, HttpRequestPreCredentialsServer, + }; use openshell_core::proto::middleware::v1::supervisor_middleware_server::{ SupervisorMiddleware, SupervisorMiddlewareServer, }; use openshell_core::proto::{ExistingHeaderAction, header_mutation}; use openshell_supervisor_middleware_builtins::{BUILTIN_REGEX, services}; - use tokio_stream::wrappers::TcpListenerStream; + use tokio_stream::wrappers::{ReceiverStream, TcpListenerStream}; + + #[derive(Clone, Default)] + struct TestRequestEvaluation { + phase: i32, + context: Option, + config: Option, + target: Option, + headers: Vec, + body: Vec, + middleware_name: String, + } + + #[derive(Clone, Default)] + struct TestRequestResult { + decision: i32, + reason: String, + body: Vec, + has_body: bool, + header_mutations: Vec, + findings: Vec, + metadata: HashMap, + reason_code: String, + } + + type TestBodyHandler = + Box, TestRequestEvaluation) -> TestRequestResult + Send + 'static>; + + struct TestRequestPlan { + preflight: TestRequestResult, + body: Option, + } + + fn constant_request_plan(result: TestRequestResult) -> TestRequestPlan { + if result.decision != Decision::Allow as i32 { + return TestRequestPlan { + preflight: result, + body: None, + }; + } + let preflight = TestRequestResult { + decision: result.decision, + header_mutations: result.header_mutations.clone(), + ..Default::default() + }; + TestRequestPlan { + preflight, + body: Some(Box::new(move |_body, _evaluation| result)), + } + } + + fn open_test_request_stream( + mut requests: tokio::sync::mpsc::Receiver, + plan: F, + ) -> HttpRequestResultStream + where + F: FnOnce(openshell_core::proto::HttpRequestPreflight) -> TestRequestPlan + Send + 'static, + { + use openshell_core::proto::{ + HttpRequestBlock, HttpRequestBodyPassThrough, HttpRequestBodyResult, + HttpRequestBodyTransform, HttpRequestEventResult, HttpRequestPreflightInspect, + HttpRequestPreflightResult, HttpRequestTrailersResult, http_request_body_result, + http_request_body_transform, http_request_event, http_request_event_result, + http_request_preflight_result, + }; + + let (sender, receiver) = tokio::sync::mpsc::channel(4); + tokio::spawn(async move { + let Some(openshell_core::proto::HttpRequestEvent { + event: Some(http_request_event::Event::Preflight(preflight)), + }) = requests.recv().await + else { + return; + }; + let mut evaluation = TestRequestEvaluation { + phase: SupervisorMiddlewarePhase::PreCredentials as i32, + context: preflight.context.clone(), + config: preflight.config.clone(), + target: preflight.target.clone(), + headers: preflight.headers.clone(), + body: Vec::new(), + middleware_name: preflight.middleware_name.clone(), + }; + let TestRequestPlan { + preflight: initial, + body, + } = plan(preflight); + let action = match Decision::try_from(initial.decision) { + Ok(Decision::Deny) => Some(http_request_preflight_result::Action::BlockRequest( + HttpRequestBlock {}, + )), + Ok(Decision::Allow) => Some(http_request_preflight_result::Action::Inspect( + HttpRequestPreflightInspect { + body_mode: if body.is_some() { + openshell_core::proto::HttpRequestBodyMode::WholeBodyBytes as i32 + } else { + openshell_core::proto::HttpRequestBodyMode::HeadersOnly as i32 + }, + header_mutations: initial.header_mutations, + }, + )), + Ok(Decision::Unspecified) | Err(_) => None, + }; + if sender + .send(Ok(HttpRequestEventResult { + result: Some(http_request_event_result::Result::PreflightResult( + HttpRequestPreflightResult { + action, + reason: initial.reason, + reason_code: initial.reason_code, + findings: initial.findings, + metadata: initial.metadata, + }, + )), + })) + .await + .is_err() + { + return; + } + + let Some(body_handler) = body else { + while requests.recv().await.is_some() {} + return; + }; + let Some(openshell_core::proto::HttpRequestEvent { + event: Some(http_request_event::Event::Body(body)), + }) = requests.recv().await + else { + return; + }; + evaluation.body = match body.payload { + Some(openshell_core::proto::http_request_body_unit::Payload::Data(data)) => data, + None => Vec::new(), + }; + let sequence = body.sequence; + let result = body_handler(evaluation.body.clone(), evaluation); + let action = if result.decision == Decision::Deny as i32 { + http_request_body_result::Action::BlockRequest(HttpRequestBlock {}) + } else if result.has_body { + http_request_body_result::Action::Transform(HttpRequestBodyTransform { + replacement: Some(http_request_body_transform::Replacement::Data(result.body)), + }) + } else { + http_request_body_result::Action::PassThrough(HttpRequestBodyPassThrough {}) + }; + if sender + .send(Ok(HttpRequestEventResult { + result: Some(http_request_event_result::Result::BodyResult( + HttpRequestBodyResult { + sequence, + action: Some(action), + reason: result.reason, + reason_code: result.reason_code, + findings: result.findings, + metadata: result.metadata, + }, + )), + })) + .await + .is_err() + { + return; + } + while let Some(event) = requests.recv().await { + match event.event { + Some(http_request_event::Event::Trailers(_)) => { + if sender + .send(Ok(HttpRequestEventResult { + result: Some(http_request_event_result::Result::TrailersResult( + HttpRequestTrailersResult::default(), + )), + })) + .await + .is_err() + { + break; + } + } + _ => break, + } + } + }); + Box::pin(ReceiverStream::new(receiver)) + } fn proto_duration(value: &str) -> prost_types::Duration { let duration = match (value.strip_suffix("ms"), value.strip_suffix('s')) { @@ -2161,251 +1935,6 @@ mod tests { } } - #[derive(Debug, Clone, PartialEq, Eq)] - struct RequestAddresses { - phase: SupervisorMiddlewarePhase, - context: usize, - request_id: usize, - config: usize, - target: usize, - host: usize, - headers: usize, - first_header_name: usize, - body: usize, - originating_process_present: bool, - middleware_name: String, - } - - /// Records borrowed addresses so the test can detect an owned envelope - /// being reconstructed between otherwise no-op in-process stages. - struct BorrowedRecordingService { - manifest_name: String, - received: std::sync::Mutex>, - } - - #[tonic::async_trait] - impl InProcessMiddleware for BorrowedRecordingService { - async fn describe(&self) -> MiddlewareManifest { - MiddlewareManifest { - name: self.manifest_name.clone(), - service_version: "test".into(), - bindings: vec![MiddlewareBinding { - operation: SupervisorMiddlewareOperation::HttpRequest as i32, - phase: SupervisorMiddlewarePhase::PreCredentials as i32, - max_payload_bytes: 4096, - request_timeout: None, - }], - expected_audience: String::new(), - } - } - - async fn validate_config( - &self, - _middleware_name: &str, - _config: &prost_types::Struct, - ) -> Result<()> { - Ok(()) - } - - async fn evaluate_http_request( - &self, - request: HttpRequestView<'_>, - ) -> Result { - let addresses = RequestAddresses { - phase: request.phase(), - context: std::ptr::from_ref(request.context()).addr(), - request_id: request.context().request_id.as_ptr().addr(), - config: std::ptr::from_ref(request.config()).addr(), - target: std::ptr::from_ref(request.target()).addr(), - host: request.target().host.as_ptr().addr(), - headers: request.headers().as_ptr().addr(), - first_header_name: request - .headers() - .first() - .map_or(0, |header| header.name.as_ptr().addr()), - body: request.body().as_ptr().addr(), - originating_process_present: request.context().originating_process.is_some(), - middleware_name: request.middleware_name().to_string(), - }; - self.received - .lock() - .expect("borrowed request recorder lock") - .push(addresses); - Ok(allow_result()) - } - } - - #[tokio::test] - async fn in_process_stages_share_one_borrowed_request_envelope() { - let service = Arc::new(BorrowedRecordingService { - manifest_name: "acme/redactor".into(), - received: std::sync::Mutex::new(Vec::new()), - }); - let runner = ChainRunner::new(service.clone()); - let entries = [ - ChainEntry { - name: "first".into(), - implementation: "acme/redactor".into(), - order: 0, - config: prost_types::Struct::default(), - on_error: OnError::FailClosed, - }, - ChainEntry { - name: "second".into(), - implementation: "acme/redactor".into(), - order: 10, - config: prost_types::Struct::default(), - on_error: OnError::FailClosed, - }, - ]; - let described = runner - .describe_chain(&entries) - .await - .expect("describe chain"); - let expected_configs: Vec<_> = described - .iter() - .map(|entry| std::ptr::from_ref(&entry.entry.config).addr()) - .collect(); - let mut request = input("payload"); - request.headers = vec![("x-test".into(), "value".into())]; - let expected_body = request.body.as_ptr().addr(); - let expected_request_id = request.request_id.as_ptr().addr(); - let expected_host = request.host.as_ptr().addr(); - let expected_header_name = request.headers[0].0.as_ptr().addr(); - - let outcome = runner - .evaluate_described(&described, request) - .await - .expect("evaluate borrowed chain"); - let received = service.received.lock().expect("borrowed requests"); - - assert!(outcome.allowed); - assert_eq!(outcome.body.as_ptr().addr(), expected_body); - assert_eq!(received.len(), 2); - assert_eq!(received[0].phase, SupervisorMiddlewarePhase::PreCredentials); - assert!(!received[0].originating_process_present); - assert_eq!(received[0].request_id, expected_request_id); - assert_eq!(received[0].host, expected_host); - assert_eq!(received[0].first_header_name, expected_header_name); - assert_eq!(received[0].body, expected_body); - assert_eq!(received[0].config, expected_configs[0]); - assert_eq!(received[1].config, expected_configs[1]); - assert_eq!(received[0].context, received[1].context); - assert_eq!(received[0].target, received[1].target); - assert_eq!(received[0].headers, received[1].headers); - assert_eq!(received[0].body, received[1].body); - assert!( - received - .iter() - .all(|request| request.middleware_name == "acme/redactor") - ); - } - - const TEST_REPLACEMENT_BODY: &[u8] = b"stage-one-replacement"; - - /// Records both sides of a successful body replacement so the test can - /// distinguish ownership transfer from a content-preserving body copy. - #[derive(Debug, Default)] - struct ReplacementTransferRecord { - invocations: usize, - returned_body: Option, - second_body: Option, - second_body_bytes: Vec, - } - - /// Replaces the first request body and observes the body borrowed by the - /// second stage without replacing it again. - struct ReplacementTransferService { - record: std::sync::Mutex, - } - - #[tonic::async_trait] - impl InProcessMiddleware for ReplacementTransferService { - async fn describe(&self) -> MiddlewareManifest { - MiddlewareManifest { - name: "test/replacement-transfer".into(), - service_version: "test".into(), - bindings: vec![MiddlewareBinding { - operation: SupervisorMiddlewareOperation::HttpRequest as i32, - phase: SupervisorMiddlewarePhase::PreCredentials as i32, - max_payload_bytes: 4096, - request_timeout: None, - }], - expected_audience: String::new(), - } - } - - async fn validate_config( - &self, - _middleware_name: &str, - _config: &prost_types::Struct, - ) -> Result<()> { - Ok(()) - } - - async fn evaluate_http_request( - &self, - request: HttpRequestView<'_>, - ) -> Result { - let mut record = self.record.lock().expect("replacement transfer record"); - let invocation = record.invocations; - record.invocations += 1; - - if invocation == 0 { - let replacement = TEST_REPLACEMENT_BODY.to_vec(); - record.returned_body = Some(replacement.as_ptr().addr()); - let mut result = allow_result(); - result.body = replacement; - result.has_body = true; - Ok(result) - } else { - record.second_body = Some(request.body().as_ptr().addr()); - record.second_body_bytes = request.body().to_vec(); - Ok(allow_result()) - } - } - } - - #[tokio::test] - async fn replacement_body_allocation_moves_through_next_stage_and_outcome() { - let service = Arc::new(ReplacementTransferService { - record: std::sync::Mutex::new(ReplacementTransferRecord::default()), - }); - let runner = ChainRunner::new(service.clone()); - let entries = [ - ChainEntry { - name: "replace".into(), - implementation: "test/replacement-transfer".into(), - order: 0, - config: prost_types::Struct::default(), - on_error: OnError::FailClosed, - }, - ChainEntry { - name: "observe".into(), - implementation: "test/replacement-transfer".into(), - order: 10, - config: prost_types::Struct::default(), - on_error: OnError::FailClosed, - }, - ]; - - let outcome = runner - .evaluate(&entries, input("original-body")) - .await - .expect("evaluate replacement transfer chain"); - let record = service.record.lock().expect("replacement transfer record"); - let returned_body = record - .returned_body - .expect("first-stage replacement pointer"); - - assert!(outcome.allowed); - assert_eq!(record.invocations, 2); - assert_eq!(record.second_body_bytes, TEST_REPLACEMENT_BODY); - assert_eq!(record.second_body, Some(returned_body)); - assert_eq!(outcome.body, TEST_REPLACEMENT_BODY); - assert_eq!(outcome.body.as_ptr().addr(), returned_body); - } - /// An in-process service that yields forever so the runtime must enforce /// the binding timeout around borrowed validation and evaluation futures. struct PendingInProcessService; @@ -2434,14 +1963,859 @@ mod tests { std::future::pending().await } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - _request: HttpRequestView<'_>, - ) -> Result { + _requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { std::future::pending().await } } + struct OwnedStreamService { + invalid_finalization: bool, + } + + #[derive(Default)] + struct StreamSequenceRecorder { + streams: std::sync::Mutex>>, + } + + struct StreamSequenceService { + recorder: Arc, + findings_per_body: bool, + } + + #[tonic::async_trait] + impl InProcessMiddleware for StreamSequenceService { + async fn describe(&self) -> MiddlewareManifest { + MiddlewareManifest { + name: "test/stream-sequence".into(), + service_version: "test".into(), + bindings: vec![MiddlewareBinding { + operation: SupervisorMiddlewareOperation::HttpRequest as i32, + phase: SupervisorMiddlewarePhase::PreCredentials as i32, + max_payload_bytes: MAX_HTTP_REQUEST_STREAM_UNIT_BYTES as u64, + request_timeout: None, + }], + expected_audience: String::new(), + } + } + + async fn validate_config( + &self, + _middleware_name: &str, + _config: &prost_types::Struct, + ) -> Result<()> { + Ok(()) + } + + async fn open_http_request_pre_credentials( + &self, + mut requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { + use openshell_core::proto::{ + HttpRequestBodyMode, HttpRequestBodyPassThrough, HttpRequestBodyResult, + HttpRequestEventResult, HttpRequestPreflightInspect, HttpRequestPreflightResult, + HttpRequestTrailersResult, http_request_body_result, http_request_body_unit, + http_request_event, http_request_event_result, http_request_preflight_result, + }; + + let stream_index = { + let mut streams = self.recorder.streams.lock().expect("stream recorder lock"); + streams.push(Vec::new()); + streams.len() - 1 + }; + let recorder = Arc::clone(&self.recorder); + let findings_per_body = self.findings_per_body; + let (sender, receiver) = tokio::sync::mpsc::channel(4); + tokio::spawn(async move { + while let Some(event) = requests.recv().await { + let result = match event.event { + Some(http_request_event::Event::Preflight(_)) => HttpRequestEventResult { + result: Some(http_request_event_result::Result::PreflightResult( + HttpRequestPreflightResult { + action: Some(http_request_preflight_result::Action::Inspect( + HttpRequestPreflightInspect { + body_mode: HttpRequestBodyMode::StreamBytes as i32, + header_mutations: Vec::new(), + }, + )), + ..Default::default() + }, + )), + }, + Some(http_request_event::Event::Body(body)) => { + let size = match body.payload { + Some(http_request_body_unit::Payload::Data(ref data)) => data.len(), + None => break, + }; + recorder.streams.lock().expect("stream recorder lock")[stream_index] + .push((size, body.end_of_stream)); + HttpRequestEventResult { + result: Some(http_request_event_result::Result::BodyResult( + HttpRequestBodyResult { + sequence: body.sequence, + action: Some( + http_request_body_result::Action::PassThrough( + HttpRequestBodyPassThrough {}, + ), + ), + findings: findings_per_body + .then(|| Finding { + r#type: "test.stream".into(), + label: "Stream finding".into(), + count: 1, + confidence: "high".into(), + severity: "informational".into(), + }) + .into_iter() + .collect(), + ..Default::default() + }, + )), + } + } + Some(http_request_event::Event::Trailers(_)) => HttpRequestEventResult { + result: Some(http_request_event_result::Result::TrailersResult( + HttpRequestTrailersResult::default(), + )), + }, + Some(http_request_event::Event::SessionEnd(_)) | None => break, + }; + if sender.send(Ok(result)).await.is_err() { + break; + } + } + }); + Ok(Box::pin(ReceiverStream::new(receiver))) + } + } + + struct CancellationRecordingService { + terminal: std::sync::Mutex< + Option>, + >, + } + + struct BodyDenialService { + manifest_name: String, + deny: bool, + session_ends: tokio::sync::mpsc::UnboundedSender<( + String, + openshell_core::proto::MiddlewareSessionEndReason, + )>, + } + + #[tonic::async_trait] + impl InProcessMiddleware for BodyDenialService { + async fn describe(&self) -> MiddlewareManifest { + MiddlewareManifest { + name: self.manifest_name.clone(), + service_version: "test".into(), + bindings: vec![MiddlewareBinding { + operation: SupervisorMiddlewareOperation::HttpRequest as i32, + phase: SupervisorMiddlewarePhase::PreCredentials as i32, + max_payload_bytes: MAX_HTTP_REQUEST_STREAM_UNIT_BYTES as u64, + request_timeout: None, + }], + expected_audience: String::new(), + } + } + + async fn validate_config( + &self, + _middleware_name: &str, + _config: &prost_types::Struct, + ) -> Result<()> { + Ok(()) + } + + async fn open_http_request_pre_credentials( + &self, + mut requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { + use openshell_core::proto::{ + HttpRequestBlock, HttpRequestBodyMode, HttpRequestBodyPassThrough, + HttpRequestBodyResult, HttpRequestEventResult, HttpRequestPreflightInspect, + HttpRequestPreflightResult, http_request_body_result, http_request_event, + http_request_event_result, http_request_preflight_result, + }; + + let name = self.manifest_name.clone(); + let deny = self.deny; + let session_ends = self.session_ends.clone(); + let (sender, receiver) = tokio::sync::mpsc::channel(4); + tokio::spawn(async move { + while let Some(event) = requests.recv().await { + let result = match event.event { + Some(http_request_event::Event::Preflight(_)) => HttpRequestEventResult { + result: Some(http_request_event_result::Result::PreflightResult( + HttpRequestPreflightResult { + action: Some(http_request_preflight_result::Action::Inspect( + HttpRequestPreflightInspect { + body_mode: HttpRequestBodyMode::StreamBytes as i32, + header_mutations: Vec::new(), + }, + )), + ..Default::default() + }, + )), + }, + Some(http_request_event::Event::Body(body)) => HttpRequestEventResult { + result: Some(http_request_event_result::Result::BodyResult( + HttpRequestBodyResult { + sequence: body.sequence, + action: Some(if deny { + http_request_body_result::Action::BlockRequest( + HttpRequestBlock {}, + ) + } else { + http_request_body_result::Action::PassThrough( + HttpRequestBodyPassThrough {}, + ) + }), + reason_code: if deny { + "body_denied".into() + } else { + String::new() + }, + findings: deny + .then(|| Finding { + r#type: "test.request-body".into(), + label: "Request body denied".into(), + count: 1, + confidence: "high".into(), + severity: "medium".into(), + }) + .into_iter() + .collect(), + metadata: deny + .then(|| ("rule".into(), "deny-body".into())) + .into_iter() + .collect(), + ..Default::default() + }, + )), + }, + Some(http_request_event::Event::SessionEnd(end)) => { + if let Ok(reason) = + openshell_core::proto::MiddlewareSessionEndReason::try_from( + end.reason, + ) + { + let _ = session_ends.send((name.clone(), reason)); + } + break; + } + Some(http_request_event::Event::Trailers(_)) | None => break, + }; + if sender.send(Ok(result)).await.is_err() { + break; + } + } + }); + Ok(Box::pin(ReceiverStream::new(receiver))) + } + } + + #[tonic::async_trait] + impl InProcessMiddleware for CancellationRecordingService { + async fn describe(&self) -> MiddlewareManifest { + MiddlewareManifest { + name: "test/cancellation-recorder".into(), + service_version: "test".into(), + bindings: vec![MiddlewareBinding { + operation: SupervisorMiddlewareOperation::HttpRequest as i32, + phase: SupervisorMiddlewarePhase::PreCredentials as i32, + max_payload_bytes: MAX_HTTP_REQUEST_STREAM_UNIT_BYTES as u64, + request_timeout: None, + }], + expected_audience: String::new(), + } + } + + async fn validate_config( + &self, + _middleware_name: &str, + _config: &prost_types::Struct, + ) -> Result<()> { + Ok(()) + } + + async fn open_http_request_pre_credentials( + &self, + mut requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { + use openshell_core::proto::{ + HttpRequestBodyMode, HttpRequestEventResult, HttpRequestPreflightInspect, + HttpRequestPreflightResult, http_request_event, http_request_event_result, + http_request_preflight_result, + }; + + let terminal = self.terminal.lock().expect("terminal sender lock").take(); + let (sender, receiver) = tokio::sync::mpsc::channel(4); + tokio::spawn(async move { + let Some(openshell_core::proto::HttpRequestEvent { + event: Some(http_request_event::Event::Preflight(_)), + }) = requests.recv().await + else { + return; + }; + if sender + .send(Ok(HttpRequestEventResult { + result: Some(http_request_event_result::Result::PreflightResult( + HttpRequestPreflightResult { + action: Some(http_request_preflight_result::Action::Inspect( + HttpRequestPreflightInspect { + body_mode: HttpRequestBodyMode::StreamBytes as i32, + header_mutations: Vec::new(), + }, + )), + ..Default::default() + }, + )), + })) + .await + .is_err() + { + return; + } + while let Some(event) = requests.recv().await { + if let Some(http_request_event::Event::SessionEnd(end)) = event.event { + if let (Some(terminal), Ok(reason)) = ( + terminal, + openshell_core::proto::MiddlewareSessionEndReason::try_from(end.reason), + ) { + let _ = terminal.send(reason); + } + break; + } + } + }); + Ok(Box::pin(ReceiverStream::new(receiver))) + } + } + + #[tonic::async_trait] + impl InProcessMiddleware for OwnedStreamService { + async fn describe(&self) -> MiddlewareManifest { + MiddlewareManifest { + name: "test/owned-stream".into(), + service_version: "test".into(), + bindings: vec![MiddlewareBinding { + operation: SupervisorMiddlewareOperation::HttpRequest as i32, + phase: SupervisorMiddlewarePhase::PreCredentials as i32, + max_payload_bytes: MAX_HTTP_REQUEST_STREAM_UNIT_BYTES as u64, + request_timeout: None, + }], + expected_audience: String::new(), + } + } + + async fn validate_config( + &self, + _middleware_name: &str, + _config: &prost_types::Struct, + ) -> Result<()> { + Ok(()) + } + + async fn open_http_request_pre_credentials( + &self, + mut requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { + use openshell_core::proto::{ + HttpRequestBodyFinalize, HttpRequestBodyMode, HttpRequestBodyOutput, + HttpRequestBodyResult, HttpRequestBodyTakeOwnership, HttpRequestEventResult, + HttpRequestPreflightInspect, HttpRequestPreflightResult, HttpRequestTrailersResult, + http_request_body_result, http_request_event, http_request_event_result, + http_request_preflight_result, + }; + + let invalid_finalization = self.invalid_finalization; + let (sender, receiver) = tokio::sync::mpsc::channel(4); + tokio::spawn(async move { + while let Some(event) = requests.recv().await { + let result = match event.event { + Some(http_request_event::Event::Preflight(_)) => HttpRequestEventResult { + result: Some(http_request_event_result::Result::PreflightResult( + HttpRequestPreflightResult { + action: Some(http_request_preflight_result::Action::Inspect( + HttpRequestPreflightInspect { + body_mode: HttpRequestBodyMode::OwnedStreamBytes as i32, + header_mutations: Vec::new(), + }, + )), + ..Default::default() + }, + )), + }, + Some(http_request_event::Event::Body(body)) => { + let sequence = body.sequence; + let end_of_stream = body.end_of_stream; + let result = HttpRequestEventResult { + result: Some(http_request_event_result::Result::BodyResult( + HttpRequestBodyResult { + sequence, + action: Some( + http_request_body_result::Action::TakeOwnership( + HttpRequestBodyTakeOwnership {}, + ), + ), + ..Default::default() + }, + )), + }; + if sender.send(Ok(result)).await.is_err() { + break; + } + if end_of_stream { + if sender + .send(Ok(HttpRequestEventResult { + result: Some( + http_request_event_result::Result::BodyOutput( + HttpRequestBodyOutput { + sequence: 1, + data: b"signed".to_vec(), + }, + ), + ), + })) + .await + .is_err() + { + break; + } + if sender + .send(Ok(HttpRequestEventResult { + result: Some( + http_request_event_result::Result::BodyFinalize( + HttpRequestBodyFinalize { + through_input_sequence: if invalid_finalization + { + sequence + 1 + } else { + sequence + }, + through_output_sequence: 1, + reason_code: "owned_complete".into(), + findings: vec![Finding { + r#type: "test.owned".into(), + label: "Owned transformation complete" + .into(), + count: 1, + confidence: "high".into(), + severity: "informational".into(), + }], + metadata: HashMap::from([( + "mode".into(), + "owned".into(), + )]), + ..Default::default() + }, + ), + ), + })) + .await + .is_err() + { + break; + } + } + continue; + } + Some(http_request_event::Event::Trailers(_)) => HttpRequestEventResult { + result: Some(http_request_event_result::Result::TrailersResult( + HttpRequestTrailersResult::default(), + )), + }, + Some(http_request_event::Event::SessionEnd(_)) | None => break, + }; + if sender.send(Ok(result)).await.is_err() { + break; + } + } + }); + Ok(Box::pin(ReceiverStream::new(receiver))) + } + } + + fn owned_entry(on_error: OnError) -> ChainEntry { + ChainEntry { + name: "owned".into(), + implementation: "test/owned-stream".into(), + order: 0, + config: prost_types::Struct::default(), + on_error, + } + } + + #[tokio::test] + async fn owned_request_stream_replaces_a_body_larger_than_the_former_unary_limit() { + let runner = ChainRunner::new(Arc::new(OwnedStreamService { + invalid_finalization: false, + })); + let body = "x".repeat(4 * 1024 * 1024 + 17); + let mut preflight = runner + .preflight_http_request( + &[owned_entry(OnError::FailClosed)], + HttpRequestPreflightInput { + context: RequestContext::default(), + target: HttpRequestTarget::default(), + declared_body_length: Some(body.len() as u64), + headers: Vec::new(), + connection_nominated_headers: Vec::new(), + }, + ) + .await + .expect("owned request preflight"); + let mut session = preflight.session.take().expect("owned request session"); + for chunk in body.as_bytes().chunks(session.stream_unit_limit()) { + assert!(session.push_body(chunk.to_vec()).await.unwrap().is_empty()); + } + let finish = session + .finish(Vec::new()) + .await + .expect("owned finalization"); + assert_eq!(finish.body_units.concat(), b"signed"); + assert!(finish.body_transformed); + assert_eq!(finish.findings[0].middleware, "owned"); + assert_eq!(finish.findings[0].finding.r#type, "test.owned"); + assert_eq!(finish.metadata["owned"]["mode"], "owned"); + } + + #[tokio::test] + async fn owned_request_stream_requires_fail_closed_and_valid_finalization() { + let fail_open_runner = ChainRunner::new(Arc::new(OwnedStreamService { + invalid_finalization: false, + })); + let fail_open = fail_open_runner + .evaluate(&[owned_entry(OnError::FailOpen)], input("original")) + .await + .expect("fail-open evaluation"); + assert!(fail_open.allowed); + assert_eq!(fail_open.body, b"original"); + assert!(fail_open.applied[0].failed); + + let buffered_fail_closed = ChainRunner::new(Arc::new(OwnedStreamService { + invalid_finalization: false, + })) + .evaluate(&[owned_entry(OnError::FailClosed)], input("original")) + .await + .expect("buffered compatibility evaluation"); + assert!(!buffered_fail_closed.allowed); + assert_eq!( + buffered_fail_closed.reason, + "middleware_failed: request_body_mode_not_permitted" + ); + + let invalid_runner = ChainRunner::new(Arc::new(OwnedStreamService { + invalid_finalization: true, + })); + let mut preflight = invalid_runner + .preflight_http_request( + &[owned_entry(OnError::FailClosed)], + HttpRequestPreflightInput { + context: RequestContext::default(), + target: HttpRequestTarget::default(), + declared_body_length: Some(8), + headers: Vec::new(), + connection_nominated_headers: Vec::new(), + }, + ) + .await + .expect("invalid finalization preflight"); + let mut session = preflight.session.take().expect("owned request session"); + assert!( + session + .push_body(b"original".to_vec()) + .await + .unwrap() + .is_empty() + ); + let invalid = session.finish(Vec::new()).await.unwrap_err(); + assert_eq!( + invalid.reason, + "middleware_failed: invalid_owned_finalization" + ); + assert!(invalid.denial.is_none()); + } + + #[tokio::test] + async fn request_stream_stages_receive_one_empty_final_unit_only() { + let recorder = Arc::new(StreamSequenceRecorder::default()); + let runner = ChainRunner::new(Arc::new(StreamSequenceService { + recorder: Arc::clone(&recorder), + findings_per_body: false, + })); + let entries = [ + ChainEntry { + name: "first".into(), + implementation: "test/stream-sequence".into(), + order: 1, + config: prost_types::Struct::default(), + on_error: OnError::FailClosed, + }, + ChainEntry { + name: "second".into(), + implementation: "test/stream-sequence".into(), + order: 2, + config: prost_types::Struct::default(), + on_error: OnError::FailClosed, + }, + ]; + let preflight = runner + .preflight_http_request( + &entries, + HttpRequestPreflightInput { + context: RequestContext::default(), + target: HttpRequestTarget::default(), + declared_body_length: Some(3), + headers: Vec::new(), + connection_nominated_headers: Vec::new(), + }, + ) + .await + .expect("request preflight"); + let mut session = preflight.session.expect("streaming request session"); + assert_eq!( + session.push_body(b"abc".to_vec()).await.unwrap(), + vec![b"abc".to_vec()] + ); + let finish = session.finish(Vec::new()).await.expect("request finish"); + assert!(finish.body_units.is_empty()); + + let streams = recorder.streams.lock().expect("stream recorder lock"); + assert_eq!( + streams.as_slice(), + [vec![(3, false), (0, true)], vec![(3, false), (0, true)]] + ); + } + + #[tokio::test] + async fn request_stream_bounds_findings_across_body_results() { + let runner = ChainRunner::new(Arc::new(StreamSequenceService { + recorder: Arc::new(StreamSequenceRecorder::default()), + findings_per_body: true, + })); + let entry = ChainEntry { + name: "finding-stream".into(), + implementation: "test/stream-sequence".into(), + order: 0, + config: prost_types::Struct::default(), + on_error: OnError::FailClosed, + }; + let preflight = runner + .preflight_http_request( + &[entry], + HttpRequestPreflightInput { + context: RequestContext::default(), + target: HttpRequestTarget::default(), + declared_body_length: Some(33), + headers: Vec::new(), + connection_nominated_headers: Vec::new(), + }, + ) + .await + .expect("request preflight"); + let mut session = preflight.session.expect("streaming request session"); + for _ in 0..MAX_MIDDLEWARE_FINDINGS_PER_STAGE { + assert_eq!( + session.push_body(vec![b'x']).await.unwrap(), + vec![vec![b'x']] + ); + } + let failure = session + .push_body(vec![b'x']) + .await + .expect_err("the aggregate finding limit must fail closed"); + + assert_eq!( + failure.reason, + "middleware_failed: request_findings_over_capacity" + ); + assert_eq!( + failure.diagnostics.findings.len(), + MAX_MIDDLEWARE_FINDINGS_PER_STAGE + ); + } + + #[tokio::test] + async fn request_stream_bounds_retained_invocation_records() { + let runner = ChainRunner::new(Arc::new(StreamSequenceService { + recorder: Arc::new(StreamSequenceRecorder::default()), + findings_per_body: false, + })); + let entry = ChainEntry { + name: "long-stream".into(), + implementation: "test/stream-sequence".into(), + order: 0, + config: prost_types::Struct::default(), + on_error: OnError::FailClosed, + }; + let preflight = runner + .preflight_http_request( + &[entry], + HttpRequestPreflightInput { + context: RequestContext::default(), + target: HttpRequestTarget::default(), + declared_body_length: Some(1025), + headers: Vec::new(), + connection_nominated_headers: Vec::new(), + }, + ) + .await + .expect("request preflight"); + let mut session = preflight.session.expect("streaming request session"); + for _ in 0..1025 { + assert_eq!( + session.push_body(vec![b'x']).await.unwrap(), + vec![vec![b'x']] + ); + } + let finish = session.finish(Vec::new()).await.expect("request finish"); + + assert_eq!(finish.invocations.len(), 1024); + assert!(!finish.invocations[0].failed); + assert!(finish.invocations[0].input_size > 1); + } + + #[tokio::test] + async fn dropped_request_session_sends_best_effort_cancellation() { + let (terminal_tx, terminal_rx) = tokio::sync::oneshot::channel(); + let runner = ChainRunner::new(Arc::new(CancellationRecordingService { + terminal: std::sync::Mutex::new(Some(terminal_tx)), + })); + let entry = ChainEntry { + name: "recorder".into(), + implementation: "test/cancellation-recorder".into(), + order: 0, + config: prost_types::Struct::default(), + on_error: OnError::FailClosed, + }; + let preflight = runner + .preflight_http_request( + &[entry], + HttpRequestPreflightInput { + context: RequestContext::default(), + target: HttpRequestTarget::default(), + declared_body_length: Some(1), + headers: Vec::new(), + connection_nominated_headers: Vec::new(), + }, + ) + .await + .expect("request preflight"); + + drop(preflight.session.expect("streaming request session")); + let reason = tokio::time::timeout(Duration::from_secs(1), terminal_rx) + .await + .expect("cancellation delivery timeout") + .expect("cancellation sender dropped"); + assert_eq!( + reason, + openshell_core::proto::MiddlewareSessionEndReason::Cancellation + ); + } + + #[tokio::test] + async fn body_denial_preserves_diagnostics_and_ends_every_open_stage() { + use openshell_core::proto::MiddlewareSessionEndReason; + + let (session_ends_tx, mut session_ends_rx) = tokio::sync::mpsc::unbounded_channel(); + let endpoints: Vec> = vec![ + Arc::new(BodyDenialService { + manifest_name: "test/body-denier".into(), + deny: true, + session_ends: session_ends_tx.clone(), + }), + Arc::new(BodyDenialService { + manifest_name: "test/body-observer".into(), + deny: false, + session_ends: session_ends_tx, + }), + ]; + let runner = ChainRunner::from_registry( + MiddlewareRegistry::connect_services(endpoints, Vec::new()) + .await + .expect("connect body middleware services"), + ); + let entries = [ + ChainEntry { + name: "denier".into(), + implementation: "test/body-denier".into(), + order: 0, + config: prost_types::Struct::default(), + on_error: OnError::FailClosed, + }, + ChainEntry { + name: "observer".into(), + implementation: "test/body-observer".into(), + order: 1, + config: prost_types::Struct::default(), + on_error: OnError::FailClosed, + }, + ]; + let preflight = runner + .preflight_http_request( + &entries, + HttpRequestPreflightInput { + context: RequestContext::default(), + target: HttpRequestTarget::default(), + declared_body_length: Some(7), + headers: Vec::new(), + connection_nominated_headers: Vec::new(), + }, + ) + .await + .expect("request preflight"); + let mut session = preflight.session.expect("streaming request session"); + let failure = session + .push_body(b"blocked".to_vec()) + .await + .expect_err("body denial must stop the request"); + + assert_eq!(failure.reason, "middleware_denied:denier:body_denied"); + assert_eq!( + failure + .denial + .as_ref() + .map(|denial| denial.config_name.as_str()), + Some("denier") + ); + assert_eq!(failure.diagnostics.invocations.len(), 1); + assert_eq!( + failure.diagnostics.invocations[0].outcome, + HttpRequestInvocationOutcome::BlockRequest + ); + assert_eq!(failure.diagnostics.findings.len(), 1); + assert_eq!(failure.diagnostics.findings[0].middleware, "denier"); + assert_eq!( + failure.diagnostics.findings[0].finding.r#type, + "test.request-body" + ); + assert_eq!(failure.diagnostics.metadata["denier"]["rule"], "deny-body"); + + let mut ended = BTreeMap::new(); + for _ in 0..2 { + let (name, reason) = + tokio::time::timeout(Duration::from_secs(1), session_ends_rx.recv()) + .await + .expect("session end delivery timeout") + .expect("session end sender dropped"); + ended.insert(name, reason); + } + assert_eq!( + ended.get("test/body-denier"), + Some(&MiddlewareSessionEndReason::MiddlewareDenial) + ); + assert_eq!( + ended.get("test/body-observer"), + Some(&MiddlewareSessionEndReason::MiddlewareDenial) + ); + assert!(session_ends_rx.try_recv().is_err()); + } + #[tokio::test] async fn in_process_evaluation_remains_interruptible_by_stage_timeout() { let runner = ChainRunner::new(Arc::new(PendingInProcessService)); @@ -2629,14 +3003,8 @@ mod tests { #[tokio::test] async fn injected_services_cannot_duplicate_middleware_names() { - let first: Arc = Arc::new(BorrowedRecordingService { - manifest_name: "openshell/test".into(), - received: std::sync::Mutex::new(Vec::new()), - }); - let second: Arc = Arc::new(BorrowedRecordingService { - manifest_name: "openshell/test".into(), - received: std::sync::Mutex::new(Vec::new()), - }); + let first: Arc = Arc::new(PendingInProcessService); + let second: Arc = Arc::new(PendingInProcessService); let error = MiddlewareRegistry::connect_services(vec![first, second], Vec::new()) .await @@ -2651,24 +3019,15 @@ mod tests { /// A mock middleware that returns a fixed, caller-supplied result for every /// evaluation. Used to exercise chain behavior the built-in cannot produce /// (explicit deny, metadata, findings, unsafe header mutations). + #[derive(Clone)] struct ScriptedService { manifest_name: String, max_body_bytes: u64, - result: openshell_core::proto::HttpRequestResult, + result: TestRequestResult, } #[tonic::async_trait] - impl SupervisorMiddleware for ScriptedService { - type EvaluateWebSocketSessionStream = WebSocketResponseStream; - - async fn evaluate_web_socket_session( - &self, - _request: Request>, - ) -> std::result::Result, tonic::Status> - { - Err(tonic::Status::unimplemented("HTTP-only test middleware")) - } - + impl SupervisorMiddlewareEndpoint for ScriptedService { async fn describe( &self, _request: Request<()>, @@ -2696,14 +3055,69 @@ mod tests { })) } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - _request: Request, - ) -> std::result::Result< - tonic::Response, - tonic::Status, - > { - Ok(tonic::Response::new(self.result.clone())) + requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { + let result = self.result.clone(); + Ok(open_test_request_stream(requests, move |_| { + constant_request_plan(result) + })) + } + } + + #[tonic::async_trait] + impl SupervisorMiddleware for ScriptedService { + type EvaluateWebSocketSessionStream = WebSocketResponseStream; + + async fn describe( + &self, + request: Request<()>, + ) -> std::result::Result, tonic::Status> { + SupervisorMiddlewareEndpoint::describe(self, request).await + } + + async fn validate_config( + &self, + request: Request, + ) -> std::result::Result, tonic::Status> { + SupervisorMiddlewareEndpoint::validate_config(self, request).await + } + + async fn evaluate_web_socket_session( + &self, + _request: Request>, + ) -> std::result::Result, tonic::Status> + { + Err(tonic::Status::unimplemented("HTTP-only test middleware")) + } + } + + #[tonic::async_trait] + impl HttpRequestPreCredentials for ScriptedService { + type EvaluateStream = HttpRequestResultStream; + + async fn evaluate( + &self, + request: Request>, + ) -> std::result::Result, tonic::Status> { + let (sender, receiver) = tokio::sync::mpsc::channel(4); + let mut requests = request.into_inner(); + tokio::spawn(async move { + while let Some(request) = requests.next().await { + let Ok(request) = request else { + break; + }; + if sender.send(request).await.is_err() { + break; + } + } + }); + let result = self.result.clone(); + Ok(tonic::Response::new(open_test_request_stream( + receiver, + move |_| constant_request_plan(result), + ))) } } @@ -2713,17 +3127,7 @@ mod tests { } #[tonic::async_trait] - impl SupervisorMiddleware for SlowService { - type EvaluateWebSocketSessionStream = WebSocketResponseStream; - - async fn evaluate_web_socket_session( - &self, - _request: Request>, - ) -> std::result::Result, tonic::Status> - { - Err(tonic::Status::unimplemented("HTTP-only test middleware")) - } - + impl SupervisorMiddlewareEndpoint for SlowService { async fn describe( &self, _request: Request<()>, @@ -2752,15 +3156,14 @@ mod tests { })) } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - _request: Request, - ) -> std::result::Result< - tonic::Response, - tonic::Status, - > { + requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { tokio::time::sleep(self.delay).await; - Ok(tonic::Response::new(allow_result())) + Ok(open_test_request_stream(requests, |_| { + constant_request_plan(allow_result()) + })) } } @@ -2772,17 +3175,7 @@ mod tests { } #[tonic::async_trait] - impl SupervisorMiddleware for TwoStageService { - type EvaluateWebSocketSessionStream = WebSocketResponseStream; - - async fn evaluate_web_socket_session( - &self, - _request: Request>, - ) -> std::result::Result, tonic::Status> - { - Err(tonic::Status::unimplemented("HTTP-only test middleware")) - } - + impl SupervisorMiddlewareEndpoint for TwoStageService { async fn describe( &self, _request: Request<()>, @@ -2810,30 +3203,34 @@ mod tests { })) } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - request: Request, - ) -> std::result::Result< - tonic::Response, - tonic::Status, - > { - let evaluation = request.into_inner(); - let mut result = allow_result(); - if evaluation.config.as_ref().is_some_and(|config| { - config.fields.get("transform").is_some_and(|value| { - matches!( - value.kind.as_ref(), - Some(prost_types::value::Kind::BoolValue(true)) - ) - }) - }) { - result.body = b"TRANSFORMED".to_vec(); - result.has_body = true; - } else { - self.second_ran - .store(true, std::sync::atomic::Ordering::SeqCst); - } - Ok(tonic::Response::new(result)) + requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { + let second_ran = Arc::clone(&self.second_ran); + Ok(open_test_request_stream(requests, move |preflight| { + let transform = preflight.config.as_ref().is_some_and(|config| { + config.fields.get("transform").is_some_and(|value| { + matches!( + value.kind.as_ref(), + Some(prost_types::value::Kind::BoolValue(true)) + ) + }) + }); + TestRequestPlan { + preflight: allow_result(), + body: Some(Box::new(move |_body, _evaluation| { + let mut result = allow_result(); + if transform { + result.body = b"TRANSFORMED".to_vec(); + result.has_body = true; + } else { + second_ran.store(true, std::sync::atomic::Ordering::SeqCst); + } + result + })), + } + })) } } @@ -2843,7 +3240,7 @@ mod tests { // must stop there: the second stage never runs, so it never sees a // payload the policy would reject. let second_ran = Arc::new(std::sync::atomic::AtomicBool::new(false)); - let service: Arc = Arc::new(TwoStageService { + let service: Arc = Arc::new(TwoStageService { second_ran: Arc::clone(&second_ran), }); let runner = ChainRunner::new_protobuf_for_tests(service); @@ -2905,7 +3302,7 @@ mod tests { // A validator that accepts every body lets both stages run; the second // stage sees the first stage's output. let second_ran = Arc::new(std::sync::atomic::AtomicBool::new(false)); - let service: Arc = Arc::new(TwoStageService { + let service: Arc = Arc::new(TwoStageService { second_ran: Arc::clone(&second_ran), }); let runner = ChainRunner::new_protobuf_for_tests(service); @@ -2957,7 +3354,7 @@ mod tests { #[tokio::test] async fn per_stage_validator_error_becomes_structured_denial() { let second_ran = Arc::new(std::sync::atomic::AtomicBool::new(false)); - let service: Arc = Arc::new(TwoStageService { + let service: Arc = Arc::new(TwoStageService { second_ran: Arc::clone(&second_ran), }); let runner = ChainRunner::new_protobuf_for_tests(service); @@ -3013,7 +3410,7 @@ mod tests { assert!(!second_ran.load(std::sync::atomic::Ordering::SeqCst)); } - fn scripted_service(result: openshell_core::proto::HttpRequestResult) -> ScriptedService { + fn scripted_service(result: TestRequestResult) -> ScriptedService { ScriptedService { manifest_name: BUILTIN_REGEX.into(), max_body_bytes: 256 * 1024, @@ -3021,8 +3418,8 @@ mod tests { } } - fn allow_result() -> openshell_core::proto::HttpRequestResult { - openshell_core::proto::HttpRequestResult { + fn allow_result() -> TestRequestResult { + TestRequestResult { decision: Decision::Allow as i32, reason: String::new(), body: Vec::new(), @@ -3037,22 +3434,12 @@ mod tests { /// A middleware that records every evaluation it receives and allows the /// request, for asserting what the supervisor actually sends to services. struct RecordingService { - validated: std::sync::Mutex>, - received: std::sync::Mutex>, + validated: Arc>>, + received: Arc>>, } #[tonic::async_trait] - impl SupervisorMiddleware for RecordingService { - type EvaluateWebSocketSessionStream = WebSocketResponseStream; - - async fn evaluate_web_socket_session( - &self, - _request: Request>, - ) -> std::result::Result, tonic::Status> - { - Err(tonic::Status::unimplemented("HTTP-only test middleware")) - } - + impl SupervisorMiddlewareEndpoint for RecordingService { async fn describe( &self, _request: Request<()>, @@ -3084,18 +3471,20 @@ mod tests { })) } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - request: Request, - ) -> std::result::Result< - tonic::Response, - tonic::Status, - > { - self.received - .lock() - .expect("recording lock") - .push(request.into_inner()); - Ok(tonic::Response::new(allow_result())) + requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { + let received = Arc::clone(&self.received); + Ok(open_test_request_stream(requests, move |_| { + TestRequestPlan { + preflight: allow_result(), + body: Some(Box::new(move |_body, evaluation| { + received.lock().expect("recording lock").push(evaluation); + allow_result() + })), + } + })) } } @@ -3103,11 +3492,11 @@ mod tests { /// state produced by all preceding stages. struct HeaderChainService { second_action: ExistingHeaderAction, - received: std::sync::Mutex>, + received: Arc>>, } struct InProcessHeaderChainService { - received: std::sync::Mutex>>, + received: Arc>>>, } #[tonic::async_trait] @@ -3134,40 +3523,36 @@ mod tests { Ok(()) } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - request: HttpRequestView<'_>, - ) -> Result { - let invocation = { - let mut received = self.received.lock().expect("in-process header chain lock"); - let invocation = received.len(); - received.push(request.headers().to_vec()); - invocation - }; - let mut result = allow_result(); - if invocation == 0 { - result.header_mutations.push(write_header( - "cache-control", - "no-store", - ExistingHeaderAction::Overwrite, - )); - } - Ok(result) + requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { + let received = Arc::clone(&self.received); + Ok(open_test_request_stream(requests, move |preflight| { + let invocation = { + let mut received = received.lock().expect("in-process header chain lock"); + let invocation = received.len(); + received.push(preflight.headers); + invocation + }; + let mut result = allow_result(); + if invocation == 0 { + result.header_mutations.push(write_header( + "cache-control", + "no-store", + ExistingHeaderAction::Overwrite, + )); + } + TestRequestPlan { + preflight: result, + body: None, + } + })) } } #[tonic::async_trait] - impl SupervisorMiddleware for HeaderChainService { - type EvaluateWebSocketSessionStream = WebSocketResponseStream; - - async fn evaluate_web_socket_session( - &self, - _request: Request>, - ) -> std::result::Result, tonic::Status> - { - Err(tonic::Status::unimplemented("HTTP-only test middleware")) - } - + impl SupervisorMiddlewareEndpoint for HeaderChainService { async fn describe( &self, _request: Request<()>, @@ -3195,35 +3580,47 @@ mod tests { })) } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - request: Request, - ) -> std::result::Result< - tonic::Response, - tonic::Status, - > { - let evaluation = request.into_inner(); - let invocation = { - let mut received = self.received.lock().expect("header chain lock"); - let invocation = received.len(); - received.push(evaluation); - invocation - }; - let mut result = allow_result(); - if invocation == 0 { - result.header_mutations.push(write_header( - "cache-control", - "first", - ExistingHeaderAction::Overwrite, - )); - } else if invocation == 1 { - result.header_mutations.push(write_header( - "cache-control", - "second", - self.second_action, - )); - } - Ok(tonic::Response::new(result)) + requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { + let received = Arc::clone(&self.received); + let second_action = self.second_action; + Ok(open_test_request_stream(requests, move |preflight| { + let evaluation = TestRequestEvaluation { + phase: SupervisorMiddlewarePhase::PreCredentials as i32, + context: preflight.context, + config: preflight.config, + target: preflight.target, + headers: preflight.headers, + body: Vec::new(), + middleware_name: preflight.middleware_name, + }; + let invocation = { + let mut received = received.lock().expect("header chain lock"); + let invocation = received.len(); + received.push(evaluation); + invocation + }; + let mut result = allow_result(); + if invocation == 0 { + result.header_mutations.push(write_header( + "cache-control", + "first", + ExistingHeaderAction::Overwrite, + )); + } else if invocation == 1 { + result.header_mutations.push(write_header( + "cache-control", + "second", + second_action, + )); + } + TestRequestPlan { + preflight: result, + body: None, + } + })) } } @@ -3236,7 +3633,7 @@ mod tests { ] { let service = Arc::new(HeaderChainService { second_action: action, - received: std::sync::Mutex::new(Vec::new()), + received: Arc::new(std::sync::Mutex::new(Vec::new())), }); let runner = ChainRunner::new_protobuf_for_tests(service.clone()); let entries = [ @@ -3282,7 +3679,7 @@ mod tests { #[tokio::test] async fn in_process_request_middleware_writes_end_to_end_header_without_namespace() { let service = Arc::new(InProcessHeaderChainService { - received: std::sync::Mutex::new(Vec::new()), + received: Arc::new(std::sync::Mutex::new(Vec::new())), }); let runner = ChainRunner::new(service.clone()); let entries = [ @@ -3329,10 +3726,10 @@ mod tests { // inspection differential. The service must see each entry in wire // order. let service = Arc::new(RecordingService { - validated: std::sync::Mutex::new(Vec::new()), - received: std::sync::Mutex::new(Vec::new()), + validated: Arc::new(std::sync::Mutex::new(Vec::new())), + received: Arc::new(std::sync::Mutex::new(Vec::new())), }); - let recorder: Arc = service.clone(); + let recorder: Arc = service.clone(); let runner = ChainRunner::new_protobuf_for_tests(recorder); let validation_config = prost_types::Struct { fields: std::iter::once(( @@ -3386,7 +3783,8 @@ mod tests { let received = service.received.lock().expect("recorded evaluations"); assert_eq!(received.len(), 1); - assert_eq!(outcome.body.as_ptr().addr(), original_body); + assert_eq!(outcome.body, b"payload"); + assert_ne!(outcome.body.as_ptr().addr(), original_body); assert_ne!(received[0].body.as_ptr().addr(), original_body); assert_eq!(received[0].body, b"payload"); assert_eq!( @@ -3433,7 +3831,7 @@ mod tests { } async fn registry_with_external( - service: Arc, + service: Arc, registration: SupervisorMiddlewareService, ) -> MiddlewareRegistry { let builtin_service = services() @@ -3474,7 +3872,7 @@ mod tests { Arc::new(MiddlewareServiceState { attachment_name: Some(registration_name.clone()), service: MiddlewareDispatch::Grpc(remote::GrpcMiddlewareService::from_service( - Arc::new(GeneratedMiddlewareEndpoint { service }), + service, )), manifest: manifest_cell, diagnostic_policy: MiddlewareDiagnosticPolicy::Normalize, @@ -3656,7 +4054,7 @@ mod tests { assert!(!outcome.allowed); assert_eq!( outcome.reason, - "middleware_failed: request_body_over_capacity" + "middleware_failed: request_body_mode_not_permitted" ); assert_eq!(outcome.applied.len(), 2); assert!( @@ -3759,6 +4157,30 @@ mod tests { .expect("HTTP response pre-return binding is supported"); } + #[test] + fn external_manifest_rejects_post_credentials_binding() { + let registration = external_registration(4096); + let manifest = MiddlewareManifest { + name: "example/credential-visible".into(), + service_version: "test".into(), + bindings: vec![MiddlewareBinding { + operation: SupervisorMiddlewareOperation::HttpRequest as i32, + phase: SupervisorMiddlewarePhase::PostCredentials as i32, + max_payload_bytes: 4096, + request_timeout: None, + }], + expected_audience: String::new(), + }; + + let error = validate_external_manifest(®istration, &manifest, 4096, true) + .expect_err("external middleware must never observe resolved credentials"); + assert!( + error + .to_string() + .contains("reserved for trusted in-process") + ); + } + #[test] fn manifest_accepts_forward_websocket_binding_and_reserves_return_phase() { let binding = |phase| MiddlewareBinding { @@ -4033,12 +4455,14 @@ mod tests { .expect("bind test middleware"); let address = listener.local_addr().expect("test middleware address"); let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel(); + let service = ScriptedService { + manifest_name: "test/middleware".into(), + max_body_bytes: 4096, + result: allow_result(), + }; let server = tonic::transport::Server::builder() - .add_service(SupervisorMiddlewareServer::new(ScriptedService { - manifest_name: "test/middleware".into(), - max_body_bytes: 4096, - result: allow_result(), - })) + .add_service(SupervisorMiddlewareServer::new(service.clone())) + .add_service(HttpRequestPreCredentialsServer::new(service)) .serve_with_incoming_shutdown(TcpListenerStream::new(listener), async { let _ = shutdown_rx.await; }); @@ -4128,32 +4552,38 @@ mod tests { severity: "medium".into(), }) .collect(); + let service = ScriptedService { + manifest_name: "test/middleware".into(), + max_body_bytes: MAX_MIDDLEWARE_PAYLOAD_BYTES as u64, + result: TestRequestResult { + reason: "r".repeat(MAX_MIDDLEWARE_REASON_BYTES - 128), + reason_code: "r".repeat(MAX_MIDDLEWARE_REASON_CODE_BYTES), + body: vec![b'x'; MAX_MIDDLEWARE_PAYLOAD_BYTES], + has_body: true, + header_mutations: vec![write_header( + "x-openshell-middleware-envelope", + &"h".repeat(headers::MAX_HEADER_MUTATION_BYTES - 128), + ExistingHeaderAction::Append, + )], + findings: response_findings, + metadata: std::iter::once(( + "diagnostic".into(), + "m".repeat(MAX_MIDDLEWARE_METADATA_BYTES - 128), + )) + .collect(), + ..allow_result() + }, + }; let server = tonic::transport::Server::builder() .add_service( - SupervisorMiddlewareServer::new(ScriptedService { - manifest_name: "test/middleware".into(), - max_body_bytes: MAX_MIDDLEWARE_PAYLOAD_BYTES as u64, - result: openshell_core::proto::HttpRequestResult { - reason: "r".repeat(MAX_MIDDLEWARE_REASON_BYTES - 128), - reason_code: "r".repeat(MAX_MIDDLEWARE_REASON_CODE_BYTES), - body: vec![b'x'; MAX_MIDDLEWARE_PAYLOAD_BYTES], - has_body: true, - header_mutations: vec![write_header( - "x-openshell-middleware-envelope", - &"h".repeat(headers::MAX_HEADER_MUTATION_BYTES - 128), - ExistingHeaderAction::Append, - )], - findings: response_findings, - metadata: std::iter::once(( - "diagnostic".into(), - "m".repeat(MAX_MIDDLEWARE_METADATA_BYTES - 128), - )) - .collect(), - ..allow_result() - }, - }) - .max_decoding_message_size(MIDDLEWARE_GRPC_MESSAGE_BYTES) - .max_encoding_message_size(MIDDLEWARE_GRPC_MESSAGE_BYTES), + SupervisorMiddlewareServer::new(service.clone()) + .max_decoding_message_size(MIDDLEWARE_GRPC_MESSAGE_BYTES) + .max_encoding_message_size(MIDDLEWARE_GRPC_MESSAGE_BYTES), + ) + .add_service( + HttpRequestPreCredentialsServer::new(service) + .max_decoding_message_size(MIDDLEWARE_GRPC_MESSAGE_BYTES) + .max_encoding_message_size(MIDDLEWARE_GRPC_MESSAGE_BYTES), ) .serve_with_incoming_shutdown(TcpListenerStream::new(listener), async { let _ = shutdown_rx.await; @@ -4226,7 +4656,7 @@ mod tests { let service = Arc::new(ScriptedService { manifest_name: "test/middleware".into(), max_body_bytes: 4096, - result: openshell_core::proto::HttpRequestResult { + result: TestRequestResult { decision: Decision::Deny as i32, reason: format!("denied body={secret}\nFINDING:FORGED"), reason_code: "content_match".into(), @@ -4277,13 +4707,12 @@ mod tests { #[tokio::test] async fn invalid_reason_code_is_a_middleware_failure() { - let runner = ChainRunner::new_protobuf_for_tests(Arc::new(scripted_service( - openshell_core::proto::HttpRequestResult { + let runner = + ChainRunner::new_protobuf_for_tests(Arc::new(scripted_service(TestRequestResult { decision: Decision::Deny as i32, reason_code: "Secret value!".into(), ..allow_result() - }, - ))); + }))); let outcome = runner .evaluate( &[entry("content-guard", OnError::FailClosed)], @@ -4295,7 +4724,7 @@ mod tests { assert!(!outcome.allowed); assert_eq!( outcome.reason, - "middleware_failed: response_reason_code_invalid" + "middleware_failed: request_reason_code_invalid" ); assert!(outcome.denial.is_none()); assert!(outcome.applied[0].failed); @@ -4308,7 +4737,7 @@ mod tests { let service = Arc::new(ScriptedService { manifest_name: "test/middleware".into(), max_body_bytes: 4096, - result: openshell_core::proto::HttpRequestResult { + result: TestRequestResult { header_mutations: vec![write_header( &format!("x-openshell-middleware-invalid\n{secret}"), "value", @@ -4347,7 +4776,7 @@ mod tests { let service = Arc::new(ScriptedService { manifest_name: "test/middleware".into(), max_body_bytes: 4096, - result: openshell_core::proto::HttpRequestResult { + result: TestRequestResult { header_mutations: vec![write_header( "x-api-key", placeholder, @@ -4409,7 +4838,7 @@ mod tests { let service = Arc::new(ScriptedService { manifest_name: "test/middleware".into(), max_body_bytes: 4096, - result: openshell_core::proto::HttpRequestResult { + result: TestRequestResult { header_mutations: vec![mutation], ..allow_result() }, @@ -4446,7 +4875,7 @@ mod tests { let service = Arc::new(ScriptedService { manifest_name: "test/middleware".into(), max_body_bytes: 4096, - result: openshell_core::proto::HttpRequestResult { + result: TestRequestResult { findings: vec![Finding::default(); MAX_MIDDLEWARE_FINDINGS_PER_STAGE + 1], ..allow_result() }, @@ -4476,7 +4905,7 @@ mod tests { if !allowed { assert_eq!( outcome.reason, - "middleware_failed: response_findings_over_capacity" + "middleware_failed: request_findings_over_capacity" ); } } @@ -4487,7 +4916,7 @@ mod tests { let runner = ChainRunner::new_protobuf_for_tests(Arc::new(ScriptedService { manifest_name: "test/middleware".into(), max_body_bytes: 4096, - result: openshell_core::proto::HttpRequestResult { + result: TestRequestResult { findings: vec![ Finding { r#type: "example.finding".into(), @@ -4534,13 +4963,12 @@ mod tests { #[tokio::test] async fn deny_decision_short_circuits_chain() { - let runner = ChainRunner::new_protobuf_for_tests(Arc::new(scripted_service( - openshell_core::proto::HttpRequestResult { + let runner = + ChainRunner::new_protobuf_for_tests(Arc::new(scripted_service(TestRequestResult { decision: Decision::Deny as i32, reason: "blocked_by_policy".into(), ..allow_result() - }, - ))); + }))); let outcome = runner .evaluate( &[ @@ -4569,8 +4997,8 @@ mod tests { #[tokio::test] async fn deny_decision_ignores_unsafe_mutations_under_fail_open() { - let runner = ChainRunner::new_protobuf_for_tests(Arc::new(scripted_service( - openshell_core::proto::HttpRequestResult { + let runner = + ChainRunner::new_protobuf_for_tests(Arc::new(scripted_service(TestRequestResult { decision: Decision::Deny as i32, reason: "blocked_by_policy".into(), header_mutations: vec![write_header( @@ -4579,8 +5007,7 @@ mod tests { ExistingHeaderAction::Append, )], ..allow_result() - }, - ))); + }))); let outcome = runner .evaluate(&[entry("guard", OnError::FailOpen)], input("hello")) @@ -4600,7 +5027,7 @@ mod tests { let runner = ChainRunner::new_protobuf_for_tests(Arc::new(ScriptedService { manifest_name: BUILTIN_REGEX.into(), max_body_bytes: 4, - result: openshell_core::proto::HttpRequestResult { + result: TestRequestResult { decision: Decision::Deny as i32, reason: "blocked_by_policy".into(), body: b"too large".to_vec(), @@ -4625,8 +5052,8 @@ mod tests { #[tokio::test] async fn metadata_and_findings_are_namespaced_per_config() { - let runner = ChainRunner::new_protobuf_for_tests(Arc::new(scripted_service( - openshell_core::proto::HttpRequestResult { + let runner = + ChainRunner::new_protobuf_for_tests(Arc::new(scripted_service(TestRequestResult { findings: vec![Finding { r#type: "pii.email".into(), label: "email address".into(), @@ -4637,8 +5064,7 @@ mod tests { metadata: std::iter::once(("sensitivity".to_string(), "high".to_string())) .collect(), ..allow_result() - }, - ))); + }))); let outcome = runner .evaluate( &[ @@ -4663,7 +5089,7 @@ mod tests { } fn unsafe_header_service() -> ScriptedService { - scripted_service(openshell_core::proto::HttpRequestResult { + scripted_service(TestRequestResult { header_mutations: vec![ write_header( "x-openshell-middleware-safe", @@ -4721,7 +5147,7 @@ mod tests { let runner = ChainRunner::new_protobuf_for_tests(Arc::new(ScriptedService { manifest_name: BUILTIN_REGEX.into(), max_body_bytes: 4, - result: openshell_core::proto::HttpRequestResult { + result: TestRequestResult { body: b"too large".to_vec(), has_body: true, ..allow_result() @@ -4746,7 +5172,7 @@ mod tests { assert!(!closed_outcome.allowed); assert_eq!( closed_outcome.reason, - "middleware_failed: response_body_over_capacity" + "middleware_failed: body_replacement_over_capacity" ); assert!(closed_outcome.applied[0].failed); } @@ -4777,19 +5203,18 @@ mod tests { assert!(!closed_outcome.allowed); assert_eq!( closed_outcome.reason, - "middleware_failed: request_body_over_capacity" + "middleware_failed: request_body_mode_not_permitted" ); assert!(closed_outcome.applied[0].failed); } #[tokio::test] async fn unspecified_decision_uses_fail_closed() { - let runner = ChainRunner::new_protobuf_for_tests(Arc::new(scripted_service( - openshell_core::proto::HttpRequestResult { + let runner = + ChainRunner::new_protobuf_for_tests(Arc::new(scripted_service(TestRequestResult { decision: Decision::Unspecified as i32, ..allow_result() - }, - ))); + }))); let outcome = runner .evaluate(&[entry("redact", OnError::FailClosed)], input("hello")) @@ -4799,7 +5224,7 @@ mod tests { assert!(!outcome.allowed); assert_eq!( outcome.reason, - "middleware_failed: invalid_response_decision" + "middleware_failed: missing_preflight_action" ); assert!(outcome.applied[0].failed); } @@ -4925,7 +5350,7 @@ mod tests { } } }); - Box::pin(tokio_stream::wrappers::ReceiverStream::new(responses_rx)) + Box::pin(ReceiverStream::new(responses_rx)) } } @@ -4984,18 +5409,6 @@ mod tests { })) } - async fn evaluate_http_request( - &self, - _request: Request, - ) -> std::result::Result< - tonic::Response, - tonic::Status, - > { - Err(tonic::Status::unimplemented( - "WebSocket-only test middleware", - )) - } - async fn evaluate_web_socket_session( &self, request: Request>, @@ -5023,25 +5436,11 @@ mod tests { SupervisorMiddleware::validate_config(self, request).await } - async fn evaluate_http_request( - &self, - request: Request, - ) -> std::result::Result< - tonic::Response, - tonic::Status, - > { - SupervisorMiddleware::evaluate_http_request(self, request).await - } - async fn open_websocket_session( &self, receiver: tokio::sync::mpsc::Receiver, ) -> std::result::Result { - Ok( - self.websocket_stream( - tokio_stream::wrappers::ReceiverStream::new(receiver).map(Ok), - ), - ) + Ok(self.websocket_stream(ReceiverStream::new(receiver).map(Ok))) } } diff --git a/crates/openshell-supervisor-middleware/src/remote.rs b/crates/openshell-supervisor-middleware/src/remote.rs index 80049b69d..80f43ffd6 100644 --- a/crates/openshell-supervisor-middleware/src/remote.rs +++ b/crates/openshell-supervisor-middleware/src/remote.rs @@ -3,14 +3,15 @@ use miette::{IntoDiagnostic, Result, WrapErr}; use openshell_core::middleware::{ - HttpRequestView, HttpResponseResultStream, SupervisorMiddlewareEndpoint, + HttpRequestResultStream, HttpResponseResultStream, SupervisorMiddlewareEndpoint, WebSocketResponseStream, }; +use openshell_core::proto::middleware::v1::http_request_pre_credentials_client::HttpRequestPreCredentialsClient; use openshell_core::proto::middleware::v1::http_response_pre_return_client::HttpResponsePreReturnClient; use openshell_core::proto::middleware::v1::supervisor_middleware_client::SupervisorMiddlewareClient; use openshell_core::proto::{ - HttpRequestEvaluation, HttpRequestResult, HttpResponseEvent, MiddlewareManifest, - ValidateConfigRequest, ValidateConfigResponse, WebSocketSessionEvent, + HttpRequestEvent, HttpResponseEvent, MiddlewareManifest, ValidateConfigRequest, + ValidateConfigResponse, WebSocketSessionEvent, }; use openshell_extension_core::{ BearerTokenInterceptor, BearerTokenSlot, ExtensionChannelConfig, ExtensionServerTrust, @@ -25,8 +26,7 @@ use crate::MIDDLEWARE_GRPC_MESSAGE_BYTES; type ExtensionChannel = InterceptedService; -/// Adapts the borrowed runtime request contract to the owned protobuf service -/// contract only when dispatch crosses a gRPC-shaped boundary. +/// Adapts transport-neutral middleware streams to a gRPC-shaped boundary. #[derive(Clone)] pub struct GrpcMiddlewareService { service: Arc, @@ -78,21 +78,13 @@ impl GrpcMiddlewareService { .await } - /// Materialize an owned protobuf evaluation immediately before transport. - pub async fn evaluate_http_request( + /// Open a remote HTTP request pre-credentials stream through the adapter. + pub async fn open_http_request_pre_credentials( &self, - request: HttpRequestView<'_>, - ) -> std::result::Result, Status> { + receiver: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { self.service - .evaluate_http_request(Request::new(HttpRequestEvaluation { - phase: request.phase() as i32, - context: Some(request.context().clone()), - config: Some(request.config().clone()), - target: Some(request.target().clone()), - headers: request.headers().to_vec(), - body: request.body().to_vec(), - middleware_name: request.middleware_name().to_string(), - })) + .open_http_request_pre_credentials(receiver) .await } @@ -116,6 +108,7 @@ impl GrpcMiddlewareService { #[derive(Clone)] pub struct RemoteMiddlewareService { client: SupervisorMiddlewareClient, + request_client: HttpRequestPreCredentialsClient, response_client: HttpResponsePreReturnClient, } @@ -147,6 +140,9 @@ impl RemoteMiddlewareService { client: SupervisorMiddlewareClient::new(channel.clone()) .max_decoding_message_size(MIDDLEWARE_GRPC_MESSAGE_BYTES) .max_encoding_message_size(MIDDLEWARE_GRPC_MESSAGE_BYTES), + request_client: HttpRequestPreCredentialsClient::new(channel.clone()) + .max_decoding_message_size(MIDDLEWARE_GRPC_MESSAGE_BYTES) + .max_encoding_message_size(MIDDLEWARE_GRPC_MESSAGE_BYTES), response_client: HttpResponsePreReturnClient::new(channel) .max_decoding_message_size(MIDDLEWARE_GRPC_MESSAGE_BYTES) .max_encoding_message_size(MIDDLEWARE_GRPC_MESSAGE_BYTES), @@ -172,12 +168,18 @@ impl SupervisorMiddlewareEndpoint for RemoteMiddlewareService { client.validate_config(request).await } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - request: Request, - ) -> std::result::Result, Status> { - let mut client = self.client.clone(); - client.evaluate_http_request(request).await + receiver: tokio::sync::mpsc::Receiver, + ) -> std::result::Result { + let mut client = self.request_client.clone(); + let responses = client + .evaluate(Request::new(tokio_stream::wrappers::ReceiverStream::new( + receiver, + ))) + .await? + .into_inner(); + Ok(Box::pin(responses)) } async fn open_websocket_session( diff --git a/crates/openshell-supervisor-middleware/src/request.rs b/crates/openshell-supervisor-middleware/src/request.rs new file mode 100644 index 000000000..33d83452e --- /dev/null +++ b/crates/openshell-supervisor-middleware/src/request.rs @@ -0,0 +1,1917 @@ +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! HTTP request pre-credentials middleware chain execution. + +use std::collections::BTreeMap; +use std::time::Duration; + +use futures::StreamExt as _; +use prost::Message as _; +use tokio::sync::mpsc; +use tokio::time::Instant; + +use openshell_core::proto::{ + Finding, HeaderMutation, HttpHeader, HttpRequestBodyMode, HttpRequestBodyOutput, + HttpRequestBodyUnit, HttpRequestEvent, HttpRequestEventResult, HttpRequestPreflight, + HttpRequestTarget, HttpRequestTrailers, MiddlewareSessionEnd, MiddlewareSessionEndReason, + RequestContext, http_request_body_result, http_request_body_skip_remaining, + http_request_body_transform, http_request_body_unit, http_request_event, + http_request_event_result, http_request_preflight_result, +}; + +use super::{ + ChainEntry, ChainRunner, DescribedChainEntry, EXTERNAL_FINDING_LABEL, + MAX_MIDDLEWARE_CHAIN_TIMEOUT, MAX_MIDDLEWARE_CONTEXT_BYTES, MAX_MIDDLEWARE_FINDING_BYTES, + MAX_MIDDLEWARE_FINDINGS_PER_STAGE, MAX_MIDDLEWARE_HEADER_BYTES, MAX_MIDDLEWARE_HEADERS, + MAX_MIDDLEWARE_METADATA_BYTES, MAX_MIDDLEWARE_METADATA_ENTRIES, MAX_MIDDLEWARE_REASON_BYTES, + MAX_MIDDLEWARE_REASON_CODE_BYTES, MAX_MIDDLEWARE_TARGET_BYTES, MiddlewareDiagnosticPolicy, + MiddlewareSessionAdmission, MiddlewareSessionPermit, NamespacedFinding, OnError, headers, + is_stable_reason_code, middleware_denial_reason, +}; + +const STREAM_CHANNEL_CAPACITY: usize = 4; +const SESSION_END_TIMEOUT: Duration = Duration::from_millis(10); +const MAX_RECORDED_REQUEST_INVOCATIONS: usize = 1024; + +/// Largest normalized request body unit sent in streaming modes. +pub const MAX_HTTP_REQUEST_STREAM_UNIT_BYTES: usize = 64 * 1024; +/// Largest input or output representation an owned stage may retain. +/// +/// The limit bounds logical storage, not memory. Implementations are expected +/// to spool large representations instead of retaining them in RAM. +pub const MAX_HTTP_REQUEST_DEFERRED_BYTES: usize = 1024 * 1024 * 1024; + +#[derive(Debug, Clone)] +pub struct HttpRequestPreflightInput { + pub context: RequestContext, + pub target: HttpRequestTarget, + pub declared_body_length: Option, + pub headers: Vec, + pub connection_nominated_headers: Vec, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum HttpRequestInvocationOutcome { + Skip, + BlockRequest, + HeadersOnly, + WholeBody, + Stream, + OwnedStream, + Trailers, + PassThrough, + Transform, + SkipRemaining, + TakeOwnership, + FailOpen, + FailClosed, +} + +#[derive(Debug, Clone)] +pub struct HttpRequestInvocation { + pub config_name: String, + pub implementation: String, + pub outcome: HttpRequestInvocationOutcome, + pub sequence: Option, + pub input_size: usize, + pub output_size: Option, + pub failed: bool, + pub stage_disabled: bool, + pub reason_code: Option, + pub failure_category: Option, +} + +pub struct HttpRequestPreflightOutcome { + pub allowed: bool, + pub reason: String, + pub denial: Option, + pub headers: Vec, + /// Ordered mutations to replay against the original raw header block. + pub header_mutations: Vec, + pub session: Option, + pub findings: Vec, + pub metadata: BTreeMap>, + pub invocations: Vec, + pub session_capacity_exhausted: bool, +} + +#[derive(Debug)] +pub struct HttpRequestMiddlewareFailure { + pub reason: String, + pub denial: Option, + pub diagnostics: Box, +} + +impl std::fmt::Display for HttpRequestMiddlewareFailure { + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + formatter.write_str(&self.reason) + } +} + +impl std::error::Error for HttpRequestMiddlewareFailure {} + +impl HttpRequestMiddlewareFailure { + fn with_diagnostics(mut self, mut diagnostics: HttpRequestDiagnostics) -> Self { + let existing = *std::mem::take(&mut self.diagnostics); + diagnostics.findings.extend(existing.findings); + diagnostics.metadata.extend(existing.metadata); + diagnostics.invocations.extend(existing.invocations); + self.diagnostics = Box::new(diagnostics); + self + } +} + +#[derive(Debug)] +pub struct HttpRequestFinish { + pub body_units: Vec>, + pub trailers: Vec, + pub body_transformed: bool, + pub findings: Vec, + pub metadata: BTreeMap>, + pub invocations: Vec, +} + +#[derive(Debug, Default)] +pub struct HttpRequestDiagnostics { + pub findings: Vec, + pub metadata: BTreeMap>, + pub invocations: Vec, +} + +struct HttpRequestStageTransport { + sender: mpsc::Sender, + responses: super::HttpRequestResultStream, + terminal_sent: bool, +} + +impl HttpRequestStageTransport { + async fn end(mut self, reason: MiddlewareSessionEndReason) { + let _ = tokio::time::timeout(SESSION_END_TIMEOUT, self.end_inner(reason)).await; + } + + async fn end_inner(&mut self, reason: MiddlewareSessionEndReason) { + if self.sender.send(session_end_event(reason)).await.is_err() { + self.terminal_sent = true; + return; + } + self.terminal_sent = true; + self.drain().await; + } + + async fn drain(&mut self) { + while self.responses.next().await.is_some() {} + } +} + +impl Drop for HttpRequestStageTransport { + fn drop(&mut self) { + if !self.terminal_sent { + let _ = self + .sender + .try_send(session_end_event(MiddlewareSessionEndReason::Cancellation)); + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum StageMode { + HeadersOnly, + WholeBody, + Stream, + OwnedStream, +} + +struct HttpRequestStage { + entry: DescribedChainEntry, + transport: Option, + mode: StageMode, + next_sequence: u64, + whole_body: Vec, + owned_input_bytes: usize, + owned_output_bytes: usize, + next_output_sequence: u64, + finding_count: usize, +} + +impl HttpRequestStage { + fn is_active(&self) -> bool { + self.transport.is_some() + } + + async fn end(&mut self, reason: MiddlewareSessionEndReason) { + if let Some(transport) = self.transport.take() { + transport.end(reason).await; + } + } +} + +pub struct HttpRequestSession { + runner: ChainRunner, + stages: Vec, + findings: Vec, + metadata: BTreeMap>, + invocations: Vec, + session_admission: Option, + connection_nominated_headers: Vec, + body_transformed: bool, +} + +impl HttpRequestSession { + pub fn take_diagnostics(&mut self) -> HttpRequestDiagnostics { + HttpRequestDiagnostics { + findings: std::mem::take(&mut self.findings), + metadata: std::mem::take(&mut self.metadata), + invocations: std::mem::take(&mut self.invocations), + } + } + + #[must_use] + pub fn stream_unit_limit(&self) -> usize { + self.stages + .iter() + .filter(|stage| { + stage.is_active() + && matches!(stage.mode, StageMode::Stream | StageMode::OwnedStream) + }) + .map(|stage| { + stage + .entry + .max_payload_bytes + .clamp(1, MAX_HTTP_REQUEST_STREAM_UNIT_BYTES) + }) + .min() + .unwrap_or(MAX_HTTP_REQUEST_STREAM_UNIT_BYTES) + } + + /// Whether any active stage must see the complete representation before + /// the supervisor may disclose request bytes upstream. + #[must_use] + pub fn requires_withholding(&self) -> bool { + self.stages.iter().any(|stage| { + stage.is_active() && matches!(stage.mode, StageMode::WholeBody | StageMode::OwnedStream) + }) + } + + /// Process one non-final normalized body unit through the active chain. + pub async fn push_body( + &mut self, + data: Vec, + ) -> Result>, HttpRequestMiddlewareFailure> { + if data.is_empty() { + return Err(Self::failure("request_stream_unit_empty", None)); + } + if data.len() > self.stream_unit_limit() { + return Err(Self::failure("request_stream_unit_over_capacity", None)); + } + let _work = self + .runner + .reserve_middleware_work_admission() + .await + .map_err(|error| Self::failure(&format!("middleware_failed: {error}"), None))?; + let deadline = Instant::now() + MAX_MIDDLEWARE_CHAIN_TIMEOUT; + match self.process_units_from(0, vec![data], deadline).await { + Ok(units) => Ok(units), + Err(error) => { + let reason = if error.denial.is_some() { + MiddlewareSessionEndReason::MiddlewareDenial + } else { + MiddlewareSessionEndReason::MiddlewareFailure + }; + self.end_all(reason).await; + Err(error.with_diagnostics(self.take_diagnostics())) + } + } + } + + /// Finalize all body stages and collect output in memory. + /// + /// Network relays should prefer [`Self::finish_to`] so owned output remains + /// bounded by channel and storage backpressure. + pub async fn finish( + self, + trailers: Vec, + ) -> Result { + let (sender, mut receiver) = mpsc::channel(STREAM_CHANNEL_CAPACITY); + let finish = self.finish_to(trailers, sender); + let collect = async move { + let mut units = Vec::new(); + while let Some(unit) = receiver.recv().await { + units.push(unit); + } + units + }; + let (finish, units) = tokio::join!(finish, collect); + let mut finish = finish?; + finish.body_units = units; + Ok(finish) + } + + /// Finalize all stages while sending normalized output through a bounded + /// channel. The receiver controls backpressure and may spool to storage. + pub async fn finish_to( + mut self, + mut trailers: Vec, + output: mpsc::Sender>, + ) -> Result { + let _work = match self.runner.reserve_middleware_work_admission().await { + Ok(work) => work, + Err(error) => { + return Err(HttpRequestMiddlewareFailure { + reason: format!("middleware_failed: {error}"), + denial: None, + diagnostics: Box::new(self.take_diagnostics()), + }); + } + }; + let deadline = Instant::now() + MAX_MIDDLEWARE_CHAIN_TIMEOUT; + for index in 0..self.stages.len() { + let result = self.finish_stage_to(index, deadline, &output).await; + if let Err(failure) = result { + self.end_all(MiddlewareSessionEndReason::MiddlewareFailure) + .await; + return Err(failure.with_diagnostics(self.take_diagnostics())); + } + } + + trailers = match self.process_trailers(trailers, deadline).await { + Ok(trailers) => trailers, + Err(failure) => { + self.end_all(MiddlewareSessionEndReason::MiddlewareFailure) + .await; + return Err(failure.with_diagnostics(self.take_diagnostics())); + } + }; + self.end_all(MiddlewareSessionEndReason::Normal).await; + self.session_admission.take(); + drop(output); + Ok(HttpRequestFinish { + body_units: Vec::new(), + trailers, + body_transformed: self.body_transformed, + findings: self.findings, + metadata: self.metadata, + invocations: self.invocations, + }) + } + + pub async fn end(mut self, reason: MiddlewareSessionEndReason) { + self.end_all(reason).await; + } + + async fn process_units_from( + &mut self, + start: usize, + mut units: Vec>, + deadline: Instant, + ) -> Result>, HttpRequestMiddlewareFailure> { + // An empty replacement deletes the current unit. Streaming and owned + // stages receive only nonempty data units followed by their own single + // empty end-of-stream unit during `finish_stage_to`. + units.retain(|unit| !unit.is_empty()); + for index in start..self.stages.len() { + let mut next = Vec::new(); + for unit in units { + let chunk_limit = if matches!( + self.stages[index].mode, + StageMode::Stream | StageMode::OwnedStream + ) { + self.stages[index] + .entry + .max_payload_bytes + .min(MAX_HTTP_REQUEST_STREAM_UNIT_BYTES) + } else { + unit.len().max(1) + }; + for chunk in unit.chunks(chunk_limit) { + next.extend( + self.process_stage_unit(index, chunk.to_vec(), deadline) + .await?, + ); + } + } + units = next; + if units.is_empty() + && self.stages[index + 1..].iter().all(|stage| { + !matches!(stage.mode, StageMode::WholeBody | StageMode::OwnedStream) + }) + { + break; + } + } + Ok(units) + } + + async fn process_stage_unit( + &mut self, + index: usize, + data: Vec, + deadline: Instant, + ) -> Result>, HttpRequestMiddlewareFailure> { + if !self.stages[index].is_active() || self.stages[index].mode == StageMode::HeadersOnly { + return Ok(vec![data]); + } + if self.stages[index].mode == StageMode::WholeBody { + if self.stages[index] + .whole_body + .len() + .saturating_add(data.len()) + > self.stages[index].entry.max_payload_bytes + { + let mut original = std::mem::take(&mut self.stages[index].whole_body); + original.extend_from_slice(&data); + return self + .handle_stage_failure(index, "whole_body_over_capacity", None, original) + .await; + } + self.stages[index].whole_body.extend_from_slice(&data); + return Ok(Vec::new()); + } + + let sequence = self.stages[index].next_sequence; + self.stages[index].next_sequence += 1; + if self.stages[index].mode == StageMode::OwnedStream + && self.stages[index] + .owned_input_bytes + .saturating_add(data.len()) + > MAX_HTTP_REQUEST_DEFERRED_BYTES + { + return self + .handle_stage_failure( + index, + "owned_input_over_capacity", + Some(sequence), + Vec::new(), + ) + .await; + } + let event = body_event(sequence, data.clone(), false); + let result = match exchange(&mut self.stages[index], event, deadline).await { + Ok(result) => result, + Err(reason) => { + return self + .handle_stage_failure(index, &reason, Some(sequence), data) + .await; + } + }; + self.apply_body_result(index, result, sequence, data, false) + .await + } + + async fn finish_stage_to( + &mut self, + index: usize, + deadline: Instant, + output: &mpsc::Sender>, + ) -> Result<(), HttpRequestMiddlewareFailure> { + if !self.stages[index].is_active() || self.stages[index].mode == StageMode::HeadersOnly { + return Ok(()); + } + let mode = self.stages[index].mode; + let data = if mode == StageMode::WholeBody { + std::mem::take(&mut self.stages[index].whole_body) + } else { + Vec::new() + }; + let sequence = self.stages[index].next_sequence; + self.stages[index].next_sequence += 1; + let result = match exchange( + &mut self.stages[index], + body_event(sequence, data.clone(), true), + deadline, + ) + .await + { + Ok(result) => result, + Err(reason) => { + let original = if mode == StageMode::OwnedStream { + Vec::new() + } else { + data + }; + let recovered = self + .handle_stage_failure(index, &reason, Some(sequence), original) + .await?; + return self + .emit_downstream(index + 1, recovered, deadline, output) + .await; + } + }; + let recovered = self + .apply_body_result(index, result, sequence, data, true) + .await?; + self.emit_downstream(index + 1, recovered, deadline, output) + .await?; + + if mode == StageMode::OwnedStream && self.stages[index].is_active() { + self.drain_owned_output(index, sequence, deadline, output) + .await?; + } + Ok(()) + } + + async fn drain_owned_output( + &mut self, + index: usize, + final_input_sequence: u64, + deadline: Instant, + output: &mpsc::Sender>, + ) -> Result<(), HttpRequestMiddlewareFailure> { + loop { + let result = next_result(&mut self.stages[index], deadline) + .await + .map_err(|reason| { + Self::failure(&format!("middleware_failed: {reason}"), Some(index)) + })?; + match result.result { + Some(http_request_event_result::Result::BodyOutput(body_output)) => { + self.validate_owned_output(index, &body_output)?; + let downstream = self + .process_units_from(index + 1, vec![body_output.data], deadline) + .await?; + Self::send_output(output, downstream).await?; + } + Some(http_request_event_result::Result::BodyFinalize(finalize)) => { + let stage = &self.stages[index]; + let final_output_sequence = stage.next_output_sequence.saturating_sub(1); + if finalize.through_input_sequence != final_input_sequence + || finalize.through_output_sequence != final_output_sequence + { + return Err(Self::failure( + "middleware_failed: invalid_owned_finalization", + Some(index), + )); + } + if let Err(reason) = validate_stage_diagnostics( + self.stages[index].finding_count, + &finalize.reason, + &finalize.reason_code, + &finalize.findings, + &finalize.metadata, + ) { + return Err(Self::failure( + &format!("middleware_failed: {reason}"), + Some(index), + )); + } + let reason_code = + (!finalize.reason_code.is_empty()).then(|| finalize.reason_code.clone()); + self.stages[index].finding_count += finalize.findings.len(); + collect_diagnostics( + &self.stages[index].entry, + finalize.findings, + finalize.metadata, + &mut self.findings, + &mut self.metadata, + ); + record_request_invocation( + &mut self.invocations, + body_invocation( + &self.stages[index], + HttpRequestInvocationOutcome::Transform, + final_input_sequence, + self.stages[index].owned_input_bytes, + self.stages[index].owned_output_bytes, + reason_code, + ), + ); + return Ok(()); + } + _ => { + return Err(Self::failure( + "middleware_failed: unexpected_owned_output_result", + Some(index), + )); + } + } + } + } + + fn validate_owned_output( + &mut self, + index: usize, + output: &HttpRequestBodyOutput, + ) -> Result<(), HttpRequestMiddlewareFailure> { + let stage = &mut self.stages[index]; + if output.sequence != stage.next_output_sequence { + return Err(HttpRequestMiddlewareFailure { + reason: "middleware_failed: invalid_owned_output_sequence".into(), + denial: None, + diagnostics: Box::default(), + }); + } + if output.data.len() > stage.entry.max_payload_bytes { + return Err(HttpRequestMiddlewareFailure { + reason: "middleware_failed: owned_output_unit_over_capacity".into(), + denial: None, + diagnostics: Box::default(), + }); + } + stage.owned_output_bytes = stage.owned_output_bytes.saturating_add(output.data.len()); + if stage.owned_output_bytes > MAX_HTTP_REQUEST_DEFERRED_BYTES { + return Err(HttpRequestMiddlewareFailure { + reason: "middleware_failed: owned_output_over_capacity".into(), + denial: None, + diagnostics: Box::default(), + }); + } + stage.next_output_sequence += 1; + Ok(()) + } + + async fn emit_downstream( + &mut self, + start: usize, + units: Vec>, + deadline: Instant, + output: &mpsc::Sender>, + ) -> Result<(), HttpRequestMiddlewareFailure> { + let units = self.process_units_from(start, units, deadline).await?; + Self::send_output(output, units).await + } + + async fn send_output( + output: &mpsc::Sender>, + units: Vec>, + ) -> Result<(), HttpRequestMiddlewareFailure> { + for unit in units { + output + .send(unit) + .await + .map_err(|_| HttpRequestMiddlewareFailure { + reason: "request_output_consumer_closed".into(), + denial: None, + diagnostics: Box::default(), + })?; + } + Ok(()) + } + + async fn apply_body_result( + &mut self, + index: usize, + result: HttpRequestEventResult, + sequence: u64, + original: Vec, + end_of_stream: bool, + ) -> Result>, HttpRequestMiddlewareFailure> { + let Some(http_request_event_result::Result::BodyResult(result)) = result.result else { + return self + .handle_stage_failure(index, "unexpected_body_result", Some(sequence), original) + .await; + }; + if result.sequence != sequence { + return self + .handle_stage_failure(index, "invalid_body_sequence", Some(sequence), original) + .await; + } + if let Err(reason) = validate_stage_diagnostics( + self.stages[index].finding_count, + &result.reason, + &result.reason_code, + &result.findings, + &result.metadata, + ) { + return self + .handle_stage_failure(index, reason, Some(sequence), original) + .await; + } + let reason_code = (!result.reason_code.is_empty()).then(|| result.reason_code.clone()); + self.stages[index].finding_count += result.findings.len(); + collect_diagnostics( + &self.stages[index].entry, + result.findings, + result.metadata, + &mut self.findings, + &mut self.metadata, + ); + + let owned = self.stages[index].mode == StageMode::OwnedStream; + let action = result.action; + if owned { + if !matches!( + action, + Some(http_request_body_result::Action::TakeOwnership(_)) + ) { + return self + .handle_stage_failure( + index, + "owned_stage_did_not_take_ownership", + Some(sequence), + Vec::new(), + ) + .await; + } + self.stages[index].owned_input_bytes = self.stages[index] + .owned_input_bytes + .saturating_add(original.len()); + self.body_transformed = true; + record_request_invocation( + &mut self.invocations, + body_invocation( + &self.stages[index], + HttpRequestInvocationOutcome::TakeOwnership, + sequence, + original.len(), + 0, + reason_code, + ), + ); + return Ok(Vec::new()); + } + + let (outcome, replacement, skip_remaining) = match action { + Some(http_request_body_result::Action::PassThrough(_)) => ( + HttpRequestInvocationOutcome::PassThrough, + original.clone(), + false, + ), + Some(http_request_body_result::Action::Transform(transform)) => { + let Some(http_request_body_transform::Replacement::Data(data)) = + transform.replacement + else { + return self + .handle_stage_failure( + index, + "missing_body_replacement", + Some(sequence), + original, + ) + .await; + }; + if data.len() > self.stages[index].entry.max_payload_bytes { + return self + .handle_stage_failure( + index, + "body_replacement_over_capacity", + Some(sequence), + original, + ) + .await; + } + self.body_transformed = true; + (HttpRequestInvocationOutcome::Transform, data, false) + } + Some(http_request_body_result::Action::SkipRemaining(skip)) => { + let replacement = match skip.current { + Some(http_request_body_skip_remaining::Current::PassThrough(_)) => { + original.clone() + } + Some(http_request_body_skip_remaining::Current::Transform(transform)) => { + let Some(http_request_body_transform::Replacement::Data(data)) = + transform.replacement + else { + return self + .handle_stage_failure( + index, + "missing_body_replacement", + Some(sequence), + original, + ) + .await; + }; + if data.len() > self.stages[index].entry.max_payload_bytes { + return self + .handle_stage_failure( + index, + "body_replacement_over_capacity", + Some(sequence), + original, + ) + .await; + } + self.body_transformed = true; + data + } + None => { + return self + .handle_stage_failure( + index, + "missing_skip_remaining_action", + Some(sequence), + original, + ) + .await; + } + }; + ( + HttpRequestInvocationOutcome::SkipRemaining, + replacement, + true, + ) + } + Some(http_request_body_result::Action::BlockRequest(_)) => { + let denial = super::MiddlewareDenial { + config_name: self.stages[index].entry.entry.name.clone(), + reason_code: reason_code.clone(), + }; + record_request_invocation( + &mut self.invocations, + body_invocation( + &self.stages[index], + HttpRequestInvocationOutcome::BlockRequest, + sequence, + original.len(), + 0, + reason_code, + ), + ); + self.end_all(MiddlewareSessionEndReason::MiddlewareDenial) + .await; + return Err(HttpRequestMiddlewareFailure { + reason: middleware_denial_reason( + &denial.config_name, + denial.reason_code.as_deref(), + ), + denial: Some(denial), + diagnostics: Box::default(), + }); + } + Some(http_request_body_result::Action::TakeOwnership(_)) | None => { + return self + .handle_stage_failure(index, "invalid_body_action", Some(sequence), original) + .await; + } + }; + record_request_invocation( + &mut self.invocations, + body_invocation( + &self.stages[index], + outcome, + sequence, + original.len(), + replacement.len(), + reason_code, + ), + ); + if skip_remaining { + self.stages[index] + .end(MiddlewareSessionEndReason::StageSkipped) + .await; + self.release_admission_if_idle(); + } else if end_of_stream { + // Keep the transport open for the trailers event. + } + Ok(vec![replacement]) + } + + async fn process_trailers( + &mut self, + mut trailers: Vec, + deadline: Instant, + ) -> Result, HttpRequestMiddlewareFailure> { + for index in 0..self.stages.len() { + if !self.stages[index].is_active() || self.stages[index].mode == StageMode::HeadersOnly + { + continue; + } + let event = HttpRequestEvent { + event: Some(http_request_event::Event::Trailers(HttpRequestTrailers { + headers: trailers.clone(), + })), + }; + let result = match exchange(&mut self.stages[index], event, deadline).await { + Ok(result) => result, + Err(reason) => { + trailers = self + .handle_trailer_failure(index, &reason, trailers) + .await?; + continue; + } + }; + let Some(http_request_event_result::Result::TrailersResult(result)) = result.result + else { + trailers = self + .handle_trailer_failure(index, "unexpected_trailers_result", trailers) + .await?; + continue; + }; + if let Err(reason) = validate_stage_diagnostics( + self.stages[index].finding_count, + &result.reason, + &result.reason_code, + &result.findings, + &result.metadata, + ) { + trailers = self.handle_trailer_failure(index, reason, trailers).await?; + continue; + } + self.stages[index].finding_count += result.findings.len(); + let updated = match headers::apply( + headers::HeaderAuthority::RequestTrailers, + &trailers, + &self.connection_nominated_headers, + &result.trailer_mutations, + ) { + Ok(updated) => updated, + Err(error) => { + let reason = self.stages[index].entry.service.as_ref().map_or_else( + || error.to_string(), + |service| { + service + .diagnostic_policy + .header_mutation_error_reason(&error) + }, + ); + trailers = self + .handle_trailer_failure(index, &reason, trailers) + .await?; + continue; + } + }; + collect_diagnostics( + &self.stages[index].entry, + result.findings, + result.metadata, + &mut self.findings, + &mut self.metadata, + ); + record_request_invocation( + &mut self.invocations, + HttpRequestInvocation { + config_name: self.stages[index].entry.entry.name.clone(), + implementation: self.stages[index].entry.entry.implementation.clone(), + outcome: HttpRequestInvocationOutcome::Trailers, + sequence: None, + input_size: encoded_header_bytes(&trailers), + output_size: Some(encoded_header_bytes(&updated)), + failed: false, + stage_disabled: false, + reason_code: (!result.reason_code.is_empty()).then_some(result.reason_code), + failure_category: None, + }, + ); + trailers = updated; + } + Ok(trailers) + } + + async fn handle_trailer_failure( + &mut self, + index: usize, + reason: &str, + original: Vec, + ) -> Result, HttpRequestMiddlewareFailure> { + let stage = &mut self.stages[index]; + let fail_open = stage.entry.on_error() == OnError::FailOpen; + record_request_invocation( + &mut self.invocations, + HttpRequestInvocation { + config_name: stage.entry.entry.name.clone(), + implementation: stage.entry.entry.implementation.clone(), + outcome: if fail_open { + HttpRequestInvocationOutcome::FailOpen + } else { + HttpRequestInvocationOutcome::FailClosed + }, + sequence: None, + input_size: encoded_header_bytes(&original), + output_size: None, + failed: true, + stage_disabled: true, + reason_code: None, + failure_category: Some(request_failure_category(reason).into()), + }, + ); + stage + .end(MiddlewareSessionEndReason::MiddlewareFailure) + .await; + self.release_admission_if_idle(); + if fail_open { + Ok(original) + } else { + Err(HttpRequestMiddlewareFailure { + reason: format!("middleware_failed: {reason}"), + denial: None, + diagnostics: Box::default(), + }) + } + } + + async fn handle_stage_failure( + &mut self, + index: usize, + reason: &str, + sequence: Option, + original: Vec, + ) -> Result>, HttpRequestMiddlewareFailure> { + let stage = &mut self.stages[index]; + let fail_open = + stage.entry.on_error() == OnError::FailOpen && stage.mode != StageMode::OwnedStream; + record_request_invocation( + &mut self.invocations, + HttpRequestInvocation { + config_name: stage.entry.entry.name.clone(), + implementation: stage.entry.entry.implementation.clone(), + outcome: if fail_open { + HttpRequestInvocationOutcome::FailOpen + } else { + HttpRequestInvocationOutcome::FailClosed + }, + sequence, + input_size: original.len(), + output_size: None, + failed: true, + stage_disabled: true, + reason_code: None, + failure_category: Some(request_failure_category(reason).into()), + }, + ); + stage + .end(MiddlewareSessionEndReason::MiddlewareFailure) + .await; + self.release_admission_if_idle(); + if fail_open { + Ok(vec![original]) + } else { + Err(HttpRequestMiddlewareFailure { + reason: format!("middleware_failed: {reason}"), + denial: None, + diagnostics: Box::default(), + }) + } + } + + async fn end_all(&mut self, reason: MiddlewareSessionEndReason) { + for stage in &mut self.stages { + stage.end(reason).await; + } + self.session_admission.take(); + } + + fn release_admission_if_idle(&mut self) { + if self.stages.iter().all(|stage| !stage.is_active()) { + self.session_admission.take(); + } + } + + fn failure(reason: &str, _index: Option) -> HttpRequestMiddlewareFailure { + HttpRequestMiddlewareFailure { + reason: reason.to_string(), + // Transport and protocol failures are not authoritative service + // denials. The caller still fails closed when policy requires it, + // but must not present the failure as an accepted block decision. + denial: None, + diagnostics: Box::default(), + } + } +} + +impl ChainRunner { + pub async fn preflight_http_request( + &self, + entries: &[ChainEntry], + input: HttpRequestPreflightInput, + ) -> miette::Result { + let described = self.describe_chain(entries).await?; + self.preflight_described_http_request(described, input) + .await + } + + pub async fn preflight_described_http_request( + &self, + described: Vec, + input: HttpRequestPreflightInput, + ) -> miette::Result { + self.preflight_described_http_request_with_owned(described, input, true) + .await + } + + /// Open a request stream with explicit control over storage-backed owned + /// mode. Complete-body compatibility callers disable owned mode because + /// their result is necessarily materialized in memory. + pub(crate) async fn preflight_described_http_request_with_owned( + &self, + described: Vec, + input: HttpRequestPreflightInput, + allow_owned: bool, + ) -> miette::Result { + if described.is_empty() { + return Ok(empty_preflight_outcome(input.headers)); + } + if validate_preflight_input(&input).is_err() { + return Ok(preflight_input_failure( + &described, + input.headers, + "request_input_over_capacity", + )); + } + let session_admission = match self.try_reserve_middleware_session() { + MiddlewareSessionAdmission::Admitted(admission) => admission, + MiddlewareSessionAdmission::AtCapacity => { + return Ok(session_capacity_exhausted(described, input.headers)); + } + }; + let work_admission = self.reserve_middleware_work().await?; + let _work = match work_admission { + super::MiddlewareWorkAdmissionOutcome::Admitted(admission) => admission, + super::MiddlewareWorkAdmissionOutcome::QueueExhausted => { + return Ok(session_capacity_exhausted(described, input.headers)); + } + }; + let mut headers = input.headers.clone(); + let mut header_mutations = Vec::new(); + let mut stages = Vec::new(); + let mut findings = Vec::new(); + let mut metadata = BTreeMap::new(); + let mut invocations = Vec::new(); + + for entry in described { + let Some(service) = entry.service.as_ref() else { + if let Some(reason) = + collect_preflight_failure(&entry, "binding_not_described", &mut invocations) + { + end_stages(&mut stages, MiddlewareSessionEndReason::MiddlewareFailure).await; + return Ok(failed_preflight_outcome( + headers, + header_mutations, + reason, + findings, + metadata, + invocations, + )); + } + continue; + }; + let permitted_modes = permitted_body_modes(&input, &entry, allow_owned); + let (sender, receiver) = mpsc::channel(STREAM_CHANNEL_CAPACITY); + let preflight = HttpRequestPreflight { + context: Some(input.context.clone()), + target: Some(input.target.clone()), + headers: headers.clone(), + middleware_name: entry.entry.implementation.clone(), + config: Some(entry.entry.config.clone()), + max_payload_bytes: entry.max_payload_bytes as u64, + permitted_body_modes: permitted_modes.clone(), + max_deferred_bytes: if entry.on_error() == OnError::FailClosed { + MAX_HTTP_REQUEST_DEFERRED_BYTES as u64 + } else { + 0 + }, + declared_body_length: input.declared_body_length, + }; + let timeout = entry.timeout; + let opened = tokio::time::timeout(timeout, async { + sender + .send(HttpRequestEvent { + event: Some(http_request_event::Event::Preflight(preflight)), + }) + .await + .map_err(|_| tonic::Status::unavailable("middleware request stream closed"))?; + let mut responses = service + .service + .open_http_request_pre_credentials(receiver) + .await?; + let response = responses.next().await.ok_or_else(|| { + tonic::Status::unavailable("middleware result stream closed") + })??; + Ok::<_, tonic::Status>((responses, response)) + }) + .await; + let (responses, response) = match opened { + Ok(Ok(opened)) => opened, + Ok(Err(error)) => { + let reason = service.diagnostic_policy.error_reason(&error); + if let Some(reason) = + collect_preflight_failure(&entry, &reason, &mut invocations) + { + end_stages(&mut stages, MiddlewareSessionEndReason::MiddlewareFailure) + .await; + return Ok(failed_preflight_outcome( + headers, + header_mutations, + reason, + findings, + metadata, + invocations, + )); + } + continue; + } + Err(_) => { + if let Some(reason) = + collect_preflight_failure(&entry, "middleware_timeout", &mut invocations) + { + end_stages(&mut stages, MiddlewareSessionEndReason::MiddlewareFailure) + .await; + return Ok(failed_preflight_outcome( + headers, + header_mutations, + reason, + findings, + metadata, + invocations, + )); + } + continue; + } + }; + let mut current_stage = HttpRequestStage { + entry: entry.clone(), + transport: Some(HttpRequestStageTransport { + sender, + responses, + terminal_sent: false, + }), + mode: StageMode::HeadersOnly, + next_sequence: 1, + whole_body: Vec::new(), + owned_input_bytes: 0, + owned_output_bytes: 0, + next_output_sequence: 1, + finding_count: 0, + }; + let Some(http_request_event_result::Result::PreflightResult(result)) = response.result + else { + if let Some(reason) = handle_opened_preflight_failure( + &entry, + &mut current_stage, + &mut stages, + "unexpected_preflight_result", + &mut invocations, + ) + .await + { + return Ok(failed_preflight_outcome( + headers, + header_mutations, + reason, + findings, + metadata, + invocations, + )); + } + continue; + }; + if let Err(reason) = validate_diagnostics( + &result.reason, + &result.reason_code, + &result.findings, + &result.metadata, + ) { + if let Some(reason) = handle_opened_preflight_failure( + &entry, + &mut current_stage, + &mut stages, + reason, + &mut invocations, + ) + .await + { + return Ok(failed_preflight_outcome( + headers, + header_mutations, + reason, + findings, + metadata, + invocations, + )); + } + continue; + } + let reason_code = (!result.reason_code.is_empty()).then(|| result.reason_code.clone()); + current_stage.finding_count = result.findings.len(); + let result_findings = result.findings; + let result_metadata = result.metadata; + match result.action { + Some(http_request_preflight_result::Action::Skip(_)) => { + collect_diagnostics( + &entry, + result_findings, + result_metadata, + &mut findings, + &mut metadata, + ); + invocations.push(preflight_invocation( + &entry, + HttpRequestInvocationOutcome::Skip, + reason_code, + )); + current_stage + .end(MiddlewareSessionEndReason::StageSkipped) + .await; + } + Some(http_request_preflight_result::Action::Inspect(inspect)) => { + let mode = match validate_inspect(&inspect, &permitted_modes) { + Ok(mode) => mode, + Err(reason) => { + if let Some(reason) = handle_opened_preflight_failure( + &entry, + &mut current_stage, + &mut stages, + reason, + &mut invocations, + ) + .await + { + return Ok(failed_preflight_outcome( + headers, + header_mutations, + reason, + findings, + metadata, + invocations, + )); + } + continue; + } + }; + let updated = match headers::apply( + headers::HeaderAuthority::Request, + &headers, + &input.connection_nominated_headers, + &inspect.header_mutations, + ) { + Ok(updated) => updated, + Err(error) => { + let reason = service + .diagnostic_policy + .header_mutation_error_reason(&error); + if let Some(reason) = handle_opened_preflight_failure( + &entry, + &mut current_stage, + &mut stages, + &reason, + &mut invocations, + ) + .await + { + return Ok(failed_preflight_outcome( + headers, + header_mutations, + reason, + findings, + metadata, + invocations, + )); + } + continue; + } + }; + headers = updated; + header_mutations.extend(inspect.header_mutations); + collect_diagnostics( + &entry, + result_findings, + result_metadata, + &mut findings, + &mut metadata, + ); + invocations.push(preflight_invocation( + &entry, + match mode { + StageMode::HeadersOnly => HttpRequestInvocationOutcome::HeadersOnly, + StageMode::WholeBody => HttpRequestInvocationOutcome::WholeBody, + StageMode::Stream => HttpRequestInvocationOutcome::Stream, + StageMode::OwnedStream => HttpRequestInvocationOutcome::OwnedStream, + }, + reason_code, + )); + current_stage.mode = mode; + if mode == StageMode::HeadersOnly { + current_stage.end(MiddlewareSessionEndReason::Normal).await; + } else { + stages.push(current_stage); + } + } + Some(http_request_preflight_result::Action::BlockRequest(_)) => { + collect_diagnostics( + &entry, + result_findings, + result_metadata, + &mut findings, + &mut metadata, + ); + invocations.push(preflight_invocation( + &entry, + HttpRequestInvocationOutcome::BlockRequest, + reason_code.clone(), + )); + stages.push(current_stage); + end_stages(&mut stages, MiddlewareSessionEndReason::MiddlewareDenial).await; + let denial = super::MiddlewareDenial { + config_name: entry.entry.name.clone(), + reason_code, + }; + return Ok(HttpRequestPreflightOutcome { + allowed: false, + reason: middleware_denial_reason( + &denial.config_name, + denial.reason_code.as_deref(), + ), + denial: Some(denial), + headers, + header_mutations, + session: None, + findings, + metadata, + invocations, + session_capacity_exhausted: false, + }); + } + None => { + if let Some(reason) = handle_opened_preflight_failure( + &entry, + &mut current_stage, + &mut stages, + "missing_preflight_action", + &mut invocations, + ) + .await + { + return Ok(failed_preflight_outcome( + headers, + header_mutations, + reason, + findings, + metadata, + invocations, + )); + } + } + } + } + + let session = (!stages.is_empty()).then(|| HttpRequestSession { + runner: self.clone(), + stages, + findings: Vec::new(), + metadata: BTreeMap::new(), + invocations: Vec::new(), + session_admission: Some(session_admission), + connection_nominated_headers: input.connection_nominated_headers, + body_transformed: false, + }); + Ok(HttpRequestPreflightOutcome { + allowed: true, + reason: String::new(), + denial: None, + headers, + header_mutations, + session, + findings, + metadata, + invocations, + session_capacity_exhausted: false, + }) + } +} + +async fn exchange( + stage: &mut HttpRequestStage, + event: HttpRequestEvent, + chain_deadline: Instant, +) -> Result { + let remaining = chain_deadline.saturating_duration_since(Instant::now()); + if remaining.is_zero() { + return Err("middleware_chain_timeout".into()); + } + let timeout = stage.entry.timeout.min(remaining); + let Some(transport) = stage.transport.as_mut() else { + return Err("middleware_stream_closed".into()); + }; + match tokio::time::timeout(timeout, async { + transport + .sender + .send(event) + .await + .map_err(|_| tonic::Status::unavailable("middleware request stream closed"))?; + transport + .responses + .next() + .await + .ok_or_else(|| tonic::Status::unavailable("middleware result stream closed"))? + }) + .await + { + Ok(Ok(result)) => Ok(result), + Ok(Err(error)) => { + let policy = stage + .entry + .service + .as_ref() + .map_or(MiddlewareDiagnosticPolicy::Preserve, |service| { + service.diagnostic_policy + }); + Err(policy.error_reason(&error)) + } + Err(_) => Err("middleware_timeout".into()), + } +} + +async fn next_result( + stage: &mut HttpRequestStage, + chain_deadline: Instant, +) -> Result { + let remaining = chain_deadline.saturating_duration_since(Instant::now()); + if remaining.is_zero() { + return Err("middleware_chain_timeout".into()); + } + let timeout = stage.entry.timeout.min(remaining); + let Some(transport) = stage.transport.as_mut() else { + return Err("middleware_stream_closed".into()); + }; + match tokio::time::timeout(timeout, transport.responses.next()).await { + Ok(Some(Ok(result))) => Ok(result), + Ok(Some(Err(error))) => Err(stage + .entry + .service + .as_ref() + .map_or(MiddlewareDiagnosticPolicy::Preserve, |service| { + service.diagnostic_policy + }) + .error_reason(&error)), + Ok(None) => Err("middleware_result_stream_closed".into()), + Err(_) => Err("middleware_timeout".into()), + } +} + +fn body_event(sequence: u64, data: Vec, end_of_stream: bool) -> HttpRequestEvent { + HttpRequestEvent { + event: Some(http_request_event::Event::Body(HttpRequestBodyUnit { + sequence, + payload: Some(http_request_body_unit::Payload::Data(data)), + end_of_stream, + })), + } +} + +fn session_end_event(reason: MiddlewareSessionEndReason) -> HttpRequestEvent { + HttpRequestEvent { + event: Some(http_request_event::Event::SessionEnd( + MiddlewareSessionEnd { + reason: reason as i32, + protocol_error: None, + }, + )), + } +} + +fn validate_preflight_input(input: &HttpRequestPreflightInput) -> miette::Result<()> { + if input.context.encoded_len() > MAX_MIDDLEWARE_CONTEXT_BYTES { + return Err(miette::miette!("request context exceeds platform limit")); + } + if input.target.encoded_len() > MAX_MIDDLEWARE_TARGET_BYTES { + return Err(miette::miette!("request target exceeds platform limit")); + } + if input.headers.len() > MAX_MIDDLEWARE_HEADERS { + return Err(miette::miette!( + "request header count exceeds platform limit" + )); + } + if encoded_header_bytes(&input.headers) > MAX_MIDDLEWARE_HEADER_BYTES { + return Err(miette::miette!("request headers exceed platform limit")); + } + Ok(()) +} + +fn validate_diagnostics( + reason: &str, + reason_code: &str, + findings: &[Finding], + metadata: &std::collections::HashMap, +) -> Result<(), &'static str> { + if reason.len() > MAX_MIDDLEWARE_REASON_BYTES { + return Err("request_reason_over_capacity"); + } + if !reason_code.is_empty() + && (reason_code.len() > MAX_MIDDLEWARE_REASON_CODE_BYTES + || !is_stable_reason_code(reason_code)) + { + return Err("request_reason_code_invalid"); + } + if findings.len() > MAX_MIDDLEWARE_FINDINGS_PER_STAGE { + return Err("request_findings_over_capacity"); + } + if findings + .iter() + .any(|finding| finding.encoded_len() > MAX_MIDDLEWARE_FINDING_BYTES) + { + return Err("request_finding_over_capacity"); + } + if metadata.len() > MAX_MIDDLEWARE_METADATA_ENTRIES { + return Err("request_metadata_count_over_capacity"); + } + if metadata.iter().fold(0usize, |total, (key, value)| { + total.saturating_add(key.len()).saturating_add(value.len()) + }) > MAX_MIDDLEWARE_METADATA_BYTES + { + return Err("request_metadata_bytes_over_capacity"); + } + Ok(()) +} + +fn validate_stage_diagnostics( + existing_findings: usize, + reason: &str, + reason_code: &str, + findings: &[Finding], + metadata: &std::collections::HashMap, +) -> Result<(), &'static str> { + validate_diagnostics(reason, reason_code, findings, metadata)?; + if existing_findings.saturating_add(findings.len()) > MAX_MIDDLEWARE_FINDINGS_PER_STAGE { + return Err("request_findings_over_capacity"); + } + Ok(()) +} + +fn permitted_body_modes( + input: &HttpRequestPreflightInput, + entry: &DescribedChainEntry, + allow_owned: bool, +) -> Vec { + let mut modes = vec![HttpRequestBodyMode::HeadersOnly as i32]; + if input + .declared_body_length + .is_none_or(|length| length <= entry.max_payload_bytes as u64) + { + modes.push(HttpRequestBodyMode::WholeBodyBytes as i32); + } + if entry.max_payload_bytes > 0 { + modes.push(HttpRequestBodyMode::StreamBytes as i32); + if allow_owned && entry.on_error() == OnError::FailClosed { + modes.push(HttpRequestBodyMode::OwnedStreamBytes as i32); + } + } + modes +} + +fn validate_inspect( + inspect: &openshell_core::proto::HttpRequestPreflightInspect, + permitted_modes: &[i32], +) -> Result { + if !permitted_modes.contains(&inspect.body_mode) { + return Err("request_body_mode_not_permitted"); + } + match HttpRequestBodyMode::try_from(inspect.body_mode) { + Ok(HttpRequestBodyMode::HeadersOnly) => Ok(StageMode::HeadersOnly), + Ok(HttpRequestBodyMode::WholeBodyBytes) => Ok(StageMode::WholeBody), + Ok(HttpRequestBodyMode::StreamBytes) => Ok(StageMode::Stream), + Ok(HttpRequestBodyMode::OwnedStreamBytes) => Ok(StageMode::OwnedStream), + Ok(HttpRequestBodyMode::Unspecified) | Err(_) => Err("invalid_request_body_mode"), + } +} + +fn encoded_header_bytes(headers: &[HttpHeader]) -> usize { + headers.iter().fold(0usize, |total, header| { + total.saturating_add(header.encoded_len()) + }) +} + +fn collect_diagnostics( + entry: &DescribedChainEntry, + mut findings: Vec, + mut metadata: std::collections::HashMap, + all_findings: &mut Vec, + all_metadata: &mut BTreeMap>, +) { + if entry + .service + .as_ref() + .is_some_and(|service| service.diagnostic_policy == MiddlewareDiagnosticPolicy::Normalize) + { + metadata.clear(); + for finding in &mut findings { + finding.r#type = format!("{}.finding", entry.entry.implementation); + finding.label = EXTERNAL_FINDING_LABEL.to_string(); + finding.confidence.clear(); + finding.severity = "medium".into(); + } + } + all_findings.extend(findings.into_iter().map(|finding| NamespacedFinding { + middleware: entry.entry.name.clone(), + finding, + })); + if !metadata.is_empty() { + all_metadata.insert(entry.entry.name.clone(), metadata.into_iter().collect()); + } +} + +fn body_invocation( + stage: &HttpRequestStage, + outcome: HttpRequestInvocationOutcome, + sequence: u64, + input_size: usize, + output_size: usize, + reason_code: Option, +) -> HttpRequestInvocation { + HttpRequestInvocation { + config_name: stage.entry.entry.name.clone(), + implementation: stage.entry.entry.implementation.clone(), + outcome, + sequence: Some(sequence), + input_size, + output_size: Some(output_size), + failed: false, + stage_disabled: false, + reason_code, + failure_category: None, + } +} + +fn record_request_invocation( + invocations: &mut Vec, + invocation: HttpRequestInvocation, +) { + if invocations.len() < MAX_RECORDED_REQUEST_INVOCATIONS { + invocations.push(invocation); + return; + } + + let index = invocations + .iter() + .position(|existing| existing.config_name == invocation.config_name) + .unwrap_or(invocations.len() - 1); + let existing = &mut invocations[index]; + existing.sequence = invocation.sequence.or(existing.sequence); + existing.input_size = existing.input_size.saturating_add(invocation.input_size); + existing.output_size = match (existing.output_size, invocation.output_size) { + (_, None) if invocation.failed => None, + (Some(existing), Some(incoming)) => Some(existing.saturating_add(incoming)), + (None, Some(incoming)) => Some(incoming), + (existing, None) => existing, + }; + existing.failed |= invocation.failed; + existing.stage_disabled |= invocation.stage_disabled; + if invocation.reason_code.is_some() { + existing.reason_code = invocation.reason_code; + } + if invocation.failure_category.is_some() { + existing.failure_category = invocation.failure_category; + } + if request_invocation_priority(invocation.outcome) + >= request_invocation_priority(existing.outcome) + { + existing.outcome = invocation.outcome; + } +} + +fn request_invocation_priority(outcome: HttpRequestInvocationOutcome) -> u8 { + match outcome { + HttpRequestInvocationOutcome::BlockRequest | HttpRequestInvocationOutcome::FailClosed => 5, + HttpRequestInvocationOutcome::FailOpen => 4, + HttpRequestInvocationOutcome::Transform => 3, + HttpRequestInvocationOutcome::SkipRemaining + | HttpRequestInvocationOutcome::TakeOwnership => 2, + HttpRequestInvocationOutcome::Skip + | HttpRequestInvocationOutcome::HeadersOnly + | HttpRequestInvocationOutcome::WholeBody + | HttpRequestInvocationOutcome::Stream + | HttpRequestInvocationOutcome::OwnedStream + | HttpRequestInvocationOutcome::Trailers + | HttpRequestInvocationOutcome::PassThrough => 1, + } +} + +fn preflight_invocation( + entry: &DescribedChainEntry, + outcome: HttpRequestInvocationOutcome, + reason_code: Option, +) -> HttpRequestInvocation { + HttpRequestInvocation { + config_name: entry.entry.name.clone(), + implementation: entry.entry.implementation.clone(), + outcome, + sequence: None, + input_size: 0, + output_size: None, + failed: false, + stage_disabled: false, + reason_code, + failure_category: None, + } +} + +fn collect_preflight_failure( + entry: &DescribedChainEntry, + reason: &str, + invocations: &mut Vec, +) -> Option { + let fail_closed = entry.on_error() == OnError::FailClosed; + invocations.push(HttpRequestInvocation { + config_name: entry.entry.name.clone(), + implementation: entry.entry.implementation.clone(), + outcome: if fail_closed { + HttpRequestInvocationOutcome::FailClosed + } else { + HttpRequestInvocationOutcome::FailOpen + }, + sequence: None, + input_size: 0, + output_size: None, + failed: true, + stage_disabled: true, + reason_code: None, + failure_category: Some(request_failure_category(reason).into()), + }); + fail_closed.then(|| format!("middleware_failed: {reason}")) +} + +fn empty_preflight_outcome(headers: Vec) -> HttpRequestPreflightOutcome { + HttpRequestPreflightOutcome { + allowed: true, + reason: String::new(), + denial: None, + headers, + header_mutations: Vec::new(), + session: None, + findings: Vec::new(), + metadata: BTreeMap::new(), + invocations: Vec::new(), + session_capacity_exhausted: false, + } +} + +fn failed_preflight_outcome( + headers: Vec, + header_mutations: Vec, + reason: String, + findings: Vec, + metadata: BTreeMap>, + invocations: Vec, +) -> HttpRequestPreflightOutcome { + HttpRequestPreflightOutcome { + allowed: false, + reason, + denial: None, + headers, + header_mutations, + session: None, + findings, + metadata, + invocations, + session_capacity_exhausted: false, + } +} + +fn preflight_input_failure( + entries: &[DescribedChainEntry], + headers: Vec, + reason: &str, +) -> HttpRequestPreflightOutcome { + let mut invocations = Vec::new(); + let denied = entries + .iter() + .find_map(|entry| collect_preflight_failure(entry, reason, &mut invocations)); + if let Some(reason) = denied { + failed_preflight_outcome( + headers, + Vec::new(), + reason, + Vec::new(), + BTreeMap::new(), + invocations, + ) + } else { + HttpRequestPreflightOutcome { + invocations, + ..empty_preflight_outcome(headers) + } + } +} + +fn session_capacity_exhausted( + entries: Vec, + headers: Vec, +) -> HttpRequestPreflightOutcome { + let mut outcome = preflight_input_failure(&entries, headers, "session_capacity_exhausted"); + outcome.session_capacity_exhausted = true; + outcome +} + +async fn end_stages(stages: &mut [HttpRequestStage], reason: MiddlewareSessionEndReason) { + for stage in stages { + stage.end(reason).await; + } +} + +async fn handle_opened_preflight_failure( + entry: &DescribedChainEntry, + current_stage: &mut HttpRequestStage, + prior_stages: &mut [HttpRequestStage], + reason: &str, + invocations: &mut Vec, +) -> Option { + current_stage + .end(MiddlewareSessionEndReason::MiddlewareFailure) + .await; + let failure = collect_preflight_failure(entry, reason, invocations); + if failure.is_some() { + end_stages(prior_stages, MiddlewareSessionEndReason::MiddlewareFailure).await; + } + failure +} + +fn request_failure_category(reason: &str) -> &'static str { + if reason.contains("timeout") { + "timeout" + } else if reason.contains("capacity") { + "capacity" + } else if reason.contains("header") { + "header_mutation" + } else if reason.contains("sequence") || reason.contains("result") { + "protocol" + } else { + "service" + } +} diff --git a/crates/openshell-supervisor-middleware/src/response.rs b/crates/openshell-supervisor-middleware/src/response.rs index 073bebd88..a179eaabc 100644 --- a/crates/openshell-supervisor-middleware/src/response.rs +++ b/crates/openshell-supervisor-middleware/src/response.rs @@ -1196,12 +1196,12 @@ async fn handle_opened_preflight_failure( mod tests { use std::sync::Arc; - use openshell_core::middleware::{HttpRequestView, InProcessMiddleware}; + use openshell_core::middleware::InProcessMiddleware; use openshell_core::proto::{ - Decision, ExistingHeaderAction, HeaderMutation, HttpRequestResult, HttpResponseBodyResult, - HttpResponseBodyTransform, HttpResponsePreflightInspect, HttpResponsePreflightResult, - HttpResponsePreflightSkip, HttpResponseTrailersResult, MiddlewareBinding, - MiddlewareManifest, WriteHeader, header_mutation, http_response_preflight_result, + ExistingHeaderAction, HeaderMutation, HttpResponseBodyResult, HttpResponseBodyTransform, + HttpResponsePreflightInspect, HttpResponsePreflightResult, HttpResponsePreflightSkip, + HttpResponseTrailersResult, MiddlewareBinding, MiddlewareManifest, WriteHeader, + header_mutation, http_response_preflight_result, }; use tokio_stream::wrappers::ReceiverStream; use tokio_stream::wrappers::TcpListenerStream; @@ -1267,16 +1267,6 @@ mod tests { )) } - async fn evaluate_http_request( - &self, - _request: tonic::Request, - ) -> Result, tonic::Status> { - Ok(tonic::Response::new(HttpRequestResult { - decision: Decision::Allow as i32, - ..Default::default() - })) - } - async fn evaluate_web_socket_session( &self, _request: tonic::Request< @@ -1388,16 +1378,6 @@ mod tests { Ok(()) } - async fn evaluate_http_request( - &self, - _request: HttpRequestView<'_>, - ) -> miette::Result { - Ok(HttpRequestResult { - decision: Decision::Allow as i32, - ..Default::default() - }) - } - async fn open_http_response_pre_return( &self, mut requests: mpsc::Receiver, @@ -1600,13 +1580,6 @@ mod tests { Ok(()) } - async fn evaluate_http_request( - &self, - _request: HttpRequestView<'_>, - ) -> miette::Result { - unreachable!() - } - async fn open_http_response_pre_return( &self, mut requests: mpsc::Receiver, @@ -1931,13 +1904,6 @@ mod tests { Ok(()) } - async fn evaluate_http_request( - &self, - _: HttpRequestView<'_>, - ) -> miette::Result { - unreachable!() - } - async fn open_http_response_pre_return( &self, mut requests: mpsc::Receiver, diff --git a/crates/openshell-supervisor-network/Cargo.toml b/crates/openshell-supervisor-network/Cargo.toml index d4aa9d1ee..a8f056170 100644 --- a/crates/openshell-supervisor-network/Cargo.toml +++ b/crates/openshell-supervisor-network/Cargo.toml @@ -17,13 +17,11 @@ openshell-isolation-interface = { path = "../openshell-isolation-interface" } openshell-ocsf = { path = "../openshell-ocsf" } openshell-policy = { path = "../openshell-policy" } openshell-supervisor-middleware = { path = "../openshell-supervisor-middleware" } +openshell-supervisor-middleware-builtins = { path = "../openshell-supervisor-middleware-builtins" } async-trait = "0.1" apollo-parser = { workspace = true } -aws-sigv4 = { version = "1", features = ["sign-http", "http1"] } -aws-credential-types = { version = "1", features = ["hardcoded-credentials"] } -aws-smithy-runtime-api = { version = "1", features = ["client"] } http = { workspace = true } base64 = { workspace = true } bytes = { workspace = true } @@ -62,7 +60,6 @@ bundled-ca-roots = ["dep:webpki-roots"] [dev-dependencies] openshell-ocsf = { path = "../openshell-ocsf", features = ["test-support"] } -openshell-supervisor-middleware-builtins = { path = "../openshell-supervisor-middleware-builtins" } tonic = { workspace = true } temp-env = "0.3" tokio = { workspace = true, features = ["test-util"] } diff --git a/crates/openshell-supervisor-network/src/l7/middleware.rs b/crates/openshell-supervisor-network/src/l7/middleware.rs index c469ab523..8f3a4b3b3 100644 --- a/crates/openshell-supervisor-network/src/l7/middleware.rs +++ b/crates/openshell-supervisor-network/src/l7/middleware.rs @@ -5,17 +5,29 @@ use crate::l7::relay::L7EvalContext; use crate::opa::PolicyGenerationGuard; -use miette::{Result, miette}; +use miette::{IntoDiagnostic, Result, miette}; use openshell_ocsf::{ ActionId, ActivityId, DetectionFindingBuilder, DispositionId, Endpoint, FindingInfo, HttpActivityBuilder, HttpRequest, NetworkActivityBuilder, SeverityId, StatusId, Url as OcsfUrl, ocsf_emit, }; +use std::io::SeekFrom; use std::path::PathBuf; -use tokio::io::{AsyncRead, AsyncWrite}; +use std::time::Duration; +use tokio::io::{AsyncRead, AsyncSeekExt, AsyncWrite, AsyncWriteExt}; + +/// Maximum wall-clock time spent receiving, evaluating, and spooling one +/// request body before the supervisor cancels the middleware session. +// Keep `from_secs` while the workspace MSRV predates `Duration::from_mins`. +#[allow(clippy::duration_suboptimal_units)] +pub const DEFAULT_HTTP_REQUEST_BODY_TIMEOUT: Duration = Duration::from_secs(120); pub enum MiddlewareApplyResult { Allowed(crate::l7::provider::L7Request), + Streamed { + request: crate::l7::provider::L7Request, + body: MiddlewareRequestBody, + }, Denied { denial: Option, }, @@ -26,6 +38,43 @@ pub enum MiddlewareApplyResult { AdmissionExhausted, } +/// Storage-backed body produced by request middleware. The file is rewound and +/// contains normalized bytes without HTTP transfer framing. +pub struct RequestBodySpool { + pub(crate) file: tokio::fs::File, + pub(crate) len: u64, + pub(crate) trailers: Vec, +} + +/// Body delivery selected after all request-middleware preflights complete. +pub enum MiddlewareRequestBody { + /// A whole-body or ownership barrier completed before upstream contact. + Spool(RequestBodySpool), + /// Every active body stage selected unit-local streaming, so approved + /// units can be released under network backpressure. + Live(Box), +} + +/// Whether otherwise incremental request middleware must retain the complete +/// representation for a later body-dependent policy or credential stage. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum RequestBodyDelivery { + #[default] + Incremental, + Withhold, +} + +pub struct RequestBodyStream { + pub(crate) reader: crate::l7::rest::RequestBodyReader, + session: Option, + preflight: openshell_supervisor_middleware::HttpRequestPreflightOutcome, + request: crate::l7::provider::L7Request, + context: L7EvalContext, + generation_guard: PolicyGenerationGuard, + deadline: tokio::time::Instant, + terminal_emitted: bool, +} + /// One destination-selected middleware chain shared by an HTTP request and /// its matching response. The request and response phases filter bindings /// independently, so the full chain must remain available until relay ends. @@ -63,7 +112,30 @@ impl HttpMiddlewareExchange { where C: AsyncRead + AsyncWrite + Unpin + Send, { - apply_middleware_chain_for_scheme_with_request_id( + self.apply_request_with_delivery( + request, + client, + ctx, + scheme, + transformed_body_policy, + RequestBodyDelivery::Incremental, + ) + .await + } + + pub async fn apply_request_with_delivery( + &self, + request: crate::l7::provider::L7Request, + client: &mut C, + ctx: &L7EvalContext, + scheme: &str, + transformed_body_policy: openshell_supervisor_middleware::TransformedBodyPolicy<'_>, + delivery: RequestBodyDelivery, + ) -> Result + where + C: AsyncRead + AsyncWrite + Unpin + Send, + { + apply_middleware_chain_for_scheme_with_request_id_and_delivery( request, client, ctx, @@ -73,6 +145,7 @@ impl HttpMiddlewareExchange { &self.generation_guard, transformed_body_policy, &self.request_id, + delivery, ) .await } @@ -517,7 +590,35 @@ pub async fn apply_middleware_chain_with_request_id, request_id: &str, ) -> Result { - apply_middleware_chain_for_scheme_with_request_id( + apply_middleware_chain_with_request_id_and_delivery( + req, + client, + ctx, + chain, + runner, + generation_guard, + transformed_body_policy, + request_id, + RequestBodyDelivery::Incremental, + ) + .await +} + +#[allow(clippy::too_many_arguments)] +pub async fn apply_middleware_chain_with_request_id_and_delivery< + C: AsyncRead + AsyncWrite + Unpin + Send, +>( + req: crate::l7::provider::L7Request, + client: &mut C, + ctx: &L7EvalContext, + chain: Vec, + runner: &openshell_supervisor_middleware::ChainRunner, + generation_guard: &PolicyGenerationGuard, + transformed_body_policy: openshell_supervisor_middleware::TransformedBodyPolicy<'_>, + request_id: &str, + delivery: RequestBodyDelivery, +) -> Result { + apply_middleware_chain_for_scheme_with_request_id_and_delivery( req, client, ctx, @@ -527,11 +628,13 @@ pub async fn apply_middleware_chain_with_request_id( @@ -544,16 +647,90 @@ pub async fn apply_middleware_chain_for_scheme_with_request_id< generation_guard: &PolicyGenerationGuard, transformed_body_policy: openshell_supervisor_middleware::TransformedBodyPolicy<'_>, request_id: &str, +) -> Result { + apply_middleware_chain_for_scheme_with_request_id_and_delivery( + req, + client, + ctx, + scheme, + chain, + runner, + generation_guard, + transformed_body_policy, + request_id, + RequestBodyDelivery::Incremental, + ) + .await +} + +#[allow(clippy::too_many_arguments)] +pub async fn apply_middleware_chain_for_scheme_with_request_id_and_delivery< + C: AsyncRead + AsyncWrite + Unpin + Send, +>( + req: crate::l7::provider::L7Request, + client: &mut C, + ctx: &L7EvalContext, + scheme: &str, + chain: Vec, + runner: &openshell_supervisor_middleware::ChainRunner, + generation_guard: &PolicyGenerationGuard, + transformed_body_policy: openshell_supervisor_middleware::TransformedBodyPolicy<'_>, + request_id: &str, + delivery: RequestBodyDelivery, ) -> Result { if chain.is_empty() { return Ok(MiddlewareApplyResult::Allowed(req)); } let chain = runner.describe_chain(&chain).await?; + if matches!( + transformed_body_policy, + openshell_supervisor_middleware::TransformedBodyPolicy::Reevaluate(_) + ) { + return apply_buffered_middleware_chain( + req, + client, + ctx, + scheme, + chain, + runner, + generation_guard, + transformed_body_policy, + request_id, + ) + .await; + } + + apply_streaming_middleware_chain( + req, + client, + ctx, + scheme, + chain, + runner, + generation_guard, + request_id, + delivery, + ) + .await +} + +#[allow(clippy::too_many_arguments)] +async fn apply_buffered_middleware_chain( + req: crate::l7::provider::L7Request, + client: &mut C, + ctx: &L7EvalContext, + scheme: &str, + chain: Vec, + runner: &openshell_supervisor_middleware::ChainRunner, + generation_guard: &PolicyGenerationGuard, + transformed_body_policy: openshell_supervisor_middleware::TransformedBodyPolicy<'_>, + request_id: &str, +) -> Result { let admission = if chain.is_empty() { None } else { - let outcome = runner.reserve_middleware_work().await?; - match outcome { + let work_admission = runner.reserve_middleware_work().await?; + match work_admission { openshell_supervisor_middleware::MiddlewareWorkAdmissionOutcome::Admitted( admission, ) => Some(admission), @@ -564,10 +741,6 @@ pub async fn apply_middleware_chain_for_scheme_with_request_id< } }; let Some(max_body_bytes) = middleware_chain_body_limit(&chain) else { - // No entry resolved to a registered binding, so nothing inspects the - // body. Apply each entry's `on_error` policy without buffering (an - // unresolved binding is handled before the body is read) and forward - // the original request unchanged if the chain allows. let input = middleware_request_input_with_id( openshell_ocsf::ctx::ctx(), scheme, @@ -596,18 +769,15 @@ pub async fn apply_middleware_chain_for_scheme_with_request_id< } }); }; - // Admission was reserved above, before reading any request body. Keeping - // the guard through evaluation bounds aggregate buffered input across HTTP - // requests and WebSocket messages. let admission = admission.expect("resolved middleware chain reserved work admission"); - let buffered = match crate::l7::rest::buffer_request_body_for_middleware( + let buffer_result = crate::l7::rest::buffer_request_body_for_middleware( &req, client, Some(generation_guard), max_body_bytes, ) - .await? - { + .await?; + let buffered = match buffer_result { crate::l7::rest::BufferResult::Buffered(buffered) => buffered, crate::l7::rest::BufferResult::OverCapacity { recoverable } => { return Ok(resolve_unbuffered_body(ctx, req, &chain, recoverable)); @@ -626,9 +796,6 @@ pub async fn apply_middleware_chain_for_scheme_with_request_id< buffered.body, request_id, ); - // The explicitly selected transformation policy either re-checks every - // replacement or documents that this protocol's policy is body-independent. - // An ALLOW outcome therefore means the final body is policy-compliant. let outcome = runner .evaluate_described_with_policy_admitted( &chain, @@ -652,6 +819,607 @@ pub async fn apply_middleware_chain_for_scheme_with_request_id< Ok(MiddlewareApplyResult::Allowed(rebuilt)) } +#[allow(clippy::too_many_arguments)] +async fn apply_streaming_middleware_chain( + req: crate::l7::provider::L7Request, + client: &mut C, + ctx: &L7EvalContext, + scheme: &str, + chain: Vec, + runner: &openshell_supervisor_middleware::ChainRunner, + generation_guard: &PolicyGenerationGuard, + request_id: &str, + delivery: RequestBodyDelivery, +) -> Result { + let header_end = req + .raw_header + .windows(4) + .position(|window| window == b"\r\n\r\n") + .map_or(req.raw_header.len(), |position| position + 4); + let original_headers = &req.raw_header[..header_end]; + let headers = safe_middleware_headers(original_headers)?; + let query = raw_query_from_request_headers(original_headers)?; + let sandbox = openshell_ocsf::ctx::ctx(); + let input = openshell_supervisor_middleware::HttpRequestPreflightInput { + context: openshell_core::proto::RequestContext { + request_id: request_id.to_string(), + sandbox_id: sandbox.sandbox_id.clone(), + sandbox_name: sandbox.sandbox_name.clone(), + workspace: ctx.workspace.clone(), + originating_process: None, + }, + target: openshell_core::proto::HttpRequestTarget { + scheme: scheme.to_string(), + host: ctx.host.clone(), + port: u32::from(ctx.port), + method: req.action.clone(), + path: req.target.clone(), + query, + }, + declared_body_length: match req.body_length { + crate::l7::provider::BodyLength::ContentLength(length) => Some(length), + crate::l7::provider::BodyLength::None => Some(0), + crate::l7::provider::BodyLength::Chunked => None, + }, + headers: headers + .visible + .into_iter() + .map(|(name, value)| openshell_core::proto::HttpHeader { name, value }) + .collect(), + connection_nominated_headers: headers.connection_nominated, + }; + let mut preflight = runner + .preflight_described_http_request(chain, input) + .await?; + if preflight.session_capacity_exhausted { + emit_middleware_admission_exhausted(ctx); + return Ok(MiddlewareApplyResult::AdmissionExhausted); + } + if !preflight.allowed { + emit_streaming_middleware_events( + ctx, + &req, + false, + &preflight.reason, + preflight.denial.as_ref(), + &preflight.findings, + &preflight.metadata, + &preflight.invocations, + false, + ); + return Ok(MiddlewareApplyResult::Denied { + denial: preflight.denial, + }); + } + + let Some(mut session) = preflight.session.take() else { + let rebuilt = + crate::l7::rest::rebuild_request_headers_only(&req, &preflight.header_mutations)?; + emit_streaming_middleware_events( + ctx, + &req, + true, + "", + None, + &preflight.findings, + &preflight.metadata, + &preflight.invocations, + !preflight.header_mutations.is_empty(), + ); + return Ok(MiddlewareApplyResult::Allowed(rebuilt)); + }; + + let (prepared_headers, mut body_reader) = + crate::l7::rest::prepare_request_body_stream(&req, client).await?; + + if delivery == RequestBodyDelivery::Incremental + && !session.requires_withholding() + && !matches!(req.body_length, crate::l7::provider::BodyLength::None) + { + let rebuilt = crate::l7::rest::rebuild_request_for_incremental_stream( + &req, + &prepared_headers, + &preflight.header_mutations, + )?; + let request = crate::l7::provider::L7Request { + action: req.action.clone(), + target: req.target.clone(), + query_params: req.query_params.clone(), + raw_header: req.raw_header.clone(), + body_length: req.body_length, + }; + return Ok(MiddlewareApplyResult::Streamed { + request: rebuilt, + body: MiddlewareRequestBody::Live(Box::new(RequestBodyStream { + reader: body_reader, + session: Some(session), + preflight, + request, + context: ctx.clone(), + generation_guard: generation_guard.clone(), + deadline: tokio::time::Instant::now() + DEFAULT_HTTP_REQUEST_BODY_TIMEOUT, + terminal_emitted: false, + })), + }); + } + + let std_file = tempfile::tempfile().into_diagnostic()?; + let mut file = tokio::fs::File::from_std(std_file); + let mut output_len = 0u64; + let mut body_invocations = Vec::new(); + let mut body_findings = Vec::new(); + let mut body_metadata = std::collections::BTreeMap::new(); + let body_deadline = tokio::time::Instant::now() + DEFAULT_HTTP_REQUEST_BODY_TIMEOUT; + + loop { + let next_unit = if let Ok(result) = tokio::time::timeout_at( + body_deadline, + body_reader.next_unit(client, Some(generation_guard), session.stream_unit_limit()), + ) + .await + { + result? + } else { + let diagnostics = session.take_diagnostics(); + session + .end(openshell_core::proto::MiddlewareSessionEndReason::Cancellation) + .await; + emit_request_body_timeout(ctx, &req, &preflight, diagnostics); + return Ok(MiddlewareApplyResult::Denied { denial: None }); + }; + let Some(unit) = next_unit else { + break; + }; + let pushed = tokio::time::timeout_at(body_deadline, session.push_body(unit)).await; + match pushed { + Err(_) => { + let diagnostics = session.take_diagnostics(); + session + .end(openshell_core::proto::MiddlewareSessionEndReason::Cancellation) + .await; + emit_request_body_timeout(ctx, &req, &preflight, diagnostics); + return Ok(MiddlewareApplyResult::Denied { denial: None }); + } + Ok(Ok(units)) => { + write_spooled_units(&mut file, &mut output_len, units).await?; + } + Ok(Err(error)) => { + body_invocations.extend(error.diagnostics.invocations); + body_findings.extend(error.diagnostics.findings); + body_metadata.extend(error.diagnostics.metadata); + let mut invocations = preflight.invocations; + invocations.extend(body_invocations); + let mut findings = preflight.findings; + findings.extend(body_findings); + let mut metadata = preflight.metadata; + metadata.extend(body_metadata); + emit_streaming_middleware_events( + ctx, + &req, + false, + &error.reason, + error.denial.as_ref(), + &findings, + &metadata, + &invocations, + false, + ); + return Ok(MiddlewareApplyResult::Denied { + denial: error.denial, + }); + } + } + } + let trailers = body_reader.take_trailers(); + let (output_tx, mut output_rx) = tokio::sync::mpsc::channel(4); + let finish_future = session.finish_to(trailers, output_tx); + let writer_future = async { + while let Some(unit) = output_rx.recv().await { + write_spooled_units(&mut file, &mut output_len, vec![unit]).await?; + } + Ok::<(), miette::Report>(()) + }; + let completed = tokio::time::timeout_at(body_deadline, async { + let (finish, writer) = tokio::join!(finish_future, writer_future); + writer?; + Ok::<_, miette::Report>(finish) + }) + .await; + let finish = if let Ok(completed) = completed { + completed? + } else { + emit_request_body_timeout( + ctx, + &req, + &preflight, + openshell_supervisor_middleware::HttpRequestDiagnostics::default(), + ); + return Ok(MiddlewareApplyResult::Denied { denial: None }); + }; + let finish = match finish { + Ok(finish) => finish, + Err(error) => { + let mut invocations = preflight.invocations; + invocations.extend(error.diagnostics.invocations); + let mut findings = preflight.findings; + findings.extend(error.diagnostics.findings); + let mut metadata = preflight.metadata; + metadata.extend(error.diagnostics.metadata); + emit_streaming_middleware_events( + ctx, + &req, + false, + &error.reason, + error.denial.as_ref(), + &findings, + &metadata, + &invocations, + false, + ); + return Ok(MiddlewareApplyResult::Denied { + denial: error.denial, + }); + } + }; + generation_guard.ensure_current()?; + file.flush().await.into_diagnostic()?; + file.seek(SeekFrom::Start(0)).await.into_diagnostic()?; + + let rebuilt = crate::l7::rest::rebuild_request_with_streamed_body( + &req, + &prepared_headers, + output_len, + &finish.trailers, + &preflight.header_mutations, + )?; + let mut invocations = preflight.invocations; + invocations.extend(finish.invocations); + let mut findings = preflight.findings; + findings.extend(finish.findings); + let mut metadata = preflight.metadata; + metadata.extend(finish.metadata); + emit_streaming_middleware_events( + ctx, + &req, + true, + "", + None, + &findings, + &metadata, + &invocations, + finish.body_transformed || !preflight.header_mutations.is_empty(), + ); + Ok(MiddlewareApplyResult::Streamed { + request: rebuilt, + body: MiddlewareRequestBody::Spool(RequestBodySpool { + file, + len: output_len, + trailers: finish.trailers, + }), + }) +} + +impl RequestBodyStream { + /// Run an incremental request session into a bounded output channel. + /// + /// The HTTP relay drains the channel directly into the upstream socket, + /// so channel and socket backpressure bound supervisor memory without a + /// whole-request storage cap. + pub(crate) async fn run_to( + &mut self, + client: &mut C, + output: tokio::sync::mpsc::Sender>, + ) -> Result + where + C: AsyncRead + Unpin, + { + loop { + let unit_limit = self.session.as_ref().map_or( + 1, + openshell_supervisor_middleware::HttpRequestSession::stream_unit_limit, + ); + let next = tokio::time::timeout_at( + self.deadline, + self.reader + .next_unit(client, Some(&self.generation_guard), unit_limit), + ) + .await; + let unit = match next { + Err(_) => { + self.cancel_with_diagnostics( + "middleware_failed: request_body_timeout", + openshell_supervisor_middleware::HttpRequestDiagnostics::default(), + ) + .await; + return Err(miette!("request middleware body deadline exceeded")); + } + Ok(Err(error)) => { + self.cancel_with_diagnostics( + "middleware_failed: request_body_read_failed", + openshell_supervisor_middleware::HttpRequestDiagnostics::default(), + ) + .await; + return Err(error); + } + Ok(Ok(None)) => break, + Ok(Ok(Some(unit))) => unit, + }; + + let pushed = { + let session = self + .session + .as_mut() + .ok_or_else(|| miette!("request middleware session is unavailable"))?; + tokio::time::timeout_at(self.deadline, session.push_body(unit)).await + }; + let units = match pushed { + Err(_) => { + let diagnostics = self + .session + .as_mut() + .map(openshell_supervisor_middleware::HttpRequestSession::take_diagnostics) + .unwrap_or_default(); + self.cancel_with_diagnostics( + "middleware_failed: request_body_timeout", + diagnostics, + ) + .await; + return Err(miette!("request middleware body deadline exceeded")); + } + Ok(Err(error)) => { + let reason = error.reason.clone(); + let denial = error.denial.clone(); + self.emit_failure(&reason, denial.as_ref(), *error.diagnostics); + self.session.take(); + return Err(miette!("{reason}")); + } + Ok(Ok(units)) => units, + }; + for unit in units { + match tokio::time::timeout_at(self.deadline, output.send(unit)).await { + Err(_) => { + let diagnostics = self + .session + .as_mut() + .map( + openshell_supervisor_middleware::HttpRequestSession::take_diagnostics, + ) + .unwrap_or_default(); + self.cancel_with_diagnostics( + "middleware_failed: request_body_timeout", + diagnostics, + ) + .await; + return Err(miette!("request middleware body deadline exceeded")); + } + Ok(Err(_)) => { + self.cancel_with_diagnostics( + "middleware_failed: request_output_closed", + openshell_supervisor_middleware::HttpRequestDiagnostics::default(), + ) + .await; + return Err(miette!("request middleware output consumer closed")); + } + Ok(Ok(())) => {} + } + } + } + + let trailers = self.reader.take_trailers(); + let session = self + .session + .take() + .ok_or_else(|| miette!("request middleware session is unavailable"))?; + let finish = + tokio::time::timeout_at(self.deadline, session.finish_to(trailers, output)).await; + match finish { + Err(_) => { + self.emit_failure( + "middleware_failed: request_body_timeout", + None, + openshell_supervisor_middleware::HttpRequestDiagnostics::default(), + ); + Err(miette!("request middleware body deadline exceeded")) + } + Ok(Err(error)) => { + let reason = error.reason.clone(); + let denial = error.denial.clone(); + self.emit_failure(&reason, denial.as_ref(), *error.diagnostics); + Err(miette!("{reason}")) + } + Ok(Ok(finish)) => { + self.emit_success(&finish); + Ok(finish) + } + } + } + + /// Terminate a live session after an upstream response wins the race with + /// request upload. Unread client bytes make the downstream connection + /// non-reusable, and no request replay is attempted. + pub(crate) async fn cancel_for_early_response(&mut self) { + let diagnostics = self + .session + .as_mut() + .map(openshell_supervisor_middleware::HttpRequestSession::take_diagnostics) + .unwrap_or_default(); + self.cancel_with_diagnostics("middleware_cancelled: upstream_response", diagnostics) + .await; + } + + async fn cancel_with_diagnostics( + &mut self, + reason: &str, + diagnostics: openshell_supervisor_middleware::HttpRequestDiagnostics, + ) { + if let Some(session) = self.session.take() { + session + .end(openshell_core::proto::MiddlewareSessionEndReason::Cancellation) + .await; + } + self.emit_failure(reason, None, diagnostics); + } + + fn emit_success(&mut self, finish: &openshell_supervisor_middleware::HttpRequestFinish) { + if self.terminal_emitted { + return; + } + let mut invocations = self.preflight.invocations.clone(); + invocations.extend(finish.invocations.clone()); + let mut findings = self.preflight.findings.clone(); + findings.extend(finish.findings.clone()); + let mut metadata = self.preflight.metadata.clone(); + metadata.extend(finish.metadata.clone()); + emit_streaming_middleware_events( + &self.context, + &self.request, + true, + "", + None, + &findings, + &metadata, + &invocations, + finish.body_transformed || !self.preflight.header_mutations.is_empty(), + ); + self.terminal_emitted = true; + } + + fn emit_failure( + &mut self, + reason: &str, + denial: Option<&openshell_supervisor_middleware::MiddlewareDenial>, + diagnostics: openshell_supervisor_middleware::HttpRequestDiagnostics, + ) { + if self.terminal_emitted { + return; + } + let mut invocations = self.preflight.invocations.clone(); + invocations.extend(diagnostics.invocations); + let mut findings = self.preflight.findings.clone(); + findings.extend(diagnostics.findings); + let mut metadata = self.preflight.metadata.clone(); + metadata.extend(diagnostics.metadata); + emit_streaming_middleware_events( + &self.context, + &self.request, + false, + reason, + denial, + &findings, + &metadata, + &invocations, + false, + ); + self.terminal_emitted = true; + } +} + +fn emit_request_body_timeout( + ctx: &L7EvalContext, + req: &crate::l7::provider::L7Request, + preflight: &openshell_supervisor_middleware::HttpRequestPreflightOutcome, + diagnostics: openshell_supervisor_middleware::HttpRequestDiagnostics, +) { + let mut invocations = preflight.invocations.clone(); + invocations.extend(diagnostics.invocations); + let mut findings = preflight.findings.clone(); + findings.extend(diagnostics.findings); + let mut metadata = preflight.metadata.clone(); + metadata.extend(diagnostics.metadata); + emit_streaming_middleware_events( + ctx, + req, + false, + "middleware_failed: request_body_timeout", + None, + &findings, + &metadata, + &invocations, + false, + ); +} + +async fn write_spooled_units( + file: &mut tokio::fs::File, + output_len: &mut u64, + units: Vec>, +) -> Result<()> { + for unit in units { + *output_len = output_len + .checked_add(unit.len() as u64) + .ok_or_else(|| miette!("middleware request output length overflow"))?; + if *output_len > openshell_supervisor_middleware::MAX_HTTP_REQUEST_DEFERRED_BYTES as u64 { + return Err(miette!( + "middleware request output exceeds platform storage limit" + )); + } + file.write_all(&unit).await.into_diagnostic()?; + } + Ok(()) +} + +#[allow(clippy::too_many_arguments)] +fn emit_streaming_middleware_events( + ctx: &L7EvalContext, + req: &crate::l7::provider::L7Request, + allowed: bool, + reason: &str, + denial: Option<&openshell_supervisor_middleware::MiddlewareDenial>, + findings: &[openshell_supervisor_middleware::NamespacedFinding], + metadata: &std::collections::BTreeMap>, + invocations: &[openshell_supervisor_middleware::HttpRequestInvocation], + transformed: bool, +) { + let mut applied = Vec::::new(); + for invocation in invocations { + if let Some(existing) = applied + .iter_mut() + .find(|existing| existing.name == invocation.config_name) + { + existing.failed |= invocation.failed; + existing.transformed |= matches!( + invocation.outcome, + openshell_supervisor_middleware::HttpRequestInvocationOutcome::Transform + | openshell_supervisor_middleware::HttpRequestInvocationOutcome::OwnedStream + ); + if matches!( + invocation.outcome, + openshell_supervisor_middleware::HttpRequestInvocationOutcome::BlockRequest + | openshell_supervisor_middleware::HttpRequestInvocationOutcome::FailClosed + ) { + existing.decision = openshell_core::proto::Decision::Deny; + } + continue; + } + applied.push(openshell_supervisor_middleware::MiddlewareInvocation { + name: invocation.config_name.clone(), + implementation: invocation.implementation.clone(), + decision: if matches!( + invocation.outcome, + openshell_supervisor_middleware::HttpRequestInvocationOutcome::BlockRequest + | openshell_supervisor_middleware::HttpRequestInvocationOutcome::FailClosed + ) { + openshell_core::proto::Decision::Deny + } else { + openshell_core::proto::Decision::Allow + }, + transformed, + failed: invocation.failed, + }); + } + let outcome = openshell_supervisor_middleware::ChainOutcome { + allowed, + reason: reason.to_string(), + body: Vec::new(), + header_mutations: Vec::new(), + findings: findings.to_vec(), + metadata: metadata.clone(), + applied, + denial: denial.cloned(), + }; + emit_middleware_events(ctx, req, &outcome); +} + pub async fn send_middleware_rejection_response( req: &crate::l7::provider::L7Request, client: &mut C, diff --git a/crates/openshell-supervisor-network/src/l7/mod.rs b/crates/openshell-supervisor-network/src/l7/mod.rs index e51422dbc..7fd7ba5ee 100644 --- a/crates/openshell-supervisor-network/src/l7/mod.rs +++ b/crates/openshell-supervisor-network/src/l7/mod.rs @@ -14,6 +14,7 @@ pub mod jsonrpc; pub(crate) mod mcp; pub(crate) mod middleware; pub mod path; +pub(crate) mod post_credentials; pub mod provider; pub mod relay; pub mod rest; diff --git a/crates/openshell-supervisor-network/src/l7/post_credentials.rs b/crates/openshell-supervisor-network/src/l7/post_credentials.rs new file mode 100644 index 000000000..aadf99133 --- /dev/null +++ b/crates/openshell-supervisor-network/src/l7/post_credentials.rs @@ -0,0 +1,371 @@ +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +//! Restricted in-process request middleware that runs after credential +//! resolution. External middleware can never enter this phase. + +use std::io::SeekFrom; + +use miette::{IntoDiagnostic as _, Result, miette}; +use openshell_core::secrets::SecretResolver; +use openshell_supervisor_middleware_builtins::sigv4::{ + BodyFraming as SigV4BodyFraming, PayloadMode as SigV4PayloadMode, RequestedPayloadMode, + SigV4Middleware, SigningCredentials, SigningTarget, +}; +use tokio::io::{AsyncRead, AsyncReadExt as _, AsyncSeekExt as _, AsyncWrite, AsyncWriteExt as _}; + +use crate::l7::middleware::RequestBodySpool; +use crate::l7::provider::{BodyLength, L7Request}; +use crate::opa::PolicyGenerationGuard; + +/// A trusted built-in synthesized from endpoint policy. It is deliberately not +/// representable by an operator-run middleware registration. +#[derive(Clone, Copy, Debug)] +pub enum PostCredentialsMiddleware<'a> { + SigV4 { + requested_mode: RequestedPayloadMode, + service: &'a str, + region: &'a str, + host: &'a str, + port: u16, + }, +} + +/// Result of the post-credentials stage. A complete request already contains +/// its body; a signed head leaves normalized body relay to the HTTP owner. +pub enum PostCredentialsOutput { + Complete { + request: Vec, + payload_mode: SigV4PayloadMode, + }, + Head { + headers: Vec, + payload_mode: SigV4PayloadMode, + }, +} + +impl<'a> PostCredentialsMiddleware<'a> { + pub(crate) fn from_endpoint( + signing: crate::l7::CredentialSigning, + service: &'a str, + region: &'a str, + host: &'a str, + port: u16, + ) -> Option { + let requested_mode = match signing { + crate::l7::CredentialSigning::None => return None, + crate::l7::CredentialSigning::SigV4 => RequestedPayloadMode::Auto, + crate::l7::CredentialSigning::SigV4Body => RequestedPayloadMode::SignBody, + crate::l7::CredentialSigning::SigV4NoBody => RequestedPayloadMode::UnsignedPayload, + }; + Some(Self::SigV4 { + requested_mode, + service, + region, + host, + port, + }) + } + + /// Remove caller-provided authorization fields before placeholder + /// rewriting. The trusted stage regenerates them after credentials resolve. + pub(crate) fn strip_caller_authorization(self, raw_headers: &[u8]) -> Result> { + match self { + Self::SigV4 { .. } => SigV4Middleware::strip_existing_auth(raw_headers), + } + } + + #[allow(clippy::too_many_arguments)] + pub(crate) async fn evaluate( + self, + request: &L7Request, + original_headers: &str, + rewritten_headers: &[u8], + client: &mut C, + prepared_body: Option<&mut RequestBodySpool>, + resolver: Option<&SecretResolver>, + generation_guard: Option<&PolicyGenerationGuard>, + ) -> Result + where + C: AsyncRead + AsyncWrite + Unpin, + { + match self { + Self::SigV4 { + requested_mode, + service, + region, + host, + .. + } => { + let resolver = resolver.ok_or_else(|| { + miette::Report::new(super::rest::CredentialUnavailableError::new( + "SigV4 signing configured but no secret resolver available", + )) + })?; + let access_key = resolver + .resolve_current_env_key_checked("AWS_ACCESS_KEY_ID", "sigv4") + .map_err(miette::Report::new)?; + let secret_key = resolver + .resolve_current_env_key_checked("AWS_SECRET_ACCESS_KEY", "sigv4") + .map_err(miette::Report::new)?; + let session_token = resolver + .resolve_current_env_key_checked("AWS_SESSION_TOKEN", "sigv4") + .map_err(miette::Report::new)?; + let (Some(access_key), Some(secret_key)) = (access_key, secret_key) else { + return Err(miette::Report::new( + super::rest::CredentialUnavailableError::new( + "SigV4 signing configured but AWS credentials not found in provider", + ), + )); + }; + + if service.is_empty() { + return Err(miette!( + "SigV4 signing configured but signing_service not set in policy" + )); + } + let resolved_region = if region.is_empty() { + openshell_supervisor_middleware_builtins::sigv4::extract_aws_region(host) + .ok_or_else(|| { + miette!( + "SigV4 signing: cannot extract AWS region from hostname \ + '{host}'; set signing_region in the policy endpoint" + ) + })? + } else { + region.to_string() + }; + + let framing = sigv4_body_framing(request.body_length); + let payload_mode = + openshell_supervisor_middleware_builtins::sigv4::resolve_payload_mode( + requested_mode, + original_headers, + framing, + )?; + let target = SigningTarget { + host, + region: &resolved_region, + service, + }; + let credentials = SigningCredentials { + access_key, + secret_key, + session_token, + }; + + if payload_mode == SigV4PayloadMode::SignBody { + if matches!(request.body_length, BodyLength::Chunked) { + return Err(miette!( + "SigV4 body signing requires Content-Length; chunked transfer \ + encoding is not supported in this mode" + )); + } + let body = + collect_body_for_signing(request, client, prepared_body, generation_guard) + .await?; + let mut complete = Vec::with_capacity(rewritten_headers.len() + body.len()); + complete.extend_from_slice(rewritten_headers); + complete.extend_from_slice(&body); + let request = SigV4Middleware::sign_body(&complete, target, credentials)?; + Ok(PostCredentialsOutput::Complete { + request, + payload_mode, + }) + } else { + let headers = SigV4Middleware::sign_headers( + rewritten_headers, + target, + credentials, + payload_mode, + )?; + Ok(PostCredentialsOutput::Head { + headers, + payload_mode, + }) + } + } + } + } + + pub(crate) fn emit_success(self, payload_mode: SigV4PayloadMode) { + match self { + Self::SigV4 { + service, + region, + host, + port, + .. + } => { + let resolved_region = if region.is_empty() { + openshell_supervisor_middleware_builtins::sigv4::extract_aws_region(host) + .unwrap_or_else(|| "unknown".into()) + } else { + region.to_string() + }; + let event = openshell_ocsf::NetworkActivityBuilder::new(openshell_ocsf::ctx::ctx()) + .activity(openshell_ocsf::ActivityId::Traffic) + .action(openshell_ocsf::ActionId::Allowed) + .disposition(openshell_ocsf::DispositionId::Allowed) + .severity(openshell_ocsf::SeverityId::Informational) + .status(openshell_ocsf::StatusId::Success) + .dst_endpoint(openshell_ocsf::Endpoint::from_domain(host, port)) + .message(format!( + "openshell/sigv4 signed {host}:{port} service={service} \ + region={resolved_region} mode={payload_mode}" + )) + .build(); + openshell_ocsf::ocsf_emit!(event); + } + } + } +} + +fn sigv4_body_framing(body_length: BodyLength) -> SigV4BodyFraming { + match body_length { + BodyLength::None => SigV4BodyFraming::None, + BodyLength::ContentLength(_) => SigV4BodyFraming::ContentLength, + BodyLength::Chunked => SigV4BodyFraming::Chunked, + } +} + +async fn collect_body_for_signing( + request: &L7Request, + client: &mut C, + prepared_body: Option<&mut RequestBodySpool>, + generation_guard: Option<&PolicyGenerationGuard>, +) -> Result> +where + C: AsyncRead + AsyncWrite + Unpin, +{ + use openshell_supervisor_middleware_builtins::sigv4::MAX_BODY_BYTES; + + if let Some(body) = prepared_body { + if body.len > MAX_BODY_BYTES as u64 { + return Err(miette!( + "SigV4 body signing buffers at most {MAX_BODY_BYTES} bytes" + )); + } + if !body.trailers.is_empty() { + return Err(miette!( + "SigV4 body signing does not support request trailers" + )); + } + body.file.seek(SeekFrom::Start(0)).await.into_diagnostic()?; + let capacity = usize::try_from(body.len) + .map_err(|_| miette!("SigV4 middleware body does not fit addressable memory"))?; + let mut bytes = Vec::with_capacity(capacity); + body.file.read_to_end(&mut bytes).await.into_diagnostic()?; + if bytes.len() as u64 != body.len { + return Err(miette!("middleware request spool ended early")); + } + return Ok(bytes); + } + + if has_expect_continue(original_header_text(request)?) { + client + .write_all(b"HTTP/1.1 100 Continue\r\n\r\n") + .await + .into_diagnostic()?; + client.flush().await.into_diagnostic()?; + } + + let header_end = request_header_end(request); + let overflow = &request.raw_header[header_end..]; + match request.body_length { + BodyLength::None => { + if !overflow.is_empty() { + return Err(miette!("bodyless SigV4 request contains read-ahead bytes")); + } + Ok(Vec::new()) + } + BodyLength::ContentLength(body_len) => { + if body_len > MAX_BODY_BYTES as u64 { + return Err(miette!( + "SigV4 body signing buffers at most {MAX_BODY_BYTES} bytes" + )); + } + if overflow.len() as u64 > body_len { + return Err(miette!( + "SigV4 request read-ahead exceeds its declared Content-Length" + )); + } + let body_len = usize::try_from(body_len) + .map_err(|_| miette!("SigV4 request body does not fit addressable memory"))?; + let mut body = Vec::with_capacity(body_len); + body.extend_from_slice(overflow); + let remaining = body_len - overflow.len(); + if remaining > 0 { + let start = body.len(); + body.resize(body_len, 0); + client + .read_exact(&mut body[start..]) + .await + .into_diagnostic()?; + } + if let Some(guard) = generation_guard { + guard.ensure_current()?; + } + Ok(body) + } + BodyLength::Chunked => Err(miette!( + "SigV4 body signing requires Content-Length; chunked transfer encoding is not supported" + )), + } +} + +fn request_header_end(request: &L7Request) -> usize { + request + .raw_header + .windows(4) + .position(|window| window == b"\r\n\r\n") + .map_or(request.raw_header.len(), |position| position + 4) +} + +fn original_header_text(request: &L7Request) -> Result<&str> { + std::str::from_utf8(&request.raw_header[..request_header_end(request)]) + .map_err(|_| miette!("SigV4 request headers are not valid UTF-8")) +} + +fn has_expect_continue(headers: &str) -> bool { + headers.lines().skip(1).any(|line| { + line.split_once(':').is_some_and(|(name, value)| { + name.eq_ignore_ascii_case("expect") + && value + .split(',') + .any(|token| token.trim().eq_ignore_ascii_case("100-continue")) + }) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn synthesizes_only_configured_post_credentials_middleware() { + assert!( + PostCredentialsMiddleware::from_endpoint( + crate::l7::CredentialSigning::None, + "", + "", + "example.com", + 443, + ) + .is_none() + ); + assert!(matches!( + PostCredentialsMiddleware::from_endpoint( + crate::l7::CredentialSigning::SigV4NoBody, + "s3", + "us-east-1", + "s3.us-east-1.amazonaws.com", + 443, + ), + Some(PostCredentialsMiddleware::SigV4 { + requested_mode: RequestedPayloadMode::UnsignedPayload, + .. + }) + )); + } +} diff --git a/crates/openshell-supervisor-network/src/l7/relay.rs b/crates/openshell-supervisor-network/src/l7/relay.rs index 2842562d9..ca6e1be3b 100644 --- a/crates/openshell-supervisor-network/src/l7/relay.rs +++ b/crates/openshell-supervisor-network/src/l7/relay.rs @@ -8,7 +8,8 @@ //! and either forwards or denies the request. use crate::l7::middleware::{ - MiddlewareApplyResult, UninspectableTrafficGate, apply_middleware_chain_with_request_id, + MiddlewareApplyResult, RequestBodyDelivery, UninspectableTrafficGate, + apply_middleware_chain_with_request_id, apply_middleware_chain_with_request_id_and_delivery, emit_middleware_uninspectable, middleware_network_input, uninspectable_traffic_gate, }; #[cfg(test)] @@ -446,6 +447,7 @@ async fn relay_http_request_with_credential_rejection( upstream: &mut U, options: crate::l7::rest::RelayRequestOptions<'_>, ctx: &L7EvalContext, + prepared_body: Option<&mut crate::l7::middleware::MiddlewareRequestBody>, response_middleware: Option>, ) -> Result> where @@ -458,18 +460,21 @@ where upstream, options, ctx, + prepared_body, response_middleware, None, ) .await } +#[allow(clippy::too_many_arguments)] async fn relay_http_request_with_credential_rejection_observed( request: &crate::l7::provider::L7Request, client: &mut C, upstream: &mut U, options: crate::l7::rest::RelayRequestOptions<'_>, ctx: &L7EvalContext, + prepared_body: Option<&mut crate::l7::middleware::MiddlewareRequestBody>, response_middleware: Option>, observer: Option<&EndpointObserver>, ) -> Result> @@ -483,6 +488,7 @@ where client, upstream, options, + prepared_body, response_middleware, observer, ), @@ -1085,8 +1091,14 @@ where &request_id, ) .await; - let req = match middleware_result? { + let middleware_result = middleware_result?; + let req = match middleware_result { MiddlewareApplyResult::Allowed(request) => request, + MiddlewareApplyResult::Streamed { .. } => { + return Err(miette!( + "body-aware middleware unexpectedly returned a streamed request" + )); + } MiddlewareApplyResult::Denied { denial, .. } => { if let Some(observer) = observer.as_ref() { observer.observe(EndpointResult::PolicyDenied); @@ -1200,13 +1212,19 @@ where && config.request_body_credential_rewrite, deny_uninspected_credentials: config .deny_uninspected_body_credentials(ctx.secret_resolver.is_some()), - credential_signing: config.credential_signing, - signing_service: &config.signing_service, - signing_region: &config.signing_region, + post_credentials: + crate::l7::post_credentials::PostCredentialsMiddleware::from_endpoint( + config.credential_signing, + &config.signing_service, + &config.signing_region, + &ctx.host, + ctx.port, + ), host: &ctx.host, port: ctx.port, }, ctx, + None, Some(http_response_middleware_relay( &req, ctx, @@ -1681,6 +1699,25 @@ fn jsonrpc_engine_type(protocol: L7Protocol) -> &'static str { } /// REST relay loop: parse request -> evaluate -> allow/deny -> relay response -> repeat. +fn request_body_delivery( + config: &L7EndpointConfig, + request: &crate::l7::provider::L7Request, +) -> RequestBodyDelivery { + let signing_needs_body = match config.credential_signing { + crate::l7::CredentialSigning::SigV4Body => true, + crate::l7::CredentialSigning::SigV4 => matches!( + request.body_length, + crate::l7::provider::BodyLength::ContentLength(_) + ), + crate::l7::CredentialSigning::None | crate::l7::CredentialSigning::SigV4NoBody => false, + }; + if signing_needs_body || config.request_body_credential_rewrite { + RequestBodyDelivery::Withhold + } else { + RequestBodyDelivery::Incremental + } +} + async fn relay_rest( config: &L7EndpointConfig, engine: &TunnelPolicyEngine, @@ -1839,7 +1876,8 @@ where // REST and websocket-upgrade policy evaluates only the method, // path, and query, which a middleware result cannot mutate, so no // per-stage body re-check is needed. - let middleware_result = apply_middleware_chain_with_request_id( + let request_delivery = request_body_delivery(config, &req); + let middleware_result = apply_middleware_chain_with_request_id_and_delivery( req, client, ctx, @@ -1848,10 +1886,13 @@ where engine.generation_guard(), openshell_supervisor_middleware::TransformedBodyPolicy::NotPolicyRelevant, &request_id, + request_delivery, ) .await; - let req = match middleware_result? { - MiddlewareApplyResult::Allowed(request) => request, + let middleware_result = middleware_result?; + let (req, mut prepared_body) = match middleware_result { + MiddlewareApplyResult::Allowed(request) => (request, None), + MiddlewareApplyResult::Streamed { request, body } => (request, Some(body)), MiddlewareApplyResult::Denied { denial, .. } => { let denied_request = crate::l7::provider::L7Request { action: request_info.action.clone(), @@ -1956,13 +1997,19 @@ where && config.request_body_credential_rewrite, deny_uninspected_credentials: config .deny_uninspected_body_credentials(ctx.secret_resolver.is_some()), - credential_signing: config.credential_signing, - signing_service: &config.signing_service, - signing_region: &config.signing_region, + post_credentials: + crate::l7::post_credentials::PostCredentialsMiddleware::from_endpoint( + config.credential_signing, + &config.signing_service, + &config.signing_region, + &ctx.host, + ctx.port, + ), host: &ctx.host, port: ctx.port, }, ctx, + prepared_body.as_mut(), Some(http_response_middleware_relay( &req_with_auth, ctx, @@ -2296,7 +2343,7 @@ where // stage so a middleware cannot smuggle a denied operation to the // upstream or the next stage. let validate = transformed_body_validator(config, engine, ctx, &request_info); - let req = match apply_middleware_chain_with_request_id( + let middleware_result = apply_middleware_chain_with_request_id( req, client, ctx, @@ -2306,9 +2353,14 @@ where openshell_supervisor_middleware::TransformedBodyPolicy::Reevaluate(&validate), &request_id, ) - .await? - { + .await?; + let req = match middleware_result { MiddlewareApplyResult::Allowed(request) => request, + MiddlewareApplyResult::Streamed { .. } => { + return Err(miette!( + "body-aware middleware unexpectedly returned a streamed request" + )); + } MiddlewareApplyResult::Denied { denial, .. } => { if let Some(observer) = observer.as_ref() { observer.observe(EndpointResult::PolicyDenied); @@ -2389,6 +2441,7 @@ where ..Default::default() }, ctx, + None, Some(http_response_middleware_relay( &req, ctx, @@ -2578,7 +2631,7 @@ where // stage so a middleware cannot smuggle a denied operation to the // upstream or the next stage. let validate = transformed_body_validator(config, engine, ctx, &request_info); - let req = match apply_middleware_chain_with_request_id( + let middleware_result = apply_middleware_chain_with_request_id( req, client, ctx, @@ -2588,9 +2641,14 @@ where openshell_supervisor_middleware::TransformedBodyPolicy::Reevaluate(&validate), &request_id, ) - .await? - { + .await?; + let req = match middleware_result { MiddlewareApplyResult::Allowed(request) => request, + MiddlewareApplyResult::Streamed { .. } => { + return Err(miette!( + "body-aware middleware unexpectedly returned a streamed request" + )); + } MiddlewareApplyResult::Denied { denial, .. } => { let denied_request = crate::l7::provider::L7Request { action: request_info.action.clone(), @@ -2641,6 +2699,7 @@ where ..Default::default() }, ctx, + None, Some(http_response_middleware_relay( &req, ctx, @@ -3204,6 +3263,7 @@ where let request_id = uuid::Uuid::new_v4().to_string(); let mut response_selection = None; + let mut prepared_body = None; let req = if let Some(engine) = middleware_engine { let input = middleware_network_input(ctx); let (chain, generation) = engine.query_middleware_chain_with_generation(&input)?; @@ -3230,6 +3290,10 @@ where .await?; let request = match result { MiddlewareApplyResult::Allowed(request) => request, + MiddlewareApplyResult::Streamed { request, body } => { + prepared_body = Some(body); + request + } MiddlewareApplyResult::Denied { denial, .. } => { let denied_request = crate::l7::provider::L7Request { action: "HTTP".into(), @@ -3307,6 +3371,7 @@ where ..Default::default() }, ctx, + prepared_body.as_mut(), response_middleware, ) .await? @@ -3361,6 +3426,83 @@ mod tests { use std::path::PathBuf; use tokio::io::{AsyncRead, AsyncReadExt, AsyncWriteExt}; + fn whole_body_request_stream( + mut requests: tokio::sync::mpsc::Receiver, + replacement: Option>, + gate: Option<(Arc, Arc)>, + ) -> openshell_core::middleware::HttpRequestResultStream { + use openshell_core::proto::{ + HttpRequestBodyMode, HttpRequestBodyPassThrough, HttpRequestBodyResult, + HttpRequestBodyTransform, HttpRequestEventResult, HttpRequestPreflightInspect, + HttpRequestPreflightResult, HttpRequestTrailersResult, http_request_body_result, + http_request_body_transform, http_request_event, http_request_event_result, + http_request_preflight_result, + }; + let (sender, receiver) = tokio::sync::mpsc::channel(4); + tokio::spawn(async move { + while let Some(event) = requests.recv().await { + let result = match event.event { + Some(http_request_event::Event::Preflight(_)) => HttpRequestEventResult { + result: Some(http_request_event_result::Result::PreflightResult( + HttpRequestPreflightResult { + action: Some(http_request_preflight_result::Action::Inspect( + HttpRequestPreflightInspect { + body_mode: HttpRequestBodyMode::WholeBodyBytes as i32, + header_mutations: Vec::new(), + }, + )), + ..Default::default() + }, + )), + }, + Some(http_request_event::Event::Body(body)) => { + if let Some((entered, release)) = &gate { + entered.notify_one(); + release.notified().await; + } + let action = replacement.as_ref().map_or_else( + || { + http_request_body_result::Action::PassThrough( + HttpRequestBodyPassThrough {}, + ) + }, + |replacement| { + http_request_body_result::Action::Transform( + HttpRequestBodyTransform { + replacement: Some( + http_request_body_transform::Replacement::Data( + replacement.clone(), + ), + ), + }, + ) + }, + ); + HttpRequestEventResult { + result: Some(http_request_event_result::Result::BodyResult( + HttpRequestBodyResult { + sequence: body.sequence, + action: Some(action), + ..Default::default() + }, + )), + } + } + Some(http_request_event::Event::Trailers(_)) => HttpRequestEventResult { + result: Some(http_request_event_result::Result::TrailersResult( + HttpRequestTrailersResult::default(), + )), + }, + Some(http_request_event::Event::SessionEnd(_)) | None => break, + }; + if sender.send(Ok(result)).await.is_err() { + break; + } + } + }); + Box::pin(tokio_stream::wrappers::ReceiverStream::new(receiver)) + } + #[tokio::test] async fn body_denial_returns_actionable_local_json() { let req = crate::l7::provider::L7Request { @@ -3380,6 +3522,7 @@ mod tests { }, &L7EvalContext::default(), None, + None, ) .await .unwrap(); @@ -3468,6 +3611,7 @@ mod tests { }, &L7EvalContext::default(), None, + None, Some(&observer), ) .await; @@ -3825,6 +3969,7 @@ mod tests { }, &ctx, None, + None, ) .await .expect("typed credential denial"); @@ -3918,9 +4063,14 @@ mod tests { request, resolver.as_ref(), crate::l7::rest::RelayRequestOptions { - credential_signing: crate::l7::CredentialSigning::SigV4NoBody, - signing_service: "execute-api", - signing_region: "us-west-2", + post_credentials: + crate::l7::post_credentials::PostCredentialsMiddleware::from_endpoint( + crate::l7::CredentialSigning::SigV4NoBody, + "execute-api", + "us-west-2", + "denied.example.test", + 443, + ), host: "denied.example.test", port: 443, ..Default::default() @@ -4150,6 +4300,63 @@ network_policies: (config, tunnel_engine, ctx) } + fn middleware_relay_context_with_runner( + middleware_impl: &str, + runner: openshell_supervisor_middleware::ChainRunner, + ) -> (L7EndpointConfig, TunnelPolicyEngine, L7EvalContext) { + let data = format!( + r#" +network_middlewares: + request-middleware: + middleware: {middleware_impl} + on_error: fail_closed + endpoints: + include: ["api.example.test"] +network_policies: + rest_api: + name: rest_api + endpoints: + - host: api.example.test + port: 8080 + protocol: rest + enforcement: enforce + rules: + - allow: + method: POST + path: "/v1/**" + binaries: + - {{ path: /usr/bin/curl }} +"# + ); + let engine = OpaEngine::from_strings(TEST_POLICY, &data).expect("test middleware policy"); + engine.set_middleware_runner_for_tests(runner); + let input = NetworkInput { + host: "api.example.test".into(), + port: 8080, + binary_path: PathBuf::from("/usr/bin/curl"), + binary_sha256: "unused".into(), + ancestors: vec![], + cmdline_paths: vec![], + }; + let (endpoint_config, generation) = engine + .query_endpoint_config_with_generation(&input) + .expect("endpoint config"); + let config = crate::l7::parse_l7_config(&endpoint_config.expect("REST endpoint")) + .expect("parse REST config"); + let tunnel_engine = engine + .clone_engine_for_tunnel(generation) + .expect("tunnel engine"); + let ctx = L7EvalContext { + host: "api.example.test".into(), + port: 8080, + request_default_port: Some(8080), + policy_name: "rest_api".into(), + binary_path: "/usr/bin/curl".into(), + ..Default::default() + }; + (config, tunnel_engine, ctx) + } + fn passthrough_token_grant_relay_context( resolver_response: std::result::Result<&str, &str>, ) -> ( @@ -4252,16 +4459,6 @@ network_policies: )) } - async fn evaluate_http_request( - &self, - _request: tonic::Request, - ) -> std::result::Result< - tonic::Response, - tonic::Status, - > { - Err(tonic::Status::unimplemented("WebSocket-only middleware")) - } - async fn evaluate_web_socket_session( &self, request: tonic::Request>, @@ -5049,15 +5246,13 @@ network_policies: ); app.write_all(request.as_bytes()).await.unwrap(); - let mut upstream_request = [0u8; 1024]; - let n = tokio::time::timeout( + let upstream_request = tokio::time::timeout( std::time::Duration::from_secs(1), - upstream.read(&mut upstream_request), + read_http_request(&mut upstream), ) .await - .expect("request should reach upstream") - .unwrap(); - let upstream_request = String::from_utf8_lossy(&upstream_request[..n]); + .expect("request should reach upstream"); + let upstream_request = String::from_utf8_lossy(&upstream_request); assert!(upstream_request.contains(r#""api_key":"[REDACTED]""#)); assert!(!upstream_request.contains("sk-1234567890abcdef")); @@ -5115,15 +5310,13 @@ network_policies: app.write_all(body).await.unwrap(); - let mut upstream_request = [0u8; 1024]; - let n = tokio::time::timeout( + let upstream_request = tokio::time::timeout( std::time::Duration::from_secs(1), - upstream.read(&mut upstream_request), + read_http_request(&mut upstream), ) .await - .expect("request should reach upstream after the body is released") - .unwrap(); - let upstream_request = String::from_utf8_lossy(&upstream_request[..n]); + .expect("request should reach upstream after the body is released"); + let upstream_request = String::from_utf8_lossy(&upstream_request); assert!(upstream_request.contains(r#""api_key":"[REDACTED]""#)); assert!(!upstream_request.contains("Expect: 100-continue")); @@ -6057,16 +6250,16 @@ network_policies: Ok(()) } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - _request: openshell_core::middleware::HttpRequestView<'_>, - ) -> Result { - self.entered.notify_one(); - self.release.notified().await; - Ok(openshell_core::proto::HttpRequestResult { - decision: openshell_core::proto::Decision::Allow as i32, - ..Default::default() - }) + requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result + { + Ok(whole_body_request_stream( + requests, + None, + Some((Arc::clone(&self.entered), Arc::clone(&self.release))), + )) } } @@ -6204,16 +6397,16 @@ network_policies: Ok(()) } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - _request: openshell_core::middleware::HttpRequestView<'_>, - ) -> Result { - Ok(openshell_core::proto::HttpRequestResult { - decision: openshell_core::proto::Decision::Allow as i32, - body: self.replacement.to_vec(), - has_body: true, - ..Default::default() - }) + requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result + { + Ok(whole_body_request_stream( + requests, + Some(self.replacement.to_vec()), + None, + )) } } @@ -6647,6 +6840,180 @@ network_policies: replacement: Option<&'static [u8]>, } + #[derive(Clone, Copy)] + enum RequestRelayMode { + HeadersOnly, + AppendPerUnit, + CredentialMarkerPerUnit, + WholeBodyAppend, + } + + struct RequestRelayService { + mode: RequestRelayMode, + rewrite_trace_trailer: bool, + session_end: Option>, + } + + #[tonic::async_trait] + impl openshell_core::middleware::InProcessMiddleware for RequestRelayService { + async fn describe(&self) -> openshell_core::proto::MiddlewareManifest { + openshell_core::proto::MiddlewareManifest { + name: "test/request-relay".into(), + service_version: "test".into(), + bindings: vec![openshell_core::proto::MiddlewareBinding { + operation: openshell_core::proto::SupervisorMiddlewareOperation::HttpRequest + as i32, + phase: openshell_core::proto::SupervisorMiddlewarePhase::PreCredentials as i32, + max_payload_bytes: + (openshell_supervisor_middleware::MAX_HTTP_REQUEST_STREAM_UNIT_BYTES + 1) + as u64, + request_timeout: None, + }], + expected_audience: String::new(), + } + } + + async fn validate_config( + &self, + _middleware_name: &str, + _config: &prost_types::Struct, + ) -> Result<()> { + Ok(()) + } + + async fn open_http_request_pre_credentials( + &self, + mut requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result + { + use openshell_core::proto::{ + ExistingHeaderAction, HeaderMutation, HttpRequestBodyMode, + HttpRequestBodyPassThrough, HttpRequestBodyResult, HttpRequestBodyTransform, + HttpRequestEventResult, HttpRequestPreflightInspect, HttpRequestPreflightResult, + HttpRequestTrailersResult, WriteHeader, header_mutation, http_request_body_result, + http_request_body_transform, http_request_body_unit, http_request_event, + http_request_event_result, http_request_preflight_result, + }; + + let mode = self.mode; + let rewrite_trace_trailer = self.rewrite_trace_trailer; + let session_end = self.session_end.clone(); + let (sender, receiver) = tokio::sync::mpsc::channel(4); + tokio::spawn(async move { + while let Some(event) = requests.recv().await { + let result = match event.event { + Some(http_request_event::Event::Preflight(_)) => HttpRequestEventResult { + result: Some(http_request_event_result::Result::PreflightResult( + HttpRequestPreflightResult { + action: Some(http_request_preflight_result::Action::Inspect( + HttpRequestPreflightInspect { + body_mode: match mode { + RequestRelayMode::HeadersOnly => { + HttpRequestBodyMode::HeadersOnly as i32 + } + RequestRelayMode::AppendPerUnit + | RequestRelayMode::CredentialMarkerPerUnit => { + HttpRequestBodyMode::StreamBytes as i32 + } + RequestRelayMode::WholeBodyAppend => { + HttpRequestBodyMode::WholeBodyBytes as i32 + } + }, + header_mutations: Vec::new(), + }, + )), + ..Default::default() + }, + )), + }, + Some(http_request_event::Event::Body(body)) => { + let data = match body.payload { + Some(http_request_body_unit::Payload::Data(data)) => data, + None => Vec::new(), + }; + let replacement = match mode { + RequestRelayMode::AppendPerUnit + | RequestRelayMode::WholeBodyAppend + if !data.is_empty() => + { + let mut replacement = data; + replacement.push(b'!'); + Some(replacement) + } + RequestRelayMode::CredentialMarkerPerUnit if !data.is_empty() => { + Some(b"openshell:resolve:env:v1_API_TOKEN".to_vec()) + } + _ => None, + }; + let action = replacement.map_or_else( + || { + http_request_body_result::Action::PassThrough( + HttpRequestBodyPassThrough {}, + ) + }, + |replacement| { + http_request_body_result::Action::Transform( + HttpRequestBodyTransform { + replacement: Some( + http_request_body_transform::Replacement::Data( + replacement, + ), + ), + }, + ) + }, + ); + HttpRequestEventResult { + result: Some(http_request_event_result::Result::BodyResult( + HttpRequestBodyResult { + sequence: body.sequence, + action: Some(action), + ..Default::default() + }, + )), + } + } + Some(http_request_event::Event::Trailers(_)) => { + let trailer_mutations = rewrite_trace_trailer + .then(|| HeaderMutation { + operation: Some(header_mutation::Operation::Write( + WriteHeader { + name: "x-trace".into(), + value: "rewritten".into(), + on_existing: ExistingHeaderAction::Overwrite as i32, + }, + )), + }) + .into_iter() + .collect(); + HttpRequestEventResult { + result: Some(http_request_event_result::Result::TrailersResult( + HttpRequestTrailersResult { + trailer_mutations, + ..Default::default() + }, + )), + } + } + Some(http_request_event::Event::SessionEnd(end)) => { + if let Some(session_end) = &session_end { + let _ = session_end.send(end.reason); + } + break; + } + None => break, + }; + if sender.send(Ok(result)).await.is_err() { + break; + } + } + }); + Ok(Box::pin(tokio_stream::wrappers::ReceiverStream::new( + receiver, + ))) + } + } + #[tonic::async_trait] impl openshell_core::middleware::InProcessMiddleware for LimitService { async fn describe(&self) -> openshell_core::proto::MiddlewareManifest { @@ -6675,19 +7042,16 @@ network_policies: Ok(()) } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - _request: openshell_core::middleware::HttpRequestView<'_>, - ) -> Result { - let mut result = openshell_core::proto::HttpRequestResult { - decision: openshell_core::proto::Decision::Allow as i32, - ..Default::default() - }; - if let Some(replacement) = self.replacement { - result.body = replacement.to_vec(); - result.has_body = true; - } - Ok(result) + requests: tokio::sync::mpsc::Receiver, + ) -> std::result::Result + { + Ok(whole_body_request_stream( + requests, + self.replacement.map(<[u8]>::to_vec), + None, + )) } } @@ -6776,6 +7140,19 @@ network_policies: "redactor must replace the body: {raw}" ); } + MiddlewareApplyResult::Streamed { request, mut body } => { + let crate::l7::middleware::MiddlewareRequestBody::Spool(body) = &mut body else { + panic!("whole-body middleware must retain output before forwarding") + }; + let mut rewritten = Vec::new(); + body.file + .read_to_end(&mut rewritten) + .await + .expect("read spooled middleware output"); + assert_eq!(rewritten, b"[SCRUBBED BY TEST REDACTOR]"); + assert_eq!(body.len, rewritten.len() as u64); + assert!(request.raw_header.ends_with(b"\r\n\r\n")); + } MiddlewareApplyResult::Denied { .. } => { panic!("body within the largest stage limit must not fail the chain") } @@ -6785,6 +7162,403 @@ network_policies: } } + #[tokio::test] + async fn header_only_request_middleware_forwards_large_body_without_collecting_it() { + let runner = + openshell_supervisor_middleware::ChainRunner::new(Arc::new(RequestRelayService { + mode: RequestRelayMode::HeadersOnly, + rewrite_trace_trailer: false, + session_end: None, + })); + let (mut config, tunnel_engine, ctx) = + middleware_relay_context_with_runner("test/request-relay", runner); + config.provider_credentialed = true; + let (mut app, mut relay_client) = tokio::io::duplex(128 * 1024); + let (mut relay_upstream, mut upstream) = tokio::io::duplex(128 * 1024); + let relay = tokio::spawn(async move { + relay_with_inspection( + &config, + tunnel_engine, + &mut relay_client, + &mut relay_upstream, + &ctx, + ) + .await + }); + + let body_len = 4 * 1024 * 1024 + 17; + let headers = format!( + "POST /v1/large HTTP/1.1\r\nHost: api.example.test\r\nContent-Length: {body_len}\r\nConnection: close\r\n\r\n" + ); + app.write_all(headers.as_bytes()).await.unwrap(); + + // Receiving the upstream head before the client sends any body proves + // the header-only mode did not collect the representation first. + let upstream_headers = tokio::time::timeout( + std::time::Duration::from_secs(1), + read_http_headers(&mut upstream), + ) + .await + .expect("header-only request should forward before its body arrives"); + let upstream_headers = String::from_utf8(upstream_headers).unwrap(); + assert!(upstream_headers.contains(&format!("Content-Length: {body_len}\r\n"))); + + let upstream_task = tokio::spawn(async move { + let mut body = vec![0; body_len]; + upstream.read_exact(&mut body).await.unwrap(); + assert!(body.iter().all(|byte| *byte == b'x')); + upstream + .write_all( + b"HTTP/1.1 204 No Content\r\nContent-Length: 0\r\nConnection: close\r\n\r\n", + ) + .await + .unwrap(); + }); + app.write_all(&vec![b'x'; body_len]).await.unwrap(); + upstream_task.await.unwrap(); + + let response = read_http_headers(&mut app).await; + assert!(String::from_utf8_lossy(&response).contains("204 No Content")); + drop(app); + tokio::time::timeout(std::time::Duration::from_secs(2), relay) + .await + .expect("large request relay should finish") + .unwrap() + .unwrap(); + } + + #[tokio::test] + async fn streaming_request_middleware_forwards_size_changing_units_before_eos() { + let runner = + openshell_supervisor_middleware::ChainRunner::new(Arc::new(RequestRelayService { + mode: RequestRelayMode::AppendPerUnit, + rewrite_trace_trailer: false, + session_end: None, + })); + let (mut config, tunnel_engine, ctx) = + middleware_relay_context_with_runner("test/request-relay", runner); + config.provider_credentialed = true; + let (mut app, mut relay_client) = tokio::io::duplex(128 * 1024); + let (mut relay_upstream, mut upstream) = tokio::io::duplex(128 * 1024); + let relay = tokio::spawn(async move { + relay_with_inspection( + &config, + tunnel_engine, + &mut relay_client, + &mut relay_upstream, + &ctx, + ) + .await + }); + + let first_len = openshell_supervisor_middleware::MAX_HTTP_REQUEST_STREAM_UNIT_BYTES; + let body_len = first_len + 5; + app.write_all( + format!( + "POST /v1/grow HTTP/1.1\r\nHost: api.example.test\r\nContent-Length: {body_len}\r\nConnection: close\r\n\r\n" + ) + .as_bytes(), + ) + .await + .unwrap(); + let upstream_headers = read_http_headers(&mut upstream).await; + let upstream_headers = String::from_utf8(upstream_headers).unwrap(); + assert!(upstream_headers.contains("Transfer-Encoding: chunked\r\n")); + assert!( + !upstream_headers + .to_ascii_lowercase() + .contains("content-length:") + ); + + app.write_all(&vec![b'a'; first_len]).await.unwrap(); + let first = tokio::time::timeout( + std::time::Duration::from_secs(1), + read_http_chunk(&mut upstream), + ) + .await + .expect("approved output should reach upstream before client EOS") + .expect("early data chunk"); + assert!(!first.is_empty()); + assert!(first.iter().all(|byte| matches!(byte, b'a' | b'!'))); + + app.write_all(b"hello").await.unwrap(); + let mut transformed = first; + while let Some(chunk) = read_http_chunk(&mut upstream).await { + transformed.extend_from_slice(&chunk); + } + let original = transformed + .iter() + .copied() + .filter(|byte| *byte != b'!') + .collect::>(); + let mut expected = vec![b'a'; first_len]; + expected.extend_from_slice(b"hello"); + assert_eq!(original, expected); + assert!(transformed.contains(&b'!')); + upstream + .write_all(b"HTTP/1.1 204 No Content\r\nContent-Length: 0\r\nConnection: close\r\n\r\n") + .await + .unwrap(); + + let response = read_http_headers(&mut app).await; + assert!(String::from_utf8_lossy(&response).contains("204 No Content")); + drop(app); + tokio::time::timeout(std::time::Duration::from_secs(1), relay) + .await + .expect("size-changing request relay should finish") + .unwrap() + .unwrap(); + } + + #[tokio::test] + async fn provider_guard_rejects_a_marker_created_by_streaming_middleware() { + let runner = + openshell_supervisor_middleware::ChainRunner::new(Arc::new(RequestRelayService { + mode: RequestRelayMode::CredentialMarkerPerUnit, + rewrite_trace_trailer: false, + session_end: None, + })); + let (mut config, tunnel_engine, ctx) = + middleware_relay_context_with_runner("test/request-relay", runner); + config.provider_credentialed = true; + let (mut app, mut relay_client) = tokio::io::duplex(8192); + let (mut relay_upstream, mut upstream) = tokio::io::duplex(8192); + let relay = tokio::spawn(async move { + relay_with_inspection( + &config, + tunnel_engine, + &mut relay_client, + &mut relay_upstream, + &ctx, + ) + .await + }); + + app.write_all( + b"POST /v1/marker HTTP/1.1\r\nHost: api.example.test\r\nContent-Length: 4\r\nConnection: close\r\n\r\nsafe", + ) + .await + .unwrap(); + let response = tokio::time::timeout( + std::time::Duration::from_secs(1), + read_http_headers(&mut app), + ) + .await + .expect("credential marker denial should reach the client"); + assert!(String::from_utf8_lossy(&response).contains("403 Forbidden")); + + drop(app); + tokio::time::timeout(std::time::Duration::from_secs(1), relay) + .await + .expect("credential marker relay should finish") + .unwrap() + .unwrap(); + let mut forwarded = Vec::new(); + upstream.read_to_end(&mut forwarded).await.unwrap(); + assert!( + !forwarded + .windows(b"openshell:resolve:".len()) + .any(|window| window == b"openshell:resolve:"), + "middleware-created credential marker reached upstream" + ); + } + + #[tokio::test] + async fn withheld_middleware_output_remains_usable_with_provider_credentials() { + let runner = + openshell_supervisor_middleware::ChainRunner::new(Arc::new(RequestRelayService { + mode: RequestRelayMode::WholeBodyAppend, + rewrite_trace_trailer: false, + session_end: None, + })); + let (mut config, tunnel_engine, ctx) = + middleware_relay_context_with_runner("test/request-relay", runner); + config.provider_credentialed = true; + let (mut app, mut relay_client) = tokio::io::duplex(8192); + let (mut relay_upstream, mut upstream) = tokio::io::duplex(8192); + let relay = tokio::spawn(async move { + relay_with_inspection( + &config, + tunnel_engine, + &mut relay_client, + &mut relay_upstream, + &ctx, + ) + .await + }); + + app.write_all( + b"POST /v1/held HTTP/1.1\r\nHost: api.example.test\r\nContent-Length: 5\r\nConnection: close\r\n\r\nhello", + ) + .await + .unwrap(); + let headers = String::from_utf8(read_http_headers(&mut upstream).await).unwrap(); + assert!(headers.contains("Content-Length: 6\r\n")); + let mut body = [0; 6]; + upstream.read_exact(&mut body).await.unwrap(); + assert_eq!(&body, b"hello!"); + upstream + .write_all(b"HTTP/1.1 204 No Content\r\nContent-Length: 0\r\nConnection: close\r\n\r\n") + .await + .unwrap(); + assert!( + String::from_utf8_lossy(&read_http_headers(&mut app).await).contains("204 No Content") + ); + drop(app); + tokio::time::timeout(std::time::Duration::from_secs(1), relay) + .await + .expect("withheld provider-backed request should finish") + .unwrap() + .unwrap(); + } + + #[tokio::test] + async fn streaming_request_stops_on_early_upstream_response_without_replay() { + let (session_end_tx, mut session_end_rx) = tokio::sync::mpsc::unbounded_channel(); + let runner = + openshell_supervisor_middleware::ChainRunner::new(Arc::new(RequestRelayService { + mode: RequestRelayMode::AppendPerUnit, + rewrite_trace_trailer: false, + session_end: Some(session_end_tx), + })); + let (config, tunnel_engine, ctx) = + middleware_relay_context_with_runner("test/request-relay", runner); + let (mut app, mut relay_client) = tokio::io::duplex(128 * 1024); + let (mut relay_upstream, mut upstream) = tokio::io::duplex(128 * 1024); + let relay = tokio::spawn(async move { + relay_with_inspection( + &config, + tunnel_engine, + &mut relay_client, + &mut relay_upstream, + &ctx, + ) + .await + }); + + let unit_len = openshell_supervisor_middleware::MAX_HTTP_REQUEST_STREAM_UNIT_BYTES; + let declared_len = 4 * 1024 * 1024 + 17; + app.write_all( + format!( + "POST /v1/early HTTP/1.1\r\nHost: api.example.test\r\nContent-Length: {declared_len}\r\nConnection: close\r\n\r\n" + ) + .as_bytes(), + ) + .await + .unwrap(); + let headers = String::from_utf8(read_http_headers(&mut upstream).await).unwrap(); + assert!(headers.contains("Transfer-Encoding: chunked\r\n")); + app.write_all(&vec![b'a'; unit_len]).await.unwrap(); + assert_eq!( + read_http_chunk(&mut upstream).await.unwrap().len(), + unit_len + 1 + ); + + upstream + .write_all( + b"HTTP/1.1 413 Payload Too Large\r\nContent-Length: 0\r\nConnection: close\r\n\r\n", + ) + .await + .unwrap(); + let response = tokio::time::timeout( + std::time::Duration::from_secs(1), + read_http_headers(&mut app), + ) + .await + .expect("early upstream response should not wait for client EOS"); + assert!(String::from_utf8_lossy(&response).contains("413 Payload Too Large")); + let reason = tokio::time::timeout(std::time::Duration::from_secs(1), session_end_rx.recv()) + .await + .expect("middleware should receive cancellation") + .expect("session-end reason"); + assert_eq!( + reason, + openshell_core::proto::MiddlewareSessionEndReason::Cancellation as i32 + ); + + drop(app); + tokio::time::timeout(std::time::Duration::from_secs(1), relay) + .await + .expect("early-response relay should terminate") + .unwrap() + .unwrap(); + } + + #[tokio::test] + async fn streaming_request_middleware_preserves_trailers_and_handles_expect_continue() { + let runner = + openshell_supervisor_middleware::ChainRunner::new(Arc::new(RequestRelayService { + mode: RequestRelayMode::AppendPerUnit, + rewrite_trace_trailer: true, + session_end: None, + })); + let (config, tunnel_engine, ctx) = + middleware_relay_context_with_runner("test/request-relay", runner); + let (mut app, mut relay_client) = tokio::io::duplex(8192); + let (mut relay_upstream, mut upstream) = tokio::io::duplex(8192); + let relay = tokio::spawn(async move { + relay_with_inspection( + &config, + tunnel_engine, + &mut relay_client, + &mut relay_upstream, + &ctx, + ) + .await + }); + + app.write_all( + b"POST /v1/chunked HTTP/1.1\r\nHost: api.example.test\r\nTransfer-Encoding: chunked\r\nTrailer: X-Trace\r\nExpect: 100-continue\r\nConnection: close\r\n\r\n", + ) + .await + .unwrap(); + let mut interim = [0; 25]; + tokio::time::timeout( + std::time::Duration::from_secs(1), + app.read_exact(&mut interim), + ) + .await + .expect("middleware should acknowledge Expect") + .unwrap(); + assert_eq!(&interim, b"HTTP/1.1 100 Continue\r\n\r\n"); + app.write_all(b"4\r\nWiki\r\n5\r\npedia\r\n0\r\nX-Trace: original\r\n\r\n") + .await + .unwrap(); + + let upstream_headers = read_http_headers(&mut upstream).await; + let upstream_headers = String::from_utf8(upstream_headers).unwrap(); + assert!(upstream_headers.contains("Transfer-Encoding: chunked\r\n")); + assert!( + upstream_headers + .to_ascii_lowercase() + .contains("trailer: x-trace\r\n") + ); + assert!(!upstream_headers.to_ascii_lowercase().contains("expect:")); + assert!( + !upstream_headers + .to_ascii_lowercase() + .contains("content-length:") + ); + assert_eq!(read_http_chunk(&mut upstream).await.unwrap(), b"Wiki!"); + assert_eq!(read_http_chunk(&mut upstream).await.unwrap(), b"pedia!"); + assert!(read_http_chunk(&mut upstream).await.is_none()); + let trailers = read_http_chunk_trailers(&mut upstream).await; + assert_eq!(trailers, vec!["x-trace: rewritten"]); + upstream + .write_all(b"HTTP/1.1 204 No Content\r\nContent-Length: 0\r\nConnection: close\r\n\r\n") + .await + .unwrap(); + + let response = read_http_headers(&mut app).await; + assert!(String::from_utf8_lossy(&response).contains("204 No Content")); + drop(app); + tokio::time::timeout(std::time::Duration::from_secs(1), relay) + .await + .expect("chunked request relay should finish") + .unwrap() + .unwrap(); + } + #[tokio::test] async fn all_unresolved_fail_open_forwards_body_unbuffered() { // A chain whose only entry is an unregistered binding has no resolvable @@ -7167,15 +7941,13 @@ network_policies: ); app.write_all(request.as_bytes()).await.unwrap(); - let mut upstream_request = [0u8; 1024]; - let n = tokio::time::timeout( + let upstream_request = tokio::time::timeout( std::time::Duration::from_secs(1), - upstream.read(&mut upstream_request), + read_http_request(&mut upstream), ) .await - .expect("request should reach upstream") - .unwrap(); - let upstream_request = String::from_utf8_lossy(&upstream_request[..n]); + .expect("request should reach upstream"); + let upstream_request = String::from_utf8_lossy(&upstream_request); assert!( upstream_request.contains(r#""api_key":"[REDACTED]""#), "unexpected upstream request: {upstream_request:?}" @@ -9012,6 +9784,65 @@ network_policies: } } + async fn read_http_line(reader: &mut R) -> Vec { + let mut line = Vec::new(); + let mut byte = [0u8; 1]; + loop { + reader.read_exact(&mut byte).await.unwrap(); + line.push(byte[0]); + if line.ends_with(b"\r\n") { + line.truncate(line.len() - 2); + return line; + } + } + } + + async fn read_http_chunk(reader: &mut R) -> Option> { + let size = String::from_utf8(read_http_line(reader).await).unwrap(); + let size = usize::from_str_radix(size.split(';').next().unwrap(), 16).unwrap(); + if size == 0 { + return None; + } + let mut payload = vec![0; size]; + reader.read_exact(&mut payload).await.unwrap(); + let mut terminator = [0; 2]; + reader.read_exact(&mut terminator).await.unwrap(); + assert_eq!(&terminator, b"\r\n"); + Some(payload) + } + + async fn read_http_chunk_trailers(reader: &mut R) -> Vec { + let mut trailers = Vec::new(); + loop { + let line = read_http_line(reader).await; + if line.is_empty() { + return trailers; + } + trailers.push(String::from_utf8(line).unwrap()); + } + } + + async fn read_http_request(reader: &mut R) -> Vec { + let mut request = read_http_headers(reader).await; + let headers = std::str::from_utf8(&request).expect("HTTP request headers"); + let content_length = headers + .lines() + .find_map(|line| { + line.split_once(':').and_then(|(name, value)| { + name.eq_ignore_ascii_case("content-length") + .then(|| value.trim().parse::().expect("Content-Length")) + }) + }) + .unwrap_or_default(); + let header_len = request.len(); + request.resize(header_len + content_length, 0); + reader + .read_exact(&mut request[header_len..]) + .await + .expect("HTTP request body"); + request + } + async fn read_text_frame( reader: &mut R, ) -> std::io::Result<(bool, String)> { diff --git a/crates/openshell-supervisor-network/src/l7/rest.rs b/crates/openshell-supervisor-network/src/l7/rest.rs index 4c65c983e..16e723a21 100644 --- a/crates/openshell-supervisor-network/src/l7/rest.rs +++ b/crates/openshell-supervisor-network/src/l7/rest.rs @@ -23,7 +23,6 @@ use http_response::{ use crate::l7::EndpointObserver; use crate::l7::provider::{BodyLength, L7Provider, L7Request, RelayOutcome}; use crate::opa::PolicyGenerationGuard; -use aws_sigv4::http_request::SignableBody; use base64::Engine as _; use miette::{IntoDiagnostic, Result, miette}; use openshell_core::endpoint_status::EndpointResult; @@ -39,14 +38,12 @@ use openshell_ocsf::ctx::ctx as ocsf_ctx; use sha1::{Digest, Sha1}; use std::collections::{HashMap, HashSet}; use std::fmt::{self, Write as _}; -use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; +use std::io::SeekFrom; +use tokio::io::{AsyncRead, AsyncReadExt, AsyncSeekExt, AsyncWrite, AsyncWriteExt}; use tracing::debug; const MAX_HEADER_BYTES: usize = 16384; // 16 KiB for HTTP headers const MAX_REWRITE_BODY_BYTES: usize = 256 * 1024; -/// Maximum body bytes for `SigV4` body-signing mode. Larger than the credential -/// rewrite limit because Bedrock payloads can be several megabytes. -const MAX_SIGV4_BODY_BYTES: usize = 10 * 1024 * 1024; #[cfg(test)] async fn max_middleware_body_bytes() -> usize { let chain = openshell_supervisor_middleware::ChainRunner::new( @@ -748,9 +745,7 @@ where websocket_extensions: WebSocketExtensionMode::Preserve, request_body_credential_rewrite: false, deny_uninspected_credentials: false, - credential_signing: crate::l7::CredentialSigning::None, - signing_service: "", - signing_region: "", + post_credentials: None, host: "", port: 0, }, @@ -774,9 +769,7 @@ pub(crate) struct RelayRequestOptions<'a> { pub(crate) websocket_extensions: WebSocketExtensionMode, pub(crate) request_body_credential_rewrite: bool, pub(crate) deny_uninspected_credentials: bool, - pub(crate) credential_signing: crate::l7::CredentialSigning, - pub(crate) signing_service: &'a str, - pub(crate) signing_region: &'a str, + pub(crate) post_credentials: Option>, pub(crate) host: &'a str, pub(crate) port: u16, } @@ -797,7 +790,7 @@ pub(crate) struct CredentialUnavailableError { } impl CredentialUnavailableError { - fn new(reason: &'static str) -> Self { + pub(crate) fn new(reason: &'static str) -> Self { Self { reason } } } @@ -849,7 +842,7 @@ where U: AsyncRead + AsyncWrite + Unpin, { relay_http_request_with_response_middleware_guarded_observed( - req, client, upstream, options, None, None, + req, client, upstream, options, None, None, None, ) .await } @@ -859,7 +852,8 @@ pub(crate) async fn relay_http_request_with_response_middleware_guarded_observed client: &mut C, upstream: &mut U, options: RelayRequestOptions<'_>, - response_middleware: Option>, + mut prepared_body: Option<&mut crate::l7::middleware::MiddlewareRequestBody>, + mut response_middleware: Option>, observer: Option<&EndpointObserver>, ) -> Result where @@ -884,12 +878,12 @@ where parse_websocket_upgrade_request(&req.raw_header[..header_end])? }; - // When SigV4 signing is configured, strip AWS auth headers before credential - // rewriting so the fail-closed placeholder scan doesn't reject the SigV4 - // Authorization header (which embeds placeholder strings). + // A restricted post-credentials built-in may remove caller authorization + // before placeholder rewriting, then regenerate it from trusted provider + // credentials below. let raw_for_rewrite; - let header_source = if options.credential_signing.is_sigv4() { - raw_for_rewrite = crate::sigv4::strip_aws_headers(&req.raw_header[..header_end])?; + let header_source = if let Some(middleware) = options.post_credentials { + raw_for_rewrite = middleware.strip_caller_authorization(&req.raw_header[..header_end])?; &raw_for_rewrite[..] } else { &req.raw_header[..header_end] @@ -917,256 +911,161 @@ where guard.ensure_current()?; } - // Apply SigV4 signing if configured. - if options.credential_signing.is_sigv4() { - // Defense-in-depth: credential_signing and request_body_credential_rewrite - // are mutually exclusive (validated at policy load time). + if let Some(middleware) = options.post_credentials { + // Defense-in-depth: post-credential signing and request body credential + // rewriting are mutually exclusive (validated at policy load time). if options.request_body_credential_rewrite { return Err(miette!( "credential_signing and request_body_credential_rewrite are \ mutually exclusive on the same endpoint" )); } - // SigV4 re-signing needs the body before forwarding. If the client - // sent `Expect: 100-continue`, acknowledge it so the client transmits - // the body. Scoped to SigV4 paths only — non-SigV4 traffic forwards - // the Expect header to upstream for normal handling. - if has_expect_continue(header_str) { - client - .write_all(b"HTTP/1.1 100 Continue\r\n\r\n") - .await - .into_diagnostic()?; - client.flush().await.into_diagnostic()?; + let prepared_spool = prepared_body.as_deref_mut().and_then(|body| match body { + crate::l7::middleware::MiddlewareRequestBody::Spool(spool) => Some(spool), + crate::l7::middleware::MiddlewareRequestBody::Live(_) => None, + }); + let output = middleware + .evaluate( + req, + header_str, + &rewrite_result.rewritten, + client, + prepared_spool, + options.resolver, + options.generation_guard, + ) + .await?; + ensure_credential_generation_current(options)?; + if let Some(guard) = options.generation_guard { + guard.ensure_current()?; } - if let Some(resolver) = options.resolver { - let access_key = resolver - .resolve_current_env_key_checked("AWS_ACCESS_KEY_ID", "sigv4") - .map_err(miette::Report::new)?; - let secret_key = resolver - .resolve_current_env_key_checked("AWS_SECRET_ACCESS_KEY", "sigv4") - .map_err(miette::Report::new)?; - let session_token = resolver - .resolve_current_env_key_checked("AWS_SESSION_TOKEN", "sigv4") - .map_err(miette::Report::new)?; - - match (access_key, secret_key) { - (Some(access_key), Some(secret_key)) => { - // Use explicit signing_region from policy if set, - // otherwise extract from hostname. - let region = if options.signing_region.is_empty() { - match crate::sigv4::extract_aws_region(options.host) { - Some(r) => r, - None => { - return Err(miette!( - "SigV4 signing: cannot extract AWS region from \ - hostname '{host}'; set signing_region in the \ - policy endpoint", - host = options.host, - )); - } - } - } else { - options.signing_region.to_string() - }; - let service = &options.signing_service; - if service.is_empty() { - return Err(miette!( - "SigV4 signing configured but signing_service not set in policy" - )); - } - - let payload_mode = match options.credential_signing { - crate::l7::CredentialSigning::SigV4Body => SigV4PayloadMode::SignBody, - crate::l7::CredentialSigning::SigV4NoBody => { - SigV4PayloadMode::UnsignedPayload - } - crate::l7::CredentialSigning::SigV4 => detect_payload_mode(header_str)?, - crate::l7::CredentialSigning::None => unreachable!(), - }; - - if payload_mode == SigV4PayloadMode::SignBody { - // Buffer body and include its hash in the signature. - // This requires Content-Length — chunked bodies cannot - // be buffered for signing. detect_payload_mode() should - // route chunked requests to the streaming path, but - // guard here as defense-in-depth. - let body_length = parse_body_length(header_str)?; - match body_length { - BodyLength::ContentLength(_) | BodyLength::None => { - // ContentLength: buffer and sign the body. - // None: no body (e.g. GET/HEAD/DELETE) — sign - // with the empty-body hash. boto3 sends - // x-amz-content-sha256 set to the SHA-256 of - // "" on all requests, which routes here via - // detect_payload_mode's catch-all. - } - BodyLength::Chunked => { - return Err(miette!( - "SigV4 body signing requires Content-Length; \ - chunked transfer encoding is not supported in this mode" - )); - } - } - // NOTE(defense-in-depth): Build the full request from - // rewritten headers + body. `rewrite_result.rewritten` - // has already had AWS auth headers stripped by - // `strip_aws_headers`; `apply_sigv4_to_request` strips - // them again internally via `parse_request_parts` — - // the redundancy is intentional. - let overflow = &req.raw_header[header_end..]; - let mut full_request = rewrite_result.rewritten.clone(); - full_request.extend_from_slice(overflow); - if let BodyLength::ContentLength(body_len) = body_length { - if body_len > MAX_SIGV4_BODY_BYTES as u64 { - return Err(miette!( - "SigV4 body signing buffers at most {MAX_SIGV4_BODY_BYTES} bytes" - )); - } - let already_have = overflow.len() as u64; - if body_len > already_have { - let remaining = - usize::try_from(body_len - already_have).unwrap_or(usize::MAX); - let mut body_buf = vec![0u8; remaining]; - client.read_exact(&mut body_buf).await.into_diagnostic()?; - full_request.extend_from_slice(&body_buf); - } - } - - // Re-check policy after body buffering — a slow upload - // may have outlived a policy reload. - if let Some(guard) = options.generation_guard { - guard.ensure_current()?; - } - - let signed = crate::sigv4::apply_sigv4_to_request( - &full_request, - options.host, - ®ion, - service, - access_key, - secret_key, - session_token, - )?; - ensure_credential_generation_current(options)?; - upstream.write_all(&signed).await.into_diagnostic()?; - } else { - // Sign headers only, stream body through. - let signable_body = match payload_mode { - SigV4PayloadMode::StreamingUnsignedTrailer => { - SignableBody::StreamingUnsignedPayloadTrailer - } - _ => SignableBody::UnsignedPayload, - }; - let signed_headers = crate::sigv4::apply_sigv4_headers_only_with_body( - &rewrite_result.rewritten, - options.host, - ®ion, - service, - access_key, - secret_key, - session_token, - signable_body, - )?; - ensure_credential_generation_current(options)?; - upstream - .write_all(&signed_headers) - .await - .into_diagnostic()?; - - let overflow = &req.raw_header[header_end..]; - if !overflow.is_empty() { - if let Some(guard) = options.generation_guard { - guard.ensure_current()?; - } - upstream.write_all(overflow).await.into_diagnostic()?; - } - let overflow_len = overflow.len() as u64; - - match req.body_length { - BodyLength::ContentLength(len) => { - let remaining = len.saturating_sub(overflow_len); - if remaining > 0 { - relay_fixed( - client, - upstream, - remaining, - options.generation_guard, - ) - .await?; - } - } - BodyLength::Chunked => { - relay_chunked( - client, - upstream, - &req.raw_header[header_end..], - options.generation_guard, - ) - .await?; - } - BodyLength::None => {} - } - } - - // OCSF event after successful signing and upstream write. - let event = openshell_ocsf::NetworkActivityBuilder::new( - ocsf_ctx(), - ) - .activity(openshell_ocsf::ActivityId::Traffic) - .action(openshell_ocsf::ActionId::Allowed) - .disposition(openshell_ocsf::DispositionId::Allowed) - .severity(openshell_ocsf::SeverityId::Informational) - .status(openshell_ocsf::StatusId::Success) - .dst_endpoint(openshell_ocsf::Endpoint::from_domain( - options.host, - options.port, - )) - .message(format!( - "SigV4 re-signed {host}:{port} service={service} region={region} mode={payload_mode}", - host = options.host, - port = options.port, - )) - .build(); - openshell_ocsf::ocsf_emit!(event); - } - _ => { - return Err(miette::Report::new(CredentialUnavailableError::new( - "SigV4 signing configured but AWS credentials not found in provider", - ))); - } + let payload_mode = match output { + crate::l7::post_credentials::PostCredentialsOutput::Complete { + request, + payload_mode, + } => { + upstream.write_all(&request).await.into_diagnostic()?; + payload_mode } - } else { - return Err(miette::Report::new(CredentialUnavailableError::new( - "SigV4 signing configured but no secret resolver available", - ))); - } + crate::l7::post_credentials::PostCredentialsOutput::Head { + headers, + payload_mode, + } => { + if let Some(crate::l7::middleware::MiddlewareRequestBody::Live(body)) = + prepared_body.as_deref_mut() + { + let outcome = relay_live_request_and_response( + req, + client, + upstream, + &headers, + body, + options, + false, + RelayResponseOptions { + websocket_extensions: options.websocket_extensions, + websocket: websocket_response, + client_requested_upgrade, + observer, + }, + response_middleware.take(), + ) + .await?; + middleware.emit_success(payload_mode); + return Ok(outcome); + } + upstream.write_all(&headers).await.into_diagnostic()?; + relay_request_body( + req, + client, + upstream, + prepared_body.as_deref_mut(), + options.generation_guard, + ) + .await?; + payload_mode + } + }; + middleware.emit_success(payload_mode); } else if options.request_body_credential_rewrite { - let body = collect_and_rewrite_request_body( - req, - client, - &rewrite_result.rewritten, - header_str, - &req.raw_header[header_end..], - options.resolver, - options.generation_guard, - ) - .await?; + let body = match prepared_body.as_deref_mut() { + Some(crate::l7::middleware::MiddlewareRequestBody::Spool(body)) => { + collect_and_rewrite_spooled_request_body( + body, + &rewrite_result.rewritten, + header_str, + options.resolver, + options.generation_guard, + ) + .await? + } + Some(crate::l7::middleware::MiddlewareRequestBody::Live(_)) => { + return Err(miette!( + "request body credential rewriting requires withheld middleware output" + )); + } + None => { + collect_and_rewrite_request_body( + req, + client, + &rewrite_result.rewritten, + header_str, + &req.raw_header[header_end..], + options.resolver, + options.generation_guard, + ) + .await? + } + }; ensure_credential_generation_current(options)?; upstream.write_all(&body.headers).await.into_diagnostic()?; if !body.body.is_empty() { upstream.write_all(&body.body).await.into_diagnostic()?; } } else if options.deny_uninspected_credentials { - if let Err(error) = relay_request_body_with_marker_guard( - req, - client, - upstream, - &rewrite_result.rewritten, - &req.raw_header[header_end..], - options, - ) - .await + if let Some(crate::l7::middleware::MiddlewareRequestBody::Live(body)) = + prepared_body.as_deref_mut() { + return relay_live_request_and_response( + req, + client, + upstream, + &rewrite_result.rewritten, + body, + options, + true, + RelayResponseOptions { + websocket_extensions: options.websocket_extensions, + websocket: websocket_response, + client_requested_upgrade, + observer, + }, + response_middleware.take(), + ) + .await; + } + let guarded = if let Some(body) = prepared_body.as_deref_mut() { + relay_middleware_body_with_marker_guard( + req, + client, + upstream, + &rewrite_result.rewritten, + body, + options, + ) + .await + } else { + relay_request_body_with_marker_guard( + req, + client, + upstream, + &rewrite_result.rewritten, + &req.raw_header[header_end..], + options, + ) + .await + }; + if let Err(error) = guarded { if let Some(reason) = error.downcast_ref::() { emit_uninspected_body_credential_denial(req, &options, *reason); } @@ -1174,38 +1073,39 @@ where } } else { ensure_credential_generation_current(options)?; + if let Some(crate::l7::middleware::MiddlewareRequestBody::Live(body)) = + prepared_body.as_deref_mut() + { + return relay_live_request_and_response( + req, + client, + upstream, + &rewrite_result.rewritten, + body, + options, + false, + RelayResponseOptions { + websocket_extensions: options.websocket_extensions, + websocket: websocket_response, + client_requested_upgrade, + observer, + }, + response_middleware.take(), + ) + .await; + } upstream .write_all(&rewrite_result.rewritten) .await .into_diagnostic()?; - - let overflow = &req.raw_header[header_end..]; - if !overflow.is_empty() { - if let Some(guard) = options.generation_guard { - guard.ensure_current()?; - } - upstream.write_all(overflow).await.into_diagnostic()?; - } - let overflow_len = overflow.len() as u64; - - match req.body_length { - BodyLength::ContentLength(len) => { - let remaining = len.saturating_sub(overflow_len); - if remaining > 0 { - relay_fixed(client, upstream, remaining, options.generation_guard).await?; - } - } - BodyLength::Chunked => { - relay_chunked( - client, - upstream, - &req.raw_header[header_end..], - options.generation_guard, - ) - .await?; - } - BodyLength::None => {} - } + relay_request_body( + req, + client, + upstream, + prepared_body, + options.generation_guard, + ) + .await?; } upstream.flush().await.into_diagnostic()?; @@ -1226,6 +1126,260 @@ where Ok(outcome) } +#[allow(clippy::too_many_arguments)] +async fn relay_live_request_and_response( + req: &L7Request, + client: &mut C, + upstream: &mut U, + headers: &[u8], + body: &mut crate::l7::middleware::RequestBodyStream, + options: RelayRequestOptions<'_>, + inspect_credential_markers: bool, + response_options: RelayResponseOptions<'_>, + response_middleware: Option>, +) -> Result +where + C: AsyncRead + AsyncWrite + Unpin, + U: AsyncRead + AsyncWrite + Unpin, +{ + ensure_body_generation_current(options)?; + upstream.write_all(headers).await.into_diagnostic()?; + upstream.flush().await.into_diagnostic()?; + + let (mut client_reader, mut client_writer) = tokio::io::split(&mut *client); + let (mut upstream_reader, mut upstream_writer) = tokio::io::split(&mut *upstream); + let mut upload = Box::pin(relay_live_middleware_request_body( + body, + &mut client_reader, + &mut upstream_writer, + options, + inspect_credential_markers, + )); + let mut response = Box::pin(relay_response( + &req.action, + &mut upstream_reader, + &mut client_writer, + response_options, + response_middleware, + )); + + tokio::select! { + upload_result = upload.as_mut() => { + drop(upload); + if let Err(error) = upload_result { + drop(response); + let _ = upstream_writer.shutdown().await; + return Err(error); + } + upstream_writer.flush().await.into_diagnostic()?; + response.await + } + response_result = response.as_mut() => { + drop(response); + drop(upload); + body.cancel_for_early_response().await; + let _ = upstream_writer.shutdown().await; + response_result?; + // The client may still have unread request bytes, so neither side + // can safely reuse this HTTP/1 connection after an early response. + Ok(RelayOutcome::Consumed) + } + } +} + +async fn relay_request_body( + req: &L7Request, + client: &mut C, + upstream: &mut U, + prepared_body: Option<&mut crate::l7::middleware::MiddlewareRequestBody>, + generation_guard: Option<&PolicyGenerationGuard>, +) -> Result<()> +where + C: AsyncRead + Unpin, + U: AsyncWrite + Unpin, +{ + if let Some(body) = prepared_body { + return match body { + crate::l7::middleware::MiddlewareRequestBody::Spool(body) => { + relay_spooled_request_body(req, upstream, body, generation_guard).await + } + crate::l7::middleware::MiddlewareRequestBody::Live(body) => { + relay_live_middleware_request_body( + body, + client, + upstream, + RelayRequestOptions { + generation_guard, + ..Default::default() + }, + false, + ) + .await + } + }; + } + + let header_end = req + .raw_header + .windows(4) + .position(|window| window == b"\r\n\r\n") + .map_or(req.raw_header.len(), |position| position + 4); + let overflow = &req.raw_header[header_end..]; + match req.body_length { + BodyLength::None => { + if !overflow.is_empty() { + return Err(miette!("bodyless request contains read-ahead bytes")); + } + } + BodyLength::ContentLength(length) => { + let overflow_len = overflow.len() as u64; + if overflow_len > length { + return Err(miette!( + "request read-ahead exceeds its declared Content-Length" + )); + } + if !overflow.is_empty() { + if let Some(guard) = generation_guard { + guard.ensure_current()?; + } + upstream.write_all(overflow).await.into_diagnostic()?; + } + let remaining = length - overflow_len; + if remaining > 0 { + relay_fixed(client, upstream, remaining, generation_guard).await?; + } + } + BodyLength::Chunked => { + relay_chunked(client, upstream, overflow, generation_guard).await?; + } + } + Ok(()) +} + +async fn relay_spooled_request_body( + req: &L7Request, + upstream: &mut U, + body: &mut crate::l7::middleware::RequestBodySpool, + generation_guard: Option<&PolicyGenerationGuard>, +) -> Result<()> { + body.file.seek(SeekFrom::Start(0)).await.into_diagnostic()?; + let mut remaining = body.len; + let mut buffer = [0u8; RELAY_BUF_SIZE]; + match req.body_length { + BodyLength::None => { + if remaining != 0 || !body.trailers.is_empty() { + return Err(miette!("bodyless middleware request has spooled payload")); + } + } + BodyLength::ContentLength(expected) => { + if expected != remaining || !body.trailers.is_empty() { + return Err(miette!("middleware request spool framing mismatch")); + } + while remaining > 0 { + let limit = usize::try_from(remaining.min(buffer.len() as u64)) + .expect("relay buffer length fits usize"); + let read = body + .file + .read(&mut buffer[..limit]) + .await + .into_diagnostic()?; + if read == 0 { + return Err(miette!("middleware request spool ended early")); + } + if let Some(guard) = generation_guard { + guard.ensure_current()?; + } + upstream + .write_all(&buffer[..read]) + .await + .into_diagnostic()?; + remaining -= read as u64; + } + } + BodyLength::Chunked => { + while remaining > 0 { + let limit = usize::try_from(remaining.min(buffer.len() as u64)) + .expect("relay buffer length fits usize"); + let read = body + .file + .read(&mut buffer[..limit]) + .await + .into_diagnostic()?; + if read == 0 { + return Err(miette!("middleware request spool ended early")); + } + if let Some(guard) = generation_guard { + guard.ensure_current()?; + } + upstream + .write_all(format!("{read:X}\r\n").as_bytes()) + .await + .into_diagnostic()?; + upstream + .write_all(&buffer[..read]) + .await + .into_diagnostic()?; + upstream.write_all(b"\r\n").await.into_diagnostic()?; + remaining -= read as u64; + } + upstream.write_all(b"0\r\n").await.into_diagnostic()?; + for trailer in &body.trailers { + upstream + .write_all(format!("{}: {}\r\n", trailer.name, trailer.value).as_bytes()) + .await + .into_diagnostic()?; + } + upstream.write_all(b"\r\n").await.into_diagnostic()?; + } + } + Ok(()) +} + +async fn relay_live_middleware_request_body( + body: &mut crate::l7::middleware::RequestBodyStream, + client: &mut C, + upstream: &mut U, + options: RelayRequestOptions<'_>, + inspect_credential_markers: bool, +) -> Result<()> +where + C: AsyncRead + Unpin, + U: AsyncWrite + Unpin, +{ + let (sender, mut receiver) = tokio::sync::mpsc::channel(4); + let run = body.run_to(client, sender); + let write = async { + let mut scanner = inspect_credential_markers + .then(|| ReservedMarkerStreamGuard::new(options.body_classifier)); + while let Some(unit) = receiver.recv().await { + let output = match scanner.as_mut() { + Some(scanner) => scanner.push(&unit)?, + None => unit, + }; + write_guarded_chunk(upstream, &output, options).await?; + } + Ok::<_, miette::Report>(scanner) + }; + let (finish, scanner) = tokio::join!(run, write); + let mut scanner = scanner?; + let finish = finish?; + if let Some(scanner) = scanner.take() { + write_guarded_chunk(upstream, &scanner.finish()?, options).await?; + } + write_body_bytes(upstream, b"0\r\n", options).await?; + for trailer in finish.trailers { + let encoded = format!("{}: {}", trailer.name, trailer.value); + if inspect_credential_markers + && contains_reserved_credential_marker_bytes(encoded.as_bytes()) + { + return Err(BodyCredentialError::Trailer.into()); + } + write_body_bytes(upstream, encoded.as_bytes(), options).await?; + write_body_bytes(upstream, b"\r\n", options).await?; + } + write_body_bytes(upstream, b"\r\n", options).await +} + use openshell_core::secrets::body::{ BodyCredentialError, BodyPlaceholderGuard as ReservedMarkerStreamGuard, }; @@ -1384,6 +1538,93 @@ where Ok(()) } +async fn relay_middleware_body_with_marker_guard( + req: &L7Request, + client: &mut C, + upstream: &mut U, + headers: &[u8], + body: &mut crate::l7::middleware::MiddlewareRequestBody, + options: RelayRequestOptions<'_>, +) -> Result<()> +where + C: AsyncRead + Unpin, + U: AsyncWrite + Unpin, +{ + ensure_body_generation_current(options)?; + upstream.write_all(headers).await.into_diagnostic()?; + match body { + crate::l7::middleware::MiddlewareRequestBody::Live(body) => { + relay_live_middleware_request_body(body, client, upstream, options, true).await + } + crate::l7::middleware::MiddlewareRequestBody::Spool(body) => { + relay_spooled_request_body_with_marker_guard(req, upstream, body, options).await + } + } +} + +async fn relay_spooled_request_body_with_marker_guard( + req: &L7Request, + upstream: &mut U, + body: &mut crate::l7::middleware::RequestBodySpool, + options: RelayRequestOptions<'_>, +) -> Result<()> { + body.file.seek(SeekFrom::Start(0)).await.into_diagnostic()?; + let mut remaining = body.len; + let mut buffer = [0u8; RELAY_BUF_SIZE]; + let mut scanner = ReservedMarkerStreamGuard::new(options.body_classifier); + + while remaining > 0 { + let limit = usize::try_from(remaining.min(buffer.len() as u64)) + .expect("relay buffer length fits usize"); + let read = body + .file + .read(&mut buffer[..limit]) + .await + .into_diagnostic()?; + if read == 0 { + return Err(miette!("middleware request spool ended early")); + } + let safe = scanner.push(&buffer[..read])?; + match req.body_length { + BodyLength::None => { + return Err(miette!("bodyless middleware request has spooled payload")); + } + BodyLength::ContentLength(_) => write_body_bytes(upstream, &safe, options).await?, + BodyLength::Chunked => write_guarded_chunk(upstream, &safe, options).await?, + } + remaining -= read as u64; + } + + let tail = scanner.finish()?; + match req.body_length { + BodyLength::None => { + if body.len != 0 || !body.trailers.is_empty() { + return Err(miette!("bodyless middleware request has spooled payload")); + } + } + BodyLength::ContentLength(expected) => { + if expected != body.len || !body.trailers.is_empty() { + return Err(miette!("middleware request spool framing mismatch")); + } + write_body_bytes(upstream, &tail, options).await?; + } + BodyLength::Chunked => { + write_guarded_chunk(upstream, &tail, options).await?; + write_body_bytes(upstream, b"0\r\n", options).await?; + for trailer in &body.trailers { + let encoded = format!("{}: {}", trailer.name, trailer.value); + if contains_reserved_credential_marker_bytes(encoded.as_bytes()) { + return Err(BodyCredentialError::Trailer.into()); + } + write_body_bytes(upstream, encoded.as_bytes(), options).await?; + write_body_bytes(upstream, b"\r\n", options).await?; + } + write_body_bytes(upstream, b"\r\n", options).await?; + } + } + Ok(()) +} + async fn relay_chunked_with_marker_guard( client: &mut C, upstream: &mut U, @@ -1531,6 +1772,277 @@ pub(crate) enum BufferResult { OverCapacity { recoverable: bool }, } +/// Incremental decoder for one normalized HTTP/1 request body. +pub(crate) enum RequestBodyReader { + None, + Fixed { + buffered: Vec, + buffered_pos: usize, + remaining: u64, + }, + Chunked(ChunkedRequestBodyReader), +} + +pub(crate) struct ChunkedRequestBodyReader { + buffered: Vec, + read_state: ChunkedReadState, + chunk_remaining: usize, + finished: bool, + trailers: Vec, + trailer_bytes: usize, +} + +/// Prepare headers and an incremental normalized body reader for request +/// middleware. This consumes `Expect: 100-continue` locally because middleware +/// must receive the body before `OpenShell` can contact the upstream. +pub(crate) async fn prepare_request_body_stream( + req: &L7Request, + client: &mut C, +) -> Result<(Vec, RequestBodyReader)> { + let header_end = req + .raw_header + .windows(4) + .position(|window| window == b"\r\n\r\n") + .map_or(req.raw_header.len(), |position| position + 4); + let mut headers = req.raw_header[..header_end].to_vec(); + let already_read = req.raw_header[header_end..].to_vec(); + let reader = match req.body_length { + BodyLength::None => { + if !already_read.is_empty() { + return Err(miette!( + "HTTP request with no body framing has {} unread byte(s) after headers", + already_read.len() + )); + } + handle_buffered_expect_continue(client, &mut headers, false).await?; + RequestBodyReader::None + } + BodyLength::ContentLength(length) => { + if already_read.len() as u64 > length { + return Err(miette!( + "HTTP request read-ahead exceeds its declared Content-Length" + )); + } + let needs_client_read = already_read.len() as u64 != length; + handle_buffered_expect_continue(client, &mut headers, needs_client_read).await?; + RequestBodyReader::Fixed { + buffered: already_read, + buffered_pos: 0, + remaining: length, + } + } + BodyLength::Chunked => { + let needs_client_read = !chunked_body_is_fully_buffered(&already_read); + handle_buffered_expect_continue(client, &mut headers, needs_client_read).await?; + RequestBodyReader::Chunked(ChunkedRequestBodyReader { + buffered: already_read, + read_state: ChunkedReadState { + buffered_pos: 0, + wire_bytes: 0, + max_wire_bytes: None, + }, + chunk_remaining: 0, + finished: false, + trailers: Vec::new(), + trailer_bytes: 0, + }) + } + }; + Ok((headers, reader)) +} + +impl RequestBodyReader { + pub(crate) async fn next_unit( + &mut self, + client: &mut C, + generation_guard: Option<&PolicyGenerationGuard>, + limit: usize, + ) -> Result>> { + if limit == 0 { + return Err(miette!("request middleware stream unit limit is zero")); + } + match self { + Self::None => Ok(None), + Self::Fixed { + buffered, + buffered_pos, + remaining, + } => { + if *remaining == 0 { + return Ok(None); + } + let length = usize::try_from((*remaining).min(limit as u64)) + .expect("stream unit limit fits usize"); + let mut unit = Vec::with_capacity(length); + let available = length.min(buffered.len().saturating_sub(*buffered_pos)); + if available > 0 { + let end = *buffered_pos + available; + unit.extend_from_slice(&buffered[*buffered_pos..end]); + *buffered_pos = end; + } + while unit.len() < length { + let start = unit.len(); + unit.resize(length, 0); + let read = client.read(&mut unit[start..]).await.into_diagnostic()?; + if read == 0 { + return Err(miette!( + "connection closed with {} request body bytes remaining", + remaining.saturating_sub(start as u64) + )); + } + unit.truncate(start + read); + if let Some(guard) = generation_guard { + guard.ensure_current()?; + } + } + *remaining -= unit.len() as u64; + Ok(Some(unit)) + } + Self::Chunked(reader) => reader.next_unit(client, generation_guard, limit).await, + } + } + + pub(crate) fn take_trailers(&mut self) -> Vec { + match self { + Self::Chunked(reader) => std::mem::take(&mut reader.trailers), + Self::None | Self::Fixed { .. } => Vec::new(), + } + } +} + +impl ChunkedRequestBodyReader { + async fn next_unit( + &mut self, + client: &mut C, + generation_guard: Option<&PolicyGenerationGuard>, + limit: usize, + ) -> Result>> { + if self.finished { + return Ok(None); + } + if self.chunk_remaining == 0 { + let size_line = read_chunked_line( + client, + &self.buffered, + &mut self.read_state, + generation_guard, + ) + .await + .map_err(CollectChunkedError::into_report)?; + let size_line = std::str::from_utf8(&size_line) + .map_err(|_| miette!("invalid UTF-8 in chunk-size line"))?; + let size_token = size_line + .split(';') + .next() + .map(str::trim) + .unwrap_or_default(); + self.chunk_remaining = usize::from_str_radix(size_token, 16) + .map_err(|_| miette!("invalid chunk size token: {size_token:?}"))?; + if self.chunk_remaining == 0 { + self.read_trailers(client, generation_guard).await?; + self.finished = true; + return Ok(None); + } + } + + let length = self.chunk_remaining.min(limit); + let mut unit = Vec::with_capacity(length); + read_buffered_exact( + client, + &self.buffered, + &mut self.read_state, + length, + &mut unit, + generation_guard, + ) + .await + .map_err(CollectChunkedError::into_report)?; + self.chunk_remaining -= length; + if self.chunk_remaining == 0 { + let mut terminator = Vec::with_capacity(2); + read_buffered_exact( + client, + &self.buffered, + &mut self.read_state, + 2, + &mut terminator, + generation_guard, + ) + .await + .map_err(CollectChunkedError::into_report)?; + if terminator.as_slice() != b"\r\n" { + return Err(miette!("chunk missing terminating CRLF")); + } + } + Ok(Some(unit)) + } + + async fn read_trailers( + &mut self, + client: &mut C, + generation_guard: Option<&PolicyGenerationGuard>, + ) -> Result<()> { + loop { + let line = read_chunked_line( + client, + &self.buffered, + &mut self.read_state, + generation_guard, + ) + .await + .map_err(CollectChunkedError::into_report)?; + if line.is_empty() { + return Ok(()); + } + self.trailer_bytes = self.trailer_bytes.saturating_add(line.len()); + if self.trailer_bytes > openshell_supervisor_middleware::MAX_MIDDLEWARE_HEADER_BYTES { + return Err(miette!("request trailers exceed platform limit")); + } + if self.trailers.len() >= openshell_supervisor_middleware::MAX_MIDDLEWARE_HEADERS { + return Err(miette!("request trailer count exceeds platform limit")); + } + self.trailers.push(parse_request_trailer(&line)?); + } + } +} + +fn parse_request_trailer(line: &[u8]) -> Result { + let Some(separator) = line.iter().position(|byte| *byte == b':') else { + return Err(miette!("request trailer is missing ':'")); + }; + let name = &line[..separator]; + let value = &line[separator + 1..]; + if name.is_empty() || !name.iter().copied().all(is_http_field_name_byte) { + return Err(miette!("request trailer has an invalid field name")); + } + if !value.iter().copied().all(is_http_field_value_byte) { + return Err(miette!("request trailer has an invalid field value")); + } + let name = std::str::from_utf8(name) + .expect("validated HTTP field names are ASCII") + .to_ascii_lowercase(); + if matches!( + name.as_str(), + "authorization" + | "content-length" + | "cookie" + | "host" + | "proxy-authorization" + | "te" + | "trailer" + | "transfer-encoding" + ) || name.starts_with("x-amz-") + || name.starts_with("x-openshell-credential") + { + return Err(miette!("request trailer uses protected field '{name}'")); + } + let value = std::str::from_utf8(value) + .map_err(|_| miette!("request trailer value is not valid UTF-8"))? + .trim() + .to_string(); + Ok(HttpHeader { name, value }) +} + pub(crate) async fn buffer_request_body_for_middleware( req: &L7Request, client: &mut C, @@ -1666,7 +2178,17 @@ fn chunked_body_is_fully_buffered(bytes: &[u8]) -> bool { }; pos = line_end + 2; if chunk_size == 0 { - return bytes.get(pos..pos.saturating_add(2)) == Some(b"\r\n"); + loop { + let Some(trailer_end) = + bytes[pos..].windows(2).position(|window| window == b"\r\n") + else { + return false; + }; + if trailer_end == 0 { + return true; + } + pos = pos.saturating_add(trailer_end + 2); + } } let Some(chunk_end) = pos.checked_add(chunk_size) else { return false; @@ -1720,6 +2242,93 @@ pub(crate) fn rebuild_request_with_buffered_body( }) } +/// Apply request-head mutations without consuming or changing body framing. +pub(crate) fn rebuild_request_headers_only( + req: &L7Request, + header_mutations: &[HeaderMutation], +) -> Result { + let header_end = req + .raw_header + .windows(4) + .position(|window| window == b"\r\n\r\n") + .map_or(req.raw_header.len(), |position| position + 4); + let mut raw_header = apply_header_mutations(&req.raw_header[..header_end], header_mutations)?; + raw_header.extend_from_slice(&req.raw_header[header_end..]); + Ok(L7Request { + action: req.action.clone(), + target: req.target.clone(), + query_params: req.query_params.clone(), + raw_header, + body_length: req.body_length, + }) +} + +/// Reframe an incrementally transformed request as HTTP/1.1 chunked transfer. +/// The middleware may change every unit's length, so the supervisor cannot +/// preserve a caller-provided `Content-Length` before the stream completes. +pub(crate) fn rebuild_request_for_incremental_stream( + req: &L7Request, + headers: &[u8], + header_mutations: &[HeaderMutation], +) -> Result { + let mut header_bytes = strip_header(headers, "content-length")?; + header_bytes = strip_header(&header_bytes, "transfer-encoding")?; + header_bytes = append_header(&header_bytes, "Transfer-Encoding", "chunked"); + header_bytes = apply_header_mutations(&header_bytes, header_mutations)?; + Ok(L7Request { + action: req.action.clone(), + target: req.target.clone(), + query_params: req.query_params.clone(), + raw_header: header_bytes, + body_length: BodyLength::Chunked, + }) +} + +/// Rebuild a request whose normalized middleware output lives in a separate +/// spool. The raw request contains only the new head; relay writes the spool. +pub(crate) fn rebuild_request_with_streamed_body( + req: &L7Request, + headers: &[u8], + body_length: u64, + trailers: &[HttpHeader], + header_mutations: &[HeaderMutation], +) -> Result { + let (mut header_bytes, framing) = + if matches!(req.body_length, BodyLength::None) && body_length == 0 && trailers.is_empty() { + let mut headers = strip_header(headers, "content-length")?; + headers = strip_header(&headers, "transfer-encoding")?; + headers = strip_header(&headers, "trailer")?; + (headers, BodyLength::None) + } else if trailers.is_empty() { + let length = usize::try_from(body_length) + .map_err(|_| miette!("middleware request body length is not representable"))?; + let mut headers = set_content_length(headers, length)?; + headers = strip_header(&headers, "transfer-encoding")?; + headers = strip_header(&headers, "trailer")?; + (headers, BodyLength::ContentLength(body_length)) + } else { + let mut headers = strip_header(headers, "content-length")?; + headers = strip_header(&headers, "transfer-encoding")?; + headers = append_header(&headers, "Transfer-Encoding", "chunked"); + headers = strip_header(&headers, "trailer")?; + let names = trailers + .iter() + .map(|trailer| trailer.name.as_str()) + .collect::>() + .join(", "); + headers = append_header(&headers, "Trailer", &names); + (headers, BodyLength::Chunked) + }; + header_bytes = apply_header_mutations(&header_bytes, header_mutations)?; + Ok(L7Request { + action: req.action.clone(), + target: req.target.clone(), + query_params: req.query_params.clone(), + raw_header: header_bytes, + body_length: framing, + }) +} + async fn collect_and_rewrite_request_body( req: &L7Request, client: &mut C, @@ -1791,6 +2400,46 @@ async fn collect_and_rewrite_request_body( } } +async fn collect_and_rewrite_spooled_request_body( + spool: &mut crate::l7::middleware::RequestBodySpool, + rewritten_headers: &[u8], + original_header_str: &str, + resolver: Option<&SecretResolver>, + generation_guard: Option<&PolicyGenerationGuard>, +) -> Result { + if spool.len > MAX_REWRITE_BODY_BYTES as u64 { + return Err(miette!( + "request body credential rewrite buffers at most {MAX_REWRITE_BODY_BYTES} bytes" + )); + } + if !spool.trailers.is_empty() { + return Err(miette!( + "request body credential rewrite does not support transformed request trailers" + )); + } + if let Some(guard) = generation_guard { + guard.ensure_current()?; + } + spool + .file + .seek(SeekFrom::Start(0)) + .await + .into_diagnostic()?; + let capacity = usize::try_from(spool.len) + .map_err(|_| miette!("middleware request body is too large for credential rewrite"))?; + let mut bytes = Vec::with_capacity(capacity); + spool.file.read_to_end(&mut bytes).await.into_diagnostic()?; + if bytes.len() as u64 != spool.len { + return Err(miette!("middleware request spool ended early")); + } + let (mut headers, body) = + rewrite_buffered_body(rewritten_headers, original_header_str, bytes, resolver)?; + headers = set_content_length(&headers, body.len())?; + headers = strip_header(&headers, "transfer-encoding")?; + headers = strip_header(&headers, "trailer")?; + Ok(PreparedRequestBody { headers, body }) +} + fn rewrite_buffered_body( headers: &[u8], original_header_str: &str, @@ -2957,65 +3606,6 @@ fn has_expect_continue(headers: &str) -> bool { }) } -/// Resolved payload signing mode for a `SigV4` request. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum SigV4PayloadMode { - /// Buffer body and include its SHA-256 hash in the signature. - SignBody, - /// Use literal `UNSIGNED-PAYLOAD` — no body buffering needed. - UnsignedPayload, - /// Use `STREAMING-UNSIGNED-PAYLOAD-TRAILER` for `aws-chunked` streams. - StreamingUnsignedTrailer, -} - -impl fmt::Display for SigV4PayloadMode { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::SignBody => write!(f, "sign_body"), - Self::UnsignedPayload => write!(f, "unsigned_payload"), - Self::StreamingUnsignedTrailer => write!(f, "streaming_unsigned_trailer"), - } - } -} - -/// Auto-detect the payload signing mode from the client's original headers. -/// -/// Mirrors the mode the client SDK chose by inspecting `x-amz-content-sha256`: -/// - `STREAMING-UNSIGNED-PAYLOAD-TRAILER` → `StreamingUnsignedTrailer` -/// - `UNSIGNED-PAYLOAD` → `UnsignedPayload` -/// - Hex hash → `SignBody` (buffer + hash, requires `Content-Length`) -/// - `STREAMING-AWS4-HMAC-SHA256-PAYLOAD` → **rejected** (the proxy cannot -/// reproduce per-chunk signatures; use `sigv4:no_body` instead) -/// - Other `STREAMING-*` values → **rejected** (unsupported streaming mode) -/// - Absent → `SignBody` if `Content-Length` present, else `UnsignedPayload` -fn detect_payload_mode(headers: &str) -> Result { - for line in headers.lines().skip(1) { - let lower = line.to_ascii_lowercase(); - if lower.starts_with("x-amz-content-sha256:") { - let val = lower.split_once(':').map_or("", |(_, v)| v.trim()); - return match val { - "streaming-unsigned-payload-trailer" => { - Ok(SigV4PayloadMode::StreamingUnsignedTrailer) - } - "unsigned-payload" => Ok(SigV4PayloadMode::UnsignedPayload), - v if v.starts_with("streaming-") => Err(miette!( - "SigV4 auto-detect does not support chunk-signed streaming mode \ - '{v}'; use credential_signing: sigv4:no_body to stream \ - with UNSIGNED-PAYLOAD instead" - )), - _ => Ok(SigV4PayloadMode::SignBody), - }; - } - } - Ok( - if matches!(parse_body_length(headers)?, BodyLength::ContentLength(_)) { - SigV4PayloadMode::SignBody - } else { - SigV4PayloadMode::UnsignedPayload - }, - ) -} - /// Parse Content-Length or Transfer-Encoding from HTTP headers. /// /// Per RFC 7230 Section 3.3.3, rejects requests containing both @@ -3539,13 +4129,13 @@ mod tests { use openshell_core::endpoint_status::{EndpointStatusCommand, EndpointStatusReceiver}; use openshell_core::proposals::AgentProposals; use openshell_core::proto::{ - Decision, HttpRequestResult, HttpResponseBlockDelivery, HttpResponseBodyMode, - HttpResponseBodyResult, HttpResponseBodyTransform, HttpResponseEvent, - HttpResponseEventResult, HttpResponsePreflightInspect, HttpResponsePreflightResult, - HttpResponseTrailersResult, MiddlewareBinding, MiddlewareManifest, - SupervisorMiddlewareOperation, SupervisorMiddlewarePhase, http_response_body_result, - http_response_body_transform, http_response_body_unit, http_response_event, - http_response_event_result, http_response_preflight_result, + HttpResponseBlockDelivery, HttpResponseBodyMode, HttpResponseBodyResult, + HttpResponseBodyTransform, HttpResponseEvent, HttpResponseEventResult, + HttpResponsePreflightInspect, HttpResponsePreflightResult, HttpResponseTrailersResult, + MiddlewareBinding, MiddlewareManifest, SupervisorMiddlewareOperation, + SupervisorMiddlewarePhase, http_response_body_result, http_response_body_transform, + http_response_body_unit, http_response_event, http_response_event_result, + http_response_preflight_result, }; use openshell_core::secrets::SecretResolver; use std::pin::Pin; @@ -3560,6 +4150,119 @@ mod tests { const VALID_WS_ACCEPT: &str = "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="; const TEXT_OPCODE: u8 = 0x1; + fn request_with_body(raw_header: Vec, body_length: BodyLength) -> L7Request { + L7Request { + action: "POST".into(), + target: "/push".into(), + query_params: HashMap::new(), + raw_header, + body_length, + } + } + + #[tokio::test] + async fn request_body_stream_reads_fixed_body_larger_than_former_unary_limit() { + let body_len = 4 * 1024 * 1024 + 17; + let headers = format!( + "POST /push HTTP/1.1\r\nHost: example.com\r\nContent-Length: {body_len}\r\n\r\n" + ) + .into_bytes(); + let request = request_with_body(headers, BodyLength::ContentLength(body_len as u64)); + let (mut client, mut writer) = tokio::io::duplex(1024); + let write = tokio::spawn(async move { + let chunk = vec![b'x'; 64 * 1024]; + let mut remaining = body_len; + while remaining > 0 { + let length = remaining.min(chunk.len()); + writer.write_all(&chunk[..length]).await.unwrap(); + remaining -= length; + } + }); + + let (_headers, mut reader) = prepare_request_body_stream(&request, &mut client) + .await + .unwrap(); + let mut received = 0usize; + while let Some(unit) = reader + .next_unit(&mut client, None, 64 * 1024) + .await + .unwrap() + { + assert!(unit.len() <= 64 * 1024); + assert!(unit.iter().all(|byte| *byte == b'x')); + received += unit.len(); + } + write.await.unwrap(); + assert_eq!(received, body_len); + assert!(reader.take_trailers().is_empty()); + } + + #[tokio::test] + async fn request_body_stream_normalizes_chunks_and_preserves_safe_trailers() { + let headers = b"POST /push HTTP/1.1\r\nHost: example.com\r\nTransfer-Encoding: chunked\r\nTrailer: X-Trace\r\n\r\n".to_vec(); + let request = request_with_body(headers, BodyLength::Chunked); + let (mut client, mut writer) = tokio::io::duplex(128); + let write = tokio::spawn(async move { + writer + .write_all(b"4\r\nWiki\r\n5;sample=yes\r\npedia\r\n0\r\nX-Trace: complete\r\n\r\n") + .await + .unwrap(); + }); + + let (prepared_headers, mut reader) = prepare_request_body_stream(&request, &mut client) + .await + .unwrap(); + let mut normalized = Vec::new(); + while let Some(unit) = reader.next_unit(&mut client, None, 3).await.unwrap() { + assert!(unit.len() <= 3); + normalized.extend_from_slice(&unit); + } + write.await.unwrap(); + assert_eq!(normalized, b"Wikipedia"); + let trailers = reader.take_trailers(); + assert_eq!( + trailers, + [HttpHeader { + name: "x-trace".into(), + value: "complete".into(), + }] + ); + + let rebuilt = rebuild_request_with_streamed_body( + &request, + &prepared_headers, + normalized.len() as u64, + &trailers, + &[], + ) + .unwrap(); + assert!(matches!(rebuilt.body_length, BodyLength::Chunked)); + let rebuilt_headers = String::from_utf8(rebuilt.raw_header.clone()).unwrap(); + assert!(rebuilt_headers.contains("Transfer-Encoding: chunked\r\n")); + assert!(rebuilt_headers.contains("Trailer: x-trace\r\n")); + assert!( + !rebuilt_headers + .to_ascii_lowercase() + .contains("content-length:") + ); + + let mut file = tokio::fs::File::from_std(tempfile::tempfile().unwrap()); + file.write_all(&normalized).await.unwrap(); + let mut spool = crate::l7::middleware::RequestBodySpool { + file, + len: normalized.len() as u64, + trailers: trailers.clone(), + }; + let mut upstream = Vec::new(); + relay_spooled_request_body(&rebuilt, &mut upstream, &mut spool, None) + .await + .unwrap(); + assert_eq!( + upstream, + b"9\r\nWikipedia\r\n0\r\nx-trace: complete\r\n\r\n" + ); + } + #[derive(Clone, Copy)] enum ResponseRelayScript { HeadersOnly, @@ -3620,14 +4323,53 @@ mod tests { Ok(()) } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - _request: openshell_supervisor_middleware::HttpRequestView<'_>, - ) -> Result { - Ok(HttpRequestResult { - decision: Decision::Allow as i32, - ..Default::default() - }) + mut requests: mpsc::Receiver, + ) -> std::result::Result< + openshell_supervisor_middleware::HttpRequestResultStream, + tonic::Status, + > { + assert!( + self.request_only, + "response-only service received a request" + ); + let (sender, receiver) = mpsc::channel(2); + tokio::spawn(async move { + use openshell_core::proto::{ + HttpRequestBodyMode, HttpRequestEventResult, HttpRequestPreflightInspect, + HttpRequestPreflightResult, http_request_event, http_request_event_result, + http_request_preflight_result, + }; + while let Some(event) = requests.recv().await { + match event.event { + Some(http_request_event::Event::Preflight(_)) => { + let result = HttpRequestEventResult { + result: Some(http_request_event_result::Result::PreflightResult( + HttpRequestPreflightResult { + action: Some( + http_request_preflight_result::Action::Inspect( + HttpRequestPreflightInspect { + body_mode: HttpRequestBodyMode::HeadersOnly + as i32, + header_mutations: Vec::new(), + }, + ), + ), + ..Default::default() + }, + )), + }; + if sender.send(Ok(result)).await.is_err() { + break; + } + } + Some(http_request_event::Event::SessionEnd(_)) | None => break, + Some(_) => panic!("headers-only request received an unexpected event"), + } + } + }); + Ok(Box::pin(ReceiverStream::new(receiver))) } async fn open_http_response_pre_return( @@ -4980,6 +5722,46 @@ mod tests { ); } + #[tokio::test] + async fn credential_rewrite_uses_spooled_middleware_output() { + let (child_env, resolver) = SecretResolver::from_provider_env( + [("API_TOKEN".to_string(), "provider-real-token".to_string())] + .into_iter() + .collect(), + ); + let resolver = resolver.expect("resolver"); + let body = format!(r#"{{"token":"{}"}}"#, child_env["API_TOKEN"]); + let headers = format!( + "POST /api HTTP/1.1\r\nHost: example.com\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n", + body.len() + ); + let mut file = tokio::fs::File::from_std(tempfile::tempfile().unwrap()); + file.write_all(body.as_bytes()).await.unwrap(); + file.flush().await.unwrap(); + let mut spool = crate::l7::middleware::RequestBodySpool { + file, + len: body.len() as u64, + trailers: Vec::new(), + }; + + let rewritten = collect_and_rewrite_spooled_request_body( + &mut spool, + headers.as_bytes(), + &headers, + Some(&resolver), + None, + ) + .await + .expect("rewrite middleware output"); + + assert_eq!(rewritten.body, br#"{"token":"provider-real-token"}"#); + assert!( + String::from_utf8(rewritten.headers) + .unwrap() + .contains(&format!("Content-Length: {}\r\n", rewritten.body.len())) + ); + } + #[tokio::test] async fn collect_chunked_body_reads_payload_in_blocks() { let payload_len = 64 * 1024; @@ -9556,9 +10338,14 @@ mod tests { &mut proxy_to_upstream, RelayRequestOptions { resolver: Some(&resolver), - credential_signing: crate::l7::CredentialSigning::SigV4Body, - signing_service: "s3", - signing_region: "us-east-1", + post_credentials: + crate::l7::post_credentials::PostCredentialsMiddleware::from_endpoint( + crate::l7::CredentialSigning::SigV4Body, + "s3", + "us-east-1", + "s3.us-east-1.amazonaws.com", + 443, + ), host: "s3.us-east-1.amazonaws.com", port: 443, ..Default::default() @@ -9724,70 +10511,6 @@ mod tests { ); } - #[test] - fn detect_payload_mode_unsigned_payload() { - let headers = "PUT /bucket/key HTTP/1.1\r\nHost: s3.us-east-1.amazonaws.com\r\nX-Amz-Content-Sha256: UNSIGNED-PAYLOAD\r\n\r\n"; - assert_eq!( - detect_payload_mode(headers).unwrap(), - SigV4PayloadMode::UnsignedPayload - ); - } - - #[test] - fn detect_payload_mode_streaming_unsigned_trailer() { - let headers = "PUT /bucket/key HTTP/1.1\r\nHost: s3.us-east-1.amazonaws.com\r\nX-Amz-Content-Sha256: STREAMING-UNSIGNED-PAYLOAD-TRAILER\r\n\r\n"; - assert_eq!( - detect_payload_mode(headers).unwrap(), - SigV4PayloadMode::StreamingUnsignedTrailer - ); - } - - #[test] - fn detect_payload_mode_hex_hash_is_sign_body() { - let headers = "POST /model/invoke HTTP/1.1\r\nHost: bedrock.amazonaws.com\r\nX-Amz-Content-Sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\r\nContent-Length: 10\r\n\r\n"; - assert_eq!( - detect_payload_mode(headers).unwrap(), - SigV4PayloadMode::SignBody - ); - } - - #[test] - fn detect_payload_mode_rejects_chunk_signed_streaming() { - let headers = "PUT /bucket/key HTTP/1.1\r\nHost: s3.us-east-1.amazonaws.com\r\nX-Amz-Content-Sha256: STREAMING-AWS4-HMAC-SHA256-PAYLOAD\r\n\r\n"; - let result = detect_payload_mode(headers); - assert!(result.is_err()); - let msg = result.unwrap_err().to_string(); - assert!( - msg.contains("sigv4:no_body"), - "error should suggest sigv4:no_body, got: {msg}" - ); - } - - #[test] - fn detect_payload_mode_rejects_unknown_streaming() { - let headers = "PUT /bucket/key HTTP/1.1\r\nHost: s3.us-east-1.amazonaws.com\r\nX-Amz-Content-Sha256: STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER\r\n\r\n"; - let result = detect_payload_mode(headers); - assert!(result.is_err()); - } - - #[test] - fn detect_payload_mode_absent_with_content_length() { - let headers = "POST /model/invoke HTTP/1.1\r\nHost: bedrock.amazonaws.com\r\nContent-Length: 42\r\n\r\n"; - assert_eq!( - detect_payload_mode(headers).unwrap(), - SigV4PayloadMode::SignBody - ); - } - - #[test] - fn detect_payload_mode_absent_without_content_length() { - let headers = "GET /bucket HTTP/1.1\r\nHost: s3.amazonaws.com\r\n\r\n"; - assert_eq!( - detect_payload_mode(headers).unwrap(), - SigV4PayloadMode::UnsignedPayload - ); - } - #[test] fn response_body_transform_strips_stale_integrity_headers() { let mut headers = [ diff --git a/crates/openshell-supervisor-network/src/l7/websocket.rs b/crates/openshell-supervisor-network/src/l7/websocket.rs index 77ed82508..ac6b65f76 100644 --- a/crates/openshell-supervisor-network/src/l7/websocket.rs +++ b/crates/openshell-supervisor-network/src/l7/websocket.rs @@ -3587,14 +3587,6 @@ network_policies: )) } - async fn evaluate_http_request( - &self, - _request: Request, - ) -> std::result::Result, Status> - { - Err(Status::unimplemented("WebSocket-only test middleware")) - } - async fn evaluate_web_socket_session( &self, request: Request>, diff --git a/crates/openshell-supervisor-network/src/lib.rs b/crates/openshell-supervisor-network/src/lib.rs index 458e236f0..810d683dd 100644 --- a/crates/openshell-supervisor-network/src/lib.rs +++ b/crates/openshell-supervisor-network/src/lib.rs @@ -19,7 +19,6 @@ pub mod policy_local; pub mod procfs; pub mod proxy; pub mod run; -pub mod sigv4; mod spiffe_endpoint; mod token_grant; pub mod upstream_proxy; diff --git a/crates/openshell-supervisor-network/src/proxy.rs b/crates/openshell-supervisor-network/src/proxy.rs index 02206063a..bbcb9c8c5 100644 --- a/crates/openshell-supervisor-network/src/proxy.rs +++ b/crates/openshell-supervisor-network/src/proxy.rs @@ -1548,6 +1548,7 @@ impl ForwardMiddlewarePipeline<'_> { C: TokioAsyncRead + TokioAsyncWrite + Unpin + Send, { let validate; + let accept_without_reevaluation = |_body: &[u8]| Ok(None); let transformed_body_policy = match &self.l7_reevaluation { Some(l7) => { validate = crate::l7::relay::transformed_body_validator( @@ -1558,7 +1559,12 @@ impl ForwardMiddlewarePipeline<'_> { ); openshell_supervisor_middleware::TransformedBodyPolicy::Reevaluate(&validate) } - None => openshell_supervisor_middleware::TransformedBodyPolicy::NotPolicyRelevant, + // The forward-proxy request is already fully buffered before this + // pipeline runs. Keep it on the compatibility path until the + // forward relay can carry a storage-backed request body. + None => openshell_supervisor_middleware::TransformedBodyPolicy::Reevaluate( + &accept_without_reevaluation, + ), }; self.exchange @@ -4708,9 +4714,7 @@ struct ForwardRelayOptions<'a> { secret_resolver: Option<&'a SecretResolver>, request_body_credential_rewrite: bool, deny_uninspected_credentials: bool, - credential_signing: crate::l7::CredentialSigning, - signing_service: &'a str, - signing_region: &'a str, + post_credentials: Option>, host: &'a str, port: u16, response_middleware: Option>, @@ -4768,12 +4772,11 @@ where websocket_extensions: options.websocket_extensions, request_body_credential_rewrite: options.request_body_credential_rewrite, deny_uninspected_credentials: options.deny_uninspected_credentials, - credential_signing: options.credential_signing, - signing_service: options.signing_service, - signing_region: options.signing_region, + post_credentials: options.post_credentials, host: options.host, port: options.port, }, + None, response_middleware, options.endpoint_observer, ) @@ -5768,8 +5771,14 @@ async fn handle_forward_proxy( exchange: &middleware_exchange, l7_reevaluation, }; - forward_request_bytes = match pipeline.apply(request, client).await? { + let middleware_result = pipeline.apply(request, client).await?; + forward_request_bytes = match middleware_result { crate::l7::middleware::MiddlewareApplyResult::Allowed(request) => request.raw_header, + crate::l7::middleware::MiddlewareApplyResult::Streamed { .. } => { + return Err(miette::miette!( + "forward middleware unexpectedly returned a storage-backed request body" + )); + } crate::l7::middleware::MiddlewareApplyResult::Denied { denial, .. } => { emit_activity_simple(activity_tx, true, "middleware"); let response = denial.as_ref().map_or_else( @@ -6094,17 +6103,15 @@ async fn handle_forward_proxy( return Ok(()); } - let credential_signing = forward_upgrade_config - .as_ref() - .map_or(crate::l7::CredentialSigning::None, |config| { - config.credential_signing - }); - let signing_service = forward_upgrade_config - .as_ref() - .map_or("", |config| config.signing_service.as_str()); - let signing_region = forward_upgrade_config - .as_ref() - .map_or("", |config| config.signing_region.as_str()); + let post_credentials = forward_upgrade_config.as_ref().and_then(|config| { + crate::l7::post_credentials::PostCredentialsMiddleware::from_endpoint( + config.credential_signing, + &config.signing_service, + &config.signing_region, + &host_lc, + port, + ) + }); let outcome_result = relay_rewritten_forward_request( method, &upstream_target, @@ -6119,9 +6126,7 @@ async fn handle_forward_proxy( body_classifier: endpoint_credentials.body_classifier.as_deref(), request_body_credential_rewrite, deny_uninspected_credentials, - credential_signing, - signing_service, - signing_region, + post_credentials, host: &host_lc, port, response_middleware: response_selection.as_ref().map(|exchange| { @@ -6709,16 +6714,6 @@ process: )) } - async fn evaluate_http_request( - &self, - _request: tonic::Request, - ) -> std::result::Result< - tonic::Response, - tonic::Status, - > { - Err(tonic::Status::unimplemented("WebSocket-only test service")) - } - async fn open_websocket_session( &self, mut receiver: mpsc::Receiver, @@ -6791,16 +6786,6 @@ process: Ok(()) } - async fn evaluate_http_request( - &self, - _request: openshell_core::middleware::HttpRequestView<'_>, - ) -> Result { - Ok(openshell_core::proto::HttpRequestResult { - decision: openshell_core::proto::Decision::Allow as i32, - ..Default::default() - }) - } - async fn open_http_response_pre_return( &self, mut requests: mpsc::Receiver, @@ -6897,16 +6882,48 @@ process: Ok(()) } - async fn evaluate_http_request( + async fn open_http_request_pre_credentials( &self, - _request: openshell_core::middleware::HttpRequestView<'_>, - ) -> Result { - self.entered.notify_one(); - self.release.notified().await; - Ok(openshell_core::proto::HttpRequestResult { - decision: openshell_core::proto::Decision::Allow as i32, - ..Default::default() - }) + mut requests: mpsc::Receiver, + ) -> std::result::Result + { + let entered = Arc::clone(&self.entered); + let release = Arc::clone(&self.release); + let (sender, receiver) = mpsc::channel(2); + tokio::spawn(async move { + use openshell_core::proto::{ + HttpRequestBodyMode, HttpRequestEventResult, HttpRequestPreflightInspect, + HttpRequestPreflightResult, http_request_event, http_request_event_result, + http_request_preflight_result, + }; + let Some(openshell_core::proto::HttpRequestEvent { + event: Some(http_request_event::Event::Preflight(_)), + }) = requests.recv().await + else { + return; + }; + entered.notify_one(); + release.notified().await; + let _ = sender + .send(Ok(HttpRequestEventResult { + result: Some(http_request_event_result::Result::PreflightResult( + HttpRequestPreflightResult { + action: Some(http_request_preflight_result::Action::Inspect( + HttpRequestPreflightInspect { + body_mode: HttpRequestBodyMode::HeadersOnly as i32, + header_mutations: Vec::new(), + }, + )), + ..Default::default() + }, + )), + })) + .await; + while requests.recv().await.is_some() {} + }); + Ok(Box::pin(tokio_stream::wrappers::ReceiverStream::new( + receiver, + ))) } } @@ -8350,6 +8367,9 @@ network_policies: crate::l7::middleware::MiddlewareApplyResult::Denied { denial } => { assert!(denial.is_none()); } + crate::l7::middleware::MiddlewareApplyResult::Streamed { .. } => { + panic!("body-aware forward middleware must use the buffered policy path") + } crate::l7::middleware::MiddlewareApplyResult::Allowed(_) => { panic!("policy-invalid transformed request must be denied") } @@ -8447,6 +8467,9 @@ network_policies: let (outcome, ()) = tokio::join!(pipeline.apply(request, &mut client), revoke); let request = match outcome.expect("middleware pipeline") { crate::l7::middleware::MiddlewareApplyResult::Allowed(request) => request, + crate::l7::middleware::MiddlewareApplyResult::Streamed { .. } => { + panic!("forward middleware compatibility path must stay buffered") + } crate::l7::middleware::MiddlewareApplyResult::Denied { .. } => { panic!("blocking middleware should allow after release") } @@ -8615,9 +8638,7 @@ network_policies: secret_resolver: None, request_body_credential_rewrite: false, deny_uninspected_credentials: false, - credential_signing: crate::l7::CredentialSigning::None, - signing_service: "", - signing_region: "", + post_credentials: None, host: "api.example.test", port: 80, response_middleware: Some(ForwardResponseMiddleware { @@ -8704,9 +8725,7 @@ network_policies: secret_resolver: None, request_body_credential_rewrite: false, deny_uninspected_credentials: false, - credential_signing: crate::l7::CredentialSigning::None, - signing_service: "", - signing_region: "", + post_credentials: None, host: "api.example.test", port: 80, response_middleware: Some(ForwardResponseMiddleware { @@ -8826,9 +8845,7 @@ network_policies: secret_resolver: resolver, request_body_credential_rewrite, deny_uninspected_credentials: body_classifier.is_some(), - credential_signing: crate::l7::CredentialSigning::None, - signing_service: "", - signing_region: "", + post_credentials: None, host: "", port: 0, response_middleware: None, @@ -9093,9 +9110,7 @@ network_policies: secret_resolver: None, request_body_credential_rewrite: false, deny_uninspected_credentials: false, - credential_signing: crate::l7::CredentialSigning::None, - signing_service: "", - signing_region: "", + post_credentials: None, host: "", port: 0, response_middleware: None, @@ -11641,9 +11656,7 @@ network_policies: secret_resolver: Some(&resolver), request_body_credential_rewrite: true, deny_uninspected_credentials: false, - credential_signing: crate::l7::CredentialSigning::None, - signing_service: "", - signing_region: "", + post_credentials: None, host: "", port: 0, response_middleware: None, @@ -11725,9 +11738,14 @@ network_policies: secret_resolver: Some(&resolver), request_body_credential_rewrite: false, deny_uninspected_credentials: false, - credential_signing: crate::l7::CredentialSigning::SigV4NoBody, - signing_service: "execute-api", - signing_region: "us-west-2", + post_credentials: + crate::l7::post_credentials::PostCredentialsMiddleware::from_endpoint( + crate::l7::CredentialSigning::SigV4NoBody, + "execute-api", + "us-west-2", + "api.example.com", + 80, + ), host: "api.example.com", port: 80, response_middleware: None, @@ -11816,9 +11834,7 @@ network_policies: secret_resolver: None, request_body_credential_rewrite: false, deny_uninspected_credentials: false, - credential_signing: crate::l7::CredentialSigning::None, - signing_service: "", - signing_region: "", + post_credentials: None, host: "", port: 0, response_middleware: None, @@ -11869,9 +11885,7 @@ network_policies: secret_resolver: None, request_body_credential_rewrite: false, deny_uninspected_credentials: false, - credential_signing: crate::l7::CredentialSigning::None, - signing_service: "", - signing_region: "", + post_credentials: None, host: "", port: 0, response_middleware: None, diff --git a/crates/openshell-supervisor-network/tests/sigv4_localstack.rs b/crates/openshell-supervisor-network/tests/sigv4_localstack.rs index 2d76b7e2f..d5d103f6e 100644 --- a/crates/openshell-supervisor-network/tests/sigv4_localstack.rs +++ b/crates/openshell-supervisor-network/tests/sigv4_localstack.rs @@ -5,7 +5,9 @@ // Requires LocalStack running on localhost:4566. // Run with: cargo test -p openshell-supervisor-network --test sigv4_localstack -- --ignored --nocapture -use openshell_supervisor_network::sigv4::{apply_sigv4_headers_only, apply_sigv4_to_request}; +use openshell_supervisor_middleware_builtins::sigv4::{ + apply_sigv4_headers_only, apply_sigv4_to_request, +}; use std::sync::atomic::{AtomicU32, Ordering}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpStream; diff --git a/docs/extensibility/supervisor-middleware.mdx b/docs/extensibility/supervisor-middleware.mdx index 13935436c..d04044aec 100644 --- a/docs/extensibility/supervisor-middleware.mdx +++ b/docs/extensibility/supervisor-middleware.mdx @@ -17,10 +17,13 @@ For each inspected HTTP request, the supervisor: 1. Evaluates network and L7 policy. 2. Selects middleware whose host selectors match the admitted destination. -3. Buffers the request body using the largest body limit in the selected chain. -4. Runs matching middleware by ascending `order`. Policy validation rejects duplicate order values. -5. Re-checks body-aware protocol policy (GraphQL, JSON-RPC, MCP) after each stage that replaces the body. Every middleware receives a payload the policy admits, and a transformation cannot smuggle a denied or unparseable operation to a later stage or the upstream. -6. Applies allowed transformations, injects provider credentials, and forwards the request. +3. Opens one `HttpRequestPreCredentials.Evaluate` bidirectional stream for each matching request binding, in ascending `order`. +4. Sends preflight with the admitted target, safe headers, config, limits, and permitted body modes. The stage skips, blocks, or selects header-only, whole-body, lockstep streaming, or owned streaming inspection. +5. Streams normalized body bytes and trailers through the active chain with bounded units and backpressure. A chain made only of `STREAM_BYTES` stages can forward each approved unit upstream immediately. Whole-body, owned, and other withholding paths spool transformed output outside the RPC envelope first. +6. Re-checks body-aware protocol policy (GraphQL, JSON-RPC, MCP) after each stage that replaces the body. These protocols retain a bounded hold barrier so every later stage and the upstream receive a payload admitted by policy. +7. Injects provider credentials and forwards the request. + +The request stream carries HTTP/1.x payload bytes without transfer framing. OpenShell decodes chunked input before evaluation, validates request trailers, and computes the final `Content-Length` or chunked framing after the chain completes. A stage never receives credential, routing, framing, hop-by-hop, or `Connection`-nominated headers. Response middleware advertising `HTTP_RESPONSE/PRE_RETURN` inspects the final upstream response before OpenShell returns it to the workload. Its preflight includes upstream `Content-Length`, `Content-Encoding`, and `Content-Range` fields as read-only metadata, except when `Connection` nominates the field as hop-by-hop. Middleware cannot write or remove these fields. OpenShell computes downstream framing separately and emits the final `Content-Length` or `Transfer-Encoding` exactly once after middleware processing. @@ -35,7 +38,7 @@ The protobuf represents each logical message with a `text` or `binary` payload v The network supervisor reserves process-wide assembly capacity before buffering every parsed WebSocket text message, even when no middleware is selected. At most 32 assemblies run while 64 additional callers wait without buffering payload bytes. When both bounds are full, OpenShell closes the WebSocket with code `1013` before reading the new message payload. A text message may contain at most 4,096 fragments, must make input progress within 30 seconds, and must finish assembly within 2 minutes. Forwarding the completed text frame must finish within another 2 minutes. The assembly budget lasts for the supervisor process lifetime, so policy reloads do not reset its capacity. -Active middleware sessions additionally reserve shared middleware capacity before buffering WebSocket text, and HTTP middleware reserves the same capacity before buffering request bodies; at most 32 evaluations run and 64 additional unbuffered callers wait for capacity. When both middleware bounds are full, OpenShell sheds an HTTP request with `503 Service Unavailable` before reading its body. Persistent middleware streams use a separate process-wide budget of 32 sessions. WebSocket session admission does not wait: if the budget is full, OpenShell applies each selected config's `on_error` behavior before opening a stream. +Active middleware sessions additionally reserve shared middleware capacity before buffering WebSocket text or advancing an HTTP request stream; at most 32 evaluations run and 64 additional callers wait without buffering payload bytes. When both middleware bounds are full, OpenShell sheds an HTTP request with `503 Service Unavailable` before reading its body. Persistent middleware streams use a separate process-wide budget of 32 sessions. WebSocket session admission does not wait: if the budget is full, OpenShell applies each selected config's `on_error` behavior before opening a stream. Because each transformed body is re-checked before the next stage runs, a middleware hook always receives a request that satisfies the sandbox policy. A stage whose output the policy rejects stops the chain; under `enforcement: audit` the rejection is logged and the request proceeds. @@ -54,7 +57,9 @@ The request context identifies the originating sandbox to operator-run services. `openshell/regex` is an example built-in middleware. It replaces only simple, self-contained token patterns in UTF-8 HTTP bodies and client WebSocket text messages; the initial pattern recognizes `sk-` tokens. It does not infer values from keyword assignments such as JSON `password` fields. This best-effort text transformation is not parser-aware and does not guarantee that it will detect or fully remove sensitive values. Its `config` accepts one field, `mode: redact`, which is also the default when the field is omitted. Unknown config fields and non-string values are rejected at policy validation. Custom expressions are not configurable yet. -Operator-run services expose bindings for supported operation and phase pairs. A binding is identified by its operation and phase. V1 supports `HttpRequest/pre_credentials` and `WebSocketMessage/pre_credentials`; a service may expose either or both. Policies attach the complete middleware by its operator-owned gateway registration name. +Operator-run services expose bindings for supported operation and phase pairs. A binding is identified by its operation and phase. V1 supports external `HTTP_REQUEST/PRE_CREDENTIALS`, `HTTP_RESPONSE/PRE_RETURN`, and `WEBSOCKET_MESSAGE/PRE_CREDENTIALS` bindings. Policies attach the complete middleware by its operator-owned gateway registration name. + +`openshell/sigv4` is a restricted in-process built-in at `HTTP_REQUEST/POST_CREDENTIALS`. The REST endpoint's `credential_signing` fields configure it; do not attach it through `network_middlewares`. External registrations that advertise `POST_CREDENTIALS` are rejected because that phase can see supervisor-resolved credentials. ## Register a Middleware Service @@ -77,15 +82,40 @@ timeout = "500ms" | `tls_ca_cert_path` | Optional PEM trust roots for a private HTTPS service. Custom roots replace platform roots and retain hostname verification. | | `audience` | Exact audience expected by the service. Defaults to `urn:openshell:extension:middleware:`. | | `allow_insecure_transport` | Opt this registration out of extension authentication, permitting a plaintext `http://` endpoint with no bearer credential. Defaults to `false`. Development and trusted-network deployments only. | -| `max_payload_bytes` | Shared operator limit applied to inspectable logical payloads across every binding exposed by the service, up to the 4 MiB platform maximum. It caps HTTP bodies and complete WebSocket text messages. | +| `max_payload_bytes` | Shared operator limit applied across every binding, up to the 4 MiB platform maximum. For streamed HTTP request and response modes it caps each unit; for whole-body modes and WebSocket text it caps the complete logical payload. | | `timeout` | Optional service-wide RPC timeout using an integer with an `ms` or `s` suffix. Defaults to `500ms`; valid values range from `10ms` through `30s`. | -Each binding returned by `Describe` may advertise a shorter `timeout` using the same syntax and bounds. The operator-configured service timeout is a ceiling: OpenShell uses the smaller of the binding and service values. An omitted binding timeout inherits the service setting, and an omitted service setting uses the 500 ms platform default. OpenShell rejects an invalid timeout before accepting the manifest. The operator-configured service timeout applies to `Describe` and `ValidateConfig`. The effective binding timeout applies only to `EvaluateHttpRequest`, WebSocket preflight, and each WebSocket message. WebSocket streams have no connection-wide deadline. +Each binding returned by `Describe` may advertise a shorter `timeout` using the same syntax and bounds. The operator-configured service timeout is a ceiling: OpenShell uses the smaller of the binding and service values. An omitted binding timeout inherits the service setting, and an omitted service setting uses the 500 ms platform default. OpenShell rejects an invalid timeout before accepting the manifest. The operator-configured service timeout applies to `Describe` and `ValidateConfig`. The effective binding timeout applies to HTTP stream open and each request or response exchange, WebSocket preflight, and each WebSocket message. A 30-second chain deadline separately bounds one HTTP body-unit pass, one HTTP finalization pass, or one WebSocket message across all stages; it is not a lifetime limit on an accepted stream. Request body receipt, middleware processing, and output delivery share a 2-minute wall-clock deadline. WebSocket streams have no connection-wide middleware deadline. The gateway connects to every registered service and verifies its capabilities before accepting traffic. Gateway startup fails when a service is unavailable, reports an invalid capability, or exposes more than one binding for the same operation and phase. The manifest `name` is diagnostic metadata and does not need to match the operator registration name. Operator-run registration names cannot claim the reserved `openshell/` namespace. Registration is static. Restart the gateway after adding, removing, or changing a service. See [Gateway Configuration](/reference/gateway-config#supervisor-middleware-services) for the complete gateway TOML context. +### Implement the HTTP request stream + +HTTP request middleware implements the `HttpRequestPreCredentials` service. Its `Evaluate` RPC is bidirectional streaming and follows this lifecycle: + +1. OpenShell sends one `HttpRequestPreflight`. Return `skip`, `block_request`, or `inspect`. An inspecting stage applies any request-header mutations and selects one permitted body mode. +2. For a body mode, OpenShell sends contiguous `HttpRequestBodyUnit` sequences. `WHOLE_BODY_BYTES` receives the complete body in its final unit. Streaming and owned modes receive nonempty data units followed by one empty unit with `end_of_stream`; an empty body also produces that empty final unit. +3. Return one matching `HttpRequestBodyResult` per input unit. `STREAM_BYTES` must account for the current unit without retaining bytes. `OWNED_STREAM_BYTES` acknowledges each unit with `take_ownership`, then returns contiguous `body_output` units and exactly one `body_finalize` after final input. +4. OpenShell sends one trailers event to active body stages, including an empty trailer set. Return ordered mutations only for safe, existing trailer fields. +5. OpenShell sends a best-effort `session_end`, half-closes the request stream, and briefly drains results. + +The body modes are: + +| Mode | Contract | Failure recovery | +| --- | --- | --- | +| `HEADERS_ONLY` | Inspect preflight and mutate safe request headers. | Follows `on_error`. | +| `WHOLE_BODY_BYTES` | Receive one final normalized body unit. The complete body and replacement must fit `max_payload_bytes`. | Follows `on_error`; the original remains available. | +| `STREAM_BYTES` | Receive and return bounded units in lockstep. A result cannot retain input across units. | Follows `on_error`; the current unit remains available. | +| `OWNED_STREAM_BYTES` | Durably accept each input unit, then emit a new bounded representation and final accounting. Intended for transformations such as Git pack signing. | Permitted only for `fail_closed`; the original is no longer replayable. | + +OpenShell caps request stream units at 64 KiB even when the binding advertises more. Owned input and output are each capped at 1 GiB and should be spooled rather than retained in memory. The service must respect the `max_deferred_bytes` advertised in preflight. + +For a chain made only of `STREAM_BYTES` stages, OpenShell may send each approved unit upstream before receiving the rest of the request. A later block or failure terminates the upstream upload but cannot retract bytes already sent. OpenShell does not retry or replay the request. It also reads the upstream response concurrently; an early response cancels the middleware session, stops upload, and makes the downstream HTTP/1 connection non-reusable. Any whole-body or owned stage forces withholding for the complete chain. Body-aware policy re-evaluation, request-body credential rewriting, and body-dependent request signing also force withholding. + +The former unary `SupervisorMiddleware.EvaluateHttpRequest` RPC and its `HttpRequestEvaluation` and `HttpRequestResult` messages have been removed. Services must expose `HttpRequestPreCredentials` alongside `SupervisorMiddleware`; OpenShell does not fall back to the unary API. + ### Authenticate OpenShell Callers When gateway JWT signing is configured, OpenShell attaches a short-lived EdDSA bearer token to every remote middleware RPC. Gateway calls use `caller_kind: gateway`; sandbox supervisor calls use `caller_kind: supervisor` and include the sandbox ID. Supervisors request credentials by registration name through `RefreshSandboxToken`. The gateway derives the audience from operator-owned configuration and authorizes each name against the sandbox's effective policy. @@ -170,16 +200,18 @@ Middleware decisions are enforced regardless of the endpoint's `enforcement` mod ## Set Payload Limits -Every middleware binding declares the largest logical payload or replacement it supports through `max_payload_bytes`. For `HTTP_REQUEST`, that payload is one request body. For `WEBSOCKET_MESSAGE`, it is one complete message rather than the whole session. +Every middleware binding declares the largest payload unit or complete buffered payload it supports through `max_payload_bytes`. - Built-in middleware uses its OpenShell-defined limit. - Each operator-run registration sets one `max_payload_bytes` ceiling no higher than any binding's advertised `max_payload_bytes` capability. -- A selected chain buffers using its largest stage limit, so every stage that can process the body receives it. -- The same per-stage limit applies to request bodies and replacement bodies. +- Whole-body request and response modes apply it to the complete body and replacement. +- Streaming request and response modes apply it to each input or output unit. OpenShell further caps request units at 64 KiB. +- WebSocket mode applies it to one complete text message or replacement, not the whole session. +- Owned request streams separately advertise a 1 GiB deferred input/output limit and require `fail_closed`. -The gateway rejects a registration whose operator limit exceeds the service capability or the 4 MiB platform maximum instead of silently clamping it. OpenShell also bounds the non-payload protobuf components: 64 KiB for service config, 4 KiB for request context, 32 KiB for the target, and 128 request header lines totaling at most 64 KiB encoded. Results allow a 4 KiB discarded free-form reason, a 64-byte validated reason code, 64 header mutations totaling at most 64 KiB encoded, 32 findings of at most 4 KiB encoded each, and 64 metadata entries totaling at most 32 KiB. Middleware gRPC servers should configure request and response message limits to at least 4 MiB plus 293 KiB so every platform-valid envelope fits. +The gateway rejects a registration whose operator limit exceeds the service capability or the 4 MiB platform maximum instead of silently clamping it. OpenShell also bounds the non-payload protobuf components: 64 KiB for service config, 4 KiB for request context, 32 KiB for the target, and 128 request header lines totaling at most 64 KiB encoded. Results allow a 4 KiB discarded free-form reason, a 64-byte validated reason code, 64 header mutations totaling at most 64 KiB encoded, 32 findings of at most 4 KiB encoded each, and 64 metadata entries totaling at most 32 KiB. Middleware gRPC servers should configure messages for their advertised limit plus the bounded envelope. Request streaming needs only the 64 KiB unit plus that envelope even when the complete body is larger than 4 MiB. -At request time, exceeding a selected stage's limit is a middleware failure for that stage alone and follows that config's `on_error` behavior; other stages in the chain still run against their own limits. OpenShell can apply `fail_open` to an oversized `Content-Length` before consuming body bytes. A chunked body can cross the limit only after bytes have been consumed, so OpenShell denies that request because it cannot safely resume the original stream. +At request time, an unavailable selected mode, oversized unit, invalid sequence, or incomplete finalization is a middleware failure. A non-owned stage follows its config's `on_error`; an owned stage always fails closed after ownership begins. Whole-body stages can fail independently when the complete body exceeds their limit, while streaming stages continue with bounded units. For a WebSocket binding, `max_payload_bytes` covers complete client text messages and replacements. Exceeding a selected stage's effective text-message limit follows that stage's `on_error`. The 4 MiB parsed-text platform cap and other protocol-safety limits are independent of middleware failure policy. Binary messages are not delivered to middleware, so the operator ceiling does not become a binary relay limit; individual raw binary frames retain the 16 MiB relay-safety bound. Oversized parsed text closes the connection with code `1009`; invalid UTF-8 uses `1007`; protocol errors use `1002`; middleware or policy denials use `1008`; and policy reload uses `1012`. @@ -230,6 +262,8 @@ The [content guard example](https://github.com/NVIDIA/OpenShell/tree/main/exampl The example includes a policy, local fixture, and smoke launcher. +The [Git signing example](https://github.com/NVIDIA/OpenShell/tree/main/examples/supervisor-middleware-git-signing) uses owned request streaming to spool a Git smart-HTTP push, sign rewritten commit objects with a host-held SSH key, and stream the replacement pack back. Its tests exercise a receive-pack body larger than 4 MiB. It is a proof of concept, not a production signing service. + ## Current Limitations - Middleware applies only through operation bindings advertised by each implementation. For protocols that have no supported middleware operation at all, such as HTTP/2 prior knowledge or non-HTTP TCP, the existing uninspectable-traffic gate denies a host match containing `fail_closed` and relays an all-`fail_open` match with a detection finding. diff --git a/docs/providers/aws-sigv4.mdx b/docs/providers/aws-sigv4.mdx index 101d796e5..cc5b98b10 100644 --- a/docs/providers/aws-sigv4.mdx +++ b/docs/providers/aws-sigv4.mdx @@ -7,7 +7,9 @@ description: "Configure proxy-side AWS SigV4 request signing so sandbox agents c keywords: "Generative AI, Cybersecurity, AI Agents, AWS, SigV4, Bedrock, S3, Credential Signing, Sandbox" --- -AWS SigV4 credential signing lets sandbox agents call AWS services (Bedrock, S3, STS, and others) through the proxy's CONNECT tunnel. The proxy intercepts outbound requests, strips the sandbox client's placeholder `Authorization` header, and re-signs the request with real AWS credentials from the provider. The sandbox never sees the real credentials. +AWS SigV4 credential signing lets sandbox agents call AWS services (Bedrock, S3, STS, and others) through the proxy's CONNECT tunnel. The restricted in-process `openshell/sigv4` middleware runs at `HTTP_REQUEST/POST_CREDENTIALS`, strips the sandbox client's placeholder `Authorization` header, and re-signs the request with real AWS credentials from the provider. The sandbox and external middleware never see the real credentials. + +Configure this built-in with the endpoint fields below. Do not attach it through `network_middlewares`; OpenShell reserves the credential-visible `POST_CREDENTIALS` phase for trusted in-process implementations. ## Prerequisites diff --git a/docs/reference/gateway-config.mdx b/docs/reference/gateway-config.mdx index 75599ae19..380a54583 100644 --- a/docs/reference/gateway-config.mdx +++ b/docs/reference/gateway-config.mdx @@ -356,11 +356,11 @@ max_payload_bytes = 262144 timeout = "500ms" ``` -Each service implements the supervisor middleware gRPC contract and exposes bindings through `Describe`. Policies reference the operator-owned registration `name`, attaching the complete middleware and all of its bindings. Bindings are identified by operation and phase. A manifest may expose at most one binding for each operation and phase pair. V1 supports `HttpRequest/pre_credentials`, `HttpResponse/pre_return`, and `WebSocketMessage/pre_credentials`. Registration names must be unique, and operator-run registrations cannot claim the reserved `openshell/` namespace. The service-reported manifest name is diagnostic metadata and does not need to match the registration name. +Each service implements the supervisor middleware gRPC contract and exposes bindings through `Describe`. Policies reference the operator-owned registration `name`, attaching the complete middleware and all of its bindings. Bindings are identified by operation and phase. A manifest may expose at most one binding for each operation and phase pair. Operator-run services may expose `HTTP_REQUEST/PRE_CREDENTIALS`, `HTTP_RESPONSE/PRE_RETURN`, and `WEBSOCKET_MESSAGE/PRE_CREDENTIALS`. `HTTP_REQUEST/POST_CREDENTIALS` is reserved for trusted in-process built-ins such as `openshell/sigv4`; the gateway rejects an external manifest that advertises it. Registration names must be unique, and operator-run registrations cannot claim the reserved `openshell/` namespace. The service-reported manifest name is diagnostic metadata and does not need to match the registration name. The gateway connects to every registered service and validates `Describe` before it starts. The service must therefore be running before the gateway. Policy creation and full policy updates call `ValidateConfig`; an unavailable service or invalid middleware configuration rejects the policy before persistence. -`max_payload_bytes` is the shared operator limit for inspectable logical payloads across every binding exposed by the service. It caps HTTP request and response units, replacement bodies, and complete WebSocket text messages and replacements. Whole-response inspection uses it as the stage's total body limit. Streaming response inspection applies it to each unit. The value must be greater than zero, no larger than each binding's advertised `max_payload_bytes` capability, and no larger than the 4 MiB platform maximum. OpenShell rejects oversized values instead of silently clamping them. Binary WebSocket messages are not exposed to V1 middleware, so this field does not limit binary pass-through. Middleware gRPC servers should allow messages of at least 4 MiB plus 293 KiB so a maximum-size payload and its protobuf envelope fit on the transport. +`max_payload_bytes` is the shared operator limit across every binding exposed by the service. Whole-body request and response inspection uses it as the stage's total body and replacement limit. Streaming request and response inspection applies it to each unit; OpenShell caps request units at 64 KiB. Complete WebSocket text messages and replacements also use it. Owned request streams can process a larger complete representation under a separately advertised, fail-closed 1 GiB deferred-storage bound. The value must be greater than zero, no larger than each binding's advertised capability, and no larger than the 4 MiB platform maximum. OpenShell rejects oversized values instead of silently clamping them. Binary WebSocket messages are not exposed to V1 middleware, so this field does not limit binary pass-through. Configure gRPC message limits for the advertised payload plus the bounded protobuf envelope. `timeout` is the operator-configured service-wide RPC timeout. It accepts the same compact duration syntax as gateway interceptors: an integer followed by `ms` or `s`, such as `500ms` or `2s`. Values must be between `10ms` and `30s`, inclusive. Omit the field to use the 500 ms platform default. A binding may advertise a shorter `timeout` in the `Describe` manifest, but it cannot extend the operator-configured deadline; OpenShell uses the smaller value. OpenShell validates both levels before accepting the service. The operator-configured service timeout applies to `Describe` and `ValidateConfig`. The effective binding timeout applies to HTTP request evaluation, HTTP response preflight and unit exchanges, WebSocket preflight, and each WebSocket message. Accepted streaming protocols have no connection-wide RPC deadline. diff --git a/examples/supervisor-middleware-content-guard/src/main.rs b/examples/supervisor-middleware-content-guard/src/main.rs index c6489d742..81eedb97b 100644 --- a/examples/supervisor-middleware-content-guard/src/main.rs +++ b/examples/supervisor-middleware-content-guard/src/main.rs @@ -6,7 +6,12 @@ use std::net::SocketAddr; use std::ops::Range; use clap::Parser; -use openshell_core::middleware::{HttpResponseResultStream, WebSocketResponseStream}; +use openshell_core::middleware::{ + HttpRequestResultStream, HttpResponseResultStream, WebSocketResponseStream, +}; +use openshell_core::proto::middleware::v1::http_request_pre_credentials_server::{ + HttpRequestPreCredentials, HttpRequestPreCredentialsServer, +}; use openshell_core::proto::middleware::v1::http_response_pre_return_server::{ HttpResponsePreReturn, HttpResponsePreReturnServer, }; @@ -14,17 +19,21 @@ use openshell_core::proto::middleware::v1::supervisor_middleware_server::{ SupervisorMiddleware, SupervisorMiddlewareServer, }; use openshell_core::proto::{ - Decision, Finding, HttpRequestEvaluation, HttpRequestResult, HttpResponseBlockDelivery, - HttpResponseBodyMode, HttpResponseBodyResult, HttpResponseBodyTransform, HttpResponseEvent, - HttpResponseEventResult, HttpResponsePreflightInspect, HttpResponsePreflightResult, - HttpResponseTrailersResult, MiddlewareBinding, MiddlewareManifest, - SupervisorMiddlewareOperation, SupervisorMiddlewarePhase, ValidateConfigRequest, - ValidateConfigResponse, WebSocketMessage, WebSocketMessageResult, WebSocketPreflightAction, - WebSocketPreflightDecision, WebSocketSessionEvent, WebSocketSessionEventResult, - http_response_body_result, http_response_body_transform, http_response_body_unit, - http_response_event, http_response_event_result, http_response_preflight_result, - web_socket_message, web_socket_message_result, web_socket_session_event, - web_socket_session_event_result, + Decision, Finding, HttpRequestBlock, HttpRequestBodyMode, HttpRequestBodyPassThrough, + HttpRequestBodyResult, HttpRequestBodyTransform, HttpRequestEvent, HttpRequestEventResult, + HttpRequestPreflightInspect, HttpRequestPreflightResult, HttpRequestTrailersResult, + HttpResponseBlockDelivery, HttpResponseBodyMode, HttpResponseBodyResult, + HttpResponseBodyTransform, HttpResponseEvent, HttpResponseEventResult, + HttpResponsePreflightInspect, HttpResponsePreflightResult, HttpResponseTrailersResult, + MiddlewareBinding, MiddlewareManifest, SupervisorMiddlewareOperation, + SupervisorMiddlewarePhase, ValidateConfigRequest, ValidateConfigResponse, WebSocketMessage, + WebSocketMessageResult, WebSocketPreflightAction, WebSocketPreflightDecision, + WebSocketSessionEvent, WebSocketSessionEventResult, http_request_body_result, + http_request_body_transform, http_request_body_unit, http_request_event, + http_request_event_result, http_request_preflight_result, http_response_body_result, + http_response_body_transform, http_response_body_unit, http_response_event, + http_response_event_result, http_response_preflight_result, web_socket_message, + web_socket_message_result, web_socket_session_event, web_socket_session_event_result, }; use prost_types::Struct; use prost_types::value::Kind; @@ -277,19 +286,6 @@ impl SupervisorMiddleware for ContentGuard { })) } - async fn evaluate_http_request( - &self, - request: Request, - ) -> Result, Status> { - let request = request.into_inner(); - validate_phase(request.phase).map_err(Status::invalid_argument)?; - let config = - GuardConfig::parse(request.config.as_ref()).map_err(Status::invalid_argument)?; - let body = String::from_utf8(request.body) - .map_err(|_| Status::invalid_argument("content guard requires a UTF-8 body"))?; - Ok(Response::new(evaluate(&config, &body))) - } - async fn evaluate_web_socket_session( &self, request: Request>, @@ -298,6 +294,147 @@ impl SupervisorMiddleware for ContentGuard { } } +#[derive(Debug, Default)] +struct RequestSessionState { + config: Option, + body_ended: bool, + trailers_seen: bool, +} + +impl RequestSessionState { + fn preflight( + &mut self, + preflight: openshell_core::proto::HttpRequestPreflight, + ) -> Result { + if self.config.is_some() { + return Err(Status::failed_precondition("duplicate preflight")); + } + let config = + GuardConfig::parse(preflight.config.as_ref()).map_err(Status::invalid_argument)?; + if !preflight + .permitted_body_modes + .contains(&(HttpRequestBodyMode::WholeBodyBytes as i32)) + { + return Err(Status::failed_precondition( + "content guard requires WHOLE_BODY_BYTES", + )); + } + self.config = Some(config); + Ok(HttpRequestEventResult { + result: Some(http_request_event_result::Result::PreflightResult( + HttpRequestPreflightResult { + action: Some(http_request_preflight_result::Action::Inspect( + HttpRequestPreflightInspect { + body_mode: HttpRequestBodyMode::WholeBodyBytes as i32, + header_mutations: vec![], + }, + )), + ..Default::default() + }, + )), + }) + } + + fn body( + &mut self, + body: openshell_core::proto::HttpRequestBodyUnit, + ) -> Result { + let config = self + .config + .as_ref() + .ok_or_else(|| Status::failed_precondition("body before preflight"))?; + if self.body_ended || body.sequence != 1 || !body.end_of_stream { + return Err(Status::failed_precondition( + "expected one complete request body", + )); + } + let Some(http_request_body_unit::Payload::Data(data)) = body.payload else { + return Err(Status::invalid_argument("body data required")); + }; + let text = std::str::from_utf8(&data) + .map_err(|_| Status::invalid_argument("content guard requires a UTF-8 body"))?; + let result = inspect(config, text); + let action = if result.denied { + http_request_body_result::Action::BlockRequest(HttpRequestBlock {}) + } else if let Some(replacement) = result.replacement { + http_request_body_result::Action::Transform(HttpRequestBodyTransform { + replacement: Some(http_request_body_transform::Replacement::Data( + replacement.into_bytes(), + )), + }) + } else { + http_request_body_result::Action::PassThrough(HttpRequestBodyPassThrough {}) + }; + self.body_ended = true; + Ok(HttpRequestEventResult { + result: Some(http_request_event_result::Result::BodyResult( + HttpRequestBodyResult { + sequence: body.sequence, + action: Some(action), + reason: result.reason, + reason_code: result.reason_code, + findings: result.findings, + metadata: result.metadata, + }, + )), + }) + } + + fn trailers(&mut self) -> Result { + if !self.body_ended || self.trailers_seen { + return Err(Status::failed_precondition("expected trailers after body")); + } + self.trailers_seen = true; + Ok(HttpRequestEventResult { + result: Some(http_request_event_result::Result::TrailersResult( + HttpRequestTrailersResult::default(), + )), + }) + } +} + +#[tonic::async_trait] +impl HttpRequestPreCredentials for ContentGuard { + type EvaluateStream = HttpRequestResultStream; + + async fn evaluate( + &self, + request: Request>, + ) -> Result, Status> { + let mut events = request.into_inner(); + let (sender, receiver) = mpsc::channel(4); + tokio::spawn(async move { + let mut state = RequestSessionState::default(); + while let Some(event) = events.next().await { + let result = match event { + Ok(event) => match event.event { + Some(http_request_event::Event::Preflight(preflight)) => { + state.preflight(preflight) + } + Some(http_request_event::Event::Body(body)) => state.body(body), + Some(http_request_event::Event::Trailers(_)) => state.trailers(), + Some(http_request_event::Event::SessionEnd(_)) => break, + None => Err(Status::invalid_argument("request event is required")), + }, + Err(error) => Err(error), + }; + match result { + Ok(result) => { + if sender.send(Ok(result)).await.is_err() { + break; + } + } + Err(error) => { + let _ = sender.send(Err(error)).await; + break; + } + } + } + }); + Ok(Response::new(Box::pin(ReceiverStream::new(receiver)))) + } +} + #[derive(Debug, Default)] struct ResponseSessionState { config: Option, @@ -454,23 +591,6 @@ struct GuardOutcome { findings: Vec, metadata: HashMap, } -fn evaluate(config: &GuardConfig, body: &str) -> HttpRequestResult { - let result = inspect(config, body); - HttpRequestResult { - decision: if result.denied { - Decision::Deny - } else { - Decision::Allow - } as i32, - has_body: result.replacement.is_some(), - body: result.replacement.unwrap_or_default().into_bytes(), - reason: result.reason, - reason_code: result.reason_code, - findings: result.findings, - metadata: result.metadata, - ..Default::default() - } -} fn inspect(config: &GuardConfig, body: &str) -> GuardOutcome { let (ranges, match_count, matched_term_count) = find_match_ranges(body, &config.terms); @@ -632,6 +752,7 @@ async fn main() -> Result<(), Box> { println!("serving {MANIFEST_NAME} on http://{}", cli.bind); Server::builder() .add_service(SupervisorMiddlewareServer::new(ContentGuard)) + .add_service(HttpRequestPreCredentialsServer::new(ContentGuard)) .add_service(HttpResponsePreReturnServer::new(ContentGuard)) .serve(cli.bind) .await?; @@ -642,8 +763,8 @@ async fn main() -> Result<(), Box> { mod tests { use super::*; use openshell_core::proto::{ - HttpResponseBodyUnit, HttpResponsePreflight, MiddlewareSessionEnd, WebSocketPreflight, - WebSocketSessionStart, + HttpRequestBodyUnit, HttpRequestPreflight, HttpResponseBodyUnit, HttpResponsePreflight, + MiddlewareSessionEnd, WebSocketPreflight, WebSocketSessionStart, }; use prost_types::{ListValue, Value}; use std::collections::BTreeMap; @@ -711,6 +832,89 @@ mod tests { ..Default::default() } } + + fn request_preflight(mode: &str) -> HttpRequestPreflight { + HttpRequestPreflight { + config: Some(config(mode, &["prototype-secret", "秘密"], None)), + permitted_body_modes: vec![HttpRequestBodyMode::WholeBodyBytes as i32], + ..Default::default() + } + } + + #[test] + fn request_guard_uses_streamed_whole_body_contract() { + let mut state = RequestSessionState::default(); + let preflight = state.preflight(request_preflight("redact")).unwrap(); + assert!(matches!( + preflight.result, + Some(http_request_event_result::Result::PreflightResult( + HttpRequestPreflightResult { + action: Some(http_request_preflight_result::Action::Inspect( + HttpRequestPreflightInspect { body_mode, .. } + )), + .. + } + )) if body_mode == HttpRequestBodyMode::WholeBodyBytes as i32 + )); + + let result = state + .body(HttpRequestBodyUnit { + sequence: 1, + payload: Some(http_request_body_unit::Payload::Data( + b"contains prototype-secret".to_vec(), + )), + end_of_stream: true, + }) + .unwrap(); + let Some(http_request_event_result::Result::BodyResult(result)) = result.result else { + panic!("body result"); + }; + let Some(http_request_body_result::Action::Transform(transform)) = result.action else { + panic!("transform"); + }; + assert_eq!( + transform.replacement, + Some(http_request_body_transform::Replacement::Data( + b"contains [REDACTED]".to_vec() + )) + ); + assert!(matches!( + state.trailers().unwrap().result, + Some(http_request_event_result::Result::TrailersResult(_)) + )); + } + + #[test] + fn request_guard_denies_and_rejects_unavailable_whole_body_mode() { + let mut unavailable = request_preflight("redact"); + unavailable.permitted_body_modes = vec![HttpRequestBodyMode::HeadersOnly as i32]; + assert!( + RequestSessionState::default() + .preflight(unavailable) + .is_err() + ); + + let mut state = RequestSessionState::default(); + state.preflight(request_preflight("deny")).unwrap(); + let result = state + .body(HttpRequestBodyUnit { + sequence: 1, + payload: Some(http_request_body_unit::Payload::Data( + b"prototype-secret".to_vec(), + )), + end_of_stream: true, + }) + .unwrap(); + let Some(http_request_event_result::Result::BodyResult(result)) = result.result else { + panic!("body result"); + }; + assert!(matches!( + result.action, + Some(http_request_body_result::Action::BlockRequest(_)) + )); + assert_eq!(result.reason_code, "content_match"); + } + #[test] fn response_guard_passes_redacts_and_denies() { for (mode, input, expected) in [ @@ -887,17 +1091,16 @@ mod tests { Some("[FILTERED]"), ))) .expect("valid config"); - let result = evaluate( + let result = inspect( &config, "prototype-secret then internal-only then prototype-secret", ); - assert_eq!(result.decision, Decision::Allow as i32); assert_eq!( - String::from_utf8(result.body).unwrap(), - "[FILTERED] then [FILTERED] then [FILTERED]" + result.replacement.as_deref(), + Some("[FILTERED] then [FILTERED] then [FILTERED]") ); - assert!(result.has_body); + assert!(!result.denied); assert_eq!(result.findings[0].count, 3); } @@ -907,9 +1110,9 @@ mod tests { GuardConfig::parse(Some(&config("redact", &["aba", "bab"], Some("[FILTERED]")))) .expect("valid config"); - let result = evaluate(&config, "abab"); + let result = inspect(&config, "abab"); - assert_eq!(String::from_utf8(result.body).unwrap(), "[FILTERED]"); + assert_eq!(result.replacement.as_deref(), Some("[FILTERED]")); assert_eq!(result.findings[0].count, 2); assert_eq!(result.metadata["matched_term_count"], "2"); } @@ -919,9 +1122,9 @@ mod tests { let config = GuardConfig::parse(Some(&config("redact", &["aba"], Some("[FILTERED]")))) .expect("valid config"); - let result = evaluate(&config, "ababa"); + let result = inspect(&config, "ababa"); - assert_eq!(String::from_utf8(result.body).unwrap(), "[FILTERED]"); + assert_eq!(result.replacement.as_deref(), Some("[FILTERED]")); assert_eq!(result.findings[0].count, 2); assert_eq!(result.metadata["matched_term_count"], "1"); } @@ -931,12 +1134,9 @@ mod tests { let config = GuardConfig::parse(Some(&config("redact", &["abc"], Some("[FILTERED]")))) .expect("valid config"); - let result = evaluate(&config, "abcabc"); + let result = inspect(&config, "abcabc"); - assert_eq!( - String::from_utf8(result.body).unwrap(), - "[FILTERED][FILTERED]" - ); + assert_eq!(result.replacement.as_deref(), Some("[FILTERED][FILTERED]")); assert_eq!(result.findings[0].count, 2); } @@ -944,23 +1144,22 @@ mod tests { fn deny_returns_a_generic_reason_without_echoing_the_term() { let config = GuardConfig::parse(Some(&config("deny", &["prototype-secret"], None))) .expect("valid config"); - let result = evaluate(&config, "contains prototype-secret"); + let result = inspect(&config, "contains prototype-secret"); - assert_eq!(result.decision, Decision::Deny as i32); + assert!(result.denied); assert!(!result.reason.contains("prototype-secret")); assert_eq!(result.reason_code, "content_match"); - assert!(!result.has_body); + assert!(result.replacement.is_none()); } #[test] fn no_match_allows_without_replacing_the_body() { let config = GuardConfig::parse(Some(&config("redact", &["blocked"], None))).expect("valid config"); - let result = evaluate(&config, "safe content"); + let result = inspect(&config, "safe content"); - assert_eq!(result.decision, Decision::Allow as i32); - assert!(!result.has_body); - assert!(result.body.is_empty()); + assert!(!result.denied); + assert!(result.replacement.is_none()); } #[test] diff --git a/examples/supervisor-middleware-git-signing/Cargo.lock b/examples/supervisor-middleware-git-signing/Cargo.lock new file mode 100644 index 000000000..f5ad14da3 --- /dev/null +++ b/examples/supervisor-middleware-git-signing/Cargo.lock @@ -0,0 +1,2555 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "addr2line" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b5d307320b3181d6d7954e663bd7c774a838b8220fe0593c86d9fb09f498b4b" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + +[[package]] +name = "anyhow" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" + +[[package]] +name = "async-trait" +version = "0.1.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "aws-lc-rs" +version = "1.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b281d307588d634de920874890732659e2e7672f72b5e10e81badc1a8a83621e" +dependencies = [ + "aws-lc-sys", + "untrusted 0.7.1", + "zeroize", +] + +[[package]] +name = "aws-lc-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bff6c3b54fad79a2e60b8102caf565819711497c1f5f092f49508e2f5c31b27" +dependencies = [ + "cc", + "cmake", + "dunce", + "fs_extra", + "pkg-config", +] + +[[package]] +name = "axum" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31b698c5f9a010f6573133b09e0de5408834d0c82f8d7475a89fc1867a71cd90" +dependencies = [ + "axum-core", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "serde_core", + "sync_wrapper", + "tower", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum-core" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "sync_wrapper", + "tower-layer", + "tower-service", +] + +[[package]] +name = "backtrace" +version = "0.3.76" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb531853791a215d7c62a30daf0dde835f381ab5de4589cfe7c649d2cbe92bd6" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-link", +] + +[[package]] +name = "backtrace-ext" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "537beee3be4a18fb023b570f80e3ae28003db9167a751266b259926e25539d50" +dependencies = [ + "backtrace", +] + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "bytes" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" + +[[package]] +name = "cc" +version = "1.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3eb0f42d6c360dc3f8a821f6bf2fdea7f72bfd36b3076eb0e6d1e9e0752fff4" +dependencies = [ + "find-msvc-tools", + "jobserver", + "libc", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "cfg_aliases" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" + +[[package]] +name = "clap" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa8876b300ab35ba921adea3dfd70157a46249b33f95c9084ae5709785478946" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0797fb7aeb1406c84efac526901f7ec3ead2124f946b494e72879d4b54704d" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9c751b79415d4e559e3d1fcf128e09e720eb673a06d26cf6f392d37d75b66e0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 3.0.6", +] + +[[package]] +name = "clap_lex" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486" + +[[package]] +name = "cmake" +version = "0.1.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0f78a02292a74a88ac736019ab962ece0bc380e3f977bf72e376c5d78ff0678" +dependencies = [ + "cc", +] + +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + +[[package]] +name = "core-foundation" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "deranged" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c" + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "displaydoc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + +[[package]] +name = "dunce" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" + +[[package]] +name = "either" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "252afb9ae5eaa683babdc6a068b3f5726eb19e05070c731f9b2a23a7c3e8ed34" + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "fastrand" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" + +[[package]] +name = "find-msvc-tools" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e0f1c7c3a72c66fd80abe965175f7523475c0489a87d3ff9d6e8c87d87a9d2d" + +[[package]] +name = "fixedbitset" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "fs_extra" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" + +[[package]] +name = "futures-channel" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-sink" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1944426bf7d03f1d14f708785e4b33efd750b36d48a157b836b3efc15ede8e1d" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "libc", + "r-efi", +] + +[[package]] +name = "gimli" +version = "0.32.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7" + +[[package]] +name = "glob" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b" + +[[package]] +name = "h2" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef8e5e5a340588f4452631496976cf8636d4a7ecf600239fdc27615d2530bc16" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "http" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23169fe34a5fbcdd3f3862e78fb9b6fccd5f02a6dc6f732547005d45631ce71c" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27b501faa50e7a26c3d3560ca625132f4078a17771f4810baf70475ae48cbe43" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-timeout" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b90d566bffbce6a75bd8b09a05aa8c2cb1fabb6cb348f8840c9e4c90a0d83b0" +dependencies = [ + "hyper", + "hyper-util", + "pin-project-lite", + "tokio", + "tower-service", +] + +[[package]] +name = "hyper-util" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "hyper", + "libc", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", +] + +[[package]] +name = "icu_collections" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa68d21081c4a05d5a901a1c62add574c77048b6a1c67be3b50ce0b60d4ca513" +dependencies = [ + "displaydoc", + "potential_utf", + "utf8_iter", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d56e28588da92eee5c3201a6eff33fabdd49b62269c8938d4ff050ce4d900deb" +dependencies = [ + "displaydoc", + "litemap", + "serde", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_locale_fallback" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "251af8e57c9400e3eb58242fe5b8b1152b2a64fdf4cf632f923c38ccee6f2fa9" +dependencies = [ + "icu_locale_core", + "icu_locale_fallback_data", + "icu_provider", + "potential_utf", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_locale_fallback_data" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "decf2a22ec8fa68f1a0c1129a3f8583f8f8bc24e8b9ccbe98ead99f62a4dc3a8" + +[[package]] +name = "icu_normalizer" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12f9cf5f235641ed274641dd81c3f28d870e276763d0797aeeab72317b1c646f" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1563da1ed3e0b3bf3d74c9b85917ac9c56464d2f57242270c09c9e752f8021a0" + +[[package]] +name = "icu_properties" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e7ca276ad3145661a65914e6daf131ca5120cd3dcee8f8f3214b8875184a148" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e590f038c1464a96894fd6d10127e90a8be4509f56ff7ecef851b15cee0b7caa" + +[[package]] +name = "icu_provider" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d27bbb9d3abbefac45d55f647c9de1d44aafcd1186eb91879afef17c396c3e73" +dependencies = [ + "displaydoc", + "icu_locale_core", + "serde", + "stable_deref_trait", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_segmenter" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82d07aafccd67af15d02512a6adf5896fbc5ed00f2e99b471d2efa14016db3db" +dependencies = [ + "icu_collections", + "icu_locale_fallback", + "icu_provider", + "icu_segmenter_data", + "potential_utf", + "smallvec", + "utf8_iter", + "zerovec", +] + +[[package]] +name = "icu_segmenter_data" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae293c039020f9ec10710af98d29ce6aa2051486638b49c9a6409f3b4a9e98ad" + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "indexmap" +version = "2.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" +dependencies = [ + "equivalent", + "hashbrown 0.17.1", + "serde", + "serde_core", +] + +[[package]] +name = "ipnet" +version = "2.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0" + +[[package]] +name = "is_ci" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7655c9839580ee829dfacba1d1278c2b7883e50a277ff7541299489d6bdfdc45" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "jobserver" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c00acbd29eabad4a2392fa0e921c874934dbbf4194312ad20f04a0ed67a3cb3" +dependencies = [ + "getrandom 0.4.3", + "libc", +] + +[[package]] +name = "js-sys" +version = "0.3.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce57d20d1ea864ce2ac172ab472d409214f4fd359f0b2a2775abdf522e2af99e" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "jsonwebtoken" +version = "10.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eba32bfb4ffdeaca3e34431072faf01745c9b26d25504aa7a6cf5684334fc4fc" +dependencies = [ + "aws-lc-rs", + "base64", + "getrandom 0.2.17", + "js-sys", + "pem", + "serde", + "serde_json", + "signature", + "simple_asn1", + "zeroize", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.189" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" + +[[package]] +name = "libm" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "litemap" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" + +[[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata", +] + +[[package]] +name = "matchit" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "miette" +version = "7.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f98efec8807c63c752b5bd61f862c165c115b0a35685bdcfd9238c7aeb592b7" +dependencies = [ + "backtrace", + "backtrace-ext", + "cfg-if", + "miette-derive", + "owo-colors", + "supports-color", + "supports-hyperlinks", + "supports-unicode", + "terminal_size", + "textwrap", + "unicode-width 0.1.14", +] + +[[package]] +name = "miette-derive" +version = "7.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db5b29714e950dbb20d5e6f74f9dcec4edbcc1067bb7f8ed198c097b8c1a818b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", +] + +[[package]] +name = "mio" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b18443e9c262bfe8fa82f51666e2642c53393f7e5c27b3e1aeab922cff5b9d8" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "multimap" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084" + +[[package]] +name = "nix" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] + +[[package]] +name = "noyalib" +version = "0.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f075ef19fa3bcf8697c0ef96c37d5c435d339a40ab8081cae3aac3a4e7fee9a" +dependencies = [ + "hashbrown 0.17.1", + "indexmap", + "libm", + "memchr", + "rustc-hash", + "serde", + "serde_core", + "smallvec", +] + +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "num-bigint" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-conv" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" + +[[package]] +name = "num-integer" +version = "0.1.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "object" +version = "0.37.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "openshell-core" +version = "0.0.0" +dependencies = [ + "async-trait", + "base64", + "glob", + "ipnet", + "miette", + "nix", + "openshell-extension-core", + "openshell-policy-schema", + "prost", + "prost-types", + "protoc-bin-vendored", + "rustix", + "rustls", + "rustls-pemfile", + "serde", + "serde_json", + "sha2", + "thiserror", + "tokio", + "tokio-stream", + "tonic", + "tonic-prost", + "tonic-prost-build", + "tonic-types", + "tracing", + "url", + "uuid", +] + +[[package]] +name = "openshell-extension-core" +version = "0.0.0" +dependencies = [ + "hyper-util", + "serde", + "thiserror", + "tokio", + "tonic", + "tower", +] + +[[package]] +name = "openshell-policy-schema" +version = "0.0.0" +dependencies = [ + "miette", + "noyalib", + "serde", + "serde_json", +] + +[[package]] +name = "openssl-probe" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe" + +[[package]] +name = "owo-colors" +version = "4.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c45bb4a6ae1280ec0803b1ef9d3455eb50f01efbbe1447ab020f1d54fba9d8" + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "pem" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d30c53c26bc5b31a98cd02d20f25a7c8567146caf63ed593a9d87b2775291be" +dependencies = [ + "base64", + "serde_core", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "petgraph" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8701b58ea97060d5e5b155d383a69952a60943f0e6dfe30b04c287beb0b27455" +dependencies = [ + "fixedbitset", + "hashbrown 0.15.5", + "indexmap", +] + +[[package]] +name = "pin-project" +version = "1.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2466b2336ed02bcdca6b294417127b90ec92038d1d5c4fbeac971a922e0e0924" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c96395f0a926bc13b1c17622aaddda1ecb55d49c8f1bf9777e4d877800a43f8b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "pkg-config" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" + +[[package]] +name = "potential_utf" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661" +dependencies = [ + "serde_core", + "writeable", + "zerovec", +] + +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn 2.0.119", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "prost" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528ac67416ff8646872a3c02cad9cc4ee5dc9f9540c9b10771855c95cb2e5ae1" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-build" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03da047801ff44bb6a4d407d4860c05fd70bb81714e6b2f3812603d5b145b042" +dependencies = [ + "heck", + "itertools", + "log", + "multimap", + "petgraph", + "prettyplease", + "prost", + "prost-types", + "pulldown-cmark", + "pulldown-cmark-to-cmark", + "regex", + "syn 2.0.119", + "tempfile", +] + +[[package]] +name = "prost-derive" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b570b25f7617e43d59005d0990ccb79e950a423952cea19671b7a876da390adf" +dependencies = [ + "anyhow", + "itertools", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "prost-types" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f94967dc7688f3054c7fac87473ffae4cc4c3904800e2d9f5b857246d8963b0a" +dependencies = [ + "prost", +] + +[[package]] +name = "protoc-bin-vendored" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1c381df33c98266b5f08186583660090a4ffa0889e76c7e9a5e175f645a67fa" +dependencies = [ + "protoc-bin-vendored-linux-aarch_64", + "protoc-bin-vendored-linux-ppcle_64", + "protoc-bin-vendored-linux-s390_64", + "protoc-bin-vendored-linux-x86_32", + "protoc-bin-vendored-linux-x86_64", + "protoc-bin-vendored-macos-aarch_64", + "protoc-bin-vendored-macos-x86_64", + "protoc-bin-vendored-win32", +] + +[[package]] +name = "protoc-bin-vendored-linux-aarch_64" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c350df4d49b5b9e3ca79f7e646fde2377b199e13cfa87320308397e1f37e1a4c" + +[[package]] +name = "protoc-bin-vendored-linux-ppcle_64" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a55a63e6c7244f19b5c6393f025017eb5d793fd5467823a099740a7a4222440c" + +[[package]] +name = "protoc-bin-vendored-linux-s390_64" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dba5565db4288e935d5330a07c264a4ee8e4a5b4a4e6f4e83fad824cc32f3b0" + +[[package]] +name = "protoc-bin-vendored-linux-x86_32" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8854774b24ee28b7868cd71dccaae8e02a2365e67a4a87a6cd11ee6cdbdf9cf5" + +[[package]] +name = "protoc-bin-vendored-linux-x86_64" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b38b07546580df720fa464ce124c4b03630a6fb83e05c336fea2a241df7e5d78" + +[[package]] +name = "protoc-bin-vendored-macos-aarch_64" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89278a9926ce312e51f1d999fee8825d324d603213344a9a706daa009f1d8092" + +[[package]] +name = "protoc-bin-vendored-macos-x86_64" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81745feda7ccfb9471d7a4de888f0652e806d5795b61480605d4943176299756" + +[[package]] +name = "protoc-bin-vendored-win32" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95067976aca6421a523e491fce939a3e65249bac4b977adee0ee9771568e8aa3" + +[[package]] +name = "pulldown-cmark" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9f068eba8e7071c5f9511831b44f32c740d5adf574e990f946ddb53db2f314e" +dependencies = [ + "bitflags", + "memchr", + "unicase", +] + +[[package]] +name = "pulldown-cmark-to-cmark" +version = "22.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab1ad36992cead65f02aa399a373a42730922f1525d988172634fdefdecb8a60" +dependencies = [ + "pulldown-cmark", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.17", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted 0.9.0", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b74b56ffa8bb2830709a538c2cbcae9aa062db0d2a42563bfb09bdaae44020eb" + +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + +[[package]] +name = "rustix" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustls" +version = "0.23.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634" +dependencies = [ + "aws-lc-rs", + "log", + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-native-certs" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dab5152771c58876a2146916e53e35057e1a4dfa2b9df0f0305b07f611fdea4d" +dependencies = [ + "openssl-probe", + "rustls-pki-types", + "schannel", + "security-framework", +] + +[[package]] +name = "rustls-pemfile" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "rustls-pki-types" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96" +dependencies = [ + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2" +dependencies = [ + "aws-lc-rs", + "ring", + "rustls-pki-types", + "untrusted 0.9.0", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "schannel" +version = "0.1.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91c1b7e4904c873ef0710c1f407dde2e6287de2bebc1bbbf7d430bb7cbffd939" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "security-framework" +version = "3.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + +[[package]] +name = "serde_json" +version = "1.0.151" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +dependencies = [ + "rand_core", +] + +[[package]] +name = "simple_asn1" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d585997b0ac10be3c5ee635f1bab02d512760d14b7c468801ac8a01d9ae5f1d" +dependencies = [ + "num-bigint", + "num-traits", + "thiserror", + "time", +] + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba467056f1b547ed52077911161fc86985becbc60e8e1857c8a144dab0def891" + +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "supervisor-middleware-git-signing" +version = "0.0.0" +dependencies = [ + "clap", + "jsonwebtoken", + "openshell-core", + "openshell-extension-core", + "prost-types", + "tempfile", + "tokio", + "tokio-stream", + "tonic", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "supports-color" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c64fc7232dd8d2e4ac5ce4ef302b1d81e0b80d055b9d77c7c4f51f6aa4c867d6" +dependencies = [ + "is_ci", +] + +[[package]] +name = "supports-hyperlinks" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e396b6523b11ccb83120b115a0b7366de372751aa6edf19844dfb13a6af97e91" + +[[package]] +name = "supports-unicode" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7401a30af6cb5818bb64852270bb722533397edcfc7344954a38f420819ece2" + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8593e8e72159ed2257d083c7a454a85cbf854f37a0966d8d483aff8c8a3ebcee" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" + +[[package]] +name = "synstructure" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom 0.4.3", + "once_cell", + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "terminal_size" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "230a1b821ccbd75b185820a1f1ff7b14d21da1e442e22c0863ea5f08771a8874" +dependencies = [ + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "textwrap" +version = "0.16.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ecfad6c3abc80a577f2b91c1e412ee57e7a060d430b553c1b0c940974ebcd49" +dependencies = [ + "icu_segmenter", + "unicode-width 0.2.2", +] + +[[package]] +name = "thiserror" +version = "2.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec86235f5fcc2a73650310756d2ac5b138a5780bbbdfae3eeccec992c435ba4f" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc04cd3e1236dd4a98afca4569f2deb3f120e5422a4023be2cb683f8486292af" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + +[[package]] +name = "thread_local" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad99c4c6d32803332c548b1af0540b357b3f5fc0be8f6c6bfe8b2e6ae784070" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "time" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb87b95ec50ddfa440816d227a17b2ccbdda963a316a727fda0fc4334f7d134" +dependencies = [ + "deranged", + "num-conv", + "powerfmt", + "serde_core", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109" + +[[package]] +name = "time-macros" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e689342a48d2ea927c87ea50cabf8594854bf940e9310208848d680d668ed85" +dependencies = [ + "num-conv", + "time-core", +] + +[[package]] +name = "tinystr" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1e27c91459209c2986af3dcf603a5a74a4368754ce37414f59acc971167f643" +dependencies = [ + "displaydoc", + "serde_core", + "zerovec", +] + +[[package]] +name = "tokio" +version = "1.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "202caea871b69668250d242070849eb495be178ed697a3e98aebce5bc81a0bed" +dependencies = [ + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-macros" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0c85f2c3ef0b1cd58b36682f4b17aaa995f0e5db534d85692b4903abce21f67" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "tokio-stream" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3d06f0b082ba57c26b79407372e57cf2a1e28124f78e9479fe80322cf53420b" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "494815d09bf52b5548659851081238f0ca39ff638363907596da739561c62c52" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "libc", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tonic" +version = "0.14.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac2a5518c70fa84342385732db33fb3f44bc4cc748936eb5833d2df34d6445ef" +dependencies = [ + "async-trait", + "axum", + "base64", + "bytes", + "h2", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-timeout", + "hyper-util", + "percent-encoding", + "pin-project", + "rustls-native-certs", + "socket2", + "sync_wrapper", + "tokio", + "tokio-rustls", + "tokio-stream", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tonic-build" +version = "0.14.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c68f61875ac5293cf72e6c8cf0158086428c82c37229e98c840878f1706b0322" +dependencies = [ + "prettyplease", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "tonic-prost" +version = "0.14.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50849f68853be452acf590cde0b146665b8d507b3b8af17261df47e02c209ea0" +dependencies = [ + "bytes", + "prost", + "tonic", +] + +[[package]] +name = "tonic-prost-build" +version = "0.14.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "654e5643eff75d7f8c99197ce1440ed19a3474eada74c12bbac488b2cafdae27" +dependencies = [ + "prettyplease", + "proc-macro2", + "prost-build", + "prost-types", + "quote", + "syn 2.0.119", + "tempfile", + "tonic-build", +] + +[[package]] +name = "tonic-types" +version = "0.14.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73ab1b02061f83d519bba3caa167f88f261ef05720ab8ebc954ade70de3348e8" +dependencies = [ + "prost", + "prost-types", + "tonic", +] + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "indexmap", + "pin-project-lite", + "slab", + "sync_wrapper", + "tokio", + "tokio-util", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex-automata", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "unicase" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbc4bc3a9f746d862c45cb89d705aa10f187bb96c76001afab07a0d35ce60142" + +[[package]] +name = "unicode-ident" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954" + +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + +[[package]] +name = "unicode-width" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" + +[[package]] +name = "untrusted" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "uuid" +version = "1.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ef6dac1e96601b4fb3acccccff2139741fcb757cb9a36089bf5be91cfb285ce" +dependencies = [ + "getrandom 0.4.3", + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasm-bindgen" +version = "0.2.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aecb87a33d3b0c5e3b7aa46336eaf486cffafbd281b195e4c8b80d50df2351bf" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a690d511e3c1a8b3a55e33511e3c2c00c78415cd23650f32b808627f5696b9ed" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "411e4887f0071ef2d2164a9d5fdf2d20efbef78fccd3a78b0c10a1dc5295e48a" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 3.0.6", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81941cd78d0c92026c33e5e01312845a4cb1e9af3407f9134b100dd03144103e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "writeable" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" + +[[package]] +name = "yoke" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "709fe23a0424b6a435d82152b1bd3fdfb0833487d5fa90d05d42762a9891fef5" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33811428bee40dbceb6d545e95754741d17a6aef9a4849f0fd62e2ba4f412a78" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", + "synstructure", +] + +[[package]] +name = "zerofrom" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f75b4683f6c7f45248d4d64056a24298c6281e0993356d7d1b4a1a962ef10d4a" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c50655cbb0fe3fc43170059e702f1ce5e19b84cec58dc87b037a09935c2f328" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "zerotrie" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "zerovec" +version = "0.11.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0464e17806c1d976d5cba29399c7f08e516e279e2ba493f63123b5fca67dd8" +dependencies = [ + "serde", + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34df6fc39dbd26ddc9c10e6a2984476e13acce22e64e4487636ef494369225da" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/examples/supervisor-middleware-git-signing/Cargo.toml b/examples/supervisor-middleware-git-signing/Cargo.toml new file mode 100644 index 000000000..d5b63eeb7 --- /dev/null +++ b/examples/supervisor-middleware-git-signing/Cargo.toml @@ -0,0 +1,28 @@ +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +[workspace] + +[package] +name = "supervisor-middleware-git-signing" +version = "0.0.0" +edition = "2024" +rust-version = "1.90" +license = "Apache-2.0" +publish = false + +[dependencies] +clap = { version = "4.5", features = ["derive"] } +jsonwebtoken = { version = "10", features = ["aws_lc_rs"] } +openshell-core = { path = "../../crates/openshell-core", default-features = false } +openshell-extension-core = { path = "../../crates/openshell-extension-core" } +prost-types = "0.14" +tempfile = "3" +tokio = { version = "1.43", features = ["fs", "io-util", "macros", "rt-multi-thread"] } +tokio-stream = "0.1" +tonic = { version = "0.14", features = ["transport", "tls-ring"] } +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter"] } + +[lints.rust] +unsafe_code = "forbid" diff --git a/examples/supervisor-middleware-git-signing/README.md b/examples/supervisor-middleware-git-signing/README.md new file mode 100644 index 000000000..ff0ab3bfa --- /dev/null +++ b/examples/supervisor-middleware-git-signing/README.md @@ -0,0 +1,100 @@ + + +# Git commit signing supervisor middleware prototype + +This example tests whether an operator-run OpenShell supervisor middleware can sign commits without mounting a signing key into the sandbox. It uses the streaming `HttpRequestPreCredentials.Evaluate` API to intercept Git smart-HTTP `git-receive-pack` requests after network policy admission and before provider credential injection. The service selects `OWNED_STREAM_BYTES`, accepts bounded 64 KiB units into an unlinked temporary file, rewrites the pushed commit objects with SSH signatures, and streams the replacement request back in bounded units. + +The sandbox sees neither the private key nor the signature operation. Its Git client creates ordinary unsigned commits and pushes over HTTPS. + +## What the prototype does + +For each direct branch update in a push, the service: + +1. Derives a bounded `github.com//.git` upstream URL from the policy-admitted request target. +2. Shallow-fetches the upstream default branch and updated branch tips into a temporary bare repository. This supplies bases omitted from normal thin pushes. +3. Decodes the receive-pack command pkt-lines and packfile, then walks only commits that are not already reachable from the fetched upstream refs. +4. Removes any existing commit signature and signs the exact rewritten commit payload with `ssh-keygen -Y sign -n git`. +5. Rewrites parent object IDs, creates a replacement thin pack, and substitutes the new branch-tip object ID in the receive-pack command. +6. Returns the modified body to the supervisor, which forwards it with the sandbox's normal GitHub credential. + +The signing key is a service startup argument, not policy-controlled middleware configuration. A sandbox cannot select an arbitrary host file to sign with. + +## Build and test + +The test constructs a two-commit push larger than the former 4 MiB unary limit, transforms it, sends the replacement request through Git's real `receive-pack --stateless-rpc`, checks the updated branch tip, and verifies both SSH signatures with `git verify-commit`. + +```shell +cargo test --manifest-path examples/supervisor-middleware-git-signing/Cargo.toml +``` + +The example requires `git` and `ssh-keygen` on the host. + +## Run the service + +Use an SSH key that is also registered as a signing key with the Git forge. GitHub distinguishes signing keys from authentication keys even when the same public key material is used. + +The service requires TLS and verifies every OpenShell caller with an exact-audience extension JWT. Provision a service certificate and private key whose DNS name matches the middleware endpoint, the issuing CA certificate for the gateway registration, and the Ed25519 public key configured in `[openshell.gateway.gateway_jwt]`. + +```shell +cargo run \ + --manifest-path examples/supervisor-middleware-git-signing/Cargo.toml \ + -- \ + --bind 0.0.0.0:50051 \ + --signing-key /run/secrets/git-signing-key \ + --tls-cert /run/secrets/git-signer.pem \ + --tls-key /run/secrets/git-signer-key.pem \ + --extension-public-key /run/secrets/openshell-extension-public.pem \ + --expected-gateway-id openshell \ + --audience urn:openshell:extension:middleware:local-git-signer \ + --max-concurrent-signings 2 \ + --signing-timeout-seconds 25 +``` + +Register the local service in `gateway.toml`. A container-backed gateway can reach a host service through `host.openshell.internal`; a host-native gateway can use `127.0.0.1`. + +```toml +[[openshell.supervisor.middleware]] +name = "local-git-signer" +grpc_endpoint = "https://host.openshell.internal:50051" +tls_ca_cert_path = "/etc/openshell/certs/git-signer-ca.pem" +audience = "urn:openshell:extension:middleware:local-git-signer" +max_payload_bytes = 65536 +timeout = "30s" + +[openshell.gateway.gateway_jwt] +signing_key_path = "/etc/openshell/jwt/signing.pem" +public_key_path = "/etc/openshell/jwt/public.pem" +kid_path = "/etc/openshell/jwt/kid" +gateway_id = "openshell" +ttl_secs = 3600 +``` + +The `public_key_path` file must be the same key supplied to the service with `--extension-public-key`. The registration audience must exactly match `--audience`, and `gateway_id` must exactly match `--expected-gateway-id`. The service authorizes gateway callers for discovery and configuration validation and sandbox supervisor callers for request evaluation. + +Set `max_payload_bytes` to 65536 as shown for full-size stream units. A lower positive value uses smaller output units; the value does not cap the complete push. OpenShell separately enforces a bounded deferred-storage limit for owned streams. Keep the registration timeout above the service's signing timeout so OpenShell can receive either the signed output or a controlled failure. + +Restart the gateway after adding the static registration, then replace `` and `` in [policy.yaml](policy.yaml) and create a sandbox with that policy. Keep the endpoint on `fail_closed`: reject a push rather than forwarding it unsigned when signing fails. + +The service skips unrelated GitHub HTTP requests during preflight. It inspects only `POST` requests whose path ends in `/git-receive-pack` and whose content type is `application/x-git-receive-pack-request`. This example accepts only validated HTTPS targets on `github.com:443` with a two-segment repository path. + +The signer implements `SupervisorMiddleware` for discovery and configuration validation, and serves `HttpRequestPreCredentials` alongside it. The request stream is the only HTTP request middleware API. + +## Resource bounds and cancellation + +Input and replacement packs stay in unlinked temporary files. The service parses only a bounded 1 MiB receive-pack prefix in memory, feeds the input pack directly to `git index-pack`, and writes the replacement pack directly to its output file. It emits the replacement to OpenShell in bounded units. + +`--max-concurrent-signings` limits active Git rewrite workers. `--signing-timeout-seconds` covers upstream fetches, graph rewriting, signing, and pack generation. If the OpenShell request is canceled or the deadline expires, the service kills the active `git` or `ssh-keygen` subprocess and waits for it to exit. Subprocess diagnostic capture is bounded and is not returned to the sandbox. + +## Prototype limits + +- Owned streams are available only with `on_error: fail_closed`. Once the service acknowledges ownership, OpenShell has no replay copy and cannot safely fail open. +- The implementation supports SHA-1 repositories and direct `refs/heads/*` updates. It rejects SHA-256 repositories, annotated-tag pushes, push certificates, and other ref types. +- Each push shallow-fetches upstream objects before signing. The host must be able to reach the repository, and private repositories need a non-interactive Git credential helper available to the local middleware user. A production deployment should use a bounded object cache and explicit credential plumbing. +- The service shells out to the host's `git` and `ssh-keygen`. Run it with OS-level CPU, memory, process, temporary-storage, and network limits in addition to its own concurrency and time bounds. +- Rewriting commit object IDs means the sandbox's local branch still points to the unsigned commit after a successful push. A subsequent fetch updates the remote-tracking ref, but the local branch must be reconciled with the rewritten history. This is the largest workflow issue for transparent push-time signing. +- The example has protocol and local end-to-end Git coverage, not a live GitHub push. Test against a disposable repository before using a real signing key. + +These constraints make the approach viable as a focused deployment or proof of concept, but not yet transparent enough for general production pushes. A first-class commit-signing operation invoked before Git creates the final local object would avoid the local/remote object-ID split while still keeping the private key outside the sandbox. diff --git a/examples/supervisor-middleware-git-signing/policy.yaml b/examples/supervisor-middleware-git-signing/policy.yaml new file mode 100644 index 000000000..45c861bfc --- /dev/null +++ b/examples/supervisor-middleware-git-signing/policy.yaml @@ -0,0 +1,48 @@ +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +version: 1 + +filesystem_policy: + include_workdir: true + read_only: [/usr, /lib, /proc, /dev/urandom, /etc] + read_write: [/sandbox, /tmp, /dev/null] + +landlock: + compatibility: best_effort + +process: + run_as_user: sandbox + run_as_group: sandbox + +network_middlewares: + sign-git-commits: + name: Sign outgoing Git commits + middleware: local-git-signer + order: 10 + config: {} + on_error: fail_closed + endpoints: + include: [github.com] + +network_policies: + github_git: + name: github-git + endpoints: + - host: github.com + port: 443 + protocol: rest + enforcement: enforce + rules: + - allow: + method: GET + path: "//.git/info/refs*" + - allow: + method: POST + path: "//.git/git-upload-pack" + - allow: + method: POST + path: "//.git/git-receive-pack" + binaries: + - { path: /usr/bin/git } + - { path: /usr/lib/git-core/git-remote-http } diff --git a/examples/supervisor-middleware-git-signing/src/main.rs b/examples/supervisor-middleware-git-signing/src/main.rs new file mode 100644 index 000000000..71d52bde8 --- /dev/null +++ b/examples/supervisor-middleware-git-signing/src/main.rs @@ -0,0 +1,1022 @@ +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +mod signer; + +use std::collections::HashMap; +use std::net::SocketAddr; +use std::path::PathBuf; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; + +use clap::Parser; +use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode, decode_header}; +use openshell_core::middleware::{HttpRequestResultStream, WebSocketResponseStream}; +use openshell_core::proto::middleware::v1::http_request_pre_credentials_server::{ + HttpRequestPreCredentials, HttpRequestPreCredentialsServer, +}; +use openshell_core::proto::middleware::v1::supervisor_middleware_server::{ + SupervisorMiddleware, SupervisorMiddlewareServer, +}; +use openshell_core::proto::{ + Finding, HttpRequestBodyFinalize, HttpRequestBodyMode, HttpRequestBodyOutput, + HttpRequestBodyResult, HttpRequestBodyTakeOwnership, HttpRequestEvent, HttpRequestEventResult, + HttpRequestPreflight, HttpRequestPreflightInspect, HttpRequestPreflightResult, + HttpRequestPreflightSkip, HttpRequestTrailersResult, MiddlewareBinding, MiddlewareManifest, + SupervisorMiddlewareOperation, SupervisorMiddlewarePhase, ValidateConfigRequest, + ValidateConfigResponse, WebSocketSessionEvent, http_request_body_result, + http_request_body_unit, http_request_event, http_request_event_result, + http_request_preflight_result, +}; +use openshell_extension_core::{ + EXTENSION_JWT_TYP, ExtensionCallerKind, ExtensionJwtClaims, MAX_EXTENSION_TOKEN_TTL, +}; +use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _}; +use tokio_stream::StreamExt as _; +use tokio_stream::wrappers::ReceiverStream; +use tonic::transport::{Identity, Server, ServerTlsConfig}; +use tonic::{Request, Response, Status}; +use tracing::{info, warn}; +use tracing_subscriber::EnvFilter; + +use crate::signer::{GitSigner, SignControl}; + +const MANIFEST_NAME: &str = "example/git-commit-signing"; +const OPERATION: SupervisorMiddlewareOperation = SupervisorMiddlewareOperation::HttpRequest; +const PHASE: SupervisorMiddlewarePhase = SupervisorMiddlewarePhase::PreCredentials; +const MAX_UNIT_BYTES: usize = 64 * 1024; +const JWT_CLOCK_SKEW_SECONDS: i64 = 60; + +#[derive(Debug, Parser)] +#[command(about = "Sign commits in Git smart-HTTP pushes outside an OpenShell sandbox")] +struct Cli { + /// Address on which to serve authenticated TLS gRPC. + #[arg(long, default_value = "127.0.0.1:50051")] + bind: SocketAddr, + + /// Local SSH private key used by ssh-keygen. This path is never sent to the sandbox. + #[arg(long)] + signing_key: PathBuf, + + /// PEM certificate presented by this middleware service. + #[arg(long)] + tls_cert: PathBuf, + + /// PEM private key for the middleware TLS certificate. + #[arg(long)] + tls_key: PathBuf, + + /// PEM Ed25519 public key used to verify OpenShell extension JWTs. + #[arg(long)] + extension_public_key: PathBuf, + + /// Gateway ID expected in extension-token issuer claims. + #[arg(long)] + expected_gateway_id: String, + + /// Exact extension JWT audience configured for this registration. + #[arg(long)] + audience: String, + + /// Maximum number of concurrent Git rewrite workers. + #[arg(long, default_value_t = 2)] + max_concurrent_signings: usize, + + /// Total deadline for fetch, rewrite, signing, and pack generation. + #[arg(long, default_value_t = 25)] + signing_timeout_seconds: u64, +} + +struct ExtensionAuth { + decoding_key: DecodingKey, + issuer: String, + audience: String, +} + +impl ExtensionAuth { + fn new(public_key_pem: &[u8], gateway_id: &str, audience: String) -> Result { + if gateway_id.is_empty() || audience.is_empty() { + return Err("expected gateway ID and audience must be nonempty".into()); + } + let decoding_key = DecodingKey::from_ed_pem(public_key_pem) + .map_err(|error| format!("invalid extension public key: {error}"))?; + Ok(Self { + decoding_key, + issuer: format!("openshell-gateway:{gateway_id}"), + audience, + }) + } + + fn authenticate( + &self, + request: &Request, + required_caller: Option, + ) -> Result<(), Status> { + let authorization = request + .metadata() + .get("authorization") + .and_then(|value| value.to_str().ok()) + .and_then(|value| value.strip_prefix("Bearer ")) + .ok_or_else(|| Status::unauthenticated("extension bearer token is required"))?; + let header = decode_header(authorization) + .map_err(|_| Status::unauthenticated("extension bearer token is invalid"))?; + if header.alg != Algorithm::EdDSA + || header.typ.as_deref() != Some(EXTENSION_JWT_TYP) + || header.kid.as_deref().is_none_or(str::is_empty) + { + return Err(Status::unauthenticated( + "extension bearer token header is invalid", + )); + } + let mut validation = Validation::new(Algorithm::EdDSA); + validation.set_issuer(&[self.issuer.as_str()]); + validation.set_audience(&[self.audience.as_str()]); + validation.set_required_spec_claims(&["iss", "aud", "sub", "iat", "exp", "jti"]); + let claims = decode::(authorization, &self.decoding_key, &validation) + .map_err(|_| Status::unauthenticated("extension bearer token is invalid"))? + .claims; + if claims.jti.is_empty() + || claims.iat < 0 + || claims.exp <= claims.iat + || u64::try_from(claims.exp - claims.iat) + .ok() + .is_none_or(|ttl| ttl > MAX_EXTENSION_TOKEN_TTL.as_secs()) + { + return Err(Status::unauthenticated( + "extension bearer token lifetime is invalid", + )); + } + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_err(|_| Status::internal("system clock is before the Unix epoch"))? + .as_secs() as i64; + if claims.iat > now.saturating_add(JWT_CLOCK_SKEW_SECONDS) { + return Err(Status::unauthenticated( + "extension bearer token issue time is invalid", + )); + } + if required_caller.is_some_and(|required| claims.caller_kind != required) { + return Err(Status::permission_denied( + "extension caller kind is not authorized for this RPC", + )); + } + match claims.caller_kind { + ExtensionCallerKind::Gateway + if claims.sandbox_id.is_none() && claims.sub == self.issuer => {} + ExtensionCallerKind::Supervisor => { + let sandbox_id = claims.sandbox_id.as_deref().filter(|id| !id.is_empty()); + if sandbox_id + .is_none_or(|id| claims.sub != format!("spiffe://openshell/sandbox/{id}")) + { + return Err(Status::permission_denied( + "extension supervisor identity is invalid", + )); + } + } + _ => { + return Err(Status::permission_denied( + "extension gateway identity is invalid", + )); + } + } + Ok(()) + } +} + +#[derive(Clone)] +struct GitSigningMiddleware { + signer: Arc, + auth: Arc, + signing_slots: Arc, + signing_timeout: Duration, + expected_audience: String, +} + +impl GitSigningMiddleware { + fn new( + signing_key: PathBuf, + max_concurrent_signings: usize, + signing_timeout: Duration, + auth: ExtensionAuth, + ) -> Result { + if max_concurrent_signings == 0 { + return Err("max concurrent signings must be positive".into()); + } + Ok(Self { + signer: Arc::new(GitSigner::new(signing_key)?), + expected_audience: auth.audience.clone(), + auth: Arc::new(auth), + signing_slots: Arc::new(tokio::sync::Semaphore::new(max_concurrent_signings)), + signing_timeout, + }) + } + + #[cfg(test)] + fn new_for_test(signing_key: PathBuf) -> Result { + Ok(Self { + signer: Arc::new(GitSigner::new(signing_key)?), + auth: Arc::new(ExtensionAuth { + decoding_key: DecodingKey::from_secret(b"unused-test-key"), + issuer: "openshell-gateway:test".into(), + audience: "urn:openshell:extension:test:git-signing".into(), + }), + signing_slots: Arc::new(tokio::sync::Semaphore::new(1)), + signing_timeout: Duration::from_secs(60), + expected_audience: "urn:openshell:extension:test:git-signing".into(), + }) + } + + fn request_stream(&self, mut events: S) -> HttpRequestResultStream + where + S: tokio_stream::Stream> + Send + Unpin + 'static, + { + let signer = Arc::clone(&self.signer); + let signing_slots = Arc::clone(&self.signing_slots); + let signing_timeout = self.signing_timeout; + let (results_tx, results_rx) = tokio::sync::mpsc::channel(4); + tokio::spawn(async move { + let mut selection = None; + let mut input = None; + let mut input_bytes = 0u64; + let mut next_input_sequence = 1u64; + let mut final_input_sequence = None; + + while let Some(event) = events.next().await { + let event = match event { + Ok(event) => event, + Err(error) => { + let _ = results_tx.send(Err(error)).await; + break; + } + }; + match event.event { + Some(http_request_event::Event::Preflight(preflight)) + if selection.is_none() => + { + match select_request(&preflight) { + Ok(None) => { + selection = Some(Selection::Skipped); + if results_tx.send(Ok(preflight_skip())).await.is_err() { + break; + } + } + Ok(Some(selected)) => { + if !preflight + .permitted_body_modes + .contains(&(HttpRequestBodyMode::OwnedStreamBytes as i32)) + || preflight.max_deferred_bytes == 0 + { + send_stream_error( + &results_tx, + Status::failed_precondition( + "Git signing requires fail-closed owned request streaming", + ), + ) + .await; + break; + } + match tempfile::tempfile() { + Ok(file) => { + input = Some(tokio::fs::File::from_std(file)); + selection = Some(selected); + if results_tx.send(Ok(preflight_owned())).await.is_err() { + break; + } + } + Err(_) => { + send_stream_error( + &results_tx, + Status::resource_exhausted( + "Git signing temporary storage is unavailable", + ), + ) + .await; + break; + } + } + } + Err(status) => { + send_stream_error(&results_tx, status).await; + break; + } + } + } + Some(http_request_event::Event::Body(body)) + if matches!(selection, Some(Selection::Sign { .. })) + && final_input_sequence.is_none() => + { + if body.sequence != next_input_sequence { + send_stream_error( + &results_tx, + Status::invalid_argument( + "Git signing input sequence is not contiguous", + ), + ) + .await; + break; + } + let Some(http_request_body_unit::Payload::Data(data)) = body.payload else { + send_stream_error( + &results_tx, + Status::invalid_argument("Git signing body data is required"), + ) + .await; + break; + }; + input_bytes = input_bytes.saturating_add(data.len() as u64); + let max_deferred_bytes = match selection.as_ref() { + Some(Selection::Sign { limits, .. }) => limits.deferred_bytes, + _ => 0, + }; + if input_bytes > max_deferred_bytes { + send_stream_error( + &results_tx, + Status::resource_exhausted( + "Git signing request exceeds deferred storage limit", + ), + ) + .await; + break; + } + let Some(file) = input.as_mut() else { + send_stream_error( + &results_tx, + Status::internal("Git signing temporary storage was lost"), + ) + .await; + break; + }; + if file.write_all(&data).await.is_err() { + send_stream_error( + &results_tx, + Status::resource_exhausted( + "Git signing temporary storage write failed", + ), + ) + .await; + break; + } + if results_tx + .send(Ok(body_take_ownership(body.sequence))) + .await + .is_err() + { + break; + } + next_input_sequence = next_input_sequence.saturating_add(1); + if body.end_of_stream { + final_input_sequence = Some(body.sequence); + let Some(Selection::Sign { + upstream_url, + request_id, + limits, + .. + }) = selection.as_ref() + else { + break; + }; + let upstream_url = upstream_url.clone(); + let request_id = request_id.clone(); + let limits = *limits; + if let Err(error) = finish_signing( + Arc::clone(&signer), + SigningRequest { + input: input + .take() + .expect("selected request has temporary storage"), + upstream_url, + request_id, + final_input_sequence: body.sequence, + limits, + }, + Arc::clone(&signing_slots), + signing_timeout, + &results_tx, + ) + .await + { + send_stream_error(&results_tx, error).await; + break; + } + } + } + Some(http_request_event::Event::Trailers(_)) + if final_input_sequence.is_some() => + { + if results_tx + .send(Ok(HttpRequestEventResult { + result: Some(http_request_event_result::Result::TrailersResult( + HttpRequestTrailersResult::default(), + )), + })) + .await + .is_err() + { + break; + } + } + Some(http_request_event::Event::SessionEnd(_)) if selection.is_some() => break, + _ => { + send_stream_error( + &results_tx, + Status::failed_precondition( + "invalid Git signing request stream lifecycle", + ), + ) + .await; + break; + } + } + } + }); + Box::pin(ReceiverStream::new(results_rx)) + } +} + +enum Selection { + Skipped, + Sign { + upstream_url: String, + request_id: String, + limits: OwnedOutputLimits, + }, +} + +#[derive(Clone, Copy)] +struct OwnedOutputLimits { + deferred_bytes: u64, + unit_bytes: usize, +} + +struct SigningRequest { + input: tokio::fs::File, + upstream_url: String, + request_id: String, + final_input_sequence: u64, + limits: OwnedOutputLimits, +} + +#[tonic::async_trait] +impl SupervisorMiddleware for GitSigningMiddleware { + type EvaluateWebSocketSessionStream = WebSocketResponseStream; + + async fn describe(&self, request: Request<()>) -> Result, Status> { + self.auth.authenticate(&request, None)?; + Ok(Response::new(MiddlewareManifest { + name: MANIFEST_NAME.into(), + service_version: env!("CARGO_PKG_VERSION").into(), + bindings: vec![MiddlewareBinding { + operation: OPERATION as i32, + phase: PHASE as i32, + max_payload_bytes: MAX_UNIT_BYTES as u64, + request_timeout: Some(prost_types::Duration { + seconds: 30, + nanos: 0, + }), + }], + expected_audience: self.expected_audience.clone(), + })) + } + + async fn validate_config( + &self, + request: Request, + ) -> Result, Status> { + self.auth + .authenticate(&request, Some(ExtensionCallerKind::Gateway))?; + let request = request.into_inner(); + let unknown = request + .config + .as_ref() + .and_then(|config| config.fields.keys().next()) + .cloned(); + Ok(Response::new(match unknown { + None => ValidateConfigResponse { + valid: true, + reason: String::new(), + }, + Some(field) => ValidateConfigResponse { + valid: false, + reason: format!("unsupported config field '{field}'"), + }, + })) + } + + async fn evaluate_web_socket_session( + &self, + request: Request>, + ) -> Result, Status> { + self.auth + .authenticate(&request, Some(ExtensionCallerKind::Supervisor))?; + Err(Status::unimplemented( + "WebSocket middleware is not supported", + )) + } +} + +#[tonic::async_trait] +impl HttpRequestPreCredentials for GitSigningMiddleware { + type EvaluateStream = HttpRequestResultStream; + + async fn evaluate( + &self, + request: Request>, + ) -> Result, Status> { + self.auth + .authenticate(&request, Some(ExtensionCallerKind::Supervisor))?; + Ok(Response::new(self.request_stream(request.into_inner()))) + } +} + +fn select_request(preflight: &HttpRequestPreflight) -> Result, Status> { + if !is_receive_pack_request(preflight) { + return Ok(None); + } + let target = preflight + .target + .as_ref() + .ok_or_else(|| Status::invalid_argument("Git push request has no target"))?; + let max_output_unit_bytes = usize::try_from(preflight.max_payload_bytes) + .ok() + .filter(|limit| *limit > 0) + .map(|limit| limit.min(MAX_UNIT_BYTES)) + .ok_or_else(|| Status::failed_precondition("Git signing output unit limit is invalid"))?; + Ok(Some(Selection::Sign { + upstream_url: github_upstream_url(target)?, + request_id: preflight + .context + .as_ref() + .map(|context| context.request_id.clone()) + .unwrap_or_default(), + limits: OwnedOutputLimits { + deferred_bytes: preflight.max_deferred_bytes, + unit_bytes: max_output_unit_bytes, + }, + })) +} + +fn preflight_skip() -> HttpRequestEventResult { + HttpRequestEventResult { + result: Some(http_request_event_result::Result::PreflightResult( + HttpRequestPreflightResult { + action: Some(http_request_preflight_result::Action::Skip( + HttpRequestPreflightSkip {}, + )), + reason_code: "not_git_receive_pack".into(), + ..Default::default() + }, + )), + } +} + +fn preflight_owned() -> HttpRequestEventResult { + HttpRequestEventResult { + result: Some(http_request_event_result::Result::PreflightResult( + HttpRequestPreflightResult { + action: Some(http_request_preflight_result::Action::Inspect( + HttpRequestPreflightInspect { + body_mode: HttpRequestBodyMode::OwnedStreamBytes as i32, + header_mutations: Vec::new(), + }, + )), + reason_code: "git_receive_pack_selected".into(), + ..Default::default() + }, + )), + } +} + +fn body_take_ownership(sequence: u64) -> HttpRequestEventResult { + HttpRequestEventResult { + result: Some(http_request_event_result::Result::BodyResult( + HttpRequestBodyResult { + sequence, + action: Some(http_request_body_result::Action::TakeOwnership( + HttpRequestBodyTakeOwnership {}, + )), + ..Default::default() + }, + )), + } +} + +async fn finish_signing( + signer: Arc, + request: SigningRequest, + signing_slots: Arc, + signing_timeout: Duration, + sender: &tokio::sync::mpsc::Sender>, +) -> Result<(), Status> { + let SigningRequest { + mut input, + upstream_url, + request_id, + final_input_sequence, + limits, + } = request; + input + .flush() + .await + .map_err(|_| Status::internal("Git signing temporary storage flush failed"))?; + let input = input.into_std().await; + let log_upstream = upstream_url.clone(); + let log_request_id = request_id.clone(); + let _permit = signing_slots + .acquire_owned() + .await + .map_err(|_| Status::unavailable("Git signing service is shutting down"))?; + let cancelled = Arc::new(AtomicBool::new(false)); + let control = SignControl::new(Arc::clone(&cancelled), Instant::now() + signing_timeout); + let mut worker = tokio::task::spawn_blocking(move || { + signer.sign_receive_pack(input, Some(&upstream_url), &control) + }); + let signed = tokio::select! { + result = &mut worker => { + result.map_err(|_| Status::internal("Git signing worker failed"))? + } + () = sender.closed() => { + cancelled.store(true, Ordering::Release); + let _ = worker.await; + return Err(Status::cancelled("Git signing request was cancelled")); + } + () = tokio::time::sleep(signing_timeout) => { + cancelled.store(true, Ordering::Release); + let _ = worker.await; + return Err(Status::deadline_exceeded("Git signing deadline exceeded")); + } + } + .map_err(|error| { + let status = if error.is_cancelled() { + Status::cancelled("Git signing request was cancelled") + } else if error.is_timed_out() { + Status::deadline_exceeded("Git signing deadline exceeded") + } else { + Status::failed_precondition(error.public_message()) + }; + warn!( + request_id = log_request_id, + upstream = %log_upstream, + category = if error.is_cancelled() { "cancelled" } else if error.is_timed_out() { "timeout" } else { "invalid_push" }, + "outgoing Git push could not be signed" + ); + status + })?; + + if signed.body_len > limits.deferred_bytes { + return Err(Status::resource_exhausted( + "signed Git request exceeds deferred storage limit", + )); + } + + let signed_commits = signed.signed_commits; + let mut body = tokio::fs::File::from_std(signed.body); + let mut chunk = vec![0; limits.unit_bytes]; + let mut output_sequence = 0u64; + loop { + let read = body + .read(&mut chunk) + .await + .map_err(|_| Status::internal("signed Git temporary storage read failed"))?; + if read == 0 { + break; + } + output_sequence += 1; + sender + .send(Ok(HttpRequestEventResult { + result: Some(http_request_event_result::Result::BodyOutput( + HttpRequestBodyOutput { + sequence: output_sequence, + data: chunk[..read].to_vec(), + }, + )), + })) + .await + .map_err(|_| Status::cancelled("Git signing request was cancelled"))?; + } + sender + .send(Ok(HttpRequestEventResult { + result: Some(http_request_event_result::Result::BodyFinalize( + HttpRequestBodyFinalize { + through_input_sequence: final_input_sequence, + through_output_sequence: output_sequence, + reason_code: "git_commits_signed".into(), + findings: vec![Finding { + r#type: "git.commits_signed".into(), + label: "Git commits signed".into(), + count: signed_commits, + confidence: "high".into(), + severity: "informational".into(), + }], + metadata: HashMap::from([( + "signed_commit_count".into(), + signed_commits.to_string(), + )]), + ..Default::default() + }, + )), + })) + .await + .map_err(|_| Status::cancelled("Git signing request was cancelled"))?; + info!( + request_id, + upstream = %log_upstream, + signed_commits, + "signed outgoing Git push" + ); + Ok(()) +} + +async fn send_stream_error( + sender: &tokio::sync::mpsc::Sender>, + status: Status, +) { + let _ = sender.send(Err(status)).await; +} + +fn github_upstream_url( + target: &openshell_core::proto::HttpRequestTarget, +) -> Result { + if target.scheme != "https" || target.host != "github.com" || target.port != 443 { + return Err(Status::invalid_argument( + "prototype supports HTTPS pushes to github.com only", + )); + } + let repository_path = target + .path + .strip_suffix("/git-receive-pack") + .ok_or_else(|| Status::invalid_argument("invalid Git receive-pack path"))?; + let segments = repository_path + .strip_prefix('/') + .and_then(|path| path.strip_suffix(".git")) + .map(|path| path.split('/').collect::>()) + .ok_or_else(|| Status::invalid_argument("invalid GitHub repository path"))?; + if segments.len() != 2 + || segments.iter().any(|segment| { + segment.is_empty() + || !segment + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.')) + }) + { + return Err(Status::invalid_argument("invalid GitHub repository path")); + } + Ok(format!("https://github.com{repository_path}")) +} + +fn is_receive_pack_request(request: &HttpRequestPreflight) -> bool { + let Some(target) = request.target.as_ref() else { + return false; + }; + target.method == "POST" + && target.path.ends_with("/git-receive-pack") + && request.headers.iter().any(|header| { + header.name.eq_ignore_ascii_case("content-type") + && header + .value + .split(';') + .next() + .is_some_and(|value| value.trim() == "application/x-git-receive-pack-request") + }) +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + tracing_subscriber::fmt() + .with_env_filter(EnvFilter::from_default_env()) + .init(); + let cli = Cli::parse(); + if cli.signing_timeout_seconds == 0 { + return Err("signing timeout must be positive".into()); + } + let tls_cert = std::fs::read(&cli.tls_cert)?; + let tls_key = std::fs::read(&cli.tls_key)?; + let extension_public_key = std::fs::read(&cli.extension_public_key)?; + let auth = ExtensionAuth::new( + &extension_public_key, + &cli.expected_gateway_id, + cli.audience, + ) + .map_err(|error| format!("invalid extension authentication configuration: {error}"))?; + let middleware = GitSigningMiddleware::new( + cli.signing_key, + cli.max_concurrent_signings, + Duration::from_secs(cli.signing_timeout_seconds), + auth, + ) + .map_err(|error| format!("invalid signing configuration: {error}"))?; + info!(bind = %cli.bind, "starting Git commit signing middleware"); + Server::builder() + .tls_config(ServerTlsConfig::new().identity(Identity::from_pem(tls_cert, tls_key)))? + .add_service(SupervisorMiddlewareServer::new(middleware.clone())) + .add_service(HttpRequestPreCredentialsServer::new(middleware)) + .serve(cli.bind) + .await?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use jsonwebtoken::{EncodingKey, Header, encode}; + use openshell_core::proto::{ + HttpHeader, HttpRequestBodyUnit, HttpRequestTarget, MiddlewareSessionEnd, + http_request_event_result, + }; + + const TEST_PRIVATE_KEY: &[u8] = br#"-----BEGIN PRIVATE KEY----- +MC4CAQAwBQYDK2VwBCIEIAR9CeOXmiSU6YscHZWTYbW7DUc5uhdO3/OeXZg3j1+u +-----END PRIVATE KEY----- +"#; + const TEST_PUBLIC_KEY: &[u8] = br#"-----BEGIN PUBLIC KEY----- +MCowBQYDK2VwAyEAuEbM0q6xP8hFxwY5kd/fD/mwr3ZpA/T7zhx6TN0DKMM= +-----END PUBLIC KEY----- +"#; + + fn authenticated_request(claims: ExtensionJwtClaims) -> Request<()> { + let mut header = Header::new(Algorithm::EdDSA); + header.typ = Some(EXTENSION_JWT_TYP.into()); + header.kid = Some("test-key".into()); + let token = encode( + &header, + &claims, + &EncodingKey::from_ed_pem(TEST_PRIVATE_KEY).unwrap(), + ) + .unwrap(); + let mut request = Request::new(()); + request + .metadata_mut() + .insert("authorization", format!("Bearer {token}").parse().unwrap()); + request + } + + fn test_claims(caller_kind: ExtensionCallerKind) -> ExtensionJwtClaims { + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() as i64; + let (sub, sandbox_id) = match caller_kind { + ExtensionCallerKind::Gateway => ("openshell-gateway:test".into(), None), + ExtensionCallerKind::Supervisor => ( + "spiffe://openshell/sandbox/sandbox-test".into(), + Some("sandbox-test".into()), + ), + }; + ExtensionJwtClaims { + iss: "openshell-gateway:test".into(), + aud: "urn:openshell:extension:test:git-signing".into(), + sub, + iat: now, + exp: now + 300, + jti: "unique-test-token".into(), + caller_kind, + sandbox_id, + } + } + + #[test] + fn extension_auth_enforces_rpc_caller_kind() { + let auth = ExtensionAuth::new( + TEST_PUBLIC_KEY, + "test", + "urn:openshell:extension:test:git-signing".into(), + ) + .unwrap(); + + assert!( + auth.authenticate( + &authenticated_request(test_claims(ExtensionCallerKind::Gateway)), + Some(ExtensionCallerKind::Gateway), + ) + .is_ok() + ); + assert_eq!( + auth.authenticate( + &authenticated_request(test_claims(ExtensionCallerKind::Gateway)), + Some(ExtensionCallerKind::Supervisor), + ) + .unwrap_err() + .code(), + tonic::Code::PermissionDenied + ); + assert!( + auth.authenticate( + &authenticated_request(test_claims(ExtensionCallerKind::Supervisor)), + Some(ExtensionCallerKind::Supervisor), + ) + .is_ok() + ); + } + + #[test] + fn extension_auth_rejects_mismatched_supervisor_identity() { + let auth = ExtensionAuth::new( + TEST_PUBLIC_KEY, + "test", + "urn:openshell:extension:test:git-signing".into(), + ) + .unwrap(); + let mut claims = test_claims(ExtensionCallerKind::Supervisor); + claims.sub = "spiffe://openshell/sandbox/another-sandbox".into(); + + assert_eq!( + auth.authenticate( + &authenticated_request(claims), + Some(ExtensionCallerKind::Supervisor), + ) + .unwrap_err() + .code(), + tonic::Code::PermissionDenied + ); + } + + fn receive_pack_preflight() -> HttpRequestPreflight { + HttpRequestPreflight { + target: Some(HttpRequestTarget { + scheme: "https".into(), + host: "github.com".into(), + port: 443, + method: "POST".into(), + path: "/NVIDIA/OpenShell.git/git-receive-pack".into(), + ..Default::default() + }), + headers: vec![HttpHeader { + name: "content-type".into(), + value: "application/x-git-receive-pack-request".into(), + }], + permitted_body_modes: vec![HttpRequestBodyMode::OwnedStreamBytes as i32], + max_payload_bytes: MAX_UNIT_BYTES as u64, + max_deferred_bytes: 16 * 1024 * 1024, + ..Default::default() + } + } + + #[test] + fn recognizes_git_receive_pack_only() { + let request = receive_pack_preflight(); + assert!(is_receive_pack_request(&request)); + + let mut fetch = request; + fetch.target.as_mut().unwrap().path = "/NVIDIA/OpenShell.git/git-upload-pack".into(); + assert!(!is_receive_pack_request(&fetch)); + } + + #[test] + fn derives_a_bounded_github_upstream_url() { + let target = receive_pack_preflight().target.unwrap(); + assert_eq!( + github_upstream_url(&target).unwrap(), + "https://github.com/NVIDIA/OpenShell.git" + ); + + let mut traversal = target; + traversal.path = "/NVIDIA/../OpenShell.git/git-receive-pack".into(); + assert!(github_upstream_url(&traversal).is_err()); + } + + #[tokio::test] + async fn owned_stream_accepts_more_than_former_unary_limit() { + let key = tempfile::NamedTempFile::new().unwrap(); + let middleware = GitSigningMiddleware::new_for_test(key.path().to_path_buf()).unwrap(); + let mut events = vec![Ok(HttpRequestEvent { + event: Some(http_request_event::Event::Preflight( + receive_pack_preflight(), + )), + })]; + for sequence in 1..=65u64 { + events.push(Ok(HttpRequestEvent { + event: Some(http_request_event::Event::Body(HttpRequestBodyUnit { + sequence, + payload: Some(http_request_body_unit::Payload::Data(vec![ + b'x'; + MAX_UNIT_BYTES + ])), + end_of_stream: false, + })), + })); + } + events.push(Ok(HttpRequestEvent { + event: Some(http_request_event::Event::SessionEnd( + MiddlewareSessionEnd::default(), + )), + })); + + let mut results = middleware.request_stream(tokio_stream::iter(events)); + assert!(matches!( + results.next().await.unwrap().unwrap().result, + Some(http_request_event_result::Result::PreflightResult(_)) + )); + for sequence in 1..=65u64 { + let result = results.next().await.unwrap().unwrap(); + let Some(http_request_event_result::Result::BodyResult(result)) = result.result else { + panic!("expected body ownership result"); + }; + assert_eq!(result.sequence, sequence); + assert!(matches!( + result.action, + Some(http_request_body_result::Action::TakeOwnership(_)) + )); + } + assert!(results.next().await.is_none()); + } +} diff --git a/examples/supervisor-middleware-git-signing/src/signer.rs b/examples/supervisor-middleware-git-signing/src/signer.rs new file mode 100644 index 000000000..e36f92aa9 --- /dev/null +++ b/examples/supervisor-middleware-git-signing/src/signer.rs @@ -0,0 +1,1180 @@ +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +use std::collections::{HashMap, HashSet}; +use std::fmt; +use std::fs::{self, File}; +use std::io::{Read, Seek, SeekFrom, Write}; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::time::{Duration, Instant}; + +use tempfile::TempDir; + +const SHA1_HEX_LEN: usize = 40; +const ZERO_SHA1: &str = "0000000000000000000000000000000000000000"; +const MAX_RECEIVE_PACK_PREFIX_BYTES: usize = 1024 * 1024; +const MAX_CAPTURE_BYTES: usize = 64 * 1024 * 1024; +const CHILD_POLL_INTERVAL: Duration = Duration::from_millis(10); + +pub struct GitSigner { + signing_key: PathBuf, + upstream_override: Option, +} + +pub struct SignedPush { + pub body: File, + pub body_len: u64, + pub signed_commits: u32, +} + +#[derive(Clone)] +pub struct SignControl { + cancelled: Arc, + deadline: Instant, +} + +impl SignControl { + pub fn new(cancelled: Arc, deadline: Instant) -> Self { + Self { + cancelled, + deadline, + } + } + + fn check(&self) -> Result<(), SignError> { + if self.cancelled.load(Ordering::Acquire) { + return Err(SignError::cancelled()); + } + if Instant::now() >= self.deadline { + return Err(SignError::timed_out()); + } + Ok(()) + } +} + +#[derive(Debug)] +pub struct SignError { + message: String, + kind: SignErrorKind, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum SignErrorKind { + InvalidRequest, + Cancelled, + TimedOut, +} + +impl SignError { + fn new(message: impl Into) -> Self { + Self { + message: message.into(), + kind: SignErrorKind::InvalidRequest, + } + } + + fn cancelled() -> Self { + Self { + message: "Git signing was cancelled".into(), + kind: SignErrorKind::Cancelled, + } + } + + fn timed_out() -> Self { + Self { + message: "Git signing exceeded its deadline".into(), + kind: SignErrorKind::TimedOut, + } + } + + pub fn is_cancelled(&self) -> bool { + self.kind == SignErrorKind::Cancelled + } + + pub fn is_timed_out(&self) -> bool { + self.kind == SignErrorKind::TimedOut + } + + pub fn public_message(&self) -> &'static str { + "outgoing Git push could not be signed" + } +} + +impl fmt::Display for SignError { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(&self.message) + } +} + +impl std::error::Error for SignError {} + +impl GitSigner { + pub fn new(signing_key: PathBuf) -> Result { + if !signing_key.is_file() { + return Err("the signing key must name a readable file".into()); + } + Ok(Self { + signing_key, + upstream_override: None, + }) + } + + #[cfg(test)] + fn new_with_upstream_override( + signing_key: PathBuf, + upstream_override: String, + ) -> Result { + let mut signer = Self::new(signing_key)?; + signer.upstream_override = Some(upstream_override); + Ok(signer) + } + + pub fn sign_receive_pack( + &self, + mut body: File, + upstream_url: Option<&str>, + control: &SignControl, + ) -> Result { + control.check()?; + let mut parsed = ReceivePackRequest::parse_file(&mut body)?; + if parsed.updates.is_empty() { + return Err(SignError::new( + "receive-pack request contains no ref updates", + )); + } + + let workspace = TempDir::new().map_err(|error| SignError::new(error.to_string()))?; + let repo = workspace.path().join("objects.git"); + run_git_controlled( + None, + &["init", "--bare", repo_string(&repo)?], + None, + control, + )?; + if let Some(upstream_url) = self.upstream_override.as_deref().or(upstream_url) { + hydrate_upstream(&repo, upstream_url, &parsed.updates, control)?; + } + body.seek(SeekFrom::Start(parsed.pack_offset)) + .map_err(|error| SignError::new(error.to_string()))?; + run_git_file_input( + Some(&repo), + &["index-pack", "--stdin", "--fix-thin"], + body, + control, + )?; + + let mut commit_ids = HashSet::new(); + for update in &parsed.updates { + if update.new_oid == ZERO_SHA1 { + continue; + } + control.check()?; + let output = run_git_controlled( + Some(&repo), + &["rev-list", &update.new_oid, "--not", "--all"], + None, + control, + )?; + let output = String::from_utf8(output) + .map_err(|_| SignError::new("git returned a non-UTF-8 commit list"))?; + commit_ids.extend(output.lines().map(str::to_string)); + } + let mut rewriter = CommitRewriter { + repo: &repo, + signing_key: &self.signing_key, + commit_ids, + rewritten: HashMap::new(), + active: HashSet::new(), + signed_count: 0, + workspace: workspace.path(), + control, + }; + + for update in &mut parsed.updates { + if update.new_oid == ZERO_SHA1 { + continue; + } + if !update.ref_name.starts_with("refs/heads/") { + return Err(SignError::new( + "prototype supports direct branch updates only", + )); + } + if !rewriter.commit_ids.contains(&update.new_oid) { + return Err(SignError::new( + "branch tip commit is not self-contained in the push pack", + )); + } + update.new_oid = rewriter.rewrite(&update.new_oid)?; + } + + if rewriter.signed_count == 0 { + return Err(SignError::new( + "receive-pack request contains no commits to sign", + )); + } + + let base_oids = list_ref_oids(&repo, "refs/middleware", control)?; + let mut revisions = parsed + .updates + .iter() + .filter(|update| update.new_oid != ZERO_SHA1) + .map(|update| update.new_oid.clone()) + .collect::>(); + revisions.extend(base_oids.into_iter().map(|oid| format!("^{oid}"))); + let revision_input = revisions.join("\n") + "\n"; + let mut prefix = parsed.prefix; + for update in &parsed.updates { + prefix[update.new_oid_range.clone()].copy_from_slice(update.new_oid.as_bytes()); + } + let mut result = tempfile::tempfile().map_err(|error| SignError::new(error.to_string()))?; + result + .write_all(&prefix) + .map_err(|error| SignError::new(error.to_string()))?; + run_git_to_file( + Some(&repo), + &["pack-objects", "--stdout", "--revs", "--thin"], + Some(revision_input.as_bytes()), + &mut result, + control, + )?; + let body_len = result + .metadata() + .map_err(|error| SignError::new(error.to_string()))? + .len(); + result + .seek(SeekFrom::Start(0)) + .map_err(|error| SignError::new(error.to_string()))?; + Ok(SignedPush { + body: result, + body_len, + signed_commits: rewriter.signed_count, + }) + } +} + +struct ReceivePackRequest { + prefix: Vec, + pack_offset: u64, + updates: Vec, +} + +struct RefUpdate { + old_oid: String, + new_oid: String, + ref_name: String, + new_oid_range: std::ops::Range, +} + +impl ReceivePackRequest { + fn parse_file(body: &mut File) -> Result { + body.seek(SeekFrom::Start(0)) + .map_err(|error| SignError::new(error.to_string()))?; + let mut prefix = Vec::new(); + let mut updates = Vec::new(); + let mut offset = 0; + let mut command_section = true; + let pack_offset = loop { + let mut marker = [0u8; 4]; + body.read_exact(&mut marker) + .map_err(|_| SignError::new("receive-pack request has no packfile"))?; + if !command_section && marker == *b"PACK" { + body.seek(SeekFrom::Current(-4)) + .map_err(|error| SignError::new(error.to_string()))?; + break offset as u64; + } + let length = parse_pkt_length(&marker)?; + prefix.extend_from_slice(&marker); + if prefix.len() > MAX_RECEIVE_PACK_PREFIX_BYTES { + return Err(SignError::new("receive-pack command prefix is too large")); + } + if length == 0 { + offset += 4; + command_section = false; + continue; + } + if length < 4 { + return Err(SignError::new("invalid receive-pack pkt-line length")); + } + let payload_start = offset + 4; + let mut payload = vec![0; length - 4]; + body.read_exact(&mut payload) + .map_err(|_| SignError::new("truncated receive-pack pkt-line"))?; + prefix.extend_from_slice(&payload); + if prefix.len() > MAX_RECEIVE_PACK_PREFIX_BYTES { + return Err(SignError::new("receive-pack command prefix is too large")); + } + if !command_section { + // Push options, when negotiated, are pkt-lines between the + // command flush and the packfile. Preserve them unchanged. + offset += length; + continue; + } + let command = payload.split(|byte| *byte == 0).next().unwrap_or(&payload); + let command = command.strip_suffix(b"\n").unwrap_or(command); + let first_space = command + .iter() + .position(|byte| *byte == b' ') + .ok_or_else(|| SignError::new("invalid receive-pack ref command"))?; + let second_space = command[first_space + 1..] + .iter() + .position(|byte| *byte == b' ') + .map(|index| first_space + 1 + index) + .ok_or_else(|| SignError::new("invalid receive-pack ref command"))?; + if first_space != SHA1_HEX_LEN || second_space - first_space - 1 != SHA1_HEX_LEN { + return Err(SignError::new( + "prototype supports SHA-1 Git repositories only", + )); + } + let new_start = payload_start + first_space + 1; + let old_oid = ascii_oid(&prefix[payload_start..payload_start + SHA1_HEX_LEN])?; + let new_oid = ascii_oid(&prefix[new_start..new_start + SHA1_HEX_LEN])?; + let ref_name = std::str::from_utf8(&command[second_space + 1..]) + .map_err(|_| SignError::new("receive-pack ref name is not UTF-8"))? + .to_string(); + updates.push(RefUpdate { + old_oid, + new_oid, + ref_name, + new_oid_range: new_start..new_start + SHA1_HEX_LEN, + }); + offset += length; + }; + + Ok(Self { + prefix, + pack_offset, + updates, + }) + } +} + +fn hydrate_upstream( + repo: &Path, + upstream_url: &str, + updates: &[RefUpdate], + control: &SignControl, +) -> Result<(), SignError> { + run_git_controlled( + Some(repo), + &[ + "-c", + "credential.interactive=false", + "fetch", + "--no-tags", + "--depth=1", + upstream_url, + "+HEAD:refs/middleware/upstream-head", + ], + None, + control, + )?; + for (index, old_oid) in updates + .iter() + .map(|update| update.old_oid.as_str()) + .filter(|oid| *oid != ZERO_SHA1) + .collect::>() + .into_iter() + .enumerate() + { + let destination = format!("+{old_oid}:refs/middleware/base-{index}"); + run_git_controlled( + Some(repo), + &[ + "-c", + "credential.interactive=false", + "fetch", + "--no-tags", + "--depth=1", + upstream_url, + &destination, + ], + None, + control, + )?; + } + Ok(()) +} + +fn list_ref_oids( + repo: &Path, + prefix: &str, + control: &SignControl, +) -> Result, SignError> { + let output = run_git_controlled( + Some(repo), + &["for-each-ref", "--format=%(objectname)", prefix], + None, + control, + )?; + let output = String::from_utf8(output) + .map_err(|_| SignError::new("git returned a non-UTF-8 ref list"))?; + Ok(output.lines().map(str::to_string).collect()) +} + +fn parse_pkt_length(bytes: &[u8]) -> Result { + let text = + std::str::from_utf8(bytes).map_err(|_| SignError::new("pkt-line length is not ASCII"))?; + usize::from_str_radix(text, 16).map_err(|_| SignError::new("invalid pkt-line length")) +} + +fn ascii_oid(bytes: &[u8]) -> Result { + if bytes.len() != SHA1_HEX_LEN || !bytes.iter().all(u8::is_ascii_hexdigit) { + return Err(SignError::new("invalid SHA-1 object id")); + } + String::from_utf8(bytes.to_vec()).map_err(|_| SignError::new("invalid SHA-1 object id")) +} + +struct CommitRewriter<'a> { + repo: &'a Path, + signing_key: &'a Path, + commit_ids: HashSet, + rewritten: HashMap, + active: HashSet, + signed_count: u32, + workspace: &'a Path, + control: &'a SignControl, +} + +impl CommitRewriter<'_> { + fn rewrite(&mut self, oid: &str) -> Result { + self.control.check()?; + if let Some(rewritten) = self.rewritten.get(oid) { + return Ok(rewritten.clone()); + } + if !self.commit_ids.contains(oid) { + return Ok(oid.to_string()); + } + if !self.active.insert(oid.to_string()) { + return Err(SignError::new("commit graph contains a cycle")); + } + + let raw = run_git_controlled( + Some(self.repo), + &["cat-file", "commit", oid], + None, + self.control, + )?; + let parsed = ParsedCommit::parse(&raw)?; + let mut parents = Vec::with_capacity(parsed.parents.len()); + for parent in &parsed.parents { + parents.push(self.rewrite(parent)?); + } + let unsigned = parsed.unsigned_with_parents(&parents); + let signature = self.sign_payload(oid, &unsigned)?; + let signed = insert_signature(&unsigned, &signature)?; + let new_oid = String::from_utf8(run_git_controlled( + Some(self.repo), + &["hash-object", "-t", "commit", "-w", "--stdin"], + Some(&signed), + self.control, + )?) + .map_err(|_| SignError::new("git returned a non-UTF-8 object id"))? + .trim() + .to_string(); + + self.active.remove(oid); + self.rewritten.insert(oid.to_string(), new_oid.clone()); + self.signed_count = self.signed_count.saturating_add(1); + Ok(new_oid) + } + + fn sign_payload(&self, oid: &str, payload: &[u8]) -> Result, SignError> { + let payload_path = self.workspace.join(format!("commit-{oid}")); + fs::write(&payload_path, payload).map_err(|error| SignError::new(error.to_string()))?; + let mut command = Command::new("ssh-keygen"); + command + .args(["-Y", "sign", "-n", "git", "-f"]) + .arg(self.signing_key) + .arg(&payload_path) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::piped()); + run_command_controlled(command, None, "ssh-keygen", self.control)?; + // ssh-keygen appends `.sig` to the input path. + let signature_path = PathBuf::from(format!("{}.sig", payload_path.display())); + fs::read(signature_path).map_err(|error| SignError::new(error.to_string())) + } +} + +struct ParsedCommit { + headers: Vec
, + parents: Vec, + message: Vec, +} + +struct Header { + name: Vec, + block: Vec, +} + +impl ParsedCommit { + fn parse(raw: &[u8]) -> Result { + let separator = raw + .windows(2) + .position(|window| window == b"\n\n") + .ok_or_else(|| SignError::new("commit object has no header separator"))?; + let header_bytes = &raw[..separator]; + let mut headers: Vec
= Vec::new(); + for line in header_bytes.split(|byte| *byte == b'\n') { + if line.starts_with(b" ") { + let previous = headers + .last_mut() + .ok_or_else(|| SignError::new("commit starts with a continuation header"))?; + previous.block.push(b'\n'); + previous.block.extend_from_slice(line); + continue; + } + let name_end = line + .iter() + .position(|byte| *byte == b' ') + .ok_or_else(|| SignError::new("invalid commit header"))?; + headers.push(Header { + name: line[..name_end].to_vec(), + block: line.to_vec(), + }); + } + let parents = headers + .iter() + .filter(|header| header.name == b"parent") + .map(|header| ascii_oid(&header.block[b"parent ".len()..])) + .collect::, _>>()?; + if !headers.iter().any(|header| header.name == b"tree") { + return Err(SignError::new("commit object has no tree")); + } + Ok(Self { + headers, + parents, + message: raw[separator + 2..].to_vec(), + }) + } + + fn unsigned_with_parents(&self, parents: &[String]) -> Vec { + let mut result = Vec::new(); + let mut parent_index = 0; + for header in &self.headers { + if header.name == b"gpgsig" || header.name == b"gpgsig-sha256" { + continue; + } + if header.name == b"parent" { + result.extend_from_slice(b"parent "); + result.extend_from_slice(parents[parent_index].as_bytes()); + parent_index += 1; + } else { + result.extend_from_slice(&header.block); + } + result.push(b'\n'); + } + result.push(b'\n'); + result.extend_from_slice(&self.message); + result + } +} + +fn insert_signature(unsigned: &[u8], signature: &[u8]) -> Result, SignError> { + let separator = unsigned + .windows(2) + .position(|window| window == b"\n\n") + .ok_or_else(|| SignError::new("unsigned commit has no header separator"))?; + let signature = signature.strip_suffix(b"\n").unwrap_or(signature); + let mut result = Vec::with_capacity(unsigned.len() + signature.len() + 16); + result.extend_from_slice(&unsigned[..separator + 1]); + for (index, line) in signature.split(|byte| *byte == b'\n').enumerate() { + result.extend_from_slice(if index == 0 { b"gpgsig " } else { b" " }); + result.extend_from_slice(line); + result.push(b'\n'); + } + result.extend_from_slice(&unsigned[separator + 1..]); + Ok(result) +} + +fn repo_string(path: &Path) -> Result<&str, SignError> { + path.to_str() + .ok_or_else(|| SignError::new("temporary repository path is not UTF-8")) +} + +fn run_git_controlled( + repo: Option<&Path>, + args: &[&str], + input: Option<&[u8]>, + control: &SignControl, +) -> Result, SignError> { + let mut command = Command::new("git"); + if let Some(repo) = repo { + command.arg("-C").arg(repo); + } + command + .args(args) + .stdin(if input.is_some() { + Stdio::piped() + } else { + Stdio::null() + }) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + run_command_controlled( + command, + input, + args.first().copied().unwrap_or("command"), + control, + ) +} + +fn run_git_file_input( + repo: Option<&Path>, + args: &[&str], + input: File, + control: &SignControl, +) -> Result, SignError> { + let mut command = Command::new("git"); + if let Some(repo) = repo { + command.arg("-C").arg(repo); + } + command + .args(args) + .stdin(Stdio::from(input)) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + run_command_controlled( + command, + None, + args.first().copied().unwrap_or("command"), + control, + ) +} + +fn run_git_to_file( + repo: Option<&Path>, + args: &[&str], + input: Option<&[u8]>, + output: &mut File, + control: &SignControl, +) -> Result<(), SignError> { + output + .seek(SeekFrom::End(0)) + .map_err(|error| SignError::new(error.to_string()))?; + let output_handle = output + .try_clone() + .map_err(|error| SignError::new(error.to_string()))?; + let mut command = Command::new("git"); + if let Some(repo) = repo { + command.arg("-C").arg(repo); + } + command + .args(args) + .stdin(if input.is_some() { + Stdio::piped() + } else { + Stdio::null() + }) + .stdout(Stdio::from(output_handle)) + .stderr(Stdio::piped()); + run_command_controlled( + command, + input, + args.first().copied().unwrap_or("command"), + control, + )?; + Ok(()) +} + +fn run_command_controlled( + mut command: Command, + input: Option<&[u8]>, + command_name: &str, + control: &SignControl, +) -> Result, SignError> { + control.check()?; + let mut child = command + .spawn() + .map_err(|error| SignError::new(format!("could not run {command_name}: {error}")))?; + let stdout = child.stdout.take().map(spawn_pipe_drain); + let stderr = child.stderr.take().map(spawn_pipe_drain); + if let Some(input) = input { + let write_result = child + .stdin + .take() + .ok_or_else(|| SignError::new(format!("{command_name} stdin was unavailable")))? + .write_all(input); + if let Err(error) = write_result { + let _ = child.kill(); + let _ = child.wait(); + return Err(SignError::new(error.to_string())); + } + } + drop(child.stdin.take()); + + let status = loop { + if let Err(error) = control.check() { + let _ = child.kill(); + let _ = child.wait(); + join_pipe(stdout)?; + join_pipe(stderr)?; + return Err(error); + } + match child + .try_wait() + .map_err(|error| SignError::new(error.to_string()))? + { + Some(status) => break status, + None => std::thread::sleep(CHILD_POLL_INTERVAL), + } + }; + let stdout = join_pipe(stdout)?; + let stderr = join_pipe(stderr)?; + if !status.success() { + let stderr = String::from_utf8_lossy(&stderr); + return Err(SignError::new(format!( + "{command_name} failed: {}", + stderr.trim() + ))); + } + Ok(stdout) +} + +type PipeDrain = std::thread::JoinHandle>>; + +fn spawn_pipe_drain(mut pipe: R) -> PipeDrain +where + R: Read + Send + 'static, +{ + std::thread::spawn(move || { + let mut captured = Vec::new(); + let mut buffer = [0u8; 8192]; + loop { + let read = pipe.read(&mut buffer)?; + if read == 0 { + return Ok(captured); + } + if captured.len() < MAX_CAPTURE_BYTES { + let keep = read.min(MAX_CAPTURE_BYTES - captured.len()); + captured.extend_from_slice(&buffer[..keep]); + } + } + }) +} + +fn join_pipe(drain: Option) -> Result, SignError> { + drain.map_or_else( + || Ok(Vec::new()), + |drain| { + drain + .join() + .map_err(|_| SignError::new("subprocess output worker failed"))? + .map_err(|error| SignError::new(error.to_string())) + }, + ) +} + +#[cfg(test)] +fn run_git(repo: Option<&Path>, args: &[&str], input: Option<&[u8]>) -> Result, SignError> { + let control = SignControl::new( + Arc::new(AtomicBool::new(false)), + Instant::now() + Duration::from_secs(300), + ); + run_git_controlled(repo, args, input, &control) +} + +#[cfg(test)] +mod tests { + use super::*; + use openshell_core::proto::{ + HttpHeader, HttpRequestBodyUnit, HttpRequestEvent, HttpRequestPreflight, HttpRequestTarget, + HttpRequestTrailers, MiddlewareSessionEnd, RequestContext, http_request_body_result, + http_request_body_unit, http_request_event, http_request_event_result, + }; + use tokio_stream::StreamExt as _; + + #[cfg(unix)] + #[test] + fn cancellation_terminates_an_active_subprocess() { + let cancelled = Arc::new(AtomicBool::new(false)); + let control = SignControl::new( + Arc::clone(&cancelled), + Instant::now() + Duration::from_secs(30), + ); + let started = Instant::now(); + let worker = std::thread::spawn(move || { + let mut command = Command::new("sleep"); + command + .arg("30") + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + run_command_controlled(command, None, "sleep", &control) + }); + + std::thread::sleep(Duration::from_millis(50)); + cancelled.store(true, Ordering::Release); + let error = worker + .join() + .expect("subprocess worker must not panic") + .expect_err("cancellation must terminate the subprocess"); + + assert!(error.is_cancelled()); + assert!(started.elapsed() < Duration::from_secs(2)); + } + + #[tokio::test] + async fn signs_every_commit_and_rewrites_the_branch_tip() { + let fixture = TempDir::new().unwrap(); + let source = fixture.path().join("source.git"); + run_git( + None, + &["init", "--bare", repo_string(&source).unwrap()], + None, + ) + .unwrap(); + let large_blob = pseudo_random_bytes(5 * 1024 * 1024); + let blob = object(&source, "blob", &large_blob); + let tree_line = format!("100644 blob {blob}\tREADME.md\n"); + let tree = String::from_utf8( + run_git(Some(&source), &["mktree"], Some(tree_line.as_bytes())).unwrap(), + ) + .unwrap() + .trim() + .to_string(); + let first = unsigned_commit(&source, &tree, None, "first"); + let second = unsigned_commit(&source, &tree, Some(&first), "second"); + let upstream_head = unsigned_commit(&source, &tree, None, "upstream"); + run_git( + Some(&source), + &["update-ref", "refs/heads/main", &upstream_head], + None, + ) + .unwrap(); + run_git( + Some(&source), + &["symbolic-ref", "HEAD", "refs/heads/main"], + None, + ) + .unwrap(); + let objects = format!("{blob}\n{tree}\n{first}\n{second}\n"); + let pack = run_git( + Some(&source), + &["pack-objects", "--stdout"], + Some(objects.as_bytes()), + ) + .unwrap(); + let body = receive_pack_body(&second, &pack); + assert!(body.len() > 4 * 1024 * 1024); + + let key = fixture.path().join("signing-key"); + let status = Command::new("ssh-keygen") + .args(["-q", "-t", "ed25519", "-N", "", "-f"]) + .arg(&key) + .status() + .unwrap(); + assert!(status.success()); + let signed = sign_through_owned_stream(&key, &source, &body).await; + assert_eq!(signed.signed_commits, 2); + + let mut signed_file = file_with_bytes(&signed.body); + let parsed = ReceivePackRequest::parse_file(&mut signed_file).unwrap(); + let new_tip = parsed.updates[0].new_oid.clone(); + assert_ne!(new_tip, second); + let verify = fixture.path().join("verify.git"); + run_git( + None, + &["init", "--bare", repo_string(&verify).unwrap()], + None, + ) + .unwrap(); + run_git( + None, + &[ + "receive-pack", + "--stateless-rpc", + repo_string(&verify).unwrap(), + ], + Some(&signed.body), + ) + .unwrap(); + let received_tip = String::from_utf8( + run_git(Some(&verify), &["rev-parse", "refs/heads/main"], None).unwrap(), + ) + .unwrap(); + assert_eq!(received_tip.trim(), new_tip); + let tip = run_git(Some(&verify), &["cat-file", "commit", &new_tip], None).unwrap(); + assert!(tip.windows(7).any(|window| window == b"gpgsig ")); + let tip = ParsedCommit::parse(&tip).unwrap(); + assert_eq!(tip.parents.len(), 1); + assert_ne!(tip.parents[0], first); + let parent = run_git( + Some(&verify), + &["cat-file", "commit", &tip.parents[0]], + None, + ) + .unwrap(); + assert!(parent.windows(7).any(|window| window == b"gpgsig ")); + + let allowed = fixture.path().join("allowed-signers"); + let public_key = fs::read_to_string(key.with_extension("pub")).unwrap(); + fs::write(&allowed, format!("agent@example.com {}", public_key.trim())).unwrap(); + verify_commit(&verify, &allowed, &new_tip); + verify_commit(&verify, &allowed, &tip.parents[0]); + } + + struct CollectedSignedPush { + body: Vec, + signed_commits: u32, + } + + async fn sign_through_owned_stream( + key: &Path, + upstream: &Path, + body: &[u8], + ) -> CollectedSignedPush { + let signer = GitSigner::new_with_upstream_override( + key.to_path_buf(), + repo_string(upstream).unwrap().to_string(), + ) + .unwrap(); + let mut middleware = crate::GitSigningMiddleware::new_for_test(key.to_path_buf()).unwrap(); + middleware.signer = std::sync::Arc::new(signer); + let mut events = vec![Ok(HttpRequestEvent { + event: Some(http_request_event::Event::Preflight(HttpRequestPreflight { + context: Some(RequestContext { + request_id: "large-push".into(), + ..Default::default() + }), + target: Some(HttpRequestTarget { + scheme: "https".into(), + host: "github.com".into(), + port: 443, + method: "POST".into(), + path: "/NVIDIA/OpenShell.git/git-receive-pack".into(), + ..Default::default() + }), + headers: vec![HttpHeader { + name: "content-type".into(), + value: "application/x-git-receive-pack-request".into(), + }], + permitted_body_modes: vec![ + openshell_core::proto::HttpRequestBodyMode::OwnedStreamBytes as i32, + ], + max_payload_bytes: crate::MAX_UNIT_BYTES as u64, + max_deferred_bytes: 16 * 1024 * 1024, + ..Default::default() + })), + })]; + let mut final_input_sequence = 1u64; + for (index, chunk) in body.chunks(crate::MAX_UNIT_BYTES).enumerate() { + final_input_sequence = index as u64 + 1; + events.push(Ok(HttpRequestEvent { + event: Some(http_request_event::Event::Body(HttpRequestBodyUnit { + sequence: final_input_sequence, + payload: Some(http_request_body_unit::Payload::Data(chunk.to_vec())), + end_of_stream: false, + })), + })); + } + final_input_sequence += 1; + events.push(Ok(HttpRequestEvent { + event: Some(http_request_event::Event::Body(HttpRequestBodyUnit { + sequence: final_input_sequence, + payload: Some(http_request_body_unit::Payload::Data(Vec::new())), + end_of_stream: true, + })), + })); + events.push(Ok(HttpRequestEvent { + event: Some(http_request_event::Event::Trailers( + HttpRequestTrailers::default(), + )), + })); + events.push(Ok(HttpRequestEvent { + event: Some(http_request_event::Event::SessionEnd( + MiddlewareSessionEnd::default(), + )), + })); + + let mut results = middleware.request_stream(tokio_stream::iter(events)); + assert!(matches!( + results.next().await.unwrap().unwrap().result, + Some(http_request_event_result::Result::PreflightResult(_)) + )); + for sequence in 1..=final_input_sequence { + let result = results.next().await.unwrap().unwrap(); + let Some(http_request_event_result::Result::BodyResult(result)) = result.result else { + panic!("expected body ownership result"); + }; + assert_eq!(result.sequence, sequence); + assert!(matches!( + result.action, + Some(http_request_body_result::Action::TakeOwnership(_)) + )); + } + + let mut output = Vec::new(); + let mut next_output_sequence = 1u64; + let signed_commits = loop { + let result = results.next().await.unwrap().unwrap(); + match result.result { + Some(http_request_event_result::Result::BodyOutput(unit)) => { + assert_eq!(unit.sequence, next_output_sequence); + next_output_sequence += 1; + output.extend_from_slice(&unit.data); + } + Some(http_request_event_result::Result::BodyFinalize(finalize)) => { + assert_eq!(finalize.through_input_sequence, final_input_sequence); + assert_eq!(finalize.through_output_sequence, next_output_sequence - 1); + break finalize.findings[0].count; + } + other => panic!("unexpected owned output result: {other:?}"), + } + }; + assert!(matches!( + results.next().await.unwrap().unwrap().result, + Some(http_request_event_result::Result::TrailersResult(_)) + )); + assert!(results.next().await.is_none()); + CollectedSignedPush { + body: output, + signed_commits, + } + } + + #[test] + fn rejects_non_branch_updates() { + let payload = format!("{ZERO_SHA1} {ZERO_SHA1} refs/tags/v1\n"); + let length = payload.len() + 4; + let mut body = format!("{length:04x}{payload}0000").into_bytes(); + body.extend_from_slice(b"PACK"); + let mut file = file_with_bytes(&body); + let parsed = ReceivePackRequest::parse_file(&mut file).unwrap(); + assert_eq!(parsed.updates[0].ref_name, "refs/tags/v1"); + } + + #[test] + fn resolves_a_thin_pack_from_the_upstream_repository() { + let fixture = TempDir::new().unwrap(); + let source = fixture.path().join("source.git"); + run_git( + None, + &["init", "--bare", repo_string(&source).unwrap()], + None, + ) + .unwrap(); + let blob = object(&source, "blob", b"base\n"); + let tree_line = format!("100644 blob {blob}\tREADME.md\n"); + let tree = String::from_utf8( + run_git(Some(&source), &["mktree"], Some(tree_line.as_bytes())).unwrap(), + ) + .unwrap() + .trim() + .to_string(); + let base = unsigned_commit(&source, &tree, None, "base"); + run_git( + Some(&source), + &["update-ref", "refs/heads/main", &base], + None, + ) + .unwrap(); + run_git( + Some(&source), + &["symbolic-ref", "HEAD", "refs/heads/main"], + None, + ) + .unwrap(); + let tip = unsigned_commit(&source, &tree, Some(&base), "tip"); + let revisions = format!("{tip}\n^{base}\n"); + let pack = run_git( + Some(&source), + &["pack-objects", "--stdout", "--revs", "--thin"], + Some(revisions.as_bytes()), + ) + .unwrap(); + let body = receive_pack_body(&tip, &pack); + + let key = fixture.path().join("signing-key"); + assert!( + Command::new("ssh-keygen") + .args(["-q", "-t", "ed25519", "-N", "", "-f"]) + .arg(&key) + .status() + .unwrap() + .success() + ); + let signed = GitSigner::new(key) + .unwrap() + .sign_receive_pack(file_with_bytes(&body), source.to_str(), &test_control()) + .unwrap(); + assert_eq!(signed.signed_commits, 1); + } + + fn file_with_bytes(bytes: &[u8]) -> File { + let mut file = tempfile::tempfile().unwrap(); + file.write_all(bytes).unwrap(); + file.seek(SeekFrom::Start(0)).unwrap(); + file + } + + fn test_control() -> SignControl { + SignControl::new( + Arc::new(AtomicBool::new(false)), + Instant::now() + Duration::from_secs(300), + ) + } + + fn object(repo: &Path, kind: &str, body: &[u8]) -> String { + String::from_utf8( + run_git( + Some(repo), + &["hash-object", "-t", kind, "-w", "--stdin"], + Some(body), + ) + .unwrap(), + ) + .unwrap() + .trim() + .to_string() + } + + fn unsigned_commit(repo: &Path, tree: &str, parent: Option<&str>, subject: &str) -> String { + let parent = parent.map_or(String::new(), |oid| format!("parent {oid}\n")); + let raw = format!( + "tree {tree}\n{parent}author Agent 1700000000 +0000\ncommitter Agent 1700000000 +0000\n\n{subject}\n" + ); + object(repo, "commit", raw.as_bytes()) + } + + fn receive_pack_body(new_oid: &str, pack: &[u8]) -> Vec { + let payload = format!( + "{ZERO_SHA1} {new_oid} refs/heads/main\0 report-status side-band-64k object-format=sha1\n" + ); + let length = payload.len() + 4; + let mut body = format!("{length:04x}{payload}0000").into_bytes(); + body.extend_from_slice(pack); + body + } + + fn pseudo_random_bytes(len: usize) -> Vec { + let mut state = 0x4d59_5df4_d0f3_3173_u64; + (0..len) + .map(|_| { + state ^= state << 13; + state ^= state >> 7; + state ^= state << 17; + state as u8 + }) + .collect() + } + + fn verify_commit(repo: &Path, allowed: &Path, oid: &str) { + let output = Command::new("git") + .arg("-C") + .arg(repo) + .args(["-c", "gpg.format=ssh", "-c"]) + .arg(format!("gpg.ssh.allowedSignersFile={}", allowed.display())) + .args(["verify-commit", oid]) + .output() + .unwrap(); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + } +} diff --git a/proto/supervisor_middleware.proto b/proto/supervisor_middleware.proto index 81e1c72f8..76059153f 100644 --- a/proto/supervisor_middleware.proto +++ b/proto/supervisor_middleware.proto @@ -19,10 +19,6 @@ service SupervisorMiddleware { // ValidateConfig checks service-specific configuration for one binding. rpc ValidateConfig(ValidateConfigRequest) returns (ValidateConfigResponse); - // EvaluateHttpRequest returns an allow, deny, or mutation decision for one - // buffered HTTP request. - rpc EvaluateHttpRequest(HttpRequestEvaluation) returns (HttpRequestResult); - // EvaluateWebSocketSession opens one ordered, phase-specific stream for a // single middleware stage and WebSocket upgrade attempt. The current // implementation supports client-to-upstream text messages at @@ -34,6 +30,17 @@ service SupervisorMiddleware { returns (stream WebSocketSessionEventResult); } +// HttpRequestPreCredentials begins after network-policy admission and before +// OpenShell injects credentials. A STREAM_BYTES stage may remain active while +// OpenShell forwards already-approved units upstream. +service HttpRequestPreCredentials { + // Evaluate starts with preflight and may continue with selected body units + // and trailers. A body unit marked end_of_stream ends body input, not the + // event stream. Trailers and one best-effort session_end may follow. + rpc Evaluate(stream HttpRequestEvent) + returns (stream HttpRequestEventResult); +} + // HttpResponsePreReturn evaluates one response for one middleware stage before // OpenShell returns it to the sandbox. service HttpResponsePreReturn { @@ -96,30 +103,6 @@ message ValidateConfigResponse { string reason = 2; } -// HttpRequestEvaluation contains one buffered HTTP request to evaluate. -message HttpRequestEvaluation { - // Evaluation phase selected for this request. - SupervisorMiddlewarePhase phase = 1; - // Sandbox and request identity available to the supervisor. - // The encoded context is limited to 4 KiB. - RequestContext context = 2; - // Validated service-specific policy configuration. - // The encoded configuration is limited to 64 KiB. - google.protobuf.Struct config = 3; - // Destination and HTTP request target. - // The encoded target is limited to 32 KiB. - HttpRequestTarget target = 4; - // HTTP request headers before OpenShell injects credentials, in wire - // order. Repeated header names are preserved as separate entries. Protected - // credential, routing, framing, and hop-by-hop headers are omitted. - // At most 128 lines and 64 KiB of encoded headers are included. - repeated HttpHeader headers = 5; - // Buffered request body, limited to 4 MiB. Empty for a bodyless request. - bytes body = 6; - // Built-in middleware name or operator-owned registration name. - string middleware_name = 7; -} - // HttpHeader is one HTTP header line. message HttpHeader { // Lowercased header name. @@ -128,6 +111,192 @@ message HttpHeader { string value = 2; } +// One ordered request event. A stream starts with preflight, may continue with +// body units ending in end_of_stream, may then include trailers, and may end +// with one best-effort session_end. +message HttpRequestEvent { + oneof event { + HttpRequestPreflight preflight = 1; + HttpRequestBodyUnit body = 2; + MiddlewareSessionEnd session_end = 3; + HttpRequestTrailers trailers = 4; + } +} + +// Each preflight, body, and trailers event requires one ordered result. +// session_end has no result. An owned stage may additionally produce output +// units and one finalization after accepting its final body unit. +message HttpRequestEventResult { + oneof result { + HttpRequestPreflightResult preflight_result = 1; + HttpRequestBodyResult body_result = 2; + HttpRequestTrailersResult trailers_result = 3; + HttpRequestBodyOutput body_output = 4; + HttpRequestBodyFinalize body_finalize = 5; + } +} + +// HttpRequestPreflight exposes the current admitted request head to one stage. +message HttpRequestPreflight { + // Request identity. Limited to 4 KiB encoded. + RequestContext context = 1; + // Admitted destination and request target. Limited to 32 KiB encoded. + HttpRequestTarget target = 2; + // Current headers after earlier stages and before credential injection, in + // wire order. Protected fields are omitted. Limited to 128 lines and 64 KiB. + repeated HttpHeader headers = 3; + // Built-in middleware name or operator-owned registration name. + string middleware_name = 4; + // Validated service configuration. Limited to 64 KiB encoded. + google.protobuf.Struct config = 5; + // Effective per-unit or whole-body input/replacement limit. + uint64 max_payload_bytes = 6; + // Modes OpenShell permits for this request and binding. HEADERS_ONLY is + // always present. OWNED_STREAM_BYTES is present only for fail-closed stages + // with a non-zero deferred-byte limit. + repeated HttpRequestBodyMode permitted_body_modes = 7; + // Effective per-representation input and output limit for + // OWNED_STREAM_BYTES. Input and output are each bounded by this value. + uint64 max_deferred_bytes = 8; + // Present when the normalized body length is known before inspection. + optional uint64 declared_body_length = 9; +} + +// Selects skip, inspect, or block. Invalid diagnostics make the complete +// result a middleware failure handled according to on_error. +message HttpRequestPreflightResult { + oneof action { + HttpRequestPreflightSkip skip = 1; + HttpRequestPreflightInspect inspect = 2; + HttpRequestBlock block_request = 7; + } + // Service diagnostic, never sent to the sandbox or security logs. + string reason = 3; + // Optional stable audit code, returned only for an accepted block. + string reason_code = 4; + repeated Finding findings = 5; + map metadata = 6; +} + +message HttpRequestPreflightSkip {} + +// Selects body inspection and request-header mutations. +message HttpRequestPreflightInspect { + HttpRequestBodyMode body_mode = 1; + repeated HeaderMutation header_mutations = 2; +} + +// Authoritatively rejects the request. A preflight rejection occurs before +// upstream contact; a streaming body rejection may terminate a partial upload. +message HttpRequestBlock {} + +// Controls which request body units a stage receives. +enum HttpRequestBodyMode { + HTTP_REQUEST_BODY_MODE_UNSPECIFIED = 0; + // Inspect only the request head. + HTTP_REQUEST_BODY_MODE_HEADERS_ONLY = 1; + // Receive the complete normalized body as one final unit. Input and + // replacement must fit max_payload_bytes. + HTTP_REQUEST_BODY_MODE_WHOLE_BODY_BYTES = 2; + // Receive normalized units in lockstep. Each result fully accounts for its + // input; the stage cannot retain bytes across units. + HTTP_REQUEST_BODY_MODE_STREAM_BYTES = 3; + // Take ownership of normalized input units without requiring OpenShell to + // retain a replay copy. The stage must accept every unit with take_ownership, + // then emit bounded output units and one body_finalize after the final input. + // Failure is always fail closed because the original bytes are no longer + // available for replay. + HTTP_REQUEST_BODY_MODE_OWNED_STREAM_BYTES = 4; +} + +// One normalized body unit. Boundaries have no transport or application +// meaning. +message HttpRequestBodyUnit { + // Contiguous and stage-local, starting at 1. + uint64 sequence = 1; + oneof payload { + bytes data = 2; + } + // Marks the final input unit. WHOLE_BODY_BYTES receives the complete body in + // this unit. STREAM_BYTES and OWNED_STREAM_BYTES receive nonempty data units + // with this unset, followed by one empty final unit. A body-capable request + // with no bytes also receives one empty final unit. Trailers and session_end + // may follow. + bool end_of_stream = 3; +} + +// Result for one body unit. +message HttpRequestBodyResult { + uint64 sequence = 1; + oneof action { + HttpRequestBodyPassThrough pass_through = 2; + HttpRequestBodyTransform transform = 3; + HttpRequestBlock block_request = 8; + HttpRequestBodySkipRemaining skip_remaining = 9; + HttpRequestBodyTakeOwnership take_ownership = 10; + } + string reason = 4; + string reason_code = 5; + repeated Finding findings = 6; + map metadata = 7; +} + +message HttpRequestBodyPassThrough {} + +message HttpRequestBodyTransform { + oneof replacement { + bytes data = 1; + } +} + +// Finalizes the current unit and ends inspection for this stage. +message HttpRequestBodySkipRemaining { + oneof current { + HttpRequestBodyPassThrough pass_through = 1; + HttpRequestBodyTransform transform = 2; + } +} + +// Acknowledges that an owned stage durably accepted the complete input unit. +message HttpRequestBodyTakeOwnership {} + +// One owned-stage output unit. Output sequence is contiguous from 1 and each +// unit fits max_payload_bytes. The complete output fits max_deferred_bytes. +message HttpRequestBodyOutput { + uint64 sequence = 1; + bytes data = 2; +} + +// Completes owned output and proves how much input and output it accounts for. +message HttpRequestBodyFinalize { + // Must equal the final accepted input sequence. + uint64 through_input_sequence = 1; + // Must equal the final output sequence, or zero for empty output. + uint64 through_output_sequence = 2; + // Final owned-stage diagnostic, never sent to the sandbox or security logs. + string reason = 3; + // Optional stable audit code for the completed transformation. + string reason_code = 4; + repeated Finding findings = 5; + map metadata = 6; +} + +// Current normalized request trailers in wire order. An inspecting body stage +// receives exactly one trailers event, including when the set is empty. +message HttpRequestTrailers { + repeated HttpHeader headers = 1; +} + +// Applies ordered mutations to existing request trailers. V1 cannot create a +// trailer name that was not announced by the sender. +message HttpRequestTrailersResult { + repeated HeaderMutation trailer_mutations = 1; + string reason = 2; + string reason_code = 3; + repeated Finding findings = 4; + map metadata = 5; +} + // One ordered response event. A stream starts with preflight, may continue with // body units ending in end_of_stream, may then include trailers, and may end // with one best-effort session_end. @@ -209,7 +378,7 @@ message HttpResponsePreflightResult { // Service diagnostic, never sent to the sandbox or security logs. Maximum // 4 KiB. string reason = 3; - // Optional audit code using the HttpRequestResult.reason_code format and + // Optional audit code using the HttpRequestPreflightResult.reason_code format and // 64-byte maximum. Returned to the sandbox only for block_delivery. string reason_code = 4; // Up to 32 audit-safe findings, each limited to 4 KiB encoded. @@ -318,7 +487,7 @@ message HttpResponseBodyResult { // Service diagnostic, never sent to the sandbox or security logs. Maximum // 4 KiB. string reason = 4; - // Optional audit code using the HttpRequestResult.reason_code format and + // Optional audit code using the HttpRequestPreflightResult.reason_code format and // 64-byte maximum. When OpenShell accepts block_delivery before response // commitment, it includes this code in the canonical denial response. It is // never returned after commitment. @@ -380,7 +549,7 @@ message HttpResponseTrailersResult { // Service diagnostic, never sent to the sandbox or security logs. Maximum // 4 KiB. string reason = 2; - // Optional audit code using the HttpRequestResult.reason_code format and + // Optional audit code using the HttpRequestPreflightResult.reason_code format and // 64-byte maximum. Never sent to the sandbox. string reason_code = 3; // Up to 32 audit-safe findings, each limited to 4 KiB encoded. @@ -456,6 +625,9 @@ enum SupervisorMiddlewarePhase { SUPERVISOR_MIDDLEWARE_PHASE_UNSPECIFIED = 0; SUPERVISOR_MIDDLEWARE_PHASE_PRE_CREDENTIALS = 1; SUPERVISOR_MIDDLEWARE_PHASE_PRE_RETURN = 2; + // Restricted to trusted in-process middleware. External services cannot + // advertise this phase because credentials must not cross the extension API. + SUPERVISOR_MIDDLEWARE_PHASE_POST_CREDENTIALS = 3; } // WebSocketSessionEvent is one ordered event in a stage-local stream. @@ -530,7 +702,7 @@ message WebSocketPreflightDecision { // Optional stable machine-readable code for a deny decision. Because // preflight runs before the HTTP upgrade completes, OpenShell may return // this code to the requester. Codes follow the same format and 64-byte - // maximum as HttpRequestResult.reason_code. + // maximum as HttpRequestPreflightResult.reason_code. string reason_code = 3; // Audit-safe findings produced during preflight. At most 32 findings of at // most 4 KiB encoded each are accepted. @@ -674,39 +846,3 @@ message HeaderMutation { RemoveHeader remove = 2; } } - -// HttpRequestResult contains the decision and optional request mutations. -message HttpRequestResult { - // Allow or deny decision for this request. - Decision decision = 1; - // Free-form service diagnostic. OpenShell does not relay this text into - // denied responses or security logs. Limited to 4 KiB before discarding. - string reason = 2; - // Replacement request body when has_body is true. Limited to 4 MiB. - bytes body = 3; - // True when body should replace the request body, including with an empty body. - bool has_body = 4; - // Ordered request-header mutations applied before the next middleware and - // before forwarding. Writes and removals may target visible end-to-end - // request headers, but credential, routing, framing, and hop-by-hop headers - // are always protected. Written values cannot contain OpenShell credential - // placeholder syntax. A violating result is a middleware failure handled - // according to the policy failure mode. At most 64 operations, 32 KiB of - // validated name/value data, and 64 KiB encoded are accepted. - repeated HeaderMutation header_mutations = 5; - // Audit-safe findings produced during evaluation. For operator-run services, - // OpenShell logs platform-owned fields derived from the operator-owned - // registration name rather than service-provided type, label, confidence, - // or metadata text. - // At most 32 findings of at most 4 KiB encoded each are accepted per stage. - // A policy selects at most 10 stages, so one chain retains at most 320. - repeated Finding findings = 6; - // Non-secret service-defined metadata included in diagnostics. At most 64 - // entries and 32 KiB of combined key/value data are accepted. - map metadata = 7; - // Optional stable machine-readable code for a deny decision. Codes must - // start with a lowercase ASCII letter and contain only lowercase ASCII - // letters, digits, and underscores, with a maximum length of 64 bytes. - // OpenShell may return this code to the requester, unlike free-form reason. - string reason_code = 8; -} diff --git a/rfc/0009-supervisor-middleware/README.md b/rfc/0009-supervisor-middleware/README.md index 1f6eacde7..4f60b0058 100644 --- a/rfc/0009-supervisor-middleware/README.md +++ b/rfc/0009-supervisor-middleware/README.md @@ -17,8 +17,9 @@ links: | Date | References | Change | |------|------------|--------| -| 2026-07-17 | [#2010](https://github.com/NVIDIA/OpenShell/issues/2010) | Added unary HTTP request middleware with built-in and operator-run services. | +| 2026-07-17 | [#2010](https://github.com/NVIDIA/OpenShell/issues/2010) | Added HTTP request middleware with built-in and operator-run services. | | 2026-07-28 | [#2428](https://github.com/NVIDIA/OpenShell/issues/2428) | Added WebSocket preflight and text-message evaluation, and aligned the middleware API names, limits, and diagnostics. | +| 2026-09-17 | [#2431](https://github.com/NVIDIA/OpenShell/issues/2431), [#3307](https://github.com/NVIDIA/OpenShell/issues/3307) | Replaced the unary request hook with a bounded bidirectional stream, added owned transformations and request trailers, and moved SigV4 into a restricted post-credentials built-in. | ## Summary @@ -57,13 +58,13 @@ This RFC uses the following terms with specific meanings. - **Egress.** An outbound request a sandbox sends to an upstream destination through the supervisor proxy. The v1 middleware hook acts on the parsed request the supervisor has already admitted and is about to forward, not on raw packets or arbitrary network activity. - **Middleware.** A service that inspects, transforms, blocks, or annotates supervisor operations through the contract defined in this RFC. In the v1 egress hook, a middleware owns its detection and transformation logic and never makes the upstream call itself; the supervisor always owns the upstream call. -- **Registered middleware.** An external middleware service an operator declares in gateway configuration as a diagnostic name plus a gRPC endpoint. Registration is an administrative action that establishes which endpoints may receive raw request content. The service exposes stable binding IDs through `Describe`, and policy refers to those binding IDs rather than to the registration name. -- **Built-in middleware.** A middleware that ships inside the supervisor binary and runs in-process, with no network hop and no gateway registration. Built-in binding IDs use the reserved `openshell/` namespace, for example `openshell/regex`. +- **Registered middleware.** An external middleware service an operator declares in gateway configuration under a stable registration name plus a gRPC endpoint. Registration is an administrative action that establishes which endpoints may receive raw request content. Policy attaches the complete service by this operator-owned name; `Describe` reports its supported operation and phase bindings. +- **Built-in middleware.** A middleware that ships inside the supervisor binary and runs in-process, with no network hop and no gateway registration. Built-in names use the reserved `openshell/` namespace, for example `openshell/regex`. - **Operation.** The typed method plus typed phase that identifies the point where OpenShell invokes middleware. This RFC's v1 middleware evaluates `method=HTTP_REQUEST, phase=PRE_CREDENTIALS`. - **Hook.** A named middleware API contract for one operation. Middleware hook names are part of the middleware API, not arbitrary strings supplied by the caller. The v1 hook is `HTTP_REQUEST/PRE_CREDENTIALS`, which runs in the HTTP relay once the request is parsed and admitted by policy and before credential injection. The design allows more typed operations later without changing the v1 hook's request shape. -- **Evaluation.** One invocation of middleware for a specific operation, request context, bounded body, and middleware config. Middleware keeps operation-specific methods such as `EvaluateHttpRequest` because inputs and outputs differ by protocol or operation type. +- **Evaluation.** One invocation of middleware for a specific operation, request context, bounded unit or body, and middleware config. Middleware keeps operation-specific streaming services because inputs and outputs differ by protocol or operation type. - **Result.** The response to an evaluation. For the v1 HTTP request hook, the result carries an allow/deny decision, optional replacement content and safe header mutations, findings, metadata, and safe error information. -- **Middleware config.** A policy entry stored under a stable policy-local map key that namespaces metadata and diagnostics. The optional `name` field is a human-readable label and defaults to the map key. The `middleware` field binds the entry to a service-owned binding ID, while the remaining fields define service-specific configuration, endpoint selectors, failure behavior, and ordering. +- **Middleware config.** A policy entry stored under a stable policy-local map key that namespaces metadata and diagnostics. The optional `name` field is a human-readable label and defaults to the map key. The `middleware` field selects a built-in or operator-owned registration name, while the remaining fields define service-specific configuration, endpoint selectors, failure behavior, and ordering. - **Manifest.** The self-description a middleware returns from `Describe`: its service version and service-owned bindings for the hooks it supports. The protobuf package `openshell.middleware.v1` defines the wire-version boundary; requests and manifests do not carry a duplicate API-version string. - **Decision.** The allow-or-deny outcome a middleware returns for a request. `allow` lets the request proceed (possibly transformed); `deny` short-circuits it. This vocabulary matches the rest of the OpenShell policy system. - **Failure policy.** The configured `on_error` behavior when middleware cannot return a valid result: `fail_closed` denies the request, while `fail_open` lets it continue without that middleware's transformation while recording an enforcement failure. `fail_closed` is the default whenever processing is required. @@ -112,7 +113,7 @@ graph LR ### Operation phases and placement -A middleware service provides hook implementations that the supervisor invokes at defined operation phases in the proxy flow. This version defines a single typed middleware operation, `HTTP_REQUEST/PRE_CREDENTIALS`, and is structured so more operations can be added later. The supervisor invokes the hook in the HTTP relay once the request has been parsed and admitted by policy, and before OpenShell injects upstream credentials. +A middleware service provides hook implementations that the supervisor invokes at defined operation phases in the proxy flow. V1 defines `HTTP_REQUEST/PRE_CREDENTIALS`, `HTTP_RESPONSE/PRE_RETURN`, and `WEBSOCKET_MESSAGE/PRE_CREDENTIALS`. It also reserves `HTTP_REQUEST/POST_CREDENTIALS` for trusted in-process built-ins. The supervisor invokes the external request hook after policy admission and before credential injection. ```mermaid graph LR @@ -126,7 +127,8 @@ graph LR RECHECK -->|"deny"| DENY RECHECK -->|"next stage or complete"| ROUTE["Route selection"] ROUTE --> CRED["Credential injection"] - CRED --> UP["Upstream forwarding"] + CRED --> SIGN["Restricted POST_CREDENTIALS
built-ins"] + SIGN --> UP["Upstream forwarding"] ``` This ordering is deliberate: @@ -145,27 +147,28 @@ The hook operates on a parsed HTTP request, so it runs wherever OpenShell can pa If a selected operation chain becomes uninspectable at runtime, OpenShell examines that chain. If any selected stage is `fail_closed`, the request is denied. If every selected stage is `fail_open`, OpenShell relays the request and emits a bypass `DetectionFinding`. This chain-level rule prevents one permissive selected stage from overriding a required stage. -Attachment and operation selection are separate. A destination host selector attaches a policy config, then the implementation manifest decides whether that config participates in `HTTP_REQUEST/PRE_CREDENTIALS`, `WEBSOCKET_MESSAGE/PRE_CREDENTIALS`, or both. The absence of an operation binding is a declared capability boundary rather than a middleware failure, so `on_error` does not apply. OpenShell records informational coverage when an attached config does not join the WebSocket chain. +Attachment and operation selection are separate. A destination host selector attaches a policy config, then the implementation manifest decides whether that config participates in `HTTP_REQUEST/PRE_CREDENTIALS`, `HTTP_RESPONSE/PRE_RETURN`, `WEBSOCKET_MESSAGE/PRE_CREDENTIALS`, or multiple phases. The absence of an operation binding is a declared capability boundary rather than a middleware failure, so `on_error` does not apply. OpenShell records informational coverage when an attached config does not join the WebSocket chain. WebSocket sits on this boundary. The upgrade request is a normal HTTP/1.1 request that an HTTP binding can inspect, allow, or deny. A separate V1 operation covers complete client-to-upstream text messages after upgrade. Binary messages, control frames, and upstream-to-client messages remain outside that operation. To keep the v1 boundary unambiguous: **In scope for v1:** - Inspectable HTTP/1.x requests that OpenShell terminates and parses, after L4 and SSRF admit them (and L7 policy too, where the endpoint declares a `protocol`). +- Final HTTP/1.x responses before delivery, using header-only, whole-body, or streaming inspection. - WebSocket upgrade (handshake) requests - the HTTP request that initiates the upgrade. - Complete client-to-upstream WebSocket text messages for implementations that advertise `WEBSOCKET_MESSAGE/PRE_CREDENTIALS`. -- Bounded request bodies: a `Content-Length` or bounded chunked body OpenShell can buffer within the applicable chain cap. +- Fixed-length and chunked request bodies normalized into bounded units, including bodies larger than one gRPC message. - Safe metadata output for later routing or audit. **Out of scope for v1:** - HTTP/2 and HTTP/3. The proxy's TLS termination pins ALPN to `http/1.1` today, so these are not introspected. -- Binary and control WebSocket messages, upstream-to-client WebSocket messages, and response-body scanning. +- Binary and control WebSocket messages and upstream-to-client WebSocket messages. - Opaque TCP streams and endpoints with `tls: skip`. -- Unbounded streaming uploads or full-duplex request processing. +- Unbounded or full-duplex request processing. Owned streams have a finite deferred-storage limit and complete before upstream contact. - Multipart or compressed body semantics, unless a selected service's manifest and policy explicitly support them within the size limits. -The request hook is synchronous and runs once for every selected stage. Timeout, failure behavior, and body buffering are therefore load-bearing parts of the design. The supervisor buffers up to the largest resolved stage limit, bounded by the 4 MiB platform maximum. A stage whose smaller limit is exceeded applies its own `on_error`, and later stages may still run when that result is `fail_open`. If an oversized `Content-Length` is known before body consumption, the supervisor may preserve streaming and fail open only when every affected stage permits it. If a chunked body crosses the cap after bytes have been consumed, the request is denied because the raw stream can no longer be resumed safely. The hook remains before any credential rewrite, which keeps OpenShell-managed credentials away from external middleware. Other operation phases such as pre-policy classification, a credential-visible `HttpRequest/post_credentials` hook for request signing (built-in-only, for example `openshell/sigv4`), response inspection, route selection for OpenShell-managed destinations, and streaming message hooks are possible future extensions and are out of scope for v1. +The request hook is synchronous and opens one bidirectional stream for every selected stage. Timeout, failure behavior, sequencing, backpressure, and ownership are therefore load-bearing parts of the design. A preflight selects headers-only, whole-body, lockstep stream, or owned-stream processing. Whole-body input and each stream unit use the binding limit; OpenShell further caps request units at 64 KiB. Whole-body mode receives its complete body in one final unit. Lockstep and owned streams receive nonempty data units followed by an empty final unit. Owned streams durably accept input without a platform replay copy, are available only to `fail_closed` stages, and cap deferred input and output at 1 GiB each. A pure lockstep chain may forward approved units upstream as they complete; any whole-body, owned, policy re-evaluation, body credential rewrite, or body-signing requirement holds the complete representation. OpenShell never replays a partially forwarded request. Request body receipt, middleware processing, and output delivery share a two-minute wall-clock deadline. The hook remains before credential rewrite, which keeps OpenShell-managed credentials away from external middleware. A restricted in-process `HTTP_REQUEST/POST_CREDENTIALS` phase hosts `openshell/sigv4`; external manifests cannot advertise that credential-visible phase. ### The middleware contract @@ -178,8 +181,9 @@ Configuration-time: Request-time: -- `EvaluateHttpRequest` carries the selected binding ID and typed operation phase (`PRE_CREDENTIALS`) plus the request context, middleware configuration from policy, HTTP request target, repeated safe headers in wire order, and bounded body. -- `HttpRequestResult` is a response OpenShell can apply directly: `allow` or `deny`, a reason, optional replacement content, ordered header mutations, findings, and namespaced metadata. +- `HttpRequestPreCredentials.Evaluate` is a bidirectional stream. Preflight carries the request context, policy config, admitted target, repeated safe headers, permitted modes, and effective limits. +- Body events carry contiguous bounded normalized units. Results pass, transform, block, skip remaining inspection, or take ownership of the corresponding unit. +- An owned stage acknowledges every input unit, then emits contiguous output units and final input/output accounting. Trailer and terminal events complete the lifecycle. A simplified sketch of the gRPC contract: @@ -188,9 +192,11 @@ service SupervisorMiddleware { // Configuration-time rpc Describe(google.protobuf.Empty) returns (MiddlewareManifest); rpc ValidateConfig(ValidateConfigRequest) returns (ValidateConfigResponse); +} - // operation=HTTP_REQUEST, phase=PRE_CREDENTIALS. - rpc EvaluateHttpRequest(HttpRequestEvaluation) returns (HttpRequestResult); +// operation=HTTP_REQUEST, phase=PRE_CREDENTIALS. +service HttpRequestPreCredentials { + rpc Evaluate(stream HttpRequestEvent) returns (stream HttpRequestEventResult); } message MiddlewareManifest { @@ -200,29 +206,47 @@ message MiddlewareManifest { } message MiddlewareBinding { - string id = 1; // service-owned stable ID - SupervisorMiddlewareOperation operation = 2; - SupervisorMiddlewarePhase phase = 3; - uint64 max_payload_bytes = 4; // one logical request or message payload - string timeout = 5; // optional binding-specific RPC timeout + SupervisorMiddlewareOperation operation = 1; + SupervisorMiddlewarePhase phase = 2; + uint64 max_payload_bytes = 3; // whole body, message, or stream unit + google.protobuf.Duration request_timeout = 104; } -message HttpRequestEvaluation { - string binding_id = 1; // selected manifest binding - SupervisorMiddlewarePhase phase = 2; +message HttpRequestEvent { + oneof event { + HttpRequestPreflight preflight = 1; + HttpRequestBodyUnit body = 2; + MiddlewareSessionEnd session_end = 3; + HttpRequestTrailers trailers = 4; + } +} - RequestContext context = 3; - google.protobuf.Struct config = 4; // service-specific, from policy +message HttpRequestPreflight { + RequestContext context = 1; + HttpRequestTarget target = 2; + repeated HttpHeader headers = 3; + string middleware_name = 4; + google.protobuf.Struct config = 5; + uint64 max_payload_bytes = 6; + repeated HttpRequestBodyMode permitted_body_modes = 7; + uint64 max_deferred_bytes = 8; + optional uint64 declared_body_length = 9; +} - HttpRequestTarget target = 5; - repeated HttpHeader headers = 6; // safe subset, duplicates and wire order preserved - bytes body = 7; // bounded +message HttpRequestBodyUnit { + uint64 sequence = 1; + oneof payload { + bytes data = 2; + } + bool end_of_stream = 3; } message RequestContext { string request_id = 1; string sandbox_id = 2; Process originating_process = 3; // optional, per-connection + string sandbox_name = 4; // display and logging only + string workspace = 5; // display and logging only } message HttpRequestTarget { @@ -271,32 +295,30 @@ message RemoveHeader { string name = 1; } -message HttpRequestResult { - Decision decision = 1; // ALLOW or DENY - string reason = 2; // normalized by OpenShell before security output - - bytes body = 3; // replacement content when transformed - bool has_body = 4; // distinguishes no replacement from an empty replacement - - repeated HeaderMutation header_mutations = 5; - repeated Finding findings = 6; - map metadata = 7; +message HttpRequestEventResult { + oneof result { + HttpRequestPreflightResult preflight_result = 1; + HttpRequestBodyResult body_result = 2; + HttpRequestTrailersResult trailers_result = 3; + HttpRequestBodyOutput body_output = 4; + HttpRequestBodyFinalize body_finalize = 5; + } } ``` -The evaluation and result are shaped so middleware composes cleanly in a chain. The allow/deny decision is a first-class result field rather than being mixed into content. If `has_body` is true, the transformed content a middleware returns (`HttpRequestResult.body`) becomes the request body the next middleware receives as `HttpRequestEvaluation.body`; if `has_body` is false, the supervisor keeps the previous body. The supervisor also feeds allowed header mutations into the next stage, so a chain is effectively a fold over a single request representation; a `deny` from any stage short-circuits the rest. See [Middleware ordering](#middleware-ordering) for how chains are assembled and ordered. +The event and result streams compose as a chain over one request representation. A stage's accepted body units and safe header or trailer mutations feed the next stage; an explicit block short-circuits the rest. Whole-body mode produces one data-bearing final unit. Lockstep and owned streams receive nonempty units without `end_of_stream`, followed by one empty terminal unit. Lockstep streaming cannot retain bytes across results. Owned streaming transfers replay responsibility to the stage, which must accept every unit before emitting a bounded replacement and final accounting. See [Middleware ordering](#middleware-ordering) for how chains are assembled and ordered. -Headers use a repeated representation so duplicate lines and wire order survive evaluation and chaining. Before an external call, OpenShell omits credential-bearing, routing, framing, hop-by-hop, and `Connection`-nominated headers. A result may return ordered writes and removals. Writes support append, overwrite, and skip modes but may target only the `x-openshell-middleware-*` namespace. Removals may target other headers visible to middleware, except credential-bearing, routing, framing, hop-by-hop, and `Connection`-nominated headers. Header values containing control characters are invalid. OpenShell validates and applies a stage's mutations atomically. If any mutation is invalid, none are applied and the stage follows its configured `on_error` behavior. +Headers use a repeated representation so duplicate lines and wire order survive evaluation and chaining. Before an external call, OpenShell omits credential-bearing, routing, framing, hop-by-hop, and `Connection`-nominated headers. A result may return ordered writes and removals for middleware-visible end-to-end headers. Writes support append, overwrite, and skip modes. Credential-bearing, routing, framing, hop-by-hop, `Connection`-nominated, and OpenShell credential headers remain protected. Header values containing control characters or credential placeholders are invalid. OpenShell validates and applies a stage's mutations atomically. If any mutation is invalid, none are applied and the stage follows its configured `on_error` behavior. -> **Update in PR #2477 - WebSocket middleware:** The following contract text adds the bidirectional `EvaluateWebSocketSession` RPC, WebSocket preflight, message limits, and the WebSocket binding for the built-in regex middleware. The unary HTTP contract does not change. +> **Update in PR #2477 - WebSocket middleware:** The following contract text adds the bidirectional `EvaluateWebSocketSession` RPC, WebSocket preflight, message limits, and the WebSocket binding for the built-in regex middleware. -The interface is gRPC. The protobuf package `openshell.middleware.v1` is the protocol version boundary, so manifests and evaluation messages do not repeat an API-version string. HTTP evaluation remains unary: the supervisor buffers the bounded body, sends one `HttpRequestEvaluation`, and receives one `HttpRequestResult`. Complete client-to-upstream WebSocket text messages use the separate bidirectional-streaming `EvaluateWebSocketSession` RPC. The supervisor sends `WebSocketSessionEvent` values; the service returns `WebSocketSessionEventResult` values for preflight and message events, while session start and end are notifications without corresponding results. Streaming is not baked into `EvaluateHttpRequest`; future chunked HTTP transport should add another operation-specific method rather than changing the existing method's cardinality. Possible extensions are collected in the [protocol-extensions appendix](appendices/protocol-extensions.md). Built-in middleware uses the same logical contracts in-process; the `openshell/regex` built-in advertises both V1 operations. +The interface is gRPC. The protobuf package `openshell.middleware.v1` is the protocol version boundary, so manifests and evaluation messages do not repeat an API-version string. HTTP requests use `HttpRequestPreCredentials.Evaluate`; client-to-upstream WebSocket text messages use the separate `EvaluateWebSocketSession` RPC. Both are bidirectional streams with operation-specific events. Built-in middleware uses the same logical contracts in-process. `openshell/regex` advertises request and WebSocket bindings. Endpoint `credential_signing` fields synthesize the restricted `openshell/sigv4` stage at `HTTP_REQUEST/POST_CREDENTIALS`; it is not attachable through `network_middlewares`. -V1 applies explicit public envelope limits before invoking a service or accepting its result: 64 KiB for encoded config, 4 KiB for request context, 32 KiB for the target, 128 header lines and 64 KiB of encoded headers, 4 MiB for the logical payload, 4 KiB for a reason, 64 header mutations with at most 32 KiB of validated name/value data and 64 KiB encoded, 32 findings per stage with each finding at most 4 KiB encoded, and 64 metadata entries totaling at most 32 KiB. A chain has at most 10 stages and therefore at most 320 findings. Middleware gRPC servers configure request and response message limits to cover the 4 MiB payload plus at least 292 KiB for the remaining envelope. +V1 applies explicit public envelope limits before invoking a service or accepting its result: 64 KiB for encoded config, 4 KiB for request context, 32 KiB for the target, 128 header lines and 64 KiB of encoded headers, 4 MiB for a whole-body payload or advertised unit, 64 KiB for each request stream unit, 4 KiB for a reason, 64 header mutations with at most 32 KiB of validated name/value data and 64 KiB encoded, 32 findings per stage with each finding at most 4 KiB encoded, and 64 metadata entries totaling at most 32 KiB. Owned request streams separately cap deferred input and output at 1 GiB. A chain has at most 10 stages and therefore at most 320 findings. -For WebSocket traffic, a service advertises `WEBSOCKET_MESSAGE/PRE_CREDENTIALS` with `max_payload_bytes`, which limits one complete message or replacement rather than the whole session. HTTP bindings use the same field for one request body or replacement. An attached service without that exact binding does not join the chain, does not apply `on_error`, and produces internal `binding_not_selected` coverage. OpenShell opens one phase-specific `EvaluateWebSocketSession` stream per selected stage and upgrade attempt. Future upstream-to-client inspection uses the same RPC with `PRE_RETURN`; a service selected for both phases receives two independent streams for the WebSocket session. A bounded preflight exposes only the admitted destination through `HttpRequestTarget`, with its path separated from query data, requested subprotocols, sandbox context, the attached API middleware name, and validated implementation config. Policy-local config identity remains internal for audit and denial metadata. OpenShell evaluates selected preflights concurrently. Each stage returns `inspect`, voluntary `skip`, or authoritative `deny` before upstream contact, plus optional bounded reason, reason code, findings, and metadata. `deny` is a successful decision enforced independently of `on_error` and takes precedence over concurrent failures; failures alone follow each stage's `on_error`. OpenShell sends the terminal reason to every still-writable stream whose preflight opened successfully, at most once per stage. Inspecting stages that continue receive `session_start`, then complete logical text messages with monotonic sequence numbers. Stages run in global policy order and each sees the prior stage's accepted replacement. Binary logical messages pass through without middleware inspection under both error modes, consume a session-global sequence, and emit `unsupported_message_type` coverage for active stages; a later text RPC may therefore contain a valid sequence gap. `PRE_RETURN` and upstream-to-client inspection are reserved for a later implementation. +For WebSocket traffic, a service advertises `WEBSOCKET_MESSAGE/PRE_CREDENTIALS` with `max_payload_bytes`, which limits one complete message or replacement rather than the whole session. For HTTP request traffic, the field limits a whole-body representation or individual stream unit. An attached service without the exact operation binding does not join that chain and does not apply `on_error`. OpenShell opens one phase-specific stream per selected stage. Preflight exposes only the admitted destination, sandbox context, attached middleware name, validated config, and bounded safe headers. Each stage returns inspect, voluntary skip, or authoritative deny before body processing begins. Explicit denial is a successful decision enforced independently of `on_error`; failures follow the stage's failure policy. OpenShell sends a terminal reason to each still-writable opened stream at most once. -Inspectable WebSocket text input and replacements share the 4 MiB platform cap. The operator's `max_payload_bytes` is the shared HTTP-body and WebSocket-text ceiling, further constrained by each operation binding's capability. It does not bound binary pass-through, which retains a separate raw-frame safety limit. Logical messages use a protobuf `oneof` with `string text` and `bytes binary` variants; results use an optional matching replacement `oneof`, whose presence also represents an empty replacement without a separate boolean. Protobuf decoding enforces UTF-8 for text, and OpenShell rejects replacement variants that would change the message type. A complete text message holds one process-wide admission permit for its entire chain; preflight fan-out holds one permit until every stage resolves. Permit waiting is backpressure and does not consume the per-message deadline. Per-stage timeouts are also bounded by a 30-second total chain budget, which applies to HTTP chains too. This bound controls concurrency and peak buffered inspection memory; it is not rate limiting. +Inspectable WebSocket text input and replacements share the 4 MiB platform cap. The operator's `max_payload_bytes` is the shared HTTP-body and WebSocket-text ceiling, further constrained by each operation binding's capability. It does not bound binary pass-through, which retains a separate raw-frame safety limit. Logical messages use a protobuf `oneof` with `string text` and `bytes binary` variants; results use an optional matching replacement `oneof`, whose presence also represents an empty replacement without a separate boolean. Protobuf decoding enforces UTF-8 for text, and OpenShell rejects replacement variants that would change the message type. A complete text message holds one process-wide admission permit for its entire chain; preflight fan-out holds one permit until every stage resolves. Permit waiting is backpressure and does not consume the per-message deadline. Per-stage timeouts are also bounded by a 30-second chain budget for one WebSocket message, one HTTP body-unit pass, or one HTTP finalization pass. This is not an accepted-stream lifetime or rate limit. Request body receipt, middleware processing, and output delivery have a separate two-minute total deadline. The `originating_process` is the same identity OpenShell resolves on the egress path - the binary, pid, and ancestor chain it uses for binary-scoped network policy and OCSF audit. It is per-connection rather than strictly per-request and is optional. Middleware must treat missing process data as unavailable rather than as an authorization failure. The initial implementation leaves this field unset until reliable propagation is available. @@ -309,7 +331,7 @@ Shared mechanics: - **Endpoint exposure and auth.** Both extension systems use gRPC network endpoints. Their stable transport contract requires confidentiality and service authentication. During phase 1 only, supervisor middleware may explicitly opt into plaintext for trusted local or isolated research environments. Endpoint declaration, identity binding, credential material, and rotation should use shared mechanics where practical. - **Manifest description.** Both extension systems use `Describe` to return a manifest that declares a diagnostic service name, implementation version, and service-owned bindings for supported hook points. - **Operation phases.** Both systems hook into a named operation plus phase. The phase sets differ by system, but the concept is the same: `method=CreateSandbox, phase=pre_request` for a gateway interceptor, and `HTTP_REQUEST/PRE_CREDENTIALS` for v1 supervisor middleware. -- **Evaluation and result.** Both systems run an evaluate-style request and return a result. Middleware keeps operation-specific methods such as `EvaluateHttpRequest` because inputs and outputs differ by protocol or operation type; interceptor methods and messages are defined by RFC 0010. +- **Evaluation and result.** Both systems run evaluate-style exchanges. Middleware keeps operation-specific streaming services such as `HttpRequestPreCredentials` because inputs and outputs differ by protocol or operation type; interceptor methods and messages are defined by RFC 0010. - **Failure policy.** Both systems use `on_error: fail_closed|fail_open`, with fail-closed as the safe default for required enforcement. - **Observability.** Both systems emit OCSF events with the details relevant to the extension point, while preserving the same no-secrets logging rules. - **Ordering.** Both systems apply multiple configured extensions in deterministic order. @@ -317,18 +339,18 @@ Shared mechanics: Intentional differences: - **Selection model.** Supervisor middleware is selected per sandbox at runtime through policy and API state. Gateway interceptors are selected for the gateway at deploy time by operators in `gateway.toml`. -- **Method naming.** Gateway interceptors register against RPC method strings that are not themselves part of the interceptor API. Supervisor middleware exposes named operation-specific hook methods such as `EvaluateHttpRequest`; those names are part of the middleware API contract. +- **Method naming.** Gateway interceptors register against RPC method strings that are not themselves part of the interceptor API. Supervisor middleware exposes named operation-specific services such as `HttpRequestPreCredentials`; those names are part of the middleware API contract. - **Control responsibility.** Gateway interceptors may enforce that sandbox requests include approved middleware configuration, but they do not replace the per-sandbox middleware selection model. ### Contract versioning The middleware gRPC contract lives under a major-versioned protobuf package (`openshell.middleware.v1`), the same convention the compute-driver contract uses in [RFC 0001](../0001-core-architecture/README.md). Within a stable major version, changes stay additive and backward compatible - new fields, RPCs, operation phases, and manifest fields can be added - while breaking wire or semantic changes require a new major version. The research preview may still make intentional breaking changes before the contract is declared stable. -The protobuf package is the wire-version handshake. `Describe` reports a diagnostic service name, implementation version, and stable binding IDs for supported hook points; it does not carry a second API-version field. Manifest validation is mandatory: if OpenShell cannot fetch the manifest, bindings conflict, a service claims the reserved `openshell/` namespace, or policy asks for an unsupported binding or invalid config, the gateway rejects the relevant configuration before traffic can depend on it. Runtime invocation failures are handled through `on_error` and use `fail_closed` by default. +The protobuf package is the wire-version handshake. `Describe` reports a diagnostic service name, implementation version, and operation/phase bindings for supported hook points; it does not carry a second API-version field. Manifest validation is mandatory: if OpenShell cannot fetch the manifest, bindings conflict, an external service advertises restricted `POST_CREDENTIALS`, or policy asks for an unsupported implementation or invalid config, the gateway rejects the relevant configuration before traffic can depend on it. Runtime invocation failures are handled through `on_error` and use `fail_closed` by default. ### Registration and delivery -The operator registers available external middleware services in gateway configuration under `openshell.supervisor.middleware`. The namespace identifies the subsystem whose behavior is extended, not the process that reads the configuration. The gateway still loads, validates, and distributes these registrations to supervisors. Each entry has a diagnostic name, gRPC endpoint, maximum logical payload size, optional RPC timeout, and transport settings. The diagnostic name identifies the configured connection in logs but is not a policy key. Policy authors select stable binding IDs returned by `Describe`, so they cannot point traffic at an arbitrary endpoint and do not depend on an operator-local registration name. +The operator registers available external middleware services in gateway configuration under `openshell.supervisor.middleware`. The namespace identifies the subsystem whose behavior is extended, not the process that reads the configuration. The gateway loads, validates, and distributes these registrations to supervisors. Each entry has an operator-owned name, gRPC endpoint, maximum payload size, optional RPC timeout, and transport settings. Policy authors select that registration name; they cannot point traffic at an arbitrary endpoint. The service-reported manifest name remains diagnostic metadata. The v1 transport is gRPC over a network endpoint reachable from every supervisor across Docker, Podman, VM, and Kubernetes drivers. In local single-player deployments, a loopback endpoint such as `127.0.0.1:1234` may be translated to `host.openshell.internal` so a supervisor can reach a service running on the local host. That loopback shorthand is not an HA deployment model: Kubernetes and other shared deployments should register a routable service DNS name or address that every supervisor can reach directly. Other deployment shapes are deferred until OpenShell has a universal way to make those endpoints reachable from the relevant supervisor environments. @@ -338,7 +360,7 @@ name = "anonymizer" grpc_endpoint = "http://127.0.0.1:1234" max_payload_bytes = 4194304 timeout = "500ms" -allow_insecure = true +allow_insecure_transport = true [[openshell.supervisor.middleware]] name = "agent-traces-exporter" @@ -346,19 +368,19 @@ grpc_endpoint = "https://middleware.example.internal:443" max_payload_bytes = 1048576 ``` -The stable transport requirement is confidentiality plus authentication of the intended middleware service. Phase 1 may temporarily accept a plaintext `http://` endpoint only when the same entry explicitly sets `allow_insecure = true`. OpenShell rejects plaintext without that opt-in, warns prominently, and records the insecure registration as auditable configuration state. This escape hatch is limited to trusted local development and isolated research environments. Phase 2 removes plaintext support and the `allow_insecure` field, requiring authenticated encrypted transport. That removal is an intentional research-preview breaking change with no long-term compatibility obligation. The exact phase 2 mechanism, such as mTLS or TLS plus explicit caller authentication, is follow-up protocol work (see [appendices/protocol-extensions.md](appendices/protocol-extensions.md#middleware-authentication)). +The stable transport requirement is confidentiality plus authentication of the intended middleware service. Phase 1 may temporarily accept a plaintext `http://` endpoint only when the same entry explicitly sets `allow_insecure_transport = true`. OpenShell rejects plaintext without that opt-in, warns prominently, and records the insecure registration as auditable configuration state. This escape hatch is limited to trusted local development and isolated research environments. Phase 2 removes plaintext support and the opt-out field, requiring authenticated encrypted transport. That removal is an intentional research-preview breaking change with no long-term compatibility obligation. The exact phase 2 mechanism, such as mTLS or TLS plus caller authentication, is follow-up protocol work. -For each binding, the operator's `max_payload_bytes` must not exceed the binding capability returned by `Describe` or the 4 MiB platform maximum. The gateway rejects an invalid registration rather than silently clamping it. The resulting operator limit applies to every binding exposed by that registration. +For each binding, the operator's `max_payload_bytes` must not exceed the binding capability returned by `Describe` or the 4 MiB platform maximum. The gateway rejects an invalid registration rather than silently clamping it. Whole-body modes use the effective limit for the complete representation; stream modes use it per unit, with request units further capped at 64 KiB. Owned request streams separately advertise the fail-closed deferred-storage limit. -RPC timeouts use an integer with an `ms` or `s` suffix, range from 10 ms through 30 s, and default to 500 ms. A binding may advertise its own timeout through `Describe`; that value overrides the service registration timeout. The service timeout applies to `Describe`, while the effective binding timeout applies to `ValidateConfig` and `EvaluateHttpRequest`. +RPC timeouts use an integer with an `ms` or `s` suffix, range from 10 ms through 30 s, and default to 500 ms. A binding may advertise its own timeout through `Describe`; that value overrides the service registration timeout. The service timeout applies to `Describe`, while the effective binding timeout applies to `ValidateConfig`, stream open, and individual request exchanges. The external-service endpoint is trusted operator infrastructure in v1. The auth design must make both directions explicit: the supervisor proves to the middleware that the call is authorized for the specific middleware identity, and the supervisor verifies it is calling the intended middleware service. -Binding IDs may be bare (`anonymizer`) or namespaced with `/` (`nvidia/anonymizer`, `acme/security/pii-redactor`). Empty path segments are invalid, so `/foo`, `foo/`, and `foo//bar` are rejected. The `openshell/` namespace is reserved for built-in OpenShell middleware, such as `openshell/regex` or `openshell/sigv4`. Policy config map keys remain stable local identities for metadata namespacing and diagnostics; the `middleware` field selects the binding. +Middleware names may be bare (`anonymizer`) or namespaced with `/` (`nvidia/anonymizer`, `acme/security/pii-redactor`). Empty path segments are invalid, so `/foo`, `foo/`, and `foo//bar` are rejected. The `openshell/` namespace is reserved for built-in OpenShell middleware, such as `openshell/regex` or `openshell/sigv4`. Policy config map keys remain stable local identities for metadata namespacing and diagnostics; the `middleware` field selects the built-in or operator registration. Built-in middleware ships in the supervisor binary and needs no external registration. Supervisors install built-in bindings before attempting external connections. -At gateway startup, OpenShell connects to every registered service and calls `Describe`. Startup rejects unavailable or invalid services, duplicate binding IDs across services, and external claims in the reserved `openshell/` namespace. Sandbox policy creation and update call the owning service's `ValidateConfig` before persistence. +At gateway startup, OpenShell connects to every registered service and calls `Describe`. Startup rejects unavailable or invalid services, duplicate registration names, conflicting operation/phase bindings, restricted external phases, and external claims in the reserved `openshell/` namespace. Sandbox policy creation and update call the owning service's `ValidateConfig` before persistence. Supervisors receive policy plus the external service registrations required by the effective policy through the existing `GetSandboxConfig` response. Built-in registrations are not delivered because they are already installed in-process. The gateway stays off the request hot path; supervisors connect to the required services and invoke them directly. @@ -376,7 +398,7 @@ Multitenancy is handled by OpenShell policy selection, not by giving middleware Policy decides which middleware runs for which traffic, how it is configured, and what happens on failure. Middleware configs live once in the top-level `network_middlewares` map, represented as `map` in `SandboxPolicy`. Each map key is the stable policy-local identity. Each config selects destination hosts directly through `endpoints.include` and `endpoints.exclude`; network policies and endpoints do not carry middleware attachment lists. -A middleware config may include an optional human-readable `name`, which defaults to the map key and does not replace that key as the config identity. `middleware` is the stable binding ID exposed by a built-in or by an external service's `Describe` response. Different map keys may reference the same binding and run as separate stages with different selectors or configuration. +A middleware config may include an optional human-readable `name`, which defaults to the map key and does not replace that key as the config identity. `middleware` is a stable built-in or operator-owned registration name. Different map keys may reference the same implementation and run as separate stages with different selectors or configuration. Each entry supplies implementation-owned configuration, `on_error` behavior, numeric `order`, and endpoint selectors. `fail_closed` is the default. `order` defaults to `0` and must be unique across the complete policy, even when selectors do not overlap, so policies with multiple configs normally set it explicitly. OpenShell validates the structure and asks the owning implementation to `ValidateConfig` before the gateway persists a policy. @@ -384,7 +406,7 @@ Selection occurs after network and L7 admission and depends only on the admitted Every config requires a non-empty `include` list. `exclude` is optional and takes precedence over `include`. Matching is case-insensitive and uses the same host-pattern implementation as network endpoints: `*` matches exactly one DNS label, `**` matches one or more DNS labels, and intra-label wildcards such as `*-api.example.com` are supported. Brace alternates are rejected; authors list each alternative explicitly. A config accepts at most 32 combined include and exclude patterns. A policy accepts at most 10 middleware configs, and runtime selection defensively rejects a chain longer than 10 stages. -The hook is a supervisor-side Rust enforcement stage selected by policy data, not a Rego rule. L4 policy admits the connection and, where the endpoint declares a `protocol`, L7 policy admits the parsed request. The supervisor then selects the chain, buffers the bounded body, invokes stages, applies valid results, and re-evaluates body-aware protocol policy after each body replacement. Request bodies do not otherwise become a new general Rego input surface. +The hook is a supervisor-side Rust enforcement stage selected by policy data, not a Rego rule. L4 policy admits the connection and, where the endpoint declares a `protocol`, L7 policy admits the parsed request. The supervisor then selects the chain, opens event streams, applies valid results, and re-evaluates body-aware protocol policy after each body replacement. Body-aware protocols retain a bounded hold barrier for this re-evaluation. Other HTTP/1 paths either forward pure lockstep output incrementally or spool when a stage takes ownership or needs the complete body. Request bodies do not otherwise become a new general Rego input surface. ```yaml network_middlewares: @@ -436,7 +458,7 @@ Beyond allow/deny and transformation, middleware emits string metadata (for exam Because `HTTP_REQUEST/PRE_CREDENTIALS` runs before route selection and credential injection, v1 does not guarantee that metadata visible at this hook includes the final routed model or upstream route. Budget-style middleware that needs post-call status, final route/model, content length, or token usage needs a later metadata-only notification hook such as `HttpResponse/completed`; that hook is listed as a future extension in the [protocol-extensions appendix](appendices/protocol-extensions.md#additional-operation-phases), not part of the v1 request hook. -The namespace is the policy-local middleware config map key, not the optional human-readable `name` or the binding ID. This means two configs that use the same binding still produce separate metadata buckets, and changing a display label or the registered service behind a binding does not rename downstream annotations. +The namespace is the policy-local middleware config map key, not the optional human-readable `name` or registered implementation name. This means two configs that use the same implementation still produce separate metadata buckets, and changing a display label or the registered service behind a config does not rename downstream annotations. ### Audit and logging @@ -444,7 +466,7 @@ A middleware decision is observable sandbox behavior, so it is recorded as an OC > **Update in PR #2477 - WebSocket middleware:** The coverage-boundary event below is new. It distinguishes an unsupported operation or message type from a middleware invocation or failure. -- **Per-invocation decisions** are `HttpActivity` events, since middleware is an L7 enforcement point. Each stage records the policy-local config key, validated binding ID, decision, transformation state, latency, and policy and endpoint context. Allowed requests are `Informational`; denials are `Medium`. +- **Per-invocation decisions** are `HttpActivity` events, since middleware is an L7 enforcement point. Each stage records the policy-local config key, registered implementation name, decision, transformation state, latency, and policy and endpoint context. Allowed requests are `Informational`; denials are `Medium`. - **Enforcement failures and bypasses** also emit `DetectionFinding` events. Required-stage failures, invalid responses, uninspectable traffic with a required stage, and body-aware policy evaluation failures are `High`. A `fail_open` bypass and uninspectable traffic allowed because every matching stage is `fail_open` are still findings so operators can alert on reduced enforcement. - **Coverage boundaries** emit informational `NetworkActivity` events separately from invocations and failures. `binding_not_selected` records an attached config whose manifest lacks the WebSocket binding. `unsupported_message_type` records binary pass-through for an active stage with its internal config identity, logical sequence, message class, and size. - **Configuration events** are `ConfigStateChange` events: middleware registration validation, registry reload success or failure, and policy validation outcome. @@ -453,7 +475,7 @@ These events must never leak the content they describe. The OCSF JSONL may be sh - Raw request content, matched values, redacted spans, and service-config secrets are never logged. - Built-ins may preserve contract-defined audit-safe reasons and finding fields. Operator-run reason text, finding text, mutation errors, and diagnostic metadata are untrusted input. OpenShell replaces or omits them in denied responses and security logs, using stable platform-owned messages derived from the validated binding and failure category. -- Events carry only safe summaries: policy-local config keys, validated binding IDs, decisions, latency, platform-owned failure categories, and aggregate counts. +- Events carry only safe summaries: policy-local config keys, validated implementation names, decisions, latency, platform-owned failure categories, and aggregate counts. This mirrors the middleware response contract, which already forbids the service from returning raw matched values. @@ -463,13 +485,13 @@ Supervisor egress middleware stays opt-in throughout: until a policy declares a > **Update in PR #2477 - WebSocket middleware:** Phase 1 now also includes the forward-text WebSocket operation, bounded WebSocket messages, and the WebSocket binding for the built-in regex middleware. -**Phase 1 - research-preview contract and execution.** Define `openshell.middleware.v1` with `Describe`, `ValidateConfig`, unary `EvaluateHttpRequest`, and forward-text `EvaluateWebSocketSession`; ship the example `openshell/regex` built-in; and support statically registered operator-run services. Policy uses a top-level selector-based `network_middlewares` map with stable config keys, unique numeric `order`, per-stage `on_error`, bounded bodies and messages, bounded RPC timeouts, atomic header mutations, post-transformation policy re-evaluation, and OCSF observability. Gateway startup validates external manifests, policy writes validate implementation-owned config, effective sandbox config carries only required external registrations, and supervisors install policy plus registry as one last-known-good runtime generation. Phase 1 requires encrypted authenticated transport for normal use but temporarily permits plaintext `http://` only with explicit `allow_insecure = true` for trusted local development or isolated research. OpenShell warns and emits auditable configuration state whenever that exception is used. +**Phase 1 - research-preview contract and execution.** Define `openshell.middleware.v1` with `Describe`, `ValidateConfig`, bidirectional `HttpRequestPreCredentials.Evaluate`, HTTP response streams, and forward-text `EvaluateWebSocketSession`; ship `openshell/regex` and restricted `openshell/sigv4` built-ins; and support statically registered operator-run services. Policy uses a top-level selector-based `network_middlewares` map with stable config keys, unique numeric `order`, per-stage `on_error`, bounded units and bodies, bounded RPC timeouts, atomic header mutations, post-transformation policy re-evaluation, and OCSF observability. Gateway startup validates external manifests, policy writes validate implementation-owned config, effective sandbox config carries only required external registrations, and supervisors install policy plus registry as one last-known-good runtime generation. Phase 1 requires encrypted authenticated transport for normal use but temporarily permits plaintext `http://` only with explicit `allow_insecure_transport = true` for trusted local development or isolated research. OpenShell warns and emits auditable configuration state whenever that exception is used. -**Phase 2 - mandatory authenticated encryption.** Remove plaintext middleware transport and remove `allow_insecure`. Every external connection must provide transport confidentiality and authenticate the intended service, with the final mechanism and credential delivery model defined by follow-up protocol work. Because phase 1 is explicitly a research preview, removing its insecure escape hatch is an intentional breaking change and does not create a long-term compatibility obligation. Operator-run service deployment otherwise keeps the same binding, policy, validation, delivery, reload, and invocation model. +**Phase 2 - mandatory authenticated encryption.** Remove plaintext middleware transport and remove `allow_insecure_transport`. Every external connection must provide transport confidentiality and authenticate the intended service, with the final mechanism and credential delivery model defined by follow-up protocol work. Because phase 1 is explicitly a research preview, removing its insecure escape hatch is an intentional breaking change and does not create a long-term compatibility obligation. Operator-run service deployment otherwise keeps the same binding, policy, validation, delivery, reload, and invocation model. ### Backwards compatibility and migration -Existing sandbox policies and gateway configs that declare no middleware remain valid and pay no per-request cost. Middleware configs that opt into phase 1 plaintext are intentionally temporary: they must migrate to authenticated encrypted endpoints before phase 2 because `allow_insecure` and plaintext support will be removed. The research-preview contract may make other breaking changes before stability. +Existing sandbox policies and gateway configs that declare no middleware remain valid and pay no per-request cost. The request API change is intentionally breaking within the research preview: services must replace the removed unary request method and messages with `HttpRequestPreCredentials.Evaluate`, implement preflight/body/trailers/session-end sequencing, and register that gRPC service beside `SupervisorMiddleware`. There is no unary fallback. Middleware configs that opt into phase 1 plaintext are intentionally temporary and must migrate to authenticated encrypted endpoints before phase 2. The research-preview contract may make other breaking changes before stability. ### Research preview @@ -483,11 +505,11 @@ Adding a synchronous, content-aware hook to the egress path has real costs. The - **Hot-path latency and a new per-request dependency.** Each selected external stage makes a synchronous call and blocks on its reply, so middleware latency becomes request latency and the service becomes a new failure surface on the data plane. This is bounded by opt-in host selectors, per-middleware timeouts, and built-ins running in-process with no network hop, but for matching traffic the tax is unavoidable. - **Fail-closed breaks workloads.** Denying traffic when a required middleware is unavailable, times out, or returns a malformed response is the safe default, but it converts a middleware outage into a sandbox outage. The opposite default leaks the very content the middleware exists to control. There is no choice that is both safe and always available; `on_error` makes the tradeoff explicit per middleware, but operators can still pick a default that surprises them. -- **Body buffering and size limits.** Inspecting content means buffering a bounded request body instead of streaming it, which adds memory cost and interacts badly with growing payloads (for example inference requests whose context expands each turn until it exceeds the cap). An over-cap request is treated as an `on_error` event for the middleware that needs the body, so it follows the same `fail_closed` default: it is denied unless the operator has explicitly set `on_error: fail_open` for that middleware. Passing an over-cap request through unprocessed is therefore never the default - it is an opt-in choice made per middleware, and one a security-critical middleware would deliberately leave off so that oversized content is denied rather than silently egressed. +- **Storage and size limits.** Whole-body inspection still buffers a bounded body. Streaming reduces protobuf message and relay-memory pressure but does not remove finite limits. Owned transformations spool input and output and can consume substantial disk; they therefore have a 1 GiB bound and require `fail_closed` because the original cannot be replayed after ownership transfer. Operators must size storage and middleware capacity for selected traffic. - **No OpenShell-side rate limiting.** OpenShell bounds concurrent middleware work and buffered memory, but does not throttle fast calls. A middleware that is slow, overloaded, or unavailable is handled by admission backpressure, its timeout, and `on_error`, so operators must still size, scale, and protect the service. - **Trusting an unsandboxed service with raw content.** Middleware receives raw request payloads, and OpenShell does not sandbox it, verify its behavior, or prevent it from mishandling or exfiltrating what it inspects. A buggy or malicious middleware is a direct data-exposure path. Trust in the middleware is the operator's responsibility, the same as trust in a sandbox image, but the blast radius here is in-flight request content. - **A false sense of coverage.** The hook runs only on traffic OpenShell terminates and parses. Opaque TCP or TLS passthrough, encrypted or otherwise opaque bodies, endpoints outside every selector, and content the middleware fails to detect can still leave without effective inspection. Policy validation rejects selector overlap with `tls: skip`, and runtime uninspectability follows the matching chain's failure policy, but detection correctness and traffic outside the selected host set remain inherent limitations. -- **Phase 1 plaintext is risky.** The research-preview exception permits plaintext gRPC only with explicit `allow_insecure = true`. Because middleware can allow, deny, or transform egress, an impersonated or eavesdropped service is a policy-enforcement bypass, not just an observability gap. The exception is unsuitable for shared or untrusted networks, produces an explicit warning and audit event, and is removed in phase 2. See [appendices/protocol-extensions.md](appendices/protocol-extensions.md#middleware-authentication). +- **Phase 1 plaintext is risky.** The research-preview exception permits plaintext gRPC only with explicit `allow_insecure_transport = true`. Because middleware can allow, deny, or transform egress, an impersonated or eavesdropped service is a policy-enforcement bypass, not just an observability gap. The exception is unsuitable for shared or untrusted networks, produces an explicit warning and audit event, and is removed in phase 2. - **Added surface to build, version, and maintain.** A new gRPC contract, policy schema, gateway configuration table, and manifest handshake are all long-lived surfaces with compatibility obligations, and middleware chains add ordering semantics operators must reason about. The research-preview framing keeps the contract provisional for now, but the long-term maintenance cost is real and is the main argument for keeping v1 deliberately small. The cost of *not* doing this is leaving content-level egress control entirely outside OpenShell: operators who need to redact, block, or annotate outbound content based on what it contains would have to build bespoke proxies around the sandbox, losing the policy integration, audit, and trust boundary the supervisor already provides. @@ -495,7 +517,7 @@ The cost of *not* doing this is leaving content-level egress control entirely ou ## Alternatives - **Build content checks into OpenShell directly.** A fixed, built-in set of DLP/redaction rules avoids a contract and an external service. Rejected as the primary model: OpenShell cannot embed every useful detection and transformation approach, and a stable contract lets dedicated tools and research scanners iterate without changing OpenShell. First-party built-in middleware still ships for narrow cases, over the same contract. -- **REST instead of gRPC.** A REST/JSON hook is simpler to call, and with OpenAPI it could still offer a manifest handshake and a typed contract. Rejected because gRPC's typing is stronger, OpenShell already uses gRPC across its service contracts, and gRPC leaves room for future streaming operations if large or incremental payload processing becomes necessary. Staying on a single toolchain avoids a second RPC stack to build, secure, and maintain. +- **REST instead of gRPC.** A REST/JSON hook is simpler to call, and with OpenAPI it could still offer a manifest handshake and a typed contract. Rejected because gRPC's typing and bidirectional streaming support match the lifecycle, and OpenShell already uses gRPC across its service contracts. Staying on a single toolchain avoids a second RPC stack to build, secure, and maintain. - **Other deployment modes (WASM, sidecar, in-sandbox).** In-process WASM filters or sidecars avoid a network hop and can tighten the trust boundary. Deferred rather than rejected: v1 supports native built-ins and statically registered external services, while other shapes remain open. See [appendices/deployment-options.md](appendices/deployment-options.md). - **Doing nothing.** The cost of declining is covered at the end of Risks: content-level egress control stays outside OpenShell, and operators must build bespoke proxies that lose the policy integration, audit, and trust boundary the supervisor already provides. @@ -503,9 +525,9 @@ The cost of *not* doing this is leaving content-level egress control entirely ou Calling an external service from a proxy to inspect, transform, or block in-flight traffic is well-established. The closest analogs: -- **Envoy `ext_proc` (External Processing).** The primary model for this RFC. Envoy streams request headers and body to an external gRPC service that can mutate the body (for example redaction), allow, or deny, and the proxy and the processing service scale independently. Our `HttpRequest/pre_credentials` hook is a buffered, single-hook v1 of the same boundary; if OpenShell later needs `ext_proc`-style streaming, it should add a separate streaming operation. +- **Envoy `ext_proc` (External Processing).** The primary model for this RFC. Envoy streams request headers and body to an external gRPC service that can mutate the body (for example redaction), allow, or deny, and the proxy and the processing service scale independently. `HTTP_REQUEST/PRE_CREDENTIALS` follows the same event-oriented boundary while adding OpenShell-specific modes for whole bodies, lockstep units, and ownership transfer. - **Envoy `ext_authz` (External Authorization).** A narrower sibling: an external service returns an allow/deny decision per request. It validates the "delegate the per-request decision to an external service in the hot path" pattern, without the content-transformation half that this RFC needs. -- **ICAP (RFC 3507).** HTTP proxies offload content adaptation, virus scanning, DLP, and content filtering to external ICAP servers that can modify or block request/response content. It is the closest *functional* precedent for content-aware egress control. Two details map directly onto our design: ICAP supports **pipelining** multiple servers (our middleware chain) and a **content preview** of the first bytes before full processing (our bounded-body buffering). What we avoid is its dated, text-based wire protocol; gRPC gives us typed contracts and room for future operation-specific streaming if we need it. +- **ICAP (RFC 3507).** HTTP proxies offload content adaptation, virus scanning, DLP, and content filtering to external ICAP servers that can modify or block request/response content. It is the closest *functional* precedent for content-aware egress control. ICAP's pipelining and preview concepts map to our ordered chain and preflight. We avoid its dated text protocol; gRPC provides typed event streams and explicit ownership accounting. - **HashiCorp `go-plugin` (Terraform, Vault).** Third-party plugins run as separate processes and communicate with the core exclusively over gRPC. It shows a strictly typed gRPC contract is a robust way to manage cross-language third-party extensions, which informs our registration plus manifest handshake (`Describe`, `ValidateConfig`). - **Kubernetes CSI / KMS.** Vendor-specific integrations are offloaded to external gRPC services rather than compiled into the core. Same "core defines the contract; integrators implement it out-of-process" split we use for middleware. - **Proxy-Wasm (Envoy/Istio Wasm filters).** In-process WebAssembly extensions with strong default-deny sandboxing and no IPC latency. Relevant to the future WASM deployment mode (see the deployment-options appendix); set aside for v1 because it is currently weak for GPU-backed or memory-heavy semantic guards. @@ -519,23 +541,23 @@ This section closes the current review themes. > **Update in PR #2477 - WebSocket middleware:** The operation-scope and failure-behavior decisions below now include WebSocket bindings, client text messages, binary pass-through, and capability coverage. - **Middleware naming.** Use the feature name "supervisor middleware." The first operation family is egress middleware, but the higher-level feature name stays extensible for future supervisor hooks. The service can inspect, transform, deny, and annotate, so narrower names such as "transformer" or "request processor" describe only part of the contract. -- **Middleware binding IDs.** Services own stable binding IDs and policy selects them through the `middleware` field. Gateway registration names are diagnostic only. Binding IDs use `/` for namespaces, `openshell/` is reserved for built-ins, and empty path segments are invalid. +- **Middleware names.** Policy selects built-ins or operator-owned external registrations through the `middleware` field. The service manifest name is diagnostic. Names use `/` for namespaces, `openshell/` is reserved for built-ins, and empty path segments are invalid. - **Operation naming.** Use typed operation and phase enums such as `HTTP_REQUEST/PRE_CREDENTIALS`. The operation describes the middleware API payload, and the phase describes the proxy position. Later protocols can add typed operations such as WebSocket message or TCP connect without renaming the v1 hook. - **Operation scope of v1.** `HTTP_REQUEST/PRE_CREDENTIALS` applies to every HTTP/1.x request that OpenShell terminates and parses, whether or not the endpoint declares a `protocol`; WebSocket upgrade requests are included. `WEBSOCKET_MESSAGE/PRE_CREDENTIALS` applies only to complete client-to-upstream text messages for attachments whose manifest advertises it. Binary and return-path messages, HTTP/2, HTTP/3, opaque TCP, and `tls: skip` traffic are excluded from those operation bindings. - **Route selection and forwarding.** V1 has no `forward_to` decision. Middleware never makes the upstream call. Future route-selection hooks may choose among OpenShell-managed destinations, such as model routes, but must not become arbitrary external endpoint rewrites. -- **SigV4/request signing.** AWS SigV4 belongs to a restricted built-in `HttpRequest/post_credentials` hook, not external `HttpRequest/pre_credentials` middleware. The middleware can be configured by policy, but it must run in-process with supervisor host capabilities so it can strip placeholder signatures and sign with real supervisor-resolved credentials without exposing those credentials over the external middleware contract. +- **SigV4/request signing.** AWS SigV4 belongs to a restricted built-in `HTTP_REQUEST/POST_CREDENTIALS` hook, not external `HTTP_REQUEST/PRE_CREDENTIALS` middleware. Endpoint policy synthesizes the stage, but it must run in-process with supervisor host capabilities so it can strip placeholder signatures and sign with real supervisor-resolved credentials without exposing those credentials over the external middleware contract. - **Composability and ordering.** Middleware is chainable and ordered by ascending numeric `order`. Order values must be unique across the policy. A stage receives the previous stage's transformed body and header mutations; `deny` short-circuits the chain; and different config map keys may invoke the same binding as separate stages. -- **Header mutation.** Headers preserve duplicates and wire order. External writes are limited to `x-openshell-middleware-*` and support append, overwrite, or skip. Removes may target other visible headers. Credential-bearing, routing, framing, hop-by-hop, and `Connection`-nominated headers remain protected. Each stage's mutations are atomic. -- **Finding shape.** Findings never include matched values or raw content. Built-ins may provide contract-defined audit-safe labels. Operator-run text and metadata are untrusted and are replaced or omitted in security outputs in favor of validated binding IDs, platform labels, and aggregate counts. +- **Header mutation.** Headers preserve duplicates and wire order. External writes and removes may target middleware-visible end-to-end headers and writes support append, overwrite, or skip. Credential-bearing, routing, framing, hop-by-hop, `Connection`-nominated, and OpenShell credential headers remain protected. Each stage's mutations are atomic. +- **Finding shape.** Findings never include matched values or raw content. Built-ins may provide contract-defined audit-safe labels. Operator-run text and metadata are untrusted and are replaced or omitted in security outputs in favor of validated implementation names, platform labels, and aggregate counts. - **Actor data.** Actor process data is optional and per-connection. Middleware must treat it as context, not a reliable per-request identity or authorization input. - **Metadata namespacing.** Metadata is stored under the policy-local middleware config map key rather than the optional human-readable name. This prevents collisions without a central key registry and lets two configs using the same implementation emit independent metadata. - **Selector-only placement.** V1 uses only config-level `endpoints.include` and `endpoints.exclude` selectors. Policy-level and endpoint-level attachment lists are not part of the schema. Selection is independent of the network rule that admitted the request and therefore remains stable after effective-policy composition. - **Failure behavior.** Middleware errors, timeouts, malformed responses, and over-cap inspectable payloads use `on_error` after an operation binding is selected; `fail_closed` is the default. An absent operation binding and binary WebSocket messages are capability coverage states, not failures, and pass with informational telemetry under both error modes. -- **Limits.** V1 caps policies at 10 middleware configs, selectors at 32 combined patterns per config, bodies at 4 MiB, findings at 32 per stage, and all non-body request and result fields at the public envelope limits in the contract section. +- **Limits.** V1 caps policies at 10 middleware configs, selectors at 32 combined patterns per config, complete buffered bodies and advertised units at 4 MiB, request stream units at 64 KiB, owned deferred input and output at 1 GiB each, findings at 32 per stage, and all non-body fields at the public envelope limits in the contract section. - **Delivery and reload.** `GetSandboxConfig` delivers only external registrations required by the effective policy. Built-ins are installed locally. Supervisors prepare candidate policy and registry state off-path, swap them as one generation, reuse connections for policy-only changes, and preserve the complete last-known-good runtime on failure. -- **Chunked and compressed bodies.** V1 operates on bounded bytes OpenShell can buffer safely. Known over-cap content length may fail open before consumption when every affected stage permits it. Chunked overflow after consumption is denied because the raw stream cannot be resumed. Compressed bodies remain opaque unless a binding explicitly supports them. +- **Chunked and compressed bodies.** V1 normalizes fixed and chunked HTTP/1 request bodies into bounded units and preserves validated trailers. Whole-body mode remains bounded by the stage limit. Owned mode permits larger finite transformations only under fail-closed ownership and deferred-storage limits. Compressed bodies remain opaque unless a binding explicitly supports them. - **Post-transformation enforcement.** Every body replacement is re-evaluated by body-aware GraphQL, JSON-RPC, or MCP policy before the next stage or upstream. An enforced denial blocks. In audit mode, a denial is logged, the remaining chain stops, and the transformed request is forwarded. Evaluation failure or an unparseable replacement is a hard denial. Middleware deny and `fail_closed` remain blocking regardless of endpoint audit mode. -- **Trust boundary and phases.** Stable external middleware transport requires confidentiality and service authentication. Phase 1 may temporarily allow plaintext only with explicit `allow_insecure = true`, a warning, and an audit event in trusted local or isolated research environments. Phase 2 removes plaintext and `allow_insecure` as an intentional research-preview breaking change. +- **Trust boundary and phases.** Stable external middleware transport requires confidentiality and service authentication. Phase 1 may temporarily allow plaintext only with explicit `allow_insecure_transport = true`, a warning, and an audit event in trusted local or isolated research environments. Phase 2 removes plaintext and `allow_insecure_transport` as an intentional research-preview breaking change. - **Multitenancy.** OpenShell controls middleware application through policy selection. A middleware may receive sandbox and policy context for audit, but OpenShell does not define a middleware-owned tenant grouping model in v1. - **API maturity qualifier.** Use `openshell.middleware.v1`, not `v1alpha1`. The project is already alpha-stage; the RFC labels this contract as a research preview, so an additional per-contract alpha package adds little. diff --git a/rfc/0009-supervisor-middleware/appendices/extension-authentication.md b/rfc/0009-supervisor-middleware/appendices/extension-authentication.md index d73a7aa9b..bb03a748b 100644 --- a/rfc/0009-supervisor-middleware/appendices/extension-authentication.md +++ b/rfc/0009-supervisor-middleware/appendices/extension-authentication.md @@ -10,7 +10,7 @@ Related: [protocol-extensions.md](protocol-extensions.md#middleware-authenticati Transport is HTTPS with either platform trust roots or an operator-provided CA bundle, with normal certificate and endpoint-hostname verification. A middleware endpoint must be reachable from every sandbox supervisor as well as the gateway, so a gateway-local Unix socket is not an option for this mechanism. -Caller identity is a short-lived Ed25519 JWT minted by the gateway's existing sandbox signing authority. The gateway attaches one to its own `Describe` and `ValidateConfig` calls; sandbox supervisors attach one to `Describe` and `EvaluateHttpRequest`. Both directions of the RFC's stated requirement are covered: TLS and the configured trust roots authenticate the middleware service to OpenShell, and the exact-audience JWT proves to the middleware that a gateway or a policy-authorized sandbox supervisor made the call. +Caller identity is a short-lived Ed25519 JWT minted by the gateway's existing sandbox signing authority. The gateway attaches one to its own `Describe` and `ValidateConfig` calls; sandbox supervisors attach one to `Describe` and operation-specific stream RPCs such as `HttpRequestPreCredentials.Evaluate`. Both directions of the RFC's stated requirement are covered: TLS and the configured trust roots authenticate the middleware service to OpenShell, and the exact-audience JWT proves to the middleware that a gateway or a policy-authorized sandbox supervisor made the call. ## Claim contract diff --git a/rfc/0009-supervisor-middleware/appendices/protocol-extensions.md b/rfc/0009-supervisor-middleware/appendices/protocol-extensions.md index 63115d9e5..8ca74362c 100644 --- a/rfc/0009-supervisor-middleware/appendices/protocol-extensions.md +++ b/rfc/0009-supervisor-middleware/appendices/protocol-extensions.md @@ -2,65 +2,55 @@ > This is an appendix to the [RFC](../README.md). Please familiarize yourself with the RFC before reading this. -**Update in PR #2477 - WebSocket middleware:** V1 now includes a forward-text WebSocket operation. The updated text below separates this implemented operation from future HTTP streaming and WebSocket return-path operations. +V1 includes event-oriented HTTP request and response streams plus a forward-text WebSocket operation. This appendix records remaining extensions the protocol should not preclude. -The v1 contract is intentionally minimal: one buffered unary HTTP request hook and one forward-text WebSocket message hook, each with an `allow`/`deny` decision plus optional transformed content, findings, and metadata. This appendix records extensions the proto should not preclude, so v1 stays small without painting future work into a corner. None of these are committed; they exist to validate that the v1 shape is forward-compatible. +## Request streaming -## Streaming - -The v1 `EvaluateHttpRequest` RPC is unary. The supervisor buffers the bounded request body, sends one `HttpRequestEvaluation`, and receives one `HttpRequestResult`. Streaming is deliberately left out of that method: if OpenShell later needs chunked payload transport or incremental processing, it should add a separate operation-specific method rather than changing `EvaluateHttpRequest` cardinality. - -This section records what such a future streaming operation would need to consider, and importantly what streaming does and does not buy, since the distinction is easy to get wrong. +`HttpRequestPreCredentials.Evaluate` is bidirectional streaming. Preflight selects header-only, whole-body, lockstep stream, or owned-stream processing. OpenShell normalizes HTTP/1 fixed and chunked bodies into bounded units, carries validated trailers as a separate event, and rebuilds transport framing after evaluation. ### Transport streaming vs processing streaming These are different concepts and are easy to conflate: -- **Transport streaming** - a separate gRPC operation carries multiple messages (chunks). This is what a service would advertise in its manifest and what the supervisor would negotiate. +- **Transport streaming** - the gRPC operation carries multiple bounded messages. Every body-aware stage uses this transport. - **Processing streaming** - the middleware can act on partial content before it has the whole body. -The manifest field would govern only the transport. It would not promise the middleware can process incrementally. +Selecting a stream mode governs processing semantics; using the streaming RPC alone does not promise incremental processing. ### Full-body guards still buffer -Many guards need the entire body to do anything: a JSON-aware redactor must parse the whole document, and a PII scan must see all of it. Such a guard, even over a streaming transport, accumulates every chunk internally, then parses, then emits a single response at end-of-stream - the decision still arrives after the last byte. Incremental processing only helps narrower cases such as byte-level regex redaction or secret scanning over a text stream. +Many guards need the entire body to do anything: a JSON-aware redactor must parse the whole document, and a PII scan may need all of it. Such a guard selects `WHOLE_BODY_BYTES` when the body fits the advertised limit. A transformation such as Git signing that needs complete input larger than one protobuf message selects fail-closed `OWNED_STREAM_BYTES`, spools accepted units, and emits output only after final input. Incremental guards select `STREAM_BYTES` and account for each unit without retaining input across results. -### Why add a streaming operation later +### What streaming provides -Even when the middleware must buffer the full body, a separate chunked transport operation would buy two things: +The request stream provides two important properties: -- It moves the large buffer off the supervisor. The supervisor does not hold a multi-MB body to put in a single message; the middleware, which needs it anyway and can be resourced for it, accumulates it. -- It avoids gRPC's per-message size limit (4 MB by default). A 20 MB inference request cannot fit in one message without raising limits, but it can be chunked. +- It removes the requirement that a complete request fit in one gRPC message. OpenShell caps request units at 64 KiB. +- Owned mode lets the service spool a larger finite transformation without OpenShell retaining a replay copy. Input and output are each bounded at 1 GiB and failure is always closed after ownership begins. -This is the strongest reason to keep the door open for a streaming operation, more so than incremental parsing. +For a chain made only of `STREAM_BYTES` stages, OpenShell may forward each approved unit upstream before the request ends. A later denial or failure terminates that upload but cannot retract prior bytes, so the relay never retries or replays it. Whole-body and owned stages, body-aware policy re-evaluation, request-body credential rewriting, and body-dependent signing retain a hold barrier. OpenShell rebuilds framing for both paths and can stop a live upload when the upstream responds early. -### How it would work - -A service would advertise chunked-transport support (and limits) in `Describe`. When supported, the supervisor could use the streaming operation and send the body as a sequence of messages. When not supported, it would continue to use the unary v1 operation, and a body over the unary cap would use the middleware config's `on_error` behavior. - -The streaming method should have its own messages instead of reusing `HttpRequestEvaluation` directly. Within a single streamed request, the first message would carry the request context plus the first body bytes, and subsequent messages would carry only further body chunks that the middleware appends; stream close would mark end of request. This keeps the v1 unary messages flat and gives streaming its own cleaner shape. - -A cleaner phased design using a `oneof` over `context` and `body_chunk`, in the style of Envoy `ext_proc`, is available for a future streaming operation because it would not need to preserve the unary v1 message shape. V1 keeps the flat unary request because it is simpler for bounded bodies and avoids making every middleware implement streaming mechanics before the need is proven. +The state machine requires one preflight, contiguous input sequences, one result for each input unit, an optional owned-output phase with contiguous sequences and exact final accounting, one trailers exchange for active body stages, and a terminal notification. Invalid modes, sequences, actions, diagnostics, or finalization follow `on_error`, except that owned stages cannot fail open. ## Additional operation phases > **Update in PR #2477 - WebSocket middleware:** This section now records `WEBSOCKET_MESSAGE/PRE_CREDENTIALS` as implemented. It keeps `WEBSOCKET_MESSAGE/PRE_RETURN` as a reserved future operation. -V1 supports `HTTP_REQUEST/PRE_CREDENTIALS` and forward-text `WEBSOCKET_MESSAGE/PRE_CREDENTIALS`. The same service interface can host more operations, each advertised through the `Describe` manifest and invoked through an operation-specific method. Each operation and phase pair encodes a different position in the proxy flow: +V1 supports `HTTP_REQUEST/PRE_CREDENTIALS`, `HTTP_RESPONSE/PRE_RETURN`, and forward-text `WEBSOCKET_MESSAGE/PRE_CREDENTIALS`. It also implements restricted built-in-only `HTTP_REQUEST/POST_CREDENTIALS`. Each operation and phase pair encodes a different position in the proxy flow: - `Connection/before_policy` / `HttpRequest/before_policy` - *before* network/L7 policy admits the request, for earlier classification. Riskier, because request content reaches a service before policy has allowed the request. - `HTTP_REQUEST/PRE_CREDENTIALS` (v1) - after policy admits the request, before credential injection. -- `HttpRequest/post_credentials` - after credential injection, immediately before the relay writes the request upstream. This hook is credential-visible, so it is built-in-only: OpenShell marks it as a restricted hook and rejects any externally registered middleware that advertises it during manifest validation. The motivating use is request signing that must run after credentials are injected - for example a built-in `openshell/sigv4` that strips placeholder-signed AWS headers and signs the finalized request with supervisor-resolved credentials just before it is sent upstream. +- `HTTP_REQUEST/POST_CREDENTIALS` (v1 restricted) - after credential resolution, immediately before the relay writes the request upstream. This hook is credential-visible, so it is built-in-only: OpenShell rejects any externally registered middleware that advertises it. `openshell/sigv4` strips placeholder-signed AWS headers and signs the finalized request with supervisor-resolved credentials. - `HttpResponse/completed` - after an upstream request completes, emit metadata such as status, content length, selected route, selected model, and model usage if available. This is notification-only: no body, no transformation, and no allow/deny verdict. It would let reservation-style budget middleware reconcile a pre-dispatch decision without introducing response-body inspection. -- `HttpResponse/before_return` - on the return path, after the upstream responds and before the response reaches the sandbox; inspect or redact upstream responses. +- `HTTP_RESPONSE/PRE_RETURN` (v1) - on the return path, after the upstream responds and before the response reaches the sandbox; inspect, redact, or block upstream responses. - `WEBSOCKET_MESSAGE/PRE_CREDENTIALS` (v1 forward text) - after a WebSocket upgrade, on each complete client text message before credential placeholder rewriting. Before upstream contact, a concurrent preflight lets each selected stage inspect, voluntarily skip, or authoritatively deny the upgrade. Explicit denial takes precedence over failures and is enforced independently of `on_error`; OpenShell best-effort ends every still-writable opened stage stream with the typed terminal reason. An attached implementation without this binding is not selected and records coverage rather than applying `on_error`. Binary messages pass without inspection, consume a logical sequence, and record unsupported-message coverage for active stages. - `WEBSOCKET_MESSAGE/PRE_RETURN` - on complete upstream messages before they return to the workload. The enum value is reserved, but manifests advertising it are rejected until return-path inspection is implemented. -Pre-policy phases run earliest, the two request phases bracket credential injection, response notifications and response phases run after the upstream call, and message phases run later on the parsed relay. V1 implements only the two pre-credentials pairs above. `HttpRequest/post_credentials` is the nearest planned request-path follow-up and is kept built-in-only because it sees injected credentials; `HttpResponse/completed` is a separate future notification hook for metadata-only post-call reconciliation. +Pre-policy phases would run earliest, the two request phases bracket credential resolution, response phases run after the upstream call, and message phases run later on the parsed relay. `HttpResponse/completed` remains a future metadata-only notification hook. ## Semantic context -v1 sends the full request and lets the middleware interpret it. A future version can carry parsed semantic context (request category, semantic protocol such as OpenAI chat completions or Anthropic messages, and modalities) on `HttpRequestEvaluation`, and let policy target a semantic scope (latest user message, image parts, tool inputs). This also requires corresponding manifest fields so OpenShell can validate that a policy only references scopes and protocols the service supports. +V1 sends normalized request bytes and lets the middleware interpret them. A future version can carry parsed semantic context (request category, semantic protocol such as OpenAI chat completions or Anthropic messages, and modalities) on request preflight, and let policy target a semantic scope (latest user message, image parts, tool inputs). This also requires corresponding manifest fields so OpenShell can validate that a policy only references scopes and protocols the service supports. ## Content preview @@ -68,23 +58,23 @@ ICAP-style previewing: send only the first N bytes so the service can decide whe ## Portable feature contracts and binding -A future version can introduce named feature contracts, such as `pii-redaction`, with a mapping from that portable contract to a concrete service binding. Policy would then stay portable across interchangeable implementations. V1 references a service-owned binding ID directly and defers this additional indirection. +A future version can introduce named feature contracts, such as `pii-redaction`, with a mapping from that portable contract to a concrete registered implementation. Policy would then stay portable across interchangeable implementations. V1 references a built-in or operator-owned registration name directly and defers this additional indirection. ## Header mutation rules -V1 preserves duplicate request headers and their wire order. Before an external invocation, OpenShell omits credential-bearing, routing, framing, hop-by-hop, and `Connection`-nominated headers. Results return ordered `write` and `remove` mutations. Writes support append, overwrite, and skip modes but may target only `x-openshell-middleware-*`. Removes may target other visible headers except the protected categories. OpenShell validates and applies a stage's mutations atomically, so one invalid mutation discards the whole set and follows that config's `on_error` behavior. +V1 preserves duplicate request headers and their wire order. Before an external invocation, OpenShell omits credential-bearing, routing, framing, hop-by-hop, and `Connection`-nominated headers. Results return ordered `write` and `remove` mutations. Writes support append, overwrite, and skip modes. Mutations may target middleware-visible end-to-end headers except the protected categories. OpenShell validates and applies a stage's mutations atomically, so one invalid mutation discards the whole set and follows that config's `on_error` behavior. ## Middleware authentication Supervisor middleware exposes gRPC services over network endpoints. The stable transport contract requires confidentiality and authentication of the intended middleware service. Endpoint declaration, identity binding, credential material, and rotation must be explicit rather than left as deployment-specific conventions. -Phase 1 may temporarily support unauthenticated plaintext gRPC only when the operator explicitly sets `allow_insecure = true` on the middleware entry. A plaintext `http://` endpoint without this opt-in is rejected. OpenShell emits a prominent warning and records auditable configuration state whenever the exception is enabled, so insecure operation is always deliberate and visible. +Phase 1 may temporarily support unauthenticated plaintext gRPC only when the operator explicitly sets `allow_insecure_transport = true` on the middleware entry. A plaintext `http://` endpoint without this opt-in is rejected. OpenShell emits a prominent warning and records auditable configuration state whenever the exception is enabled, so insecure operation is always deliberate and visible. This mode is suitable only for trusted local development, loopback services, or isolated research environments where the middleware endpoint is not reachable by untrusted clients. It is not suitable for shared clusters, multi-tenant deployments, public networks, or any environment where inspected request content needs transport confidentiality. Without middleware authentication and transport security, network observers can read inspected request content, active attackers can impersonate the middleware service, and unauthorized clients can call the middleware directly if it is reachable. Because the middleware can allow, deny, or transform egress, service impersonation is a policy-enforcement bypass, not just an observability risk. -Phase 2 removes plaintext endpoint support and removes `allow_insecure`. Every external middleware connection must then provide authenticated encrypted transport. This is an intentional research-preview breaking change, so phase 1 plaintext configurations have no long-term compatibility guarantee and must migrate before phase 2. +Phase 2 removes plaintext endpoint support and removes `allow_insecure_transport`. Every external middleware connection must then provide authenticated encrypted transport. This is an intentional research-preview breaking change, so phase 1 plaintext configurations have no long-term compatibility guarantee and must migrate before phase 2. The exact phase 2 mechanism is deferred. Follow-up protocol work should choose and specify mTLS, TLS plus explicit caller authentication, or an equivalent design, including trust roots, client identity, credential delivery, certificate or key rotation, middleware identity binding, and how supervisors receive authentication material. diff --git a/skills/debug-openshell-cluster/references/supervisor-middleware.md b/skills/debug-openshell-cluster/references/supervisor-middleware.md index a1d752466..cbaab71a7 100644 --- a/skills/debug-openshell-cluster/references/supervisor-middleware.md +++ b/skills/debug-openshell-cluster/references/supervisor-middleware.md @@ -20,7 +20,7 @@ openshell logs --tail --source sandbox ## Startup and authentication -The middleware service must start before the gateway and be reachable from both the gateway and sandbox supervisors. Gateway startup fails if `Describe` is unavailable, a manifest exposes duplicate operation/phase bindings, the registration claims the reserved `openshell/` namespace, or payload and timeout limits are invalid. Supported V1 bindings are `HTTP_REQUEST/PRE_CREDENTIALS`, `HTTP_RESPONSE/PRE_RETURN`, and `WEBSOCKET_MESSAGE/PRE_CREDENTIALS`. +The middleware service must start before the gateway and be reachable from both the gateway and sandbox supervisors. Gateway startup fails if `Describe` is unavailable, a manifest exposes duplicate operation/phase bindings, the registration claims the reserved `openshell/` namespace, or payload and timeout limits are invalid. Supported external V1 bindings are `HTTP_REQUEST/PRE_CREDENTIALS`, `HTTP_RESPONSE/PRE_RETURN`, and `WEBSOCKET_MESSAGE/PRE_CREDENTIALS`. External manifests advertising `POST_CREDENTIALS` are rejected. `openshell/sigv4` is synthesized internally from endpoint credential-signing fields and must not appear in `network_middlewares`. When gateway JWT signing is disabled, supervisors preserve the legacy unauthenticated connector and do not request extension credentials. When signing is enabled, credential acquisition and verification failures are fail closed: check HTTPS trust and hostname validation, audience and issuer agreement, the token `kid`, gateway `RefreshSandboxToken` errors, and middleware logs. Changing a registration requires a gateway restart. A policy update can also fail before persistence if the selected implementation rejects its `network_middlewares` config. @@ -30,6 +30,8 @@ For response failures, distinguish a deliberate `middleware_denied` decision fro ## Request and WebSocket failures +For request streams, confirm the service receives preflight, contiguous body sequences, one trailers event, and one best-effort terminal event. Whole-body mode receives the complete body in its final unit. `STREAM_BYTES` and `OWNED_STREAM_BYTES` receive nonempty units of at most 64 KiB followed by one empty unit with `end_of_stream`. Owned mode is available only to `fail_closed` stages; the stage must acknowledge every input, emit contiguous output, and return exact final input/output accounting. Deferred input and output are each capped at 1 GiB. A pure `STREAM_BYTES` chain can forward approved units upstream immediately; a later denial terminates the upload without replay. Whole-body, owned, policy re-evaluation, body credential rewrite, and body-signing paths spool first. Request body receipt, evaluation, and output delivery share a fixed 120-second wall-clock deadline; expiry cancels the stage and terminates the request. + At request time, distinguish attachment, binding selection, coverage, denial, and failure. A host-matched HTTP-only attachment can inspect the upgrade GET but does not join the WebSocket chain; the connection proceeds under either `on_error` mode and emits `binding_not_selected` coverage. A selected WebSocket stage receives text messages only. Binary messages pass under both modes, emit `unsupported_message_type` coverage, and consume a session sequence without an RPC. An explicit `middleware_denied` result is always enforced. WebSocket preflight returns `INSPECT`, voluntary `SKIP`, or authoritative `DENY`; `DENY` rejects the upgrade before upstream contact under both `on_error` modes. A selected-stage failure follows the policy-local `on_error`: `fail_closed` blocks the HTTP request or closes the WebSocket, while `fail_open` bypasses only that stage and emits a detection finding. A fail-open per-message capacity failure bypasses that message without disabling the stage. A timeout, transport failure, stream closure, missing or invalid response, duplicate or regressed sequence, or other failure that makes an established WebSocket stream unreliable disables that stage for later messages on the connection and emits `openshell.middleware.websocket_stage_disabled`. @@ -45,7 +47,10 @@ WebSocket message sequences are allocated session-wide; each stage receives a st | Authenticated middleware rejects gateway calls | Private CA or hostname mismatch, expected audience or issuer mismatch, stale/unknown `kid`, or malformed extension token | `tls_ca_cert_path`, registration `audience`, service verifier config and logs; fetch well-known metadata only through the already-trusted gateway TLS endpoint | | Gateway fails after registering supervisor middleware | Service unavailable, invalid manifest, duplicate binding, reserved name, or invalid payload/timeout limit | Middleware service and gateway logs; `[[openshell.supervisor.middleware]]`; `Describe` response | | Policy update rejects `network_middlewares` | Unknown middleware name, implementation-owned config invalid, duplicate order, broad/invalid host selector, or fail-closed coverage of `tls: skip` | Policy error, gateway logs, middleware `ValidateConfig`, selector and order fields | +| Policy names `openshell/sigv4` in `network_middlewares` or an external service advertises `POST_CREDENTIALS` | SigV4 is an endpoint-synthesized trusted stage; credential-visible phases are unavailable to external middleware | Remove the attachment; set endpoint `credential_signing`, `signing_service`, and `signing_region`; inspect provider endpoint bindings | | HTTP request returns `middleware_failed` or `middleware_denied`, or WebSocket closes with `1008` | Selected stage failed or explicitly denied admitted traffic | Sandbox OCSF logs; policy-local middleware config; service availability; binding operation; `on_error` | +| Large Git push fails after ownership transfer | Owned stage used fail-open, exceeded its 1 GiB deferred bound, returned noncontiguous output, or reported incorrect final accounting | Require `fail_closed`; inspect input/output sequences, `body_finalize`, spool capacity, and service logs | +| Request stalls and fails near 120 seconds | Client body receipt, middleware processing, or output delivery exceeded the fixed request deadline | Client upload progress; per-stage timeout logs; middleware and upstream backpressure; disk capacity and latency for withholding modes | | HTTP response becomes canonical `403 middleware_denied`, `502 response_delivery_failed`, or closes mid-body | Response middleware blocked, failed before commitment, or stopped delivery after commitment | Sandbox OCSF response middleware events; `HTTP_RESPONSE/PRE_RETURN` binding; `on_error`; `whole_body_accumulation_timeout`; service stream lifecycle | | WebSocket upgrades but a host-matched middleware receives no preflight or message RPC | The implementation did not advertise `WEBSOCKET_MESSAGE/PRE_CREDENTIALS` | `WEBSOCKET_MIDDLEWARE_COVERAGE state=binding_not_selected`; service `Describe`; the upgrade GET may still have used its HTTP binding | | Binary WebSocket message passes without a middleware RPC | Binary is unsupported by the V1 text-message binding under both `on_error` modes | `WEBSOCKET_MIDDLEWARE_COVERAGE state=unsupported_message_type`; the next text RPC may have a valid sequence gap | diff --git a/skills/generate-sandbox-policy/SKILL.md b/skills/generate-sandbox-policy/SKILL.md index 89e6223ac..44ccfe9bb 100644 --- a/skills/generate-sandbox-policy/SKILL.md +++ b/skills/generate-sandbox-policy/SKILL.md @@ -218,8 +218,10 @@ Is L7 inspection needed? Add `network_middlewares` only when the user asks to inspect, transform, redact, or independently authorize admitted HTTP requests, final HTTP responses, or client WebSocket text messages. Request middleware runs after network and L7 policy admission and before provider credential injection. Response middleware runs on the matching final response before it returns to the sandbox. - Use `openshell/regex` without gateway registration for fixed-pattern redaction of UTF-8 HTTP request bodies or complete client-to-upstream WebSocket text messages. +- Do not add `openshell/sigv4` to `network_middlewares`. Endpoint `credential_signing`, `signing_service`, and optional `signing_region` fields synthesize this trusted in-process `HTTP_REQUEST/POST_CREDENTIALS` stage after provider credentials resolve. External services cannot advertise that phase. - Use an operator-owned middleware name only when it is already registered under `[[openshell.supervisor.middleware]]` and reachable from both the gateway and sandbox supervisors. - Confirm that the implementation advertises the requested binding: `HTTP_REQUEST/PRE_CREDENTIALS`, `HTTP_RESPONSE/PRE_RETURN`, or `WEBSOCKET_MESSAGE/PRE_CREDENTIALS`. A host match alone does not enable inspection. +- Use `fail_closed` for an implementation that selects `OWNED_STREAM_BYTES`. Ownership removes the platform replay copy, so an owned-stage failure cannot fail open. Request stream units are at most 64 KiB; the advertised deferred input and output limit is 1 GiB each. - WebSocket middleware inspects client text messages only, over both `ws://` and `wss://`. Binary and upstream-to-client messages pass without inspection, even with `fail_closed`. - `on_error` controls selected-stage failures. Explicit denials always block traffic. A failed WebSocket stage with `fail_open` can remain bypassed for the rest of the connection. - Default `on_error` to `fail_closed`. Use `fail_open` only when bypassing the stage preserves the user's stated security requirement. @@ -386,6 +388,8 @@ Before presenting the policy to the user, verify correctness **and** flag breadt - [ ] No fail-closed middleware selector can cover a `tls: skip` endpoint - [ ] Any required WebSocket control advertises `WEBSOCKET_MESSAGE/PRE_CREDENTIALS`, and the user understands that V1 does not inspect binary messages - [ ] Any required response control advertises `HTTP_RESPONSE/PRE_RETURN` +- [ ] SigV4 uses endpoint credential-signing fields rather than a `network_middlewares` attachment, and the endpoint's provider binding covers the signed destination +- [ ] Any middleware that requires `OWNED_STREAM_BYTES` uses `on_error: fail_closed` - [ ] Endpoints contributed by a credentialed provider are not L4-only or `tls: skip` unless `allow_uninspected_credentials: true` explicitly records the exception ### Schema Warnings (log-only, but should be fixed) diff --git a/skills/openshell-cli/SKILL.md b/skills/openshell-cli/SKILL.md index 057874fa3..ce6754327 100644 --- a/skills/openshell-cli/SKILL.md +++ b/skills/openshell-cli/SKILL.md @@ -533,6 +533,8 @@ Edit `current-policy.yaml` to allow the blocked actions. **For policy content au `network_policies` and `network_middlewares` can be modified at runtime when the selected compute driver supports live policy updates. Use `--wait` to verify that the active runtime loaded the revision; do not infer enforcement from the gateway accepting the update. If `filesystem_policy`, `landlock`, or `process` need changes, the sandbox must be recreated. Built-in middleware such as `openshell/regex` needs no gateway registration. An operator-run middleware must already be registered under `[[openshell.supervisor.middleware]]`; changing that static registration requires a gateway restart. +`openshell/sigv4` is not a `network_middlewares` attachment. The endpoint's `credential_signing`, `signing_service`, and optional `signing_region` fields synthesize a trusted in-process `HTTP_REQUEST/POST_CREDENTIALS` stage after AWS credentials resolve. External middleware cannot advertise this credential-visible phase. Middleware that selects `OWNED_STREAM_BYTES`, such as a Git signing service, must use `fail_closed`; the original request is no longer replayable after ownership transfer. + Middleware can inspect HTTP requests, HTTP responses, or client WebSocket text messages when the implementation advertises the matching binding. The built-in `openshell/regex` supports request bodies and client WebSocket text messages. diff --git a/tasks/rust.toml b/tasks/rust.toml index 32928b8f6..df80185be 100644 --- a/tasks/rust.toml +++ b/tasks/rust.toml @@ -32,6 +32,7 @@ run = [ "cargo clippy --manifest-path e2e/rust/Cargo.toml --all-targets -- -D warnings", "cargo clippy --manifest-path examples/governance-interceptor/Cargo.toml --all-targets -- -D warnings", "cargo clippy --manifest-path examples/supervisor-middleware-content-guard/Cargo.toml --all-targets -- -D warnings", + "cargo clippy --manifest-path examples/supervisor-middleware-git-signing/Cargo.toml --all-targets -- -D warnings", ] run_windows = "powershell -NoProfile -ExecutionPolicy Bypass -File tasks/scripts/windows-msvc.ps1 lint native" hide = true @@ -43,6 +44,7 @@ run = [ "cargo fmt --manifest-path e2e/rust/Cargo.toml --all", "cargo fmt --manifest-path examples/governance-interceptor/Cargo.toml --all", "cargo fmt --manifest-path examples/supervisor-middleware-content-guard/Cargo.toml --all", + "cargo fmt --manifest-path examples/supervisor-middleware-git-signing/Cargo.toml --all", ] hide = true @@ -53,6 +55,7 @@ run = [ "cargo fmt --manifest-path e2e/rust/Cargo.toml --all -- --check", "cargo fmt --manifest-path examples/governance-interceptor/Cargo.toml --all -- --check", "cargo fmt --manifest-path examples/supervisor-middleware-content-guard/Cargo.toml --all -- --check", + "cargo fmt --manifest-path examples/supervisor-middleware-git-signing/Cargo.toml --all -- --check", ] hide = true diff --git a/tasks/test.toml b/tasks/test.toml index 2b0feb2bb..d19c1676d 100644 --- a/tasks/test.toml +++ b/tasks/test.toml @@ -86,6 +86,7 @@ run = [ "cargo test --workspace --exclude openshell-server", "cargo test -p openshell-server --features test-support", "cargo nextest run --config-file .config/nextest.toml --manifest-path examples/supervisor-middleware-content-guard/Cargo.toml", + "cargo nextest run --config-file .config/nextest.toml --manifest-path examples/supervisor-middleware-git-signing/Cargo.toml", ] run_windows = "powershell -NoProfile -ExecutionPolicy Bypass -File tasks/scripts/windows-msvc.ps1 test-precommit native" hide = true