Home
Why Sandlock
How It Works Use Cases Comparison Security Model
Docs
Documentation Home Getting Started CLI Reference Python SDK Sandbox Reference FAQ
Products
Overview Sandbox HTTP API Sandbox Scheduler
GitHub Schedule a Demo
Sandlock / Docs /HTTP Observer

HTTP Observer

The HTTP ACL decides what a workload may send. An observer watches what it actually sent: every request, the verdict it got, the response status, and the bodies in both directions if you ask for them.

Updated

What it is

http_observer is a callback on the Rust builder that sees every HTTP request reaching the transparent proxy, allowed or denied. It observes only; the ACL still decides. There is no CLI flag and no Python or Go form, and it requires the http compile-time feature, which is on by default.

The callback receives the request and returns Some exchange object to follow that request through its bodies and its end, or None to ignore it.

Request field Meaning
method, pathThe request line as the workload sent it.
hostThe validated authority the ACL matched against, never the raw Host header.
headersRequest headers, captured before credential injection.
allowedThe ACL verdict. This is the only place it appears.

The exchange

One object per followed request. Every method has a default, so implement only what you need, and keep state in self.

sandlock_core::http
pub trait HttpExchange: Send {
    fn request_chunk(&mut self, chunk: &[u8]) {}
    fn response(&mut self, event: &HttpResponseEvent) -> bool { false }
    fn response_chunk(&mut self, chunk: &[u8]) {}
    fn end(&mut self, end: HttpEnd) {}
}
Method When Notes
request_chunkPer chunk, as the request body is forwardedNothing is buffered.
responseUpstream status and headers are knownReturn true to receive the body. Not called for a denied request or an unreachable upstream.
response_chunkPer chunk, as the response body reaches the workloadOnly after response returned true.
endExactly once, lastComplete, Error (upstream unreachable or failed mid-body), or Aborted (the workload went away mid-stream).

Finalize in end. It is the only call that says how the exchange finished, so a record closed anywhere else cannot tell a whole body from a truncated one. A denied request ends Complete: its body is never read and nothing was truncated.

Example

rust
use std::sync::Arc;
use sandlock_core::http::{HttpEnd, HttpExchange, HttpResponseEvent};
use sandlock_core::Sandbox;

// Logs one line per exchange: status, response size, and how it ended.
struct Trace { line: String, status: u16, bytes: usize }

impl HttpExchange for Trace {
    fn response(&mut self, event: &HttpResponseEvent) -> bool {
        self.status = event.status;
        true
    }
    fn response_chunk(&mut self, chunk: &[u8]) {
        self.bytes += chunk.len();
    }
    fn end(&mut self, end: HttpEnd) {
        eprintln!("{} -> {} ({} bytes, {:?})", self.line, self.status, self.bytes, end);
    }
}

let mut sandbox = Sandbox::builder()
    .fs_read("/usr").fs_read("/lib").fs_read("/etc")
    .http_allow("POST api.example.com/*")
    .http_inject_ca("/etc/ssl/certs/ca-certificates.crt")
    .http_observer(Arc::new(|event| {
        let line = format!("{} {}{}", event.method, event.host, event.path);
        // A denial has no body and no response, so there is nothing to follow.
        if !event.allowed {
            eprintln!("denied {line}");
            return None;
        }
        Some(Box::new(Trace { line, status: 0, bytes: 0 }) as Box<dyn HttpExchange>)
    }))
    .build()?;

Coverage

  • HTTP/1.1 on intercepted ports, which is port 80 unless http_ports says otherwise.
  • HTTPS once a CA is configured with http_inject_ca or http_ca, because the proxy decrypts it. An HTTP/2 client is negotiated down to 1.1 by ALPN, so it is observed too.
  • No rules needed. An observer alone starts the proxy. With no http_allow or http_deny rule every request is allowed, and the intercepted ports become reachable on any IP.

Threading and secrets

Callbacks run on a dedicated thread, not the supervisor's runtime, so a slow callback never delays syscall handling. That thread is fed by one bounded queue shared by the whole proxy, though, so a slow callback does hold up observed traffic: when the queue fills, bodies stop being read instead of being buffered, and new requests wait before they are forwarded.

Keep callbacks fast and redact what you record. Slow work belongs on a thread of your own. Request headers are captured before credential injection, so an observer never sees an injected secret, but whatever credentials the workload sent itself are live: redact them before writing them down.