Skip to content

Commit

Permalink
Update pairIdSystem.js: add defensive code before json parse (prebid#…
Browse files Browse the repository at this point in the history
…11870)

* Update pairIdSystem.js: add defensive code before json parse

* Update pairIdSystem.js

* Update pairIdSystem.js

* Update pairIdSystem.js

fix indents

* Update pairIdSystem.js
  • Loading branch information
patmmccann authored and DecayConstant committed Jul 18, 2024
1 parent 0cb9e3c commit 200af11
Showing 1 changed file with 27 additions and 10 deletions.
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

0 comments on commit 200af11

Please sign in to comment.