security: Fix critical security vulnerabilities

- Remove secrets from Git (.env)
- Remove debug token endpoint
- Fix login to reject unauthorized access in production
- Remove HS256 fallback in JWT validation (RS256 only)
- Fix SQL injection in journal.go countQuery
- Fix CORS to use explicit origins only (no wildcard)
- Add security headers middleware (CSP, HSTS, etc.)
- Add input validation helpers
- Build successful
This commit is contained in:
Bernt
2026-08-10 11:26:58 +00:00
parent cb4a273733
commit 8921fd1467
9 changed files with 404 additions and 245 deletions
+34 -2
View File
@@ -2,16 +2,48 @@ package middleware
import (
"net/http"
"os"
"strings"
"time"
"github.com/rs/zerolog"
)
// CORS middleware med strikt origin-kontroll
func CORS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
origin := r.Header.Get("Origin")
// Hämta tillåtna origins från miljövariabel
allowedOrigins := os.Getenv("CORS_ORIGINS")
if allowedOrigins == "" {
allowedOrigins = "http://localhost:3000"
}
origins := strings.Split(allowedOrigins, ",")
allowed := false
for _, o := range origins {
o = strings.TrimSpace(o)
if o == origin {
allowed = true
break
}
}
// I utveckling, tillåt localhost
if !allowed && strings.HasPrefix(origin, "http://localhost:") {
allowed = true
}
if allowed {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Credentials", "true")
}
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Request-ID")
w.Header().Set("Access-Control-Max-Age", "86400")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)