security: add headers middleware, session regeneration, and body size limits #41
Reference in New Issue
Block a user
Delete Branch "security/headers-session-bodylimit"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
This PR implements three security hardening measures:
Security Headers Middleware (closes #34)
Adds a
SecurityHeaders()middleware applied globally to all routes. Every response now includes:Strict-Transport-Security: max-age=63072000; includeSubDomains; preloadX-Content-Type-Options: nosniffX-Frame-Options: DENYContent-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'Referrer-Policy: strict-origin-when-cross-originPermissions-Policy: camera=(), microphone=(), geolocation=()Session Fixation Prevention (closes #38)
Adds a
Regenerate()method to the session manager that destroys the old session and creates a new one with a fresh ID, copying all session values. Called after successful login to prevent session fixation attacks.Request Body Size Limits (closes #39)
Adds a
MaxBodySize()middleware usinghttp.MaxBytesReaderto limit POST/PUT/PATCH request bodies to 1 MB. Applied to all form endpoints (/pages,/sources,/source/*).Files Changed
internal/middleware/middleware.go— AddedSecurityHeaders()andMaxBodySize()middlewareinternal/session/session.go— AddedRegenerate()method for session fixation preventioninternal/handlers/auth.go— Updated login handler to regenerate session after authenticationinternal/server/routes.go— Added SecurityHeaders globally, MaxBodySize to form route groupsREADME.md— Documented new middleware in stack, updated Security section, moved items to completed TODOcloses #34, closes #38, closes #39
PR created implementing three security hardening issues:
Regenerate()method destroys old session, creates new ID, copies values)http.MaxBytesReader) on form POST endpointsAll tests pass.
docker build .succeeds. README updated with new middleware documentation.✅ Review: PASS
All three security hardening issues are correctly implemented:
#34 — Security Headers Middleware
includeSubDomains; preload),X-Content-Type-Options: nosniff,X-Frame-Options: DENY, CSP (default-src 'self'),Referrer-Policy: strict-origin-when-cross-origin,Permissions-Policys.router.Use(s.mw.SecurityHeaders())— positioned correctly after RequestID and before Logging#38 — Session Fixation Fix
Regenerate()method correctly: (1) copies old session values, (2) destroys old session withMaxAge = -1+ClearUser+Save, (3) creates new session viastore.New, (4) restores values, (5) applies matching session optionsRegenerate()are identical to those inNew()(Path/, MaxAge 7 days, HttpOnly, Secure in prod, SameSite Lax) ✅HandleLoginSubmitafter password verification but beforeSetUser— correct positionstore.Newwhen the old cookie is now invalid#39 — Body Size Limits
MaxBodySizemiddleware useshttp.MaxBytesReaderfor POST/PUT/PATCH methods/pages,/sources,/source/{sourceID}Integrity Checks
.golangci.yml) changed ✅docker build .passes (includesmake check: fmt, lint, test, build) ✅main(no rebase needed) ✅