llm-edge-security
Security layer for LLM Edge Agent, providing authentication, input validation, and PII (Personally Identifiable Information) detection and redaction capabilities.
Features
- API Key Authentication: Secure client authentication using API keys with secret management
- JWT Token Validation: JSON Web Token authentication support (planned)
- PII Detection & Redaction: Automatic detection and redaction of sensitive information
- Social Security Numbers (SSN)
- Email addresses
- Credit card numbers
- Input Validation: Comprehensive request parameter validation
- Request size limits
- Temperature parameter validation (0.0-2.0)
- Max tokens validation
- Type-Safe Error Handling: Strongly typed security errors using
thiserror - Zero-Copy Operations: Efficient security checks with minimal overhead
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Or use cargo add:
Usage
API Key Authentication
use ;
PII Detection and Redaction
use PIIRedactor;
Input Validation
use ;
use SecurityResult;
Error Handling
use ;
PII Patterns
The PII redactor currently detects and redacts the following patterns:
| Pattern | Regex | Replacement |
|---|---|---|
| SSN | \b\d{3}-\d{2}-\d{4}\b |
[SSN_REDACTED] |
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b |
[EMAIL_REDACTED] |
|
| Credit Card | \b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b |
[CC_REDACTED] |
Security Best Practices
When using this crate in production:
-
API Key Management
- Store API keys in environment variables or secret management systems
- Never hardcode API keys in source code
- Rotate API keys regularly
- Use the
secrecycrate'sSecrettype to prevent accidental logging
-
PII Handling
- Always run PII detection before logging user inputs
- Redact PII before storing in databases or sending to third-party services
- Consider regulatory requirements (GDPR, CCPA, HIPAA)
- Implement audit trails for PII access
-
Input Validation
- Validate all user inputs before processing
- Set appropriate limits for request sizes and token counts
- Use type-safe validation functions to prevent injection attacks
-
Rate Limiting
- Implement rate limiting per API key
- Monitor for suspicious authentication patterns
- Log failed authentication attempts
-
Transport Security
- Always use HTTPS/TLS for API communication
- Implement certificate pinning where appropriate
- Use secure headers (HSTS, CSP, etc.)
Architecture
llm-edge-security/
├── auth.rs # Authentication implementations (API key, JWT)
├── pii.rs # PII detection and redaction
├── validation.rs # Input validation functions
├── error.rs # Security error types
└── lib.rs # Public API
Dependencies
Core dependencies:
secrecy- Secret management to prevent accidental exposurejsonwebtoken- JWT token validationargon2- Password hashingvalidator- Data validationregex- Pattern matching for PII detectionthiserror- Error handling
Roadmap
- JWT token validation implementation
- OAuth2/OIDC authentication support
- Additional PII patterns (phone numbers, IP addresses, etc.)
- Rate limiting middleware
- Security event logging
- Encryption utilities
- HMAC signature verification
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Contributing
Contributions are welcome! Please see the Contributing Guide for details.
For security vulnerabilities, please email security@globalbusinessadvisors.com instead of filing a public issue.