From fc1484aebeb720c5b7085e9fbe94de3b73b05d74 Mon Sep 17 00:00:00 2001 From: lbenedar Date: Thu, 5 Mar 2026 12:54:29 +0300 Subject: [PATCH] ch6.2 --- cmd/web/middleware.go | 16 ++++++++++++++++ cmd/web/routes.go | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 cmd/web/middleware.go diff --git a/cmd/web/middleware.go b/cmd/web/middleware.go new file mode 100644 index 0000000..c35458e --- /dev/null +++ b/cmd/web/middleware.go @@ -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) + }) +} diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 8d37b37..da5ea9f 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -2,7 +2,7 @@ package main import "net/http" -func (app *application) routes() *http.ServeMux { +func (app *application) routes() http.Handler { mux := http.NewServeMux() 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/create", app.snippetCreate) - return mux + return secureHeaders(mux) }