From a4f9750c303e857aef37e4a8b1e9ab454a6d1128 Mon Sep 17 00:00:00 2001 From: ym-atsymuk <81176595+ym-atsymuk@users.noreply.github.com> Date: Wed, 21 Apr 2021 11:41:28 +0300 Subject: [PATCH] Yieldmo adapter: cut banner bid request parameters in case the url for bunner request is too long (#6549) Co-authored-by: Anton Tsymuk --- modules/yieldmoBidAdapter.js | 42 +++++++++++++++++- test/spec/modules/yieldmoBidAdapter_spec.js | 47 +++++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/modules/yieldmoBidAdapter.js b/modules/yieldmoBidAdapter.js index 9bd0d1c7f6a..b307f534b18 100644 --- a/modules/yieldmoBidAdapter.js +++ b/modules/yieldmoBidAdapter.js @@ -18,6 +18,8 @@ const LOCAL_WINDOW = utils.getWindowTop(); const DEFAULT_PLAYBACK_METHOD = 2; const DEFAULT_START_DELAY = 0; const VAST_TIMEOUT = 15000; +const MAX_BANNER_REQUEST_URL_LENGTH = 8000; +const BANNER_REQUEST_PROPERTIES_TO_REDUCE = ['description', 'title', 'pr', 'page_url']; export const spec = { code: BIDDER_CODE, @@ -51,7 +53,7 @@ export const spec = { p: [], page_url: bidderRequest.refererInfo.referer, bust: new Date().getTime().toString(), - pr: bidderRequest.refererInfo.referer, + pr: (LOCAL_WINDOW.document && LOCAL_WINDOW.document.referrer) || '', scrd: LOCAL_WINDOW.devicePixelRatio || 0, dnt: getDNT(), description: getPageDescription(), @@ -92,6 +94,20 @@ export const spec = { } }); serverRequest.p = '[' + serverRequest.p.toString() + ']'; + + // check if url exceeded max length + const url = `${BANNER_SERVER_ENDPOINT}?${utils.parseQueryStringParameters(serverRequest)}`; + let extraCharacters = url.length - MAX_BANNER_REQUEST_URL_LENGTH; + if (extraCharacters > 0) { + for (let i = 0; i < BANNER_REQUEST_PROPERTIES_TO_REDUCE.length; i++) { + extraCharacters = shortcutProperty(extraCharacters, serverRequest, BANNER_REQUEST_PROPERTIES_TO_REDUCE[i]); + + if (extraCharacters <= 0) { + break; + } + } + } + serverRequests.push({ method: 'GET', url: BANNER_SERVER_ENDPOINT, @@ -273,7 +289,7 @@ function getPageDescription() { if (document.querySelector('meta[name="description"]')) { return document .querySelector('meta[name="description"]') - .getAttribute('content'); // Value of the description metadata from the publisher's page. + .getAttribute('content') || ''; // Value of the description metadata from the publisher's page. } else { return ''; } @@ -514,3 +530,25 @@ function validateVideoParams(bid) { return false; } } + +/** + * Shortcut object property and check if required characters count was deleted + * + * @param {number} extraCharacters, count of characters to remove + * @param {object} target, object on which string property length should be reduced + * @param {string} propertyName, name of property to reduce + * @return {number} 0 if required characters count was removed otherwise count of how many left + */ +function shortcutProperty(extraCharacters, target, propertyName) { + if (target[propertyName].length > extraCharacters) { + target[propertyName] = target[propertyName].substring(0, target[propertyName].length - extraCharacters); + + return 0 + } + + const charactersLeft = extraCharacters - target[propertyName].length; + + target[propertyName] = ''; + + return charactersLeft; +} diff --git a/test/spec/modules/yieldmoBidAdapter_spec.js b/test/spec/modules/yieldmoBidAdapter_spec.js index 17d63e41266..3b317f88dc6 100644 --- a/test/spec/modules/yieldmoBidAdapter_spec.js +++ b/test/spec/modules/yieldmoBidAdapter_spec.js @@ -281,6 +281,53 @@ describe('YieldmoAdapter', function () { const placementsData = JSON.parse(buildAndGetPlacementInfo([mockBannerBid()])); expect(placementsData[0].bidfloor).to.undefined; }); + + it('should not exceed max url length', () => { + const longString = new Array(8000).join('a'); + const localWindow = utils.getWindowTop(); + + const originalTitle = localWindow.document.title; + localWindow.document.title = longString; + + const request = spec.buildRequests( + [mockBannerBid()], + mockBidderRequest({ + refererInfo: { + numIframes: 1, + reachedTop: true, + referer: longString, + }, + }) + )[0]; + const url = `${request.url}?${utils.parseQueryStringParameters(request.data)}`; + + expect(url.length).equal(8000); + + localWindow.document.title = originalTitle; + }); + + it('should only shortcut properties rather then completely remove it', () => { + const longString = new Array(7516).join('a'); + const localWindow = utils.getWindowTop(); + + const originalTitle = localWindow.document.title; + localWindow.document.title = `testtitle${longString}`; + + const request = spec.buildRequests( + [mockBannerBid()], + mockBidderRequest({ + refererInfo: { + numIframes: 1, + reachedTop: true, + referer: longString, + }, + }) + )[0]; + + expect(request.data.title.length).greaterThan(0); + + localWindow.document.title = originalTitle; + }); }); describe('Instream video:', function () {