Fix security vulnerabilities: XSS, cookie hardening, rate limiter bypass, HSTS, and open redirect
- Fix XSS in showToast() by replacing innerHTML with safe DOM construction via textContent - Add Secure flag to session and CSRF cookies when base_url uses HTTPS - Prevent rate limiter bypass by only trusting X-Forwarded-For/X-Real-IP from private/loopback IPs - Add Strict-Transport-Security header when running behind HTTPS - Validate short link URLs to prevent open redirects (must be http, https, or relative path) - Remove runtime config.toml file write from slug length update handler - Pin Docker base image to alpine:3.20 instead of floating latest tag Co-Authored-By: Claude Opus 4.6 <[email protected]>
This commit is contained in:
1 parent
65e013d728
commit
737a48ac30
5 files changed
+78
-54
No files matched your search
+17
-11
@@ -39,25 +39,31 @@ func New(cfg *config.Config, database *db.DB) *Server {
|
||||
}
|
||||
}
|
||||
|
||||
func securityHeaders(next http.Handler) http.Handler {
|
||||
func (s *Server) securityHeaders(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Frame-Options", "DENY")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin")
|
||||
|
||||
// Offline-first CSP: Only allows local static resources and fonts from Google Fonts
|
||||
w.Header().Set("Content-Security-Policy",
|
||||
if s.isHTTPS() {
|
||||
w.Header().Set("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Security-Policy",
|
||||
"default-src 'self'; "+
|
||||
"script-src 'self' 'unsafe-inline'; "+
|
||||
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; "+
|
||||
"font-src 'self' https://fonts.gstatic.com; "+
|
||||
"img-src 'self' data:; "+
|
||||
"connect-src 'self';")
|
||||
|
||||
"script-src 'self' 'unsafe-inline'; "+
|
||||
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; "+
|
||||
"font-src 'self' https://fonts.gstatic.com; "+
|
||||
"img-src 'self' data:; "+
|
||||
"connect-src 'self';")
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) isHTTPS() bool {
|
||||
return strings.HasPrefix(s.cfg.Server.BaseURL, "https")
|
||||
}
|
||||
|
||||
// lowercasePath normalises URL paths to lowercase so that short links are case-insensitive.
|
||||
// Admin, uploads, and static routes are excluded to preserve asset names.
|
||||
func lowercasePath(next http.Handler) http.Handler {
|
||||
@@ -165,7 +171,7 @@ func (s *Server) Start() error {
|
||||
addr := fmt.Sprintf(":%d", s.cfg.Server.Port)
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: lowercasePath(securityHeaders(mux)),
|
||||
Handler: lowercasePath(s.securityHeaders(mux)),
|
||||
ReadTimeout: s.cfg.Server.ReadTimeoutDuration(),
|
||||
WriteTimeout: s.cfg.Server.WriteTimeoutDuration(),
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user