mirror of https://github.com/hashicorp/consul
explicitly add content-type anywhere possible and add middleware to set and warn
parent
8d2178d83d
commit
52f4b86c5c
|
@ -6,6 +6,7 @@ package agent
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/hashicorp/go-hclog"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -43,6 +44,13 @@ import (
|
||||||
"github.com/hashicorp/consul/proto/private/pbcommon"
|
"github.com/hashicorp/consul/proto/private/pbcommon"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
contentTypeHeader = "Content-Type"
|
||||||
|
plainContentType = "text/plain"
|
||||||
|
htmlContentType = "text/html"
|
||||||
|
jsonContentType = "application/json"
|
||||||
|
)
|
||||||
|
|
||||||
var HTTPSummaries = []prometheus.SummaryDefinition{
|
var HTTPSummaries = []prometheus.SummaryDefinition{
|
||||||
{
|
{
|
||||||
Name: []string{"api", "http"},
|
Name: []string{"api", "http"},
|
||||||
|
@ -220,6 +228,7 @@ func (s *HTTPHandlers) handler() http.Handler {
|
||||||
// If enableDebug register wrapped pprof handlers
|
// If enableDebug register wrapped pprof handlers
|
||||||
if !s.agent.enableDebug.Load() && s.checkACLDisabled() {
|
if !s.agent.enableDebug.Load() && s.checkACLDisabled() {
|
||||||
resp.WriteHeader(http.StatusNotFound)
|
resp.WriteHeader(http.StatusNotFound)
|
||||||
|
req.Header.Set(contentTypeHeader, plainContentType)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -228,6 +237,7 @@ func (s *HTTPHandlers) handler() http.Handler {
|
||||||
|
|
||||||
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
authz, err := s.agent.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
req.Header.Set(contentTypeHeader, plainContentType)
|
||||||
resp.WriteHeader(http.StatusForbidden)
|
resp.WriteHeader(http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -238,6 +248,7 @@ func (s *HTTPHandlers) handler() http.Handler {
|
||||||
// TODO(acl-error-enhancements): We should return error details somehow here.
|
// TODO(acl-error-enhancements): We should return error details somehow here.
|
||||||
if authz.OperatorRead(nil) != acl.Allow {
|
if authz.OperatorRead(nil) != acl.Allow {
|
||||||
resp.WriteHeader(http.StatusForbidden)
|
resp.WriteHeader(http.StatusForbidden)
|
||||||
|
req.Header.Set(contentTypeHeader, plainContentType)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -317,6 +328,9 @@ func (s *HTTPHandlers) handler() http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
h = withRemoteAddrHandler(h)
|
h = withRemoteAddrHandler(h)
|
||||||
|
|
||||||
|
h = ensureContentTypeHeader(h, s.agent.logger)
|
||||||
|
|
||||||
s.h = &wrappedMux{
|
s.h = &wrappedMux{
|
||||||
mux: mux,
|
mux: mux,
|
||||||
handler: h,
|
handler: h,
|
||||||
|
@ -337,6 +351,20 @@ func withRemoteAddrHandler(next http.Handler) http.Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Injects content type explicitly if not already set into request to prevent XSS
|
||||||
|
func ensureContentTypeHeader(next http.Handler, logger hclog.Logger) http.Handler {
|
||||||
|
|
||||||
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
||||||
|
val := req.Header.Get(contentTypeHeader)
|
||||||
|
if val == "" {
|
||||||
|
req.Header.Set(contentTypeHeader, plainContentType)
|
||||||
|
logger.Debug("warning: content-type header not explicitly set.", "request-path", req.URL)
|
||||||
|
|
||||||
|
}
|
||||||
|
next.ServeHTTP(resp, req)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// nodeName returns the node name of the agent
|
// nodeName returns the node name of the agent
|
||||||
func (s *HTTPHandlers) nodeName() string {
|
func (s *HTTPHandlers) nodeName() string {
|
||||||
return s.agent.config.NodeName
|
return s.agent.config.NodeName
|
||||||
|
@ -380,6 +408,8 @@ func (s *HTTPHandlers) wrap(handler endpoint, methods []string) http.HandlerFunc
|
||||||
"from", req.RemoteAddr,
|
"from", req.RemoteAddr,
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
|
//set response type to plain to prevent XSS
|
||||||
|
resp.Header().Set(contentTypeHeader, plainContentType)
|
||||||
resp.WriteHeader(http.StatusInternalServerError)
|
resp.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -406,6 +436,8 @@ func (s *HTTPHandlers) wrap(handler endpoint, methods []string) http.HandlerFunc
|
||||||
"from", req.RemoteAddr,
|
"from", req.RemoteAddr,
|
||||||
"error", errMsg,
|
"error", errMsg,
|
||||||
)
|
)
|
||||||
|
//set response type to plain to prevent XSS
|
||||||
|
resp.Header().Set(contentTypeHeader, plainContentType)
|
||||||
resp.WriteHeader(http.StatusForbidden)
|
resp.WriteHeader(http.StatusForbidden)
|
||||||
fmt.Fprint(resp, errMsg)
|
fmt.Fprint(resp, errMsg)
|
||||||
return
|
return
|
||||||
|
@ -585,6 +617,8 @@ func (s *HTTPHandlers) wrap(handler endpoint, methods []string) http.HandlerFunc
|
||||||
resp.Header().Add("X-Consul-Reason", errPayload.Reason)
|
resp.Header().Add("X-Consul-Reason", errPayload.Reason)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
//set response type to plain to prevent XSS
|
||||||
|
resp.Header().Set(contentTypeHeader, plainContentType)
|
||||||
handleErr(err)
|
handleErr(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -596,6 +630,8 @@ func (s *HTTPHandlers) wrap(handler endpoint, methods []string) http.HandlerFunc
|
||||||
if contentType == "application/json" {
|
if contentType == "application/json" {
|
||||||
buf, err = s.marshalJSON(req, obj)
|
buf, err = s.marshalJSON(req, obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
//set response type to plain to prevent XSS
|
||||||
|
resp.Header().Set(contentTypeHeader, plainContentType)
|
||||||
handleErr(err)
|
handleErr(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -606,7 +642,7 @@ func (s *HTTPHandlers) wrap(handler endpoint, methods []string) http.HandlerFunc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resp.Header().Set("Content-Type", contentType)
|
resp.Header().Set(contentTypeHeader, contentType)
|
||||||
resp.WriteHeader(httpCode)
|
resp.WriteHeader(httpCode)
|
||||||
resp.Write(buf)
|
resp.Write(buf)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue