Skip to content

Commit

Permalink
Stop encoding HTML (prebid#236)
Browse files Browse the repository at this point in the history
* Fixed the encoding bug.

* Added newline.
  • Loading branch information
dbemiller authored and DucChau committed Dec 4, 2017
1 parent 061fae0 commit 7e089b6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
17 changes: 10 additions & 7 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"github.com/prebid/prebid-server/openrtb_ext"
"time"
"github.com/golang/glog"
)

func NewEndpoint(ex exchange.Exchange, validator openrtb_ext.BidderParamValidator) (httprouter.Handle, error) {
Expand Down Expand Up @@ -46,14 +47,16 @@ func (deps *endpointDeps) Auction(w http.ResponseWriter, r *http.Request, _ http
return
}

responseBytes, err := json.Marshal(response)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Failed to marshal auction response: %v", err)
}
// Fixes #231
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)

w.WriteHeader(200)
w.Write(responseBytes)
// If an error happens when encoding the response, there isn't much we can do.
// If we've sent _any_ bytes, then Go would have sent the 200 status code first.
// That status code can't be un-sent... so the best we can do is log the error.
if err := enc.Encode(response); err != nil {
glog.Errorf("/openrtb2/auction Error encoding response: %v", err)
}
}

// parseRequest turns the HTTP request into an OpenRTB request.
Expand Down
25 changes: 25 additions & 0 deletions endpoints/openrtb2/auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ func TestExchangeError(t *testing.T) {
}
}


// TestNoEncoding prevents #231.
func TestNoEncoding(t *testing.T) {
endpoint, _ := NewEndpoint(&mockExchange{}, &bidderParamValidator{})
request := httptest.NewRequest("POST", "/openrtb2/auction", strings.NewReader(validRequests[0]))
recorder := httptest.NewRecorder()
endpoint(recorder, request, nil)

if !strings.Contains(recorder.Body.String(), "<script></script>") {
t.Errorf("The Response from the exchange should not be html-encoded")
}
}

// nobidExchange is a well-behaved exchange which always bids "no bid".
type nobidExchange struct {}

Expand Down Expand Up @@ -283,3 +296,15 @@ var invalidRequests = []string{
}
}]}`,
}

type mockExchange struct {}

func (*mockExchange) HoldAuction(ctx context.Context, bidRequest *openrtb.BidRequest) (*openrtb.BidResponse, error) {
return &openrtb.BidResponse{
SeatBid: []openrtb.SeatBid{{
Bid: []openrtb.Bid{{
AdM: "<script></script>",
}},
}},
}, nil
}

0 comments on commit 7e089b6

Please sign in to comment.