Skip to content

Update pairIdSystem.js: add defensive code before json parse #11870

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
merged 5 commits into from
Jun 27, 2024
Merged
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
37 changes: 27 additions & 10 deletions modules/pairIdSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ export const pairIdSubmodule = {
return value && Array.isArray(value) ? {'pairId': value} : undefined
},
/**
* performs action to obtain id and return a value in the callback's response argument
* @function
* @returns {id: string | undefined }
* Performs action to obtain ID and return a value in the callback's response argument.
* @function getId
* @param {Object} config - The configuration object.
* @param {Object} config.params - The parameters from the configuration.
* @returns {{id: string[] | undefined}} The obtained IDs or undefined if no IDs are found.
*/
getId(config) {
const pairIdsString = pairIdFromLocalStorage(PAIR_ID_KEY) || pairIdFromCookie(PAIR_ID_KEY)
Expand All @@ -67,13 +69,28 @@ export const pairIdSubmodule = {

const configParams = (config && config.params) || {};
if (configParams && configParams.liveramp) {
let LRStorageLocation = configParams.liveramp.storageKey || DEFAULT_LIVERAMP_PAIR_ID_KEY
const liverampValue = pairIdFromLocalStorage(LRStorageLocation) || pairIdFromCookie(LRStorageLocation)
try {
const obj = JSON.parse(atob(liverampValue));
ids = ids.concat(obj.envelope);
} catch (error) {
logInfo(error)
let LRStorageLocation = configParams.liveramp.storageKey || DEFAULT_LIVERAMP_PAIR_ID_KEY;
const liverampValue = pairIdFromLocalStorage(LRStorageLocation) || pairIdFromCookie(LRStorageLocation);

if (liverampValue) {
try {
const parsedValue = atob(liverampValue);
if (parsedValue) {
const obj = JSON.parse(parsedValue);

if (obj && typeof obj === 'object' && obj.envelope) {
ids = ids.concat(obj.envelope);
} else {
logInfo('Pairid: Parsed object is not valid or does not contain envelope');
}
} else {
logInfo('Pairid: Decoded value is empty');
}
} catch (error) {
logInfo('Pairid: Error parsing JSON: ', error);
}
} else {
logInfo('Pairid: liverampValue for pairId from storage is empty or null');
}
}

Expand Down