Skip to content

Commit

Permalink
Fix nonce to run in processRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
unrolled committed Oct 22, 2018
1 parent ff9db2f commit 4b6b7cf
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions secure.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ func (s *Secure) SetBadHostHandler(handler http.Handler) {
// Handler implements the http.HandlerFunc for integration with the standard net/http lib.
func (s *Secure) Handler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if s.opt.nonceEnabled {
r = withCSPNonce(r, cspRandNonce())
}

// Let secure process the request. If it returns an error,
// that indicates the request should not continue.
err := s.Process(w, r)
Expand All @@ -153,10 +149,6 @@ func (s *Secure) Handler(h http.Handler) http.Handler {
// Note that this is for requests only and will not write any headers.
func (s *Secure) HandlerForRequestOnly(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if s.opt.nonceEnabled {
r = withCSPNonce(r, cspRandNonce())
}

// Let secure process the request. If it returns an error,
// that indicates the request should not continue.
responseHeader, err := s.processRequest(w, r)
Expand All @@ -176,10 +168,6 @@ func (s *Secure) HandlerForRequestOnly(h http.Handler) http.Handler {

// HandlerFuncWithNext is a special implementation for Negroni, but could be used elsewhere.
func (s *Secure) HandlerFuncWithNext(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
if s.opt.nonceEnabled {
r = withCSPNonce(r, cspRandNonce())
}

// Let secure process the request. If it returns an error,
// that indicates the request should not continue.
err := s.Process(w, r)
Expand All @@ -193,10 +181,6 @@ func (s *Secure) HandlerFuncWithNext(w http.ResponseWriter, r *http.Request, nex
// HandlerFuncWithNextForRequestOnly is a special implementation for Negroni, but could be used elsewhere.
// Note that this is for requests only and will not write any headers.
func (s *Secure) HandlerFuncWithNextForRequestOnly(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
if s.opt.nonceEnabled {
r = withCSPNonce(r, cspRandNonce())
}

// Let secure process the request. If it returns an error,
// that indicates the request should not continue.
responseHeader, err := s.processRequest(w, r)
Expand Down Expand Up @@ -226,6 +210,11 @@ func (s *Secure) Process(w http.ResponseWriter, r *http.Request) error {

// processRequest runs the actual checks on the request and returns an error if the middleware chain should stop.
func (s *Secure) processRequest(w http.ResponseWriter, r *http.Request) (http.Header, error) {
// Setup nouce if required.
if s.opt.nonceEnabled {
r = withCSPNonce(r, cspRandNonce())
}

// Resolve the host for the request, using proxy headers if present.
host := r.Host
for _, header := range s.opt.HostsProxyHeaders {
Expand Down

0 comments on commit 4b6b7cf

Please sign in to comment.