From dc4a189ed02cce974b5cb1d24efdd9eb21d55899 Mon Sep 17 00:00:00 2001 From: Hans Hjort Date: Wed, 4 Dec 2019 13:50:47 -0500 Subject: [PATCH] Better native request validation --- endpoints/openrtb2/auction.go | 25 +++++++------------ endpoints/openrtb2/auction_test.go | 7 +++--- .../asset-img-no-hmin.json | 0 .../asset-img-no-wmin.json | 0 .../request-no-context.json | 0 .../request-plcmttype-empty.json} | 0 6 files changed, 12 insertions(+), 20 deletions(-) rename endpoints/openrtb2/sample-requests/{invalid-native => valid-native}/asset-img-no-hmin.json (100%) rename endpoints/openrtb2/sample-requests/{invalid-native => valid-native}/asset-img-no-wmin.json (100%) rename endpoints/openrtb2/sample-requests/{invalid-native => valid-native}/request-no-context.json (100%) rename endpoints/openrtb2/sample-requests/{invalid-native/request-no-plcmttype.json => valid-native/request-plcmttype-empty.json} (100%) diff --git a/endpoints/openrtb2/auction.go b/endpoints/openrtb2/auction.go index fd1803597c3..a2a9cf94dcc 100644 --- a/endpoints/openrtb2/auction.go +++ b/endpoints/openrtb2/auction.go @@ -444,6 +444,10 @@ func fillAndValidateNative(n *openrtb.Native, impIndex int) error { } func validateNativeContextTypes(cType native.ContextType, cSubtype native.ContextSubType, impIndex int) error { + if cType == 0 { + // Context is only recommended, so none is a valid type. + return nil + } if cType < native.ContextTypeContent || cType > native.ContextTypeProduct { return fmt.Errorf("request.imp[%d].native.request.context is invalid. See https://iabtechlab.com/wp-content/uploads/2016/07/OpenRTB-Native-Ads-Specification-Final-1.2.pdf#page=39", impIndex) } @@ -480,6 +484,10 @@ func validateNativeContextTypes(cType native.ContextType, cSubtype native.Contex } func validateNativePlacementType(pt native.PlacementType, impIndex int) error { + if pt == 0 { + // Placement Type is only reccomended, not required. + return nil + } if pt < native.PlacementTypeFeed || pt > native.PlacementTypeRecommendationWidget { return fmt.Errorf("request.imp[%d].native.request.plcmttype is invalid. See https://iabtechlab.com/wp-content/uploads/2016/07/OpenRTB-Native-Ads-Specification-Final-1.2.pdf#page=40", impIndex) } @@ -523,9 +531,7 @@ func validateNativeAsset(asset nativeRequests.Asset, impIndex int, assetIndex in return fmt.Errorf(assetErr, impIndex, assetIndex) } foundType = true - if err := validateNativeAssetImg(asset.Img, impIndex, assetIndex); err != nil { - return err - } + // It is technically valid to have neither w/h nor wmin/hmin, so no check } if asset.Video != nil { @@ -587,19 +593,6 @@ func validateNativeEventTracker(tracker nativeRequests.EventTracker, impIndex in return nil } -func validateNativeAssetImg(image *nativeRequests.Image, impIndex int, assetIndex int) error { - // Note that w, wmin, h, and hmin cannot be negative because these variables use unsigned ints. - // Those fail during the standard json.Unmarshal() call. - if image.W == 0 && image.WMin == 0 { - return fmt.Errorf(`request.imp[%d].native.request.assets[%d].img must contain at least one of "w" or "wmin"`, impIndex, assetIndex) - } - if image.H == 0 && image.HMin == 0 { - return fmt.Errorf(`request.imp[%d].native.request.assets[%d].img must contain at least one of "h" or "hmin"`, impIndex, assetIndex) - } - - return nil -} - func validateNativeAssetVideo(video *nativeRequests.Video, impIndex int, assetIndex int) error { if len(video.MIMEs) < 1 { return fmt.Errorf("request.imp[%d].native.request.assets[%d].video.mimes must be an array with at least one MIME type", impIndex, assetIndex) diff --git a/endpoints/openrtb2/auction_test.go b/endpoints/openrtb2/auction_test.go index 4818c556661..7f3d3d4c66e 100644 --- a/endpoints/openrtb2/auction_test.go +++ b/endpoints/openrtb2/auction_test.go @@ -282,7 +282,6 @@ func TestRejectAccountRequired(t *testing.T) { func (gr *getResponseFromDirectory) assert(t *testing.T) { //t *testing.T, dir string, payloadGetter func(*testing.T, []byte) []byte, messageGetter func(*testing.T, []byte) []byte, expectedCode int, aliased bool) { t.Helper() - var filename string var filesToAssert []string if gr.file == "" { // Append every file found in `gr.dir` to the `filesToAssert` array and test them all @@ -300,14 +299,14 @@ func (gr *getResponseFromDirectory) assert(t *testing.T) { fileData = readFile(t, testFile) code, msg := gr.doRequest(t, gr.payloadGetter(t, fileData)) fmt.Printf("Processing %s\n", testFile) - assertResponseCode(t, filename, code, gr.expectedCode, msg) + assertResponseCode(t, testFile, code, gr.expectedCode, msg) expectMsg := gr.messageGetter(t, fileData) if gr.description != "" { if len(expectMsg) > 0 { - assert.Equal(t, string(expectMsg), msg, "Test failed. %s. Filename: \n", gr.description, filename) + assert.Equal(t, string(expectMsg), msg, "Test failed. %s. Filename: \n", gr.description, testFile) } else { - assert.Equal(t, string(expectMsg), msg, "file %s had bad response body", filename) + assert.Equal(t, string(expectMsg), msg, "file %s had bad response body", testFile) } } } diff --git a/endpoints/openrtb2/sample-requests/invalid-native/asset-img-no-hmin.json b/endpoints/openrtb2/sample-requests/valid-native/asset-img-no-hmin.json similarity index 100% rename from endpoints/openrtb2/sample-requests/invalid-native/asset-img-no-hmin.json rename to endpoints/openrtb2/sample-requests/valid-native/asset-img-no-hmin.json diff --git a/endpoints/openrtb2/sample-requests/invalid-native/asset-img-no-wmin.json b/endpoints/openrtb2/sample-requests/valid-native/asset-img-no-wmin.json similarity index 100% rename from endpoints/openrtb2/sample-requests/invalid-native/asset-img-no-wmin.json rename to endpoints/openrtb2/sample-requests/valid-native/asset-img-no-wmin.json diff --git a/endpoints/openrtb2/sample-requests/invalid-native/request-no-context.json b/endpoints/openrtb2/sample-requests/valid-native/request-no-context.json similarity index 100% rename from endpoints/openrtb2/sample-requests/invalid-native/request-no-context.json rename to endpoints/openrtb2/sample-requests/valid-native/request-no-context.json diff --git a/endpoints/openrtb2/sample-requests/invalid-native/request-no-plcmttype.json b/endpoints/openrtb2/sample-requests/valid-native/request-plcmttype-empty.json similarity index 100% rename from endpoints/openrtb2/sample-requests/invalid-native/request-no-plcmttype.json rename to endpoints/openrtb2/sample-requests/valid-native/request-plcmttype-empty.json