Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/22598.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:security
api: add charset in all applicable content-types.
```
37 changes: 19 additions & 18 deletions agent/checks/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
"testing"
"time"

"github.com/hashicorp/go-uuid"
"github.com/stretchr/testify/require"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"

"github.com/hashicorp/go-uuid"

"github.com/hashicorp/consul/agent/mock"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/api"
Expand Down Expand Up @@ -689,7 +690,7 @@ func TestCheckHTTPBody(t *testing.T) {
}{
{desc: "get body", method: "GET", body: "hello world"},
{desc: "post body", method: "POST", body: "hello world"},
{desc: "post json body", header: http.Header{"Content-Type": []string{"application/json"}}, method: "POST", body: "{\"foo\":\"bar\"}"},
{desc: "post json body", header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}}, method: "POST", body: "{\"foo\":\"bar\"}"},
}

for _, tt := range tests {
Expand Down Expand Up @@ -1561,7 +1562,7 @@ func TestCheck_Docker(t *testing.T) {
handlers: map[string]http.HandlerFunc{
"POST /containers/123/exec": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `this is not json`)
},
},
Expand All @@ -1573,7 +1574,7 @@ func TestCheck_Docker(t *testing.T) {
handlers: map[string]http.HandlerFunc{
"POST /containers/123/exec": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{"Id":"456"}`)
},
"POST /exec/456/start": func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -1588,7 +1589,7 @@ func TestCheck_Docker(t *testing.T) {
handlers: map[string]http.HandlerFunc{
"POST /containers/123/exec": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{"Id":"456"}`)
},
"POST /exec/456/start": func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -1603,7 +1604,7 @@ func TestCheck_Docker(t *testing.T) {
handlers: map[string]http.HandlerFunc{
"POST /containers/123/exec": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{"Id":"456"}`)
},
"POST /exec/456/start": func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -1619,7 +1620,7 @@ func TestCheck_Docker(t *testing.T) {
handlers: map[string]http.HandlerFunc{
"POST /containers/123/exec": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{"Id":"456"}`)
},
"POST /exec/456/start": func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -1638,7 +1639,7 @@ func TestCheck_Docker(t *testing.T) {
handlers: map[string]http.HandlerFunc{
"POST /containers/123/exec": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{"Id":"456"}`)
},
"POST /exec/456/start": func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -1658,7 +1659,7 @@ func TestCheck_Docker(t *testing.T) {
handlers: map[string]http.HandlerFunc{
"POST /containers/123/exec": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{"Id":"456"}`)
},
"POST /exec/456/start": func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -1667,7 +1668,7 @@ func TestCheck_Docker(t *testing.T) {
},
"GET /exec/456/json": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `this is not json`)
},
},
Expand All @@ -1679,7 +1680,7 @@ func TestCheck_Docker(t *testing.T) {
handlers: map[string]http.HandlerFunc{
"POST /containers/123/exec": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{"Id":"456"}`)
},
"POST /exec/456/start": func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -1688,7 +1689,7 @@ func TestCheck_Docker(t *testing.T) {
},
"GET /exec/456/json": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{"ExitCode":0}`)
},
},
Expand All @@ -1700,7 +1701,7 @@ func TestCheck_Docker(t *testing.T) {
handlers: map[string]http.HandlerFunc{
"POST /containers/123/exec": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{"Id":"456"}`)
},
"POST /exec/456/start": func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -1709,7 +1710,7 @@ func TestCheck_Docker(t *testing.T) {
},
"GET /exec/456/json": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{"ExitCode":0}`)
},
},
Expand All @@ -1721,7 +1722,7 @@ func TestCheck_Docker(t *testing.T) {
handlers: map[string]http.HandlerFunc{
"POST /containers/123/exec": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{"Id":"456"}`)
},
"POST /exec/456/start": func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -1730,7 +1731,7 @@ func TestCheck_Docker(t *testing.T) {
},
"GET /exec/456/json": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{"ExitCode":1}`)
},
},
Expand All @@ -1742,7 +1743,7 @@ func TestCheck_Docker(t *testing.T) {
handlers: map[string]http.HandlerFunc{
"POST /containers/123/exec": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{"Id":"456"}`)
},
"POST /exec/456/start": func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -1751,7 +1752,7 @@ func TestCheck_Docker(t *testing.T) {
},
"GET /exec/456/json": func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{"ExitCode":2}`)
},
},
Expand Down
2 changes: 1 addition & 1 deletion agent/consul/authmethod/kubeauth/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (s *TestAPIServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
s.mu.Lock()
defer s.mu.Unlock()

w.Header().Set("content-type", "application/json")
w.Header().Set("content-type", "application/json; charset=utf-8")

if req.URL.Path == "/apis/authentication.k8s.io/v1/tokenreviews" {
s.handleTokenReview(w, req)
Expand Down
4 changes: 2 additions & 2 deletions agent/hcp/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (s *MockHCPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

log.Printf("OK 200: %s %s\n", r.Method, r.URL.Path)
w.Header().Set("content-type", "application/json")
w.Header().Set("content-type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(bs)
}
Expand All @@ -137,7 +137,7 @@ func enforceMethod(w http.ResponseWriter, r *http.Request, methods []string) boo
}

func mockTokenResponse(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)

w.Write([]byte(`{"access_token": "token", "token_type": "Bearer"}`))
Expand Down
11 changes: 6 additions & 5 deletions agent/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ import (
"time"

"github.com/NYTimes/gziphandler"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/http2"

"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-hclog"

"github.com/hashicorp/consul/agent/config"
"github.com/hashicorp/consul/agent/consul"
"github.com/hashicorp/consul/agent/structs"
Expand Down Expand Up @@ -638,7 +639,7 @@ func TestHTTPAPIResponseHeaders(t *testing.T) {
`)
defer a.Shutdown()

requireHasHeadersSet(t, a, "/v1/agent/self", "application/json")
requireHasHeadersSet(t, a, "/v1/agent/self", "application/json; charset=utf-8")

// Check the Index page that just renders a simple message with UI disabled
// also gets the right headers.
Expand Down Expand Up @@ -686,7 +687,7 @@ func TestHTTPAPIValidateContentTypeHeaders(t *testing.T) {
method: http.MethodPost,
endpoint: "/v1/peering/token",
requestBody: bytes.NewBuffer([]byte("test")),
expectedContentType: "application/json",
expectedContentType: "application/json; charset=utf-8",
},
}

Expand Down Expand Up @@ -784,7 +785,7 @@ func TestErrorContentTypeHeaderSet(t *testing.T) {
`)
defer a.Shutdown()

requireHasHeadersSet(t, a, "/fake-path-doesn't-exist", "application/json")
requireHasHeadersSet(t, a, "/fake-path-doesn't-exist", "application/json; charset=utf-8")
}

func TestAcceptEncodingGzip(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion agent/kvs_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (s *HTTPHandlers) KVSGet(resp http.ResponseWriter, req *http.Request, args
if _, ok := params["raw"]; ok && method == "KVS.Get" {
body := out.Entries[0].Value
resp.Header().Set("Content-Length", strconv.FormatInt(int64(len(body)), 10))
resp.Header().Set("Content-Type", "text/plain")
resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
resp.Header().Set("X-Content-Type-Options", "nosniff")
resp.Header().Set("Content-Security-Policy", "sandbox")
resp.Write(body)
Expand Down
5 changes: 2 additions & 3 deletions agent/kvs_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import (
"reflect"
"testing"

"github.com/hashicorp/consul/testrpc"

"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/testrpc"
)

func TestKVSEndpoint_PUT_GET_DELETE(t *testing.T) {
Expand Down Expand Up @@ -458,7 +457,7 @@ func TestKVSEndpoint_GET_Raw(t *testing.T) {
if len(contentTypeHdr) != 1 {
t.Fatalf("expected 1 value for Content-Type header, got %d: %+v", len(contentTypeHdr), contentTypeHdr)
}
if contentTypeHdr[0] != "text/plain" {
if contentTypeHdr[0] != "text/plain; charset=utf-8" {
t.Fatalf("expected Content-Type header to be \"text/plain\", got %q", contentTypeHdr[0])
}

Expand Down
2 changes: 1 addition & 1 deletion agent/txn_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ func (s *HTTPHandlers) Txn(resp http.ResponseWriter, req *http.Request) (interfa
return nil, err
}

resp.Header().Set("Content-Type", "application/json")
resp.Header().Set("Content-Type", "application/json; charset=utf-8")
resp.WriteHeader(http.StatusConflict)
resp.Write(buf)
return nil, nil
Expand Down
5 changes: 2 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1033,12 +1033,11 @@ func (r *request) toHTTP() (*http.Request, error) {
req.Header = r.header

// Content-Type must always be set when a body is present
// See https://github.com/hashicorp/consul/issues/10011
if req.Body != nil && req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
}

// Setup auth
// Check for a token
if r.config.HttpAuth != nil {
req.SetBasicAuth(r.config.HttpAuth.Username, r.config.HttpAuth.Password)
}
Expand Down
4 changes: 2 additions & 2 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,11 +935,11 @@ func TestAPI_Headers(t *testing.T) {

_, _, err = kv.Get("test-headers", nil)
require.NoError(t, err)
require.Equal(t, "application/json", request.Header.Get("Content-Type"))
require.Equal(t, "application/json; charset=utf-8", request.Header.Get("Content-Type"))

_, err = kv.Delete("test-headers", nil)
require.NoError(t, err)
require.Equal(t, "application/json", request.Header.Get("Content-Type"))
require.Equal(t, "application/json; charset=utf-8", request.Header.Get("Content-Type"))

err = c.Snapshot().Restore(nil, strings.NewReader("foo"))
require.Error(t, err)
Expand Down
2 changes: 1 addition & 1 deletion api/content_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
contentTypeHeader = "Content-Type"
plainContentType = "text/plain; charset=utf-8"
octetStream = "application/octet-stream"
jsonContentType = "application/json" // Default content type
jsonContentType = "application/json; charset=utf-8" // Default content type
)

// ContentTypeRule defines a rule for determining the content type of an HTTP request.
Expand Down
5 changes: 2 additions & 3 deletions command/resource/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,12 +883,11 @@ func (r *request) toHTTP() (*http.Request, error) {
req.Header = r.header

// Content-Type must always be set when a body is present
// See https://github.com/hashicorp/consul/issues/10011
if req.Body != nil && req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
}

// Setup auth
// Check for a token
if r.config.HttpAuth != nil {
req.SetBasicAuth(r.config.HttpAuth.Username, r.config.HttpAuth.Password)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/go-sso/oidcauth/oidcauthtest/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
s.mu.Lock()
defer s.mu.Unlock()

w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json; charset=utf-8")

switch req.URL.Path {
case "/.well-known/openid-configuration":
Expand Down
2 changes: 1 addition & 1 deletion website/content/docs/discover/vm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: >-

# Discover services on virtual machines (VMs)

This page provides an overview of Consul service discovery operations on virtual machines. After you register services with Consul, you can address them using Consul DNS to perform application load balancing and static service lookups. You can also create prepared queries for dynamic service lookups and service failover.
This page provides an overview of Consul service discovery operations on virtual machines. After you register services with Consul, you can use Consul DNS to perform application load balancing and static service lookups. You can also create prepared queries for dynamic service lookups and service failover.

## Introduction

Expand Down
Loading
Loading