Skip to content

Commit

Permalink
server/tailsql: set a default Content-Security-Policy in UI responses
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed Aug 14, 2023
1 parent 71e55c2 commit 25bf316
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions server/tailsql/tailsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ var siteAccessCookie = &http.Cookie{
Name: "tailsqlQuery", Value: "1", SameSite: http.SameSiteLaxMode, HttpOnly: true,
}

// contentSecurityPolicy is the CSP value sent for all requests to the UI.
// Adapted from https://owasp.org/www-community/controls/Content_Security_Policy.
const contentSecurityPolicy = `default-src 'none'; script-src 'self'; connect-src 'self'; img-src 'self'; style-src 'self'; frame-ancestors 'self'; form-action 'self';`

func requestHasSiteAccess(r *http.Request) bool {
c, err := r.Cookie(siteAccessCookie.Name)
return err == nil && c.Value == siteAccessCookie.Value
Expand Down Expand Up @@ -282,6 +286,7 @@ func (s *Server) serveUI(w http.ResponseWriter, r *http.Request) {
// serveUIInternal handles the root GET "/" route.
func (s *Server) serveUIInternal(w http.ResponseWriter, r *http.Request, caller, src, query string) error {
http.SetCookie(w, siteAccessCookie)
w.Header().Set("Content-Security-Policy", contentSecurityPolicy)

// If a non-empty query is present, require either a site access cookie or a
// no-browsers header.
Expand Down

0 comments on commit 25bf316

Please sign in to comment.