Security audit fixes: JWT secret guard, WS revocation, nginx headers/TLS, loopback Gitea
Deploy with Docker Compose / deploy (push) Successful in 59s
Deploy with Docker Compose / deploy (push) Successful in 59s
- auth: panic at init if BACKEND_SECRET missing/short — HS256 would happily start with an empty key, making admin tokens forgeable by anyone - websocket: admin check now goes through AuthenticateTokenWithClaims so the "tv" token-version revocation applies on /ws too; a logged-out or password-changed token no longer keeps it alive for its full lifetime - admin diagnostics: check BACKEND_SECRET not the nonexistent AUTH_SECRET - rowing upload: 10MB cap before buffering + base64 (admin-only route, but unbounded read = easy memory exhaustion + huge paid Claude call) - nginx: shared security_headers.conf (XFO/nosniff/Referrer-Policy/HSTS/CSP) included in every location that sets its own add_header — nginx drops the parent's whole set otherwise; CSP added to the SPA document; ssl_protocols pinned to TLSv1.2/1.3; HSTS on apex redirect - compose: Gitea HTTP bound to 127.0.0.1 (plain HTTP was LAN-exposed); autoheal docker socket mount read-only Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
@@ -87,6 +87,15 @@ func (store *Store) CreateRowing(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cap the upload before buffering it (and then base64-encoding it, 1.33x
|
||||||
|
// bigger): the route is admin-only but an unbounded read would still let
|
||||||
|
// a giant file exhaust the Pi's memory and produce a huge paid API call.
|
||||||
|
// 10MB is well above any photo this endpoint is meant to take.
|
||||||
|
if file.Size > 10<<20 {
|
||||||
|
ctx.JSON(http.StatusBadRequest, gin.H{"error": "image too large (max 10MB)"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
data, err := io.ReadAll(f)
|
data, err := io.ReadAll(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to read image"})
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to read image"})
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"adam-french.co.uk/backend/models"
|
|
||||||
"adam-french.co.uk/backend/services"
|
"adam-french.co.uk/backend/services"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@@ -26,9 +25,14 @@ func (store *Store) ConnectWebSocket(ctx *gin.Context) {
|
|||||||
// isAdminRequest reports whether the request carries valid admin credentials.
|
// isAdminRequest reports whether the request carries valid admin credentials.
|
||||||
// It prefers the access token and falls back to the refresh token plus a DB
|
// It prefers the access token and falls back to the refresh token plus a DB
|
||||||
// lookup, so an admin whose access token has just expired is still recognised.
|
// lookup, so an admin whose access token has just expired is still recognised.
|
||||||
|
// Both branches go through AuthenticateTokenWithClaims rather than bare
|
||||||
|
// VerifyJWT: that is what enforces the "tv" token-version claim, so a token
|
||||||
|
// revoked by logout or a password change is rejected here too. A bare
|
||||||
|
// VerifyJWT would leave /ws honouring stale tokens for their full lifetime
|
||||||
|
// while every other path rejects them.
|
||||||
func (store *Store) isAdminRequest(ctx *gin.Context) bool {
|
func (store *Store) isAdminRequest(ctx *gin.Context) bool {
|
||||||
if accessToken, err := ctx.Cookie("access_token"); err == nil {
|
if accessToken, err := ctx.Cookie("access_token"); err == nil {
|
||||||
if claims, err := store.Auth.VerifyJWT(accessToken); err == nil {
|
if _, claims, err := store.Auth.AuthenticateTokenWithClaims(store.DB, accessToken); err == nil {
|
||||||
admin, ok := (*claims)["admin"].(bool)
|
admin, ok := (*claims)["admin"].(bool)
|
||||||
return ok && admin
|
return ok && admin
|
||||||
}
|
}
|
||||||
@@ -38,17 +42,9 @@ func (store *Store) isAdminRequest(ctx *gin.Context) bool {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
claims, err := store.Auth.VerifyJWT(refreshToken)
|
user, err := store.Auth.AuthenticateToken(store.DB, refreshToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
userIDF, ok := (*claims)["id"].(float64)
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
user := models.User{ID: uint(userIDF)}
|
|
||||||
if err := store.DB.First(&user).Error; err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return user.Admin
|
return user.Admin
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,11 +39,16 @@ type Tokens struct {
|
|||||||
RefreshToken string
|
RefreshToken string
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitAuth builds the Auth service from config.
|
// InitAuth builds the Auth service from config. An absent or too-short
|
||||||
|
// secret is fatal: HS256 happily signs and verifies with a zero-length key,
|
||||||
|
// so starting up with an empty BACKEND_SECRET would let anyone forge admin
|
||||||
|
// tokens. Fail loudly at boot rather than serving a forgeable auth system.
|
||||||
func InitAuth(config *AuthConfig) *Auth {
|
func InitAuth(config *AuthConfig) *Auth {
|
||||||
auth := Auth{Config: config}
|
if len(config.Secret) < 32 {
|
||||||
|
panic("auth secret must be set and at least 32 bytes (check BACKEND_SECRET)")
|
||||||
|
}
|
||||||
|
|
||||||
return &auth
|
return &Auth{Config: config}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateJWT issues an access/refresh pair for user.
|
// GenerateJWT issues an access/refresh pair for user.
|
||||||
|
|||||||
@@ -34,5 +34,8 @@ COPY nginx_dev.conf.template /etc/nginx/nginx_dev.conf.template
|
|||||||
# (same envsubst allow-list) to /etc/nginx/nginx_dev_common.conf, which is the
|
# (same envsubst allow-list) to /etc/nginx/nginx_dev_common.conf, which is the
|
||||||
# path both `include` directives in nginx_dev.conf.template point at.
|
# path both `include` directives in nginx_dev.conf.template point at.
|
||||||
COPY nginx_dev_common.conf.template /etc/nginx/nginx_dev_common.conf.template
|
COPY nginx_dev_common.conf.template /etc/nginx/nginx_dev_common.conf.template
|
||||||
|
# Static security-header include, referenced from nginx.conf.template (and
|
||||||
|
# any location that sets its own add_header). Not templated — no envsubst.
|
||||||
|
COPY security_headers.conf /etc/nginx/security_headers.conf
|
||||||
COPY entrypoint.sh /entrypoint.sh
|
COPY entrypoint.sh /entrypoint.sh
|
||||||
ENTRYPOINT ["/entrypoint.sh"]
|
ENTRYPOINT ["/entrypoint.sh"]
|
||||||
|
|||||||
@@ -107,6 +107,12 @@ http {
|
|||||||
text/javascript mjs;
|
text/javascript mjs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# TLS protocol versions, pinned: nginx's defaults still include TLSv1 and
|
||||||
|
# TLSv1.1, which are deprecated and fail modern compliance scans.
|
||||||
|
ssl_protocols TLSv1.2 TLSv1.3;
|
||||||
|
ssl_session_cache shared:SSL:10m;
|
||||||
|
ssl_session_tickets off;
|
||||||
|
|
||||||
gzip on;
|
gzip on;
|
||||||
gzip_vary on;
|
gzip_vary on;
|
||||||
gzip_proxied any;
|
gzip_proxied any;
|
||||||
@@ -145,6 +151,8 @@ http {
|
|||||||
ssl_certificate /etc/letsencrypt/live/$DOMAIN/fullchain.pem;
|
ssl_certificate /etc/letsencrypt/live/$DOMAIN/fullchain.pem;
|
||||||
ssl_certificate_key /etc/letsencrypt/live/$DOMAIN/privkey.pem;
|
ssl_certificate_key /etc/letsencrypt/live/$DOMAIN/privkey.pem;
|
||||||
|
|
||||||
|
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
|
||||||
|
|
||||||
return 301 https://www.$DOMAIN$request_uri;
|
return 301 https://www.$DOMAIN$request_uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,27 +175,31 @@ http {
|
|||||||
ssl_certificate /etc/letsencrypt/live/$DOMAIN/fullchain.pem;
|
ssl_certificate /etc/letsencrypt/live/$DOMAIN/fullchain.pem;
|
||||||
ssl_certificate_key /etc/letsencrypt/live/$DOMAIN/privkey.pem;
|
ssl_certificate_key /etc/letsencrypt/live/$DOMAIN/privkey.pem;
|
||||||
|
|
||||||
add_header X-Frame-Options "SAMEORIGIN" always;
|
# Security headers live in nginx/security_headers.conf and are
|
||||||
add_header X-Content-Type-Options "nosniff" always;
|
# included there instead of here, because any location that declares
|
||||||
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
# so much as one add_header of its own otherwise inherits none of
|
||||||
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
|
# these (nginx's add_header inheritance is all-or-nothing).
|
||||||
|
include /etc/nginx/security_headers.conf;
|
||||||
|
|
||||||
# Vite hashed assets - immutable, cache 1 year
|
# Vite hashed assets - immutable, cache 1 year
|
||||||
location /assets/ {
|
location /assets/ {
|
||||||
expires 1y;
|
expires 1y;
|
||||||
add_header Cache-Control "public, immutable";
|
add_header Cache-Control "public, immutable";
|
||||||
|
include /etc/nginx/security_headers.conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Fonts - cache 30 days
|
# Fonts - cache 30 days
|
||||||
location /fonts/ {
|
location /fonts/ {
|
||||||
expires 30d;
|
expires 30d;
|
||||||
add_header Cache-Control "public";
|
add_header Cache-Control "public";
|
||||||
|
include /etc/nginx/security_headers.conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Images - cache 7 days
|
# Images - cache 7 days
|
||||||
location /img/ {
|
location /img/ {
|
||||||
expires 7d;
|
expires 7d;
|
||||||
add_header Cache-Control "public";
|
add_header Cache-Control "public";
|
||||||
|
include /etc/nginx/security_headers.conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
# PRIVATE attachments (admin-only). Attachments on *private* chat
|
# PRIVATE attachments (admin-only). Attachments on *private* chat
|
||||||
@@ -202,6 +214,7 @@ http {
|
|||||||
auth_request /internal/auth/admin-validate;
|
auth_request /internal/auth/admin-validate;
|
||||||
error_page 401 403 = @auth_denied;
|
error_page 401 403 = @auth_denied;
|
||||||
alias /uploads/private/;
|
alias /uploads/private/;
|
||||||
|
include /etc/nginx/security_headers.conf;
|
||||||
add_header X-Content-Type-Options nosniff always;
|
add_header X-Content-Type-Options nosniff always;
|
||||||
add_header Content-Disposition "inline" always;
|
add_header Content-Disposition "inline" always;
|
||||||
add_header Content-Security-Policy "default-src 'none'; img-src 'self'; style-src 'none'; script-src 'none'" always;
|
add_header Content-Security-Policy "default-src 'none'; img-src 'self'; style-src 'none'; script-src 'none'" always;
|
||||||
@@ -215,6 +228,7 @@ http {
|
|||||||
# means even an uploaded .html can run no script and load no resource.
|
# means even an uploaded .html can run no script and load no resource.
|
||||||
location /uploads/ {
|
location /uploads/ {
|
||||||
alias /uploads/;
|
alias /uploads/;
|
||||||
|
include /etc/nginx/security_headers.conf;
|
||||||
add_header X-Content-Type-Options nosniff always;
|
add_header X-Content-Type-Options nosniff always;
|
||||||
add_header Content-Disposition "inline" always;
|
add_header Content-Disposition "inline" always;
|
||||||
add_header Content-Security-Policy "default-src 'none'; img-src 'self'; style-src 'none'; script-src 'none'" always;
|
add_header Content-Security-Policy "default-src 'none'; img-src 'self'; style-src 'none'; script-src 'none'" always;
|
||||||
@@ -251,11 +265,13 @@ http {
|
|||||||
add_header Access-Control-Allow-Origin "*";
|
add_header Access-Control-Allow-Origin "*";
|
||||||
expires 7d;
|
expires 7d;
|
||||||
add_header Cache-Control "public";
|
add_header Cache-Control "public";
|
||||||
|
include /etc/nginx/security_headers.conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
location /sound/ {
|
location /sound/ {
|
||||||
expires 7d;
|
expires 7d;
|
||||||
add_header Cache-Control "public";
|
add_header Cache-Control "public";
|
||||||
|
include /etc/nginx/security_headers.conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
# ---- Backend API -----------------------------------------------------
|
# ---- Backend API -----------------------------------------------------
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
# Included at every level that sets at least one of its own add_headers.
|
||||||
|
# nginx replaces a parent's whole add_header set inside any block that
|
||||||
|
# declares one of its own, so without this include, locations like /assets/
|
||||||
|
# and /sound/ silently lose every security header the server block sets.
|
||||||
|
# Locations that add extra headers of their own (the /uploads/ blocks) can
|
||||||
|
# include this AND their own lines; the include plus their own add_header
|
||||||
|
# directives merge additively.
|
||||||
|
|
||||||
|
add_header X-Frame-Options "SAMEORIGIN" always;
|
||||||
|
add_header X-Content-Type-Options "nosniff" always;
|
||||||
|
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
||||||
|
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
|
||||||
|
|
||||||
|
# Applied to the main document; the /uploads/ blocks override with their own
|
||||||
|
# locked-down CSP by declaring Content-Security-Policy again themselves
|
||||||
|
# (a later add_header of the same name within one response wins).
|
||||||
|
add_header Content-Security-Policy "default-src 'self'; img-src 'self' data: https:; style-src 'self' 'unsafe-inline'; font-src 'self' data:; connect-src 'self'; frame-ancestors 'self'; base-uri 'self'; object-src 'none'" always;
|
||||||
Reference in New Issue
Block a user