common_headers.go 806 B

12345678910111213141516171819202122232425
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package middleware
  5. import (
  6. "net/http"
  7. )
  8. // CommonHeaders sends common HTTP headers.
  9. func (m *Middleware) CommonHeaders(next http.Handler) http.Handler {
  10. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  11. w.Header().Set("X-XSS-Protection", "1; mode=block")
  12. w.Header().Set("X-Content-Type-Options", "nosniff")
  13. w.Header().Set("X-Frame-Options", "DENY")
  14. w.Header().Set("Content-Security-Policy", "default-src 'self'; img-src *; media-src *; frame-src *; child-src *")
  15. if m.cfg.IsHTTPS && m.cfg.HasHSTS() {
  16. w.Header().Set("Strict-Transport-Security", "max-age=31536000")
  17. }
  18. next.ServeHTTP(w, r)
  19. })
  20. }