2021-07-27 10:51:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func noIndexHeader(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("X-Robots-Tag", "noindex")
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func fixHTTPHandler(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.URL.RawPath = ""
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) securityHeaders(next http.Handler) http.Handler {
|
|
|
|
// Build CSP domains list
|
|
|
|
var cspBuilder strings.Builder
|
|
|
|
if mp := a.cfg.Micropub.MediaStorage; mp != nil && mp.MediaURL != "" {
|
|
|
|
if u, err := url.Parse(mp.MediaURL); err == nil {
|
|
|
|
cspBuilder.WriteByte(' ')
|
|
|
|
cspBuilder.WriteString(u.Hostname())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(a.cfg.Server.CSPDomains) > 0 {
|
|
|
|
cspBuilder.WriteByte(' ')
|
|
|
|
cspBuilder.WriteString(strings.Join(a.cfg.Server.CSPDomains, " "))
|
|
|
|
}
|
|
|
|
cspDomains := cspBuilder.String()
|
|
|
|
// Return handler
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Strict-Transport-Security", "max-age=31536000;")
|
|
|
|
w.Header().Set("Referrer-Policy", "no-referrer")
|
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
w.Header().Set("X-Frame-Options", "SAMEORIGIN")
|
|
|
|
w.Header().Set("X-Xss-Protection", "1; mode=block")
|
2021-11-10 10:13:30 +00:00
|
|
|
w.Header().Set("Content-Security-Policy", "default-src 'self'"+cspDomains+"; img-src 'self' "+cspDomains+" data:")
|
2021-08-02 19:10:38 +00:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) addOnionLocation(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-08-02 18:43:24 +00:00
|
|
|
if a.torAddress != "" {
|
|
|
|
w.Header().Set("Onion-Location", a.torAddress+r.RequestURI)
|
2021-07-27 10:51:08 +00:00
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|