Skip to content

Commit

Permalink
fix: [PFG-3188] Fix circular reference error in adapters (#193)
Browse files Browse the repository at this point in the history
* fix: [PFG-3188] Fix circular reference error when clone event arguments containing DOM nodes

* Bump fix version
  • Loading branch information
dmytroKononenkoFS authored Aug 5, 2024
1 parent eeb67fe commit b3b7c56
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
5 changes: 3 additions & 2 deletions modules/carbonAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deepAccess, generateUUID, logError } from '../src/utils.js';
import { cloneEventArguments, deepAccess, generateUUID, logError } from '../src/utils.js';
import { ajax } from '../src/ajax.js';
import { getStorageManager } from '../src/storageManager.js';
import {getGlobal} from '../src/prebidGlobal.js';
Expand Down Expand Up @@ -31,7 +31,8 @@ let timeLastAuctionEvent = null;

export let carbonAdapter = Object.assign(adapter({analyticsHost, ANALYTICS_TYPE}), {
track({eventType, args}) {
args = args ? JSON.parse(JSON.stringify(args)) : {};
args = cloneEventArguments(args || {});

switch (eventType) {
case EVENTS.AUCTION_END: {
registerEngagement();
Expand Down
3 changes: 2 additions & 1 deletion modules/hadronAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ let analyticsType = 'endpoint';

let hadronAnalyticsAdapter = Object.assign(adapter({url: HADRON_ANALYTICS_URL, analyticsType}), {
track({eventType, args}) {
args = args ? JSON.parse(JSON.stringify(args)) : {};
args = utils.cloneEventArguments(args || {});

var data = {};
if (!eventsToTrack.includes(eventType)) return;
switch (eventType) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prebid.js",
"version": "8.49.5",
"version": "8.49.6",
"description": "Header Bidding Management Library",
"main": "src/prebid.js",
"scripts": {
Expand Down
26 changes: 26 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1083,3 +1083,29 @@ export function binarySearch(arr, el, key = (el) => el) {
}
return left;
}

const skipPatterns = [
/^\[object HTML.*]$/,
/^\[object Window.*]$/
]

export function cloneEventArguments(args) {
try {
return JSON.parse(
JSON.stringify(
args,
// When data has a reference to window or any html elements
// JSON.stringify fails with TypeError: Converting circular structure to JSON
// replacer function below excludes referenced html elements
(_key, value) => {
if (skipPatterns.some(pattern => pattern.test(String(value))) || typeof value === 'function') {
return undefined;
}
return value;
},
),
);
} catch (e) {
return args;
}
}

0 comments on commit b3b7c56

Please sign in to comment.