Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shared memory issue when stripping authorization header from bid requests #1790

Merged
merged 5 commits into from
Apr 6, 2021
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
13 changes: 5 additions & 8 deletions exchange/bidder.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ func (bidder *bidderAdapter) requestBid(ctx context.Context, request *openrtb2.B
if debugInfo := ctx.Value(DebugContextKey); debugInfo != nil && debugInfo.(bool) {
if accountDebugAllowed {
if bidder.config.DebugInfo.Allow {
// it's safe to mutate the request headers since from this point on the
// information is only used for debugging.
removeSensitiveHeaders(httpInfo.request.Headers)

seatBid.httpCalls = append(seatBid.httpCalls, makeExt(httpInfo))
} else {
debugDisabledWarning := errortypes.Warning{
Expand Down Expand Up @@ -331,9 +327,10 @@ func getAssetByID(id int64, assets []nativeRequests.Asset) (nativeRequests.Asset

var authorizationHeader = http.CanonicalHeaderKey("authorization")

// removeSensitiveHeaders mutates the http header object to remove sensitive information.
func removeSensitiveHeaders(h http.Header) {
h.Del(authorizationHeader)
func filterHeader(h http.Header) http.Header {
clone := h.Clone()
clone.Del(authorizationHeader)
return clone
}

// makeExt transforms information about the HTTP call into the contract class for the PBS response.
Expand All @@ -343,7 +340,7 @@ func makeExt(httpInfo *httpCallInfo) *openrtb_ext.ExtHttpCall {
if httpInfo != nil && httpInfo.request != nil {
ext.Uri = httpInfo.request.Uri
ext.RequestBody = string(httpInfo.request.Body)
ext.RequestHeaders = httpInfo.request.Headers
ext.RequestHeaders = filterHeader(httpInfo.request.Headers)

if httpInfo.err == nil && httpInfo.response != nil {
ext.ResponseBody = string(httpInfo.response.Body)
Expand Down
52 changes: 48 additions & 4 deletions exchange/bidder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,50 @@ func TestMakeExt(t *testing.T) {
Status: 999,
},
},
{
description: "Request & Response - No Error with Authorization removal",
given: &httpCallInfo{
err: nil,
request: &adapters.RequestData{
Uri: "requestUri",
Body: []byte("requestBody"),
Headers: makeHeader(map[string][]string{"Key1": {"value1", "value2"}, "Authorization": {"secret"}}),
},
response: &adapters.ResponseData{
Body: []byte("responseBody"),
StatusCode: 999,
},
},
expected: &openrtb_ext.ExtHttpCall{
Uri: "requestUri",
RequestBody: "requestBody",
RequestHeaders: map[string][]string{"Key1": {"value1", "value2"}},
ResponseBody: "responseBody",
Status: 999,
},
},
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
{
description: "Request & Response - No Error with nil header",
given: &httpCallInfo{
err: nil,
request: &adapters.RequestData{
Uri: "requestUri",
Body: []byte("requestBody"),
Headers: nil,
},
response: &adapters.ResponseData{
Body: []byte("responseBody"),
StatusCode: 999,
},
},
expected: &openrtb_ext.ExtHttpCall{
Uri: "requestUri",
RequestBody: "requestBody",
RequestHeaders: nil,
ResponseBody: "responseBody",
Status: 999,
},
SyntaxNode marked this conversation as resolved.
Show resolved Hide resolved
},
{
description: "Request & Response - Error",
given: &httpCallInfo{
Expand Down Expand Up @@ -1029,7 +1073,7 @@ func TestMakeExt(t *testing.T) {
}
}

func TestRemoveSensitiveHeaders(t *testing.T) {
func TestFilterHeader(t *testing.T) {
testCases := []struct {
description string
given http.Header
Expand All @@ -1043,7 +1087,7 @@ func TestRemoveSensitiveHeaders(t *testing.T) {
{
description: "Empty",
given: http.Header{},
expected: map[string][]string{},
expected: http.Header{},
},
{
description: "One",
Expand Down Expand Up @@ -1073,8 +1117,8 @@ func TestRemoveSensitiveHeaders(t *testing.T) {
}

for _, test := range testCases {
removeSensitiveHeaders(test.given)
assert.Equal(t, test.expected, test.given, test.description)
result := filterHeader(test.given)
assert.Equal(t, test.expected, result, test.description)
}
}

Expand Down