hanzo-guard
Fast, comprehensive LLM I/O sanitization layer for Rust. Provides PII detection/redaction, prompt injection detection, rate limiting, content filtering, and audit logging with sub-millisecond latency.
Features
- PII Detection & Redaction: SSN, credit cards (Luhn validated), emails, phone numbers, IP addresses, API keys
- Prompt Injection Detection: Jailbreak attempts, system prompt leaks, role-play manipulation, encoding tricks
- Rate Limiting: Per-user request throttling with configurable burst handling
- Content Filtering: ML-based safety classification via external API (Zen Guard integration)
- Audit Logging: JSONL audit trails with content hashing for compliance
- Sub-millisecond Latency: Pure Rust implementation, no external API calls for core features
Quick Start
use ;
async
Configuration
Minimal (PII only)
let guard = new;
Builder Pattern
let guard = builder
.pii_only // Only PII detection
.with_injection // Add injection detection
.with_rate_limit // Add rate limiting
.build;
Full Configuration
use *;
let config = GuardConfig ;
let guard = new;
Feature Flags
| Feature | Default | Description |
|---|---|---|
pii |
Yes | PII detection and redaction |
rate-limit |
Yes | Rate limiting with governor |
content-filter |
No | External ML content classification |
audit |
Yes | Audit logging |
# Minimal (just core types)
= { = "0.1", = false }
# PII only
= { = "0.1", = false, = ["pii"] }
# Full features
= { = "0.1", = ["content-filter"] }
Context-Aware Sanitization
use GuardContext;
let context = new
.with_user_id
.with_session_id
.with_metadata;
let result = guard.sanitize_input_with_context.await?;
Integration with Zen Guard
For ML-based content classification, hanzo-guard integrates with Zen Guard:
┌─────────────┐ ┌──────────────┐ ┌────────────┐
│ Application │ ──► │ Hanzo Guard │ ──► │ Zen Guard │
└─────────────┘ │ (Rust, <1ms) │ │ (ML Model) │
│ │ │ │
│ • PII Redact │ │ • Content │
│ • Rate Limit │ │ Classify │
│ • Injection │ │ • Severity │
│ Detect │ │ Levels │
│ • Audit Log │ │ • 119 Lang │
└──────────────┘ └────────────┘
Performance
| Operation | Latency | Throughput |
|---|---|---|
| PII Detection | ~50μs | 20K+ ops/sec |
| Injection Check | ~20μs | 50K+ ops/sec |
| Full Sanitize | ~100μs | 10K+ ops/sec |
| Rate Limit Check | ~1μs | 1M+ ops/sec |
*Benchmarked on Apple M1 Max, single-threaded
Safety Categories
When using content filtering, content is classified into these categories:
- Violent: Violence instructions or depictions
- Illegal: Hacking, unauthorized activities
- Sexual: Adult content
- PII: Personal information disclosure
- SelfHarm: Self-harm encouragement
- Unethical: Bias, discrimination, hate
- Political: False political information
- Copyright: Copyrighted material
- Jailbreak: System prompt override attempts
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Related
- hanzo-extract - Content extraction with hanzo-guard integration
- Zen Guard - ML-based safety classification