Contains the Concourse pipeline definition for building a line-server container
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
946 B

  1. package main
  2. import (
  3. "net/http"
  4. )
  5. const (
  6. cspHeader = "Content-Security-Policy"
  7. rpHeader = "Referrer-Policy"
  8. frameOptionsHeader = "X-Frame-Options"
  9. )
  10. type csp struct {
  11. h http.Handler
  12. opts CSPOptions
  13. }
  14. type CSPOptions struct {
  15. policy string
  16. referrerPolicy string
  17. frame string
  18. }
  19. func (c csp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  20. // only add a CSP if one is not already set
  21. if existing := w.Header().Get(cspHeader); existing == "" {
  22. w.Header().Add(cspHeader, c.opts.policy)
  23. }
  24. // only add a Referrer Policy if one is not already set
  25. if existing := w.Header().Get(rpHeader); existing == "" {
  26. w.Header().Add(rpHeader, c.opts.referrerPolicy)
  27. }
  28. w.Header().Set(frameOptionsHeader, c.opts.frame)
  29. c.h.ServeHTTP(w, r)
  30. }
  31. func ContentSecurityPolicy(o CSPOptions) func(http.Handler) http.Handler {
  32. fn := func(h http.Handler) http.Handler {
  33. return csp{h, o}
  34. }
  35. return fn
  36. }