Harden security: CORS, XSS, rate limiting, CSP, SRI
- Lock down CORS to ALLOWED_ORIGINS env var (was wildcard) - Fix admin panel XSS: use data-username attributes instead of interpolating usernames into onclick handlers - Add rate limiting to /api/auth/register (3r/m) and /api/admin/* (10r/m); set limit_req_status 429 - Add Content-Security-Policy header restricting scripts to self and cdn.jsdelivr.net - Add Subresource Integrity hash to Chart.js CDN script tag Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -13,8 +13,11 @@ http {
|
||||
gzip_types text/plain text/css application/javascript application/json;
|
||||
gzip_min_length 1000;
|
||||
|
||||
# ── Rate limiting — login endpoint ────────────────────────────────────────
|
||||
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
|
||||
# ── Rate limiting ─────────────────────────────────────────────────────────
|
||||
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
|
||||
limit_req_zone $binary_remote_addr zone=register:10m rate=3r/m;
|
||||
limit_req_zone $binary_remote_addr zone=admin:10m rate=10r/m;
|
||||
limit_req_status 429;
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
@@ -24,10 +27,11 @@ http {
|
||||
index index.html;
|
||||
|
||||
# ── Security headers ──────────────────────────────────────────────────
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header X-Frame-Options "SAMEORIGIN" always;
|
||||
add_header X-XSS-Protection "1; mode=block" always;
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header X-Frame-Options "SAMEORIGIN" always;
|
||||
add_header X-XSS-Protection "1; mode=block" always;
|
||||
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
||||
add_header Content-Security-Policy "default-src 'self'; script-src 'self' https://cdn.jsdelivr.net; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; font-src 'self'; frame-ancestors 'none'" always;
|
||||
|
||||
# ── Static files ──────────────────────────────────────────────────────
|
||||
location / {
|
||||
@@ -45,7 +49,7 @@ http {
|
||||
access_log off;
|
||||
}
|
||||
|
||||
# ── Login rate limiting ───────────────────────────────────────────────
|
||||
# ── Auth / admin rate limiting ────────────────────────────────────────
|
||||
location = /api/auth/login {
|
||||
limit_req zone=login burst=3 nodelay;
|
||||
|
||||
@@ -64,6 +68,42 @@ http {
|
||||
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
||||
}
|
||||
|
||||
location = /api/auth/register {
|
||||
limit_req zone=register burst=2 nodelay;
|
||||
|
||||
proxy_pass http://api:8000;
|
||||
proxy_http_version 1.1;
|
||||
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
add_header Cache-Control "no-store" always;
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header X-Frame-Options "SAMEORIGIN" always;
|
||||
add_header X-XSS-Protection "1; mode=block" always;
|
||||
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
||||
}
|
||||
|
||||
location /api/admin/ {
|
||||
limit_req zone=admin burst=5 nodelay;
|
||||
|
||||
proxy_pass http://api:8000;
|
||||
proxy_http_version 1.1;
|
||||
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
add_header Cache-Control "no-store" always;
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header X-Frame-Options "SAMEORIGIN" always;
|
||||
add_header X-XSS-Protection "1; mode=block" always;
|
||||
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
||||
}
|
||||
|
||||
# ── API reverse proxy ─────────────────────────────────────────────────
|
||||
# All /api/* requests are forwarded to the FastAPI container.
|
||||
# The container is reachable by its service name on the Docker network.
|
||||
|
||||
Reference in New Issue
Block a user