Skip to main content

Crate guardrail_proxy

Crate guardrail_proxy 

Source
Expand description

§guardrail-proxy

HTTP reverse-proxy server for guardrail-rs.

This crate ties together guardrail_core, guardrail_classifiers, and guardrail_config into a runnable server: it accepts HTTP requests shaped like OpenAI/Anthropic chat-completion calls, runs them through the configured guardrail_core::Pipeline, and either forwards the (possibly redacted) request upstream or returns a block response.

§Architecture

┌──────────┐     ┌────────────────┐     ┌───────────┐     ┌──────────────┐
│  Client  │ ──▶ │  guardrail-rs  │ ──▶ │  Pipeline  │ ──▶ │   Upstream    │
│          │     │  HTTP server   │     │  (Stages)  │     │  (OpenAI/...) │
└──────────┘     └────────────────┘     └───────────┘     └──────────────┘
                         │                                          │
                         ▼                                          │
                  ┌─────────────┐                                  │
                  │  Block JSON │ ◀─── short-circuit on Block ─────┘
                  │  response   │
                  └─────────────┘

§Modules

  • server: the listener lifecycle — bind, accept, serve, shutdown
  • handler: per-request routing and the core proxy flow (auth check → parse → run pipeline → forward or block)
  • auth: caller authentication ([auth]), as a standalone, pure, independently-testable predicate
  • error: HTTP error-response construction and upstream error classification, shared by handler
  • state: internal connection-handler state and ServerHandle — the data every connection handler needs, with no behavior of its own
  • forward: upstream request forwarding (streaming + non-streaming)
  • translate: conversion between raw JSON bodies and guardrail_core::GuardrailRequest
  • response: output-side PII redaction for non-streaming responses
  • telemetry: OpenTelemetry OTLP layer and per-request/stage span helpers
  • metrics: Prometheus metrics registry and recording helpers
  • audit: structured audit logging (tracing events + rotating NDJSON file)
  • audit_log: rotating NDJSON file layer for tracing_subscriber

Each module above has one job. This separation exists so that, for example, the caller-authentication decision can be unit-tested as a pure function (auth::is_authorized) without spinning up a real TCP listener, while server’s own tests focus purely on proving the listener lifecycle works end to end.

§Feature flags

FlagDescriptionDefault
(none)Base proxy: regex injection scanning, PII redaction, policy engine, Prometheus metrics, OTel tracing, NDJSON audit log—
onnxEnables the semantic injection/toxicity classifiers via guardrail-classifiers/onnx and guardrail-config/onnxoff

§Quick example

Most applications run guardrail-proxy via the guardrail CLI binary rather than embedding run_server directly, but it is fully usable as a library:

use guardrail_config::ConfigHandle;
use std::sync::Arc;

let config = Arc::new(ConfigHandle::load("guardrail.toml").unwrap());
let handle = guardrail_proxy::run_server(config).await.unwrap();
println!("listening on {}", handle.local_addr());
handle.shutdown();

§Further reading

  • Configuration reference — every TOML key this server consumes, including [auth] and [observability].
  • Threat model — what this proxy protects against, caller authentication, and residual risks.
  • Architecture — request/response pipeline diagram and hot-reload behavior.
  • Changelog — release history and notable changes.

Re-exports§

pub use server::run_server;
pub use server::ServerHandle;

Modules§

audit
Structured audit logging for blocked and redacted requests.
audit_log
NDJSON audit log writer (spec §10).
auth
Caller authentication ([auth]).
error
HTTP response and error-construction helpers shared across the request handler.
forward
Upstream request forwarding.
metrics
Prometheus metrics for guardrail-rs.
response
Output-side PII redaction for non-streaming chat-completion responses.
server
Server lifecycle: bind a listener, accept connections, and serve them until shutdown is requested.
sse
Output-side PII redaction for streamed (text/event-stream) responses.
state
Shared server state and lifecycle handle.
telemetry
OpenTelemetry distributed tracing support.
translate
Conversion between raw JSON request bodies and GuardrailRequest.