Skip to content
This repository was archived by the owner on Dec 22, 2022. It is now read-only.

Commit 008d0ae

Browse files
VeronikaSolovei9Veronika Solovei
and
Veronika Solovei
authored
Added header User Agent decoding (prebid#1268)
* Added header User Agent decoding * Added header User Agent decoding: unit tests * Added header User Agent decoding: unit tests * Added check UA is encoded to avoid `+` converted to space Co-authored-by: Veronika Solovei <[email protected]>
1 parent 44744f3 commit 008d0ae

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

endpoints/openrtb2/video_auction.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import (
88
"io"
99
"io/ioutil"
1010
"net/http"
11+
"net/url"
1112
"strconv"
1213
"strings"
1314
"time"
1415

1516
"github.com/buger/jsonparser"
16-
jsonpatch "github.com/evanphx/json-patch"
17+
"github.com/evanphx/json-patch"
1718
"github.com/gofrs/uuid"
1819
"github.com/prebid/prebid-server/errortypes"
1920

@@ -617,7 +618,18 @@ func (deps *endpointDeps) parseVideoRequest(request []byte, headers http.Header)
617618

618619
//if Device.UA is not present in request body, init it with user-agent from request header if it's present
619620
if req.Device.UA == "" {
620-
req.Device.UA = headers.Get("User-Agent")
621+
ua := headers.Get("User-Agent")
622+
623+
//Check UA is encoded. Without it the `+` character would get changed to a space if not actually encoded
624+
if strings.ContainsAny(ua, "%") {
625+
var err error
626+
req.Device.UA, err = url.QueryUnescape(ua)
627+
if err != nil {
628+
req.Device.UA = ua
629+
}
630+
} else {
631+
req.Device.UA = ua
632+
}
621633
}
622634

623635
errL, podErrors := deps.validateVideoRequest(req)

endpoints/openrtb2/video_auction_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,49 @@ func TestParseVideoRequestWithoutUserAgentAndEmptyHeader(t *testing.T) {
927927

928928
}
929929

930+
func TestParseVideoRequestWithEncodedUserAgentInHeader(t *testing.T) {
931+
ex := &mockExchangeVideo{}
932+
reqData, err := ioutil.ReadFile("sample-requests/video/video_valid_sample_without_device_user_agent.json")
933+
if err != nil {
934+
t.Fatalf("Failed to fetch a valid request: %v", err)
935+
}
936+
937+
uaEncoded := "Mozilla%2F5.0%20%28Macintosh%3B%20Intel%20Mac%20OS%20X%2010_14_6%29%20AppleWebKit%2F537.36%20%28KHTML%2C%20like%20Gecko%29%20Chrome%2F78.0.3904.87%20Safari%2F537.36"
938+
uaDecoded := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36"
939+
940+
headers := http.Header{}
941+
headers.Add("User-Agent", uaEncoded)
942+
943+
deps := mockDeps(t, ex)
944+
req, valErr, podErr := deps.parseVideoRequest(reqData, headers)
945+
946+
assert.Equal(t, uaDecoded, req.Device.UA, "Device.ua should be taken from request header")
947+
assert.Equal(t, []error(nil), valErr, "No validation errors should be returned")
948+
assert.Equal(t, make([]PodError, 0), podErr, "No pod errors should be returned")
949+
950+
}
951+
952+
func TestParseVideoRequestWithDecodedUserAgentInHeader(t *testing.T) {
953+
ex := &mockExchangeVideo{}
954+
reqData, err := ioutil.ReadFile("sample-requests/video/video_valid_sample_without_device_user_agent.json")
955+
if err != nil {
956+
t.Fatalf("Failed to fetch a valid request: %v", err)
957+
}
958+
959+
uaDecoded := "Mozilla/5.0+(Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36"
960+
961+
headers := http.Header{}
962+
headers.Add("User-Agent", uaDecoded)
963+
964+
deps := mockDeps(t, ex)
965+
req, valErr, podErr := deps.parseVideoRequest(reqData, headers)
966+
967+
assert.Equal(t, uaDecoded, req.Device.UA, "Device.ua should be taken from request header")
968+
assert.Equal(t, []error(nil), valErr, "No validation errors should be returned")
969+
assert.Equal(t, make([]PodError, 0), podErr, "No pod errors should be returned")
970+
971+
}
972+
930973
func TestHandleErrorDebugLog(t *testing.T) {
931974
vo := analytics.VideoObject{
932975
Status: 200,

0 commit comments

Comments
 (0)