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

[ParrableIdSystem] Ensure base64 payload is url-safe #6258

Merged
merged 3 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion modules/parrableIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ function isValidConfig(configParams) {
return true;
}

function encodeBase64UrlSafe(base64) {
const ENC = {
'+': '-',
'/': '_',
'=': '.'
};
return base64.replace(/[+/=]/g, (m) => ENC[m]);
}

function readCookie() {
const parrableIdStr = storage.getCookie(PARRABLE_COOKIE_NAME);
if (parrableIdStr) {
Expand Down Expand Up @@ -182,7 +191,7 @@ function fetchId(configParams) {
};

const searchParams = {
data: btoa(JSON.stringify(data)),
data: encodeBase64UrlSafe(btoa(JSON.stringify(data))),
_rand: Math.random()
};

Expand Down
13 changes: 13 additions & 0 deletions test/spec/modules/parrableIdSystem_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ describe('Parrable ID System', function() {
expect(server.requests[0].url).to.contain('us_privacy=' + uspString);
});

it('xhr base64 safely encodes url data object', function() {
const urlSafeBase64EncodedData = '-_.';
const btoaStub = sinon.stub(window, 'btoa').returns('+/=');
let getIdResult = parrableIdSubmodule.getId(P_CONFIG_MOCK);

getIdResult.callback(callbackSpy);

let request = server.requests[0];
let queryParams = utils.parseQS(request.url.split('?')[1]);
expect(queryParams.data).to.equal(urlSafeBase64EncodedData);
btoaStub.restore();
});

it('should log an error and continue to callback if ajax request errors', function () {
let callBackSpy = sinon.spy();
let submoduleCallback = parrableIdSubmodule.getId({ params: {partner: 'prebid'} }).callback;
Expand Down