chio-tower
Tower middleware for Chio capability validation and receipt signing over
HTTP, plus an independent Tower stack for dispatching tool-call requests
straight through the kernel. ChioLayer/ChioService wrap any
tower::Service that speaks http::Request/http::Response, including
Axum's axum::body::Body and other bytes-backed HTTP bodies; real
tonic::body::Body replay is not yet covered.
The middleware does not decide authorization itself: it extracts caller
identity, buffers and hashes the request body, and forwards evaluation to
chio-http-core's HttpAuthority (backed by chio-kernel). It owns the
edge-only trust logic around that call - see Responsibilities.
Responsibilities
- Wrap an inner service with
ChioLayer/ChioService: evaluate every request againstHttpAuthorityand attach a signedHttpReceiptto the response (x-chio-receipt-idheader and response extensions). - Extract caller identity from
Authorization: Bearer,X-Api-Key, orCookieheaders, hashing the secret before it becomes the identity subject; anonymous otherwise. - Buffer and SHA-256 hash request bodies up to a configurable cap
(
DEFAULT_MAX_BODY_BYTES, 8 MiB by default), streamed frame-by-frame so a misreportedsize_hintcannot exhaust memory, then replay the exact bytes to the inner service. - Detect duplicate
chio_capabilityquery parameters as ambiguous presentation and force a signed deny rather than trust a collapsed map. - Persist decision, final, and transport-deny receipts to a durable
chio_kernel::ReceiptStorewhen one is configured; fail the request closed if a configured store cannot record the receipt. - Dispatch tool-call requests directly through
chio_kernel::ChioKernelviaKernelService, with tracing, per-tenant concurrency limiting, load shedding, and timeout normalization (build_layered). - Expose the fixed WASM host-call metric-label vocabulary used for guard observability.
Public API
HTTP middleware:
ChioLayer::{new, new_ephemeral, builder, from_evaluator, with_max_body_bytes}tower::LayerproducingChioService<S>.
ChioService<S>,DEFAULT_MAX_BODY_BYTES- thetower::Serviceand its default body-size cap.ChioEvaluator::{new, new_ephemeral, builder, with_fail_open, with_identity_extractor, with_route_resolver},EvaluationResult- the evaluation coreChioLayerwraps.extract_identity,IdentityExtractor- default header-based caller identity extraction.ChioTowerError- evaluation, signing, persistence, and identity errors.
Kernel dispatch:
KernelService,KernelRequest,KernelResponse,KernelServiceError,TenantId- direct tool-call dispatch throughchio_kernel::ChioKernel.KernelTraceLayer/KernelTraceService- request/response tracing.TenantConcurrencyLimitLayer/TenantConcurrencyLimitService- per-tenant concurrency limiting with load shedding.build_layered(kernel, per_tenant_limit, request_timeout)- composes trace, timeout, and per-tenant limiting/shedding aroundKernelService.
Host-call metrics: HOST_CALL_LOG, HOST_CALL_GET_CONFIG,
HOST_CALL_GET_TIME_UNIX_SECS, HOST_CALL_FETCH_BLOB,
HOST_CALL_METRIC_LABEL_VALUES, normalize_host_call_metric_label.
Usage
use ChioLayer;
use Keypair;
use Layer;
let keypair = generate;
let layer = new;
// Wrap any tower Service with Chio evaluation.
let inner = service_fn;
let _service = layer.layer;
ChioLayer::new is fail-closed with no durable store attached. Use
ChioLayer::builder to wire a ReceiptStore/RevocationStore, or
ChioLayer::new_ephemeral for a local scaffold.
Testing
cargo test -p chio-tower
Tests that drive the kernel's async tool dispatch require a multi-thread
Tokio runtime (#[tokio::test(flavor = "multi_thread", worker_threads = 2)]).
See also
chio-http-core- definesHttpAuthority,HttpReceipt,CallerIdentity,Verdict; this crate is its Tower/HTTP integration.chio-kernel-ChioKernel,ReceiptStore,RevocationStorethatHttpAuthorityandKernelServicedispatch through.chio-metrics-spec- defineschio_fail_open_suspected_total, incremented here for the"tower"surface on every fail-open bypass.