Security
ViperHTTP provides multiple layers of security for ESP32 applications. This guide covers the built-in security features, configuration best practices, and threat model considerations.
Security Architecture
Security enforcement happens at two levels:
- C Runtime (Core 0) — CORS, trusted host validation, and rate limiting are enforced before requests reach Python, providing native-speed protection.
- Python Middleware (Core 1) — Sessions, CSRF, and authentication are handled in the middleware chain with full application context.
HTTPS / TLS
Enable encrypted transport:
When HTTP/2 is enabled alongside HTTPS, HPACK header compression and stream multiplexing are automatically activated:
CORS (Cross-Origin Resource Sharing)
CORS is enforced at the C layer for performance:
from viperhttp import middleware as mw
app.add_middleware(
mw.CORSMiddleware,
allow_origins=["https://dashboard.local"],
allow_methods=["GET", "POST"],
allow_headers=["Authorization", "Content-Type"],
allow_credentials=True,
max_age=600,
)
Trusted Host Validation
Reject requests with unexpected Host headers:
Enforced in C before the request reaches Python.
Rate Limiting
Protect against request flooding:
Per-client tracking with C-native enforcement.
Session Middleware
Server-side sessions stored on VFS:
Sessions are stored as files on the ESP32 filesystem with configurable expiry. Session IDs are generated using cryptographic randomness where available.
CSRF Protection
Token-based CSRF protection with constant-time string comparison:
- Tokens are bound to sessions
- Validation uses constant-time comparison to prevent timing attacks
- Configurable token header/field names
Authentication
ViperHTTP supports multiple authentication backends:
Bearer Token
auth = vhttp_auth.BearerAuth(token="your-api-token")
@app.get("/protected", deps={"user": viperhttp.Depends(auth)})
def protected(user=None):
return {"authenticated": True}
Basic Auth
API Key
See the Authentication Guide for detailed usage.
Best Practices
- Always use HTTPS in production — unencrypted HTTP exposes all traffic
- Generate strong secrets — use
os.urandom()for session keys and tokens - Restrict CORS origins — avoid
allow_origins=["*"]in production - Set trusted hosts — prevent host header injection attacks
- Enable rate limiting — protect against denial-of-service
- Use CSRF protection — required for any state-changing browser requests
- Keep firmware updated — use OTA updates with SHA256 verification
- Limit exposed endpoints — use
include_in_schema=Falseto hide internal routes
Threat Model
ViperHTTP is designed for IoT devices on local networks or secured environments. Consider:
| Threat | Mitigation |
|---|---|
| Eavesdropping | HTTPS/TLS encryption |
| Request flooding | C-native rate limiting |
| CSRF attacks | Token-based CSRF middleware |
| Host header injection | Trusted host validation |
| Session hijacking | Secure session IDs, configurable expiry |
| Unauthorized access | Auth backends (Bearer, Basic, API Key) |
| Firmware tampering | SHA256-verified OTA updates |
| Cross-origin attacks | CORS enforcement |
Reporting Vulnerabilities
See SECURITY.md for responsible disclosure instructions.