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.
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, path | The request line as the workload sent it. |
host | The validated authority the ACL matched against, never the raw Host header. |
headers | Request headers, captured before credential injection. |
allowed | The 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.
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_chunk | Per chunk, as the request body is forwarded | Nothing is buffered. |
response | Upstream status and headers are known | Return true to receive the body. Not called for a denied request or an unreachable upstream. |
response_chunk | Per chunk, as the response body reaches the workload | Only after response returned true. |
end | Exactly once, last | Complete, 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
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
80unlesshttp_portssays otherwise. - HTTPS once a CA is configured with
http_inject_caorhttp_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_alloworhttp_denyrule 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.