Security
Ginject provides built-in security primitives including CSRF protection, Helmet headers, ThrottlerGuard, and CORS. Learn how to configure each for production.
Security
Ginject includes production-ready security primitives out of the box. Enable them in the correct order for defense-in-depth.
Recommended Global Configuration
CORS
Cross-Origin Resource Sharing controls which origins can make requests to your API.
For public APIs that don't use cookies:
Do not combine AllowedOrigins: ["*"] with AllowCredentials: true — browsers will reject this.
Helmet
Sets a suite of security HTTP response headers:
Headers set by Helmet:
| Header | Value |
|---|---|
X-Content-Type-Options | nosniff |
X-Frame-Options | DENY |
X-XSS-Protection | 1; mode=block |
Referrer-Policy | strict-origin-when-cross-origin |
Content-Security-Policy | default-src 'self' |
Permissions-Policy | Disables microphone, camera, geolocation |
CSRF
The CSRF middleware uses the double-submit cookie pattern:
- On
GET/HEAD/OPTIONS, the middleware generates a signed CSRF token and sets it as a cookie. - On
POST/PUT/PATCH/DELETE, the middleware validates that theX-CSRF-Tokenrequest header matches the cookie value.
Client usage (JavaScript):
Rate Limiting (ThrottlerGuard)
See the Guards documentation for full ThrottlerGuard configuration.
Per-User Rate Limiting
Per-API-Key Rate Limiting
Authentication Patterns
Ginject doesn't bundle a JWT library, but guards are the right place to implement authentication:
Input Validation
Validate request bodies in handlers using the body.Bind pattern with a validation library: