Overview
Auth Agent implements OAuth 2.1 with multiple security layers to protect against common attacks and ensure data privacy. This guide covers security best practices for both deploying Auth Agent and integrating it into your applications.Core Security Features
PKCE (Proof Key for Code Exchange)
Status: ✅ Required for all authorization flows PKCE prevents authorization code interception attacks by requiring the client to prove they initiated the authorization request. How it works:- Client generates random
code_verifier(128 bytes) - Client computes
code_challenge = SHA256(code_verifier) - Client sends
code_challengein/authorizerequest - Server stores
code_challengewith authorization code - Client sends
code_verifierin/tokenrequest - Server verifies
SHA256(code_verifier) === stored code_challenge
PBKDF2 Password Hashing
Status: ✅ All secrets hashed with PBKDF2 Agent secrets and client secrets are hashed using PBKDF2 with 100,000 iterations before storage. Configuration:- Resistant to brute-force attacks
- Configurable iteration count (currently 100,000)
- Native Web Crypto API support (no dependencies)
- FIPS 140-2 compliant
Secrets are never stored in plaintext. They’re only shown once at creation time.
JWT Access Tokens
Status: ✅ Signed with HS256 Access tokens are JWTs signed with HMAC-SHA256 to prevent tampering. Token Structure:Row Level Security (RLS)
Status: ✅ Enforced on all user-owned tables RLS policies ensure users can only access their own agents and clients. How it works:- Database-level enforcement (not just application layer)
- Automatic filtering of queries
- Protection against SQL injection attacks
- Admin override for management
RLS is enforced at the database level. Even if application code has bugs, users cannot access others’ data.
Threat Model & Mitigations
Authorization Code Interception
Attack: Attacker intercepts authorization code from redirect URI Mitigation: PKCE required - attacker cannot exchange code withoutcode_verifier
Status: ✅ Protected
Replay Attacks
Attack: Attacker reuses stolen authorization code Mitigation: Authorization codes are single-use (marked asused after exchange)
Status: ✅ Protected
Token Theft
Attack: Attacker steals access or refresh token Mitigation:- Short access token lifetime (1 hour)
- Refresh token rotation on use
- Token revocation endpoint Status: ✅ Protected
Cross-Site Request Forgery (CSRF)
Attack: Attacker tricks user’s browser into making unauthorized requests Mitigation:state parameter required in authorization request
Status: ✅ Protected
SQL Injection
Attack: Attacker injects malicious SQL via user input Mitigation:- Parameterized queries (no string concatenation)
- RLS policies
- Input validation Status: ✅ Protected
Session Hijacking
Attack: Attacker steals user’s session cookie Mitigation:- HTTPOnly cookies (not accessible via JavaScript)
- Secure flag (HTTPS only)
- SameSite=Lax (CSRF protection)
- Session expiration Status: ✅ Protected
Brute Force Attacks
Attack: Attacker tries many passwords/secrets to guess correct one Mitigation:- PBKDF2 with 100,000 iterations (slow to compute)
- Rate limiting (recommended at edge/firewall level)
- Account lockout after failed attempts (TODO) Status: ⚠️ Partially protected (add rate limiting)
Deployment Security
Environment Variables
Never commit secrets to version control:- Use Cloudflare Workers Secrets for production
- Use environment variables for local development
- Rotate secrets regularly
- Never log secrets
- Never send secrets to client-side code
HTTPS Enforcement
Required in production:CORS Configuration
Restrict origins in production:Database Security
Supabase best practices:-
Never expose service role key client-side
- Use anon key for client-side code
- Use service role key only in server-side code
-
Enable RLS on all tables
-
Review RLS policies regularly
- Ensure policies match intended access patterns
- Test with non-admin users
-
Monitor authentication logs
- Check for suspicious login attempts
- Alert on failed authentication spikes
-
Enable database connection pooling
- Use Supabase connection pooler for Workers
- Set reasonable pool limits
Client-Side Security
Token Storage
DO NOT store tokens in localStorage:XSS Prevention
Sanitize user input:Redirect URI Validation
Validate redirect URIs on client-side:Agent Security
Credential Management
Secure storage:Network Security
Use HTTPS for all API calls:Monitoring & Alerts
Key Metrics
Monitor these security metrics:-
Failed authentication attempts
- Sudden spike may indicate brute force attack
- Alert threshold: >100/minute
-
Invalid redirect URI attempts
- Indicates potential authorization code theft attempt
- Alert on any occurrence
-
Expired token usage
- Clients trying to use expired tokens
- May indicate stolen tokens or client bugs
-
RLS policy violations
- Users attempting to access others’ data
- Immediate investigation required
-
Admin actions
- Log all admin operations
- Alert on unusual patterns
Logging Best Practices
What to log:- Authentication attempts (success/failure)
- Authorization code generation
- Token exchanges
- Token revocations
- Admin actions
- RLS policy violations
- Secrets (agent_secret, client_secret)
- Access tokens (full token)
- Refresh tokens
- Code verifiers
- User passwords
Incident Response
If Secrets Are Compromised
Immediate actions:-
Rotate JWT_SECRET
-
Revoke all tokens
-
Force re-authentication
- All clients must obtain new tokens
-
Investigate breach
- Check logs for unauthorized access
- Identify compromised systems
If Database Is Compromised
Immediate actions:- Rotate database credentials
- Review access logs
- Check for data exfiltration
- Notify affected users
- Review RLS policies
Security Checklist
Deployment Checklist
- HTTPS enforced for all endpoints
- JWT_SECRET is strong random value (32+ bytes)
- Supabase service role key secured
- CORS origins restricted
- RLS enabled on all user tables
- Database backups configured
- Monitoring and alerting configured
- Rate limiting implemented
- Security headers configured
- Secrets stored in environment variables
Integration Checklist
- PKCE implemented correctly
- state parameter validated
- Redirect URIs validated
- Tokens stored securely (not localStorage)
- User input sanitized
- HTTPS used for all API calls
- Secrets not logged
- Error messages don’t leak sensitive info
- Token expiration handled gracefully
- Refresh token rotation implemented
Security Updates
Auth Agent follows security best practices and monitors for vulnerabilities:- Regular dependency updates
- Security patch releases
- CVE monitoring
- Security audit recommendations
- Watch the GitHub repository
- Subscribe to security advisories
- Join the Discord community
Reporting Security Issues
If you discover a security vulnerability:- DO NOT open a public issue
- Email: [email protected]
- Include:
- Description of vulnerability
- Steps to reproduce
- Potential impact
- Suggested fix (if any)
