Security audit fixes: JWT secret guard, WS revocation, nginx headers/TLS, loopback Gitea
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:
2026-09-22 23:12:56 +01:00
co-authored by Claude Code
parent 8bd93cbd8e
commit 03e10be2e4
6 changed files with 64 additions and 18 deletions
+7 -11
View File
@@ -1,7 +1,6 @@
package handlers
import (
"adam-french.co.uk/backend/models"
"adam-french.co.uk/backend/services"
"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.
// 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.
// 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 {
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)
return ok && admin
}
@@ -38,17 +42,9 @@ func (store *Store) isAdminRequest(ctx *gin.Context) bool {
if err != nil {
return false
}
claims, err := store.Auth.VerifyJWT(refreshToken)
user, err := store.Auth.AuthenticateToken(store.DB, refreshToken)
if err != nil {
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
}