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

Adjs 1517 gum gum prebid adapter invalid character error when non latin 1 character present in markup #13

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
22 changes: 21 additions & 1 deletion modules/gumgumBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,28 @@ function _getBrowserParams(topWindowUrl, mosttopLocation) {
return browserParams;
}

/**
* Some bid responses have been observed to contain non-Latin characters, which causes the browser
* to throw an error when trying to base64 encode a string using only the atob function.
* @param {*} str
* @returns {string} base64 encoded string
*/
function safeEncodeBase64(str) {
const encoder = new TextEncoder();

// Encode string to bytes (UTF-8)
const bytes = encoder.encode(str);
let safe = '';
bytes.forEach((byte) => {
safe += String.fromCharCode(byte);
});

// Then encode the bytes to base64
return btoa(safe);
}

function getWrapperCode(wrapper, data) {
return wrapper.replace('AD_JSON', window.btoa(JSON.stringify(data)))
return wrapper.replace('AD_JSON', safeEncodeBase64(JSON.stringify(data)))
}

/**
Expand Down
33 changes: 32 additions & 1 deletion test/spec/modules/gumgumBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,38 @@ describe('gumgumAdapter', function () {
const videoBidResponse = spec.interpretResponse({ body: serverResponse }, { ...bidRequest, data: { pi: 7 } })[0];
expect(videoBidResponse.vastXml).to.exist;
});
})

it('should encode a string containing a non-Latin character without failing', function () {
let serverResponse = {
'ad': {
'id': 2065333,
'height': 90,
'ipd': 2000,
'markup': 'This is a test string with an emdash – character', // non-Latin character
'ii': true,
'du': null,
'price': 1,
'zi': 0,
'impurl': 'http://g2.gumgum.com/ad/view',
'clsurl': 'http://g2.gumgum.com/ad/close'
},
'pag': {
't': 'ggumtest',
'pvid': 'aa8bbb65-427f-4689-8cee-e3eed0b89eec',
},
'thms': 10000
}

let result = spec.interpretResponse({ body: serverResponse }, bidRequest);
expect(result[0].ad).to.exist;
expect(result[0].ad).to.contain('This is a test string with an emdash – character');

// Confirm that native btoa would have thrown an error on this response if used
const nativeBtoa = () => btoa(JSON.stringify(serverResponse));
expect(nativeBtoa).to.throw(Error);
});
});

describe('getUserSyncs', function () {
const syncOptions = {
'iframeEnabled': 'true'
Expand Down
Loading