Skip to content

Liveramp ID Submodule: add try catch for atob in identitylink module #13287

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

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
14 changes: 13 additions & 1 deletion modules/identityLinkIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,19 @@ function setEnvelopeSource(src) {

export function getEnvelopeFromStorage() {
let rawEnvelope = storage.getCookie(liverampEnvelopeName) || storage.getDataFromLocalStorage(liverampEnvelopeName);
return rawEnvelope ? window.atob(rawEnvelope) : undefined;
if (!rawEnvelope) {
return undefined;
}
try {
return window.atob(rawEnvelope);
} catch (e) {
try {
return window.atob(rawEnvelope.replace(/-/g, '+').replace(/_/g, '/'));
} catch (e2) {
utils.logError('identityLink: invalid envelope format');
return undefined;
}
}
}

submodule('userId', identityLinkSubmodule);
12 changes: 12 additions & 0 deletions test/spec/modules/identityLinkIdSystem_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@ describe('IdentityLinkId tests', function () {
expect(callBackSpy.calledOnce).to.be.true;
})

it('should replace invalid characters if initial atob fails', function () {
setTestEnvelopeCookie();
const realAtob = window.atob;
const stubAtob = sinon.stub(window, 'atob');
stubAtob.onFirstCall().throws(new Error('bad'));
stubAtob.onSecondCall().callsFake(realAtob);
let envelopeValueFromStorage = getEnvelopeFromStorage();
stubAtob.restore();
expect(stubAtob.calledTwice).to.be.true;
expect(envelopeValueFromStorage).to.equal(testEnvelopeValue);
})

it('if there is no envelope in storage and ats is not present on a page try to call 3p url', function () {
let envelopeValueFromStorage = getEnvelopeFromStorage();
let callBackSpy = sinon.spy();
Expand Down