csp.go 1.4 KB

12345678910111213141516171819202122232425262728
  1. package handlers
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "codeberg.org/vnpower/pixivfe/v2/session"
  7. )
  8. func SetPrivacyHeaders(h http.Handler) http.Handler {
  9. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  10. header := w.Header()
  11. header.Add("Referrer-Policy", "same-origin") // needed for settings redirect
  12. header.Add("X-Frame-Options", "DENY")
  13. // use this if need iframe: `X-Frame-Options: SAMEORIGIN`
  14. header.Add("X-Content-Type-Options", "nosniff")
  15. header.Add("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
  16. // use this if need iframe: `frame-ancestors 'self'`
  17. header.Add("Permissions-Policy", "accelerometer=(), ambient-light-sensor=(), battery=(), camera=(), display-capture=(), document-domain=(), encrypted-media=(), execution-while-not-rendered=(), execution-while-out-of-viewport=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), navigation-override=(), payment=(), publickey-credentials-get=(), screen-wake-lock=(), sync-xhr=(), usb=(), web-share=(), xr-spatial-tracking=()")
  18. if !strings.HasPrefix(r.URL.Path, "/diagnostics") {
  19. header.Add("Content-Security-Policy", fmt.Sprintf("base-uri 'self'; default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' %s; media-src 'self' %s; connect-src 'self'; form-action 'self'; frame-ancestors 'none';", session.GetImageProxyOrigin(r), session.GetImageProxyOrigin(r)))
  20. }
  21. h.ServeHTTP(w, r)
  22. })
  23. }