Skip to content
Merged
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
16 changes: 10 additions & 6 deletions httpbin/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,19 +341,23 @@ func (h *HTTPBin) Unstable(w http.ResponseWriter, r *http.Request) {
// values in the JSON response body will be escaped.
func (h *HTTPBin) ResponseHeaders(w http.ResponseWriter, r *http.Request) {
args := r.URL.Query()

// only set our own content type if one was not already set based on
// incoming request params
contentType := args.Get("Content-Type")
if contentType == "" {
contentType = jsonContentType
args.Set("Content-Type", contentType)
}

// response headers are not escaped, regardless of content type
// actual HTTP response headers are not escaped, regardless of content type
// (unlike the JSON serialized representation of those headers in the
// response body, which MAY be escaped based on content type)
for k, vs := range args {
for _, v := range vs {
w.Header().Add(k, v)
}
}
// only set our own content type if one was not already set based on
// incoming request params
if contentType == "" {
w.Header().Set("Content-Type", jsonContentType)
}

// if response content type is dangrous, escape keys and values before
// serializing response body
Expand Down
12 changes: 11 additions & 1 deletion httpbin/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,13 @@ func TestResponseHeaders(t *testing.T) {
resultValues := result[k]
assert.DeepEqual(t, resultValues, expectedValues, "JSON response headers mismatch")
}

// if no content-type is specified in the request params, the response
// defaults to JSON.
//
// Note that if this changes, we need to ensure we maintain safety
// around escapig HTML in the response (see the subtest below)
assert.Header(t, resp, "Content-Type", jsonContentType)
})

t.Run("override content-type", func(t *testing.T) {
Expand Down Expand Up @@ -1271,8 +1278,11 @@ func TestResponseHeaders(t *testing.T) {
{"text/plain", false},
{"application/octet-string", false},

// if no content-type is provided, we default to JSON, which is
// safe
{"", false},

// everything else requires escaping
{"", true},
{"application/xml", true},
{"image/png", true},
{"text/html; charset=utf8", true},
Expand Down
Loading