Skip to content

Commit 64f20f9

Browse files
authored
Liveramp ID Submodule: add try catch for atob in identitylink module (#13287)
* liveramp: handle url base64 decode * Update identityLinkIdSystem.js * Update identityLinkIdSystem.js
1 parent 405ff60 commit 64f20f9

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

modules/identityLinkIdSystem.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,19 @@ function setEnvelopeSource(src) {
150150

151151
export function getEnvelopeFromStorage() {
152152
let rawEnvelope = storage.getCookie(liverampEnvelopeName) || storage.getDataFromLocalStorage(liverampEnvelopeName);
153-
return rawEnvelope ? window.atob(rawEnvelope) : undefined;
153+
if (!rawEnvelope) {
154+
return undefined;
155+
}
156+
try {
157+
return window.atob(rawEnvelope);
158+
} catch (e) {
159+
try {
160+
return window.atob(rawEnvelope.replace(/-/g, '+').replace(/_/g, '/'));
161+
} catch (e2) {
162+
utils.logError('identityLink: invalid envelope format');
163+
return undefined;
164+
}
165+
}
154166
}
155167

156168
submodule('userId', identityLinkSubmodule);

test/spec/modules/identityLinkIdSystem_spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,18 @@ describe('IdentityLinkId tests', function () {
208208
expect(callBackSpy.calledOnce).to.be.true;
209209
})
210210

211+
it('should replace invalid characters if initial atob fails', function () {
212+
setTestEnvelopeCookie();
213+
const realAtob = window.atob;
214+
const stubAtob = sinon.stub(window, 'atob');
215+
stubAtob.onFirstCall().throws(new Error('bad'));
216+
stubAtob.onSecondCall().callsFake(realAtob);
217+
let envelopeValueFromStorage = getEnvelopeFromStorage();
218+
stubAtob.restore();
219+
expect(stubAtob.calledTwice).to.be.true;
220+
expect(envelopeValueFromStorage).to.equal(testEnvelopeValue);
221+
})
222+
211223
it('if there is no envelope in storage and ats is not present on a page try to call 3p url', function () {
212224
let envelopeValueFromStorage = getEnvelopeFromStorage();
213225
let callBackSpy = sinon.spy();

0 commit comments

Comments
 (0)