This commit is contained in:
lbenedar
2026-03-05 12:54:29 +03:00
parent 420b876a7a
commit fc1484aebe
2 changed files with 18 additions and 2 deletions

16
cmd/web/middleware.go Normal file
View File

@@ -0,0 +1,16 @@
package main
import "net/http"
func secureHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Security-Policy",
"default-src 'self'; style-src 'self' fonts.googleapis.com; font-src fonts.gstatic.com")
w.Header().Set("Referrer-Policy", "origin-when-cross-origin")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("X-Frame-Options", "deny")
w.Header().Set("X-XSS-Protection", "0")
next.ServeHTTP(w, r)
})
}

View File

@@ -2,7 +2,7 @@ package main
import "net/http" import "net/http"
func (app *application) routes() *http.ServeMux { func (app *application) routes() http.Handler {
mux := http.NewServeMux() mux := http.NewServeMux()
fileServer := http.FileServer(http.Dir("./ui/static/")) fileServer := http.FileServer(http.Dir("./ui/static/"))
@@ -12,5 +12,5 @@ func (app *application) routes() *http.ServeMux {
mux.HandleFunc("/snippet/view", app.snippetView) mux.HandleFunc("/snippet/view", app.snippetView)
mux.HandleFunc("/snippet/create", app.snippetCreate) mux.HandleFunc("/snippet/create", app.snippetCreate)
return mux return secureHeaders(mux)
} }