Skip to content
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

Discovery Bid Adapter : fix window.top bug #11511

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
74 changes: 53 additions & 21 deletions modules/discoveryBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,36 @@ export function getPageKeywords(win = window) {
return (element && element.content) || '';
}

export function getHLen(win = window) {
let hLen;
try {
hLen = win.top.history.length;
} catch (error) {
hLen = undefined;
}
return hLen;
}

export function getHC(win = window) {
let hc;
try {
hc = win.top.navigator.hardwareConcurrency;
} catch (error) {
hc = undefined;
}
return hc;
}

export function getDM(win = window) {
let dm;
try {
dm = win.top.navigator.deviceMemory;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #11001 ; please move these functions into a library in the fpd group of imports. I don't think we want them in everyone's device object by default though, as the primary purpose is fingerprinting, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have modified it as required, please help me cr it again.
Thanks.

} catch (error) {
dm = undefined;
}
return dm;
}

/**
* get connection downlink
* @returns {number}
Expand Down Expand Up @@ -455,11 +485,31 @@ function getParam(validBidRequests, bidderRequest) {
const referer = utils.deepAccess(bidderRequest, 'refererInfo.ref');
const firstPartyData = bidderRequest.ortb2;
const tpData = utils.deepAccess(bidderRequest, 'ortb2.user.data') || undefined;
const topWindow = window.top;
const title = getPageTitle();
const desc = getPageDescription();
const keywords = getPageKeywords();

let ext = {};
try {
ext = {
eids,
firstPartyData,
ssppid: storage.getCookie(COOKIE_KEY_SSPPID) || undefined,
pmguid: getPmgUID(),
tpData,
utm: storage.getCookie(UTM_KEY),
page: {
title: title ? title.slice(0, 100) : undefined,
desc: desc ? desc.slice(0, 300) : undefined,
keywords: keywords ? keywords.slice(0, 100) : undefined,
hLen: getHLen(),
},
device: {
nbw: getConnectionDownLink(),
hc: getHC(),
dm: getDM()
}
}
} catch (error) {}
try {
buildUTMTagData(page);
} catch (error) { }
Expand All @@ -480,25 +530,7 @@ function getParam(validBidRequests, bidderRequest) {
ua: navigator.userAgent,
language: /en/.test(navigator.language) ? 'en' : navigator.language,
},
ext: {
eids,
firstPartyData,
ssppid: storage.getCookie(COOKIE_KEY_SSPPID) || undefined,
pmguid: getPmgUID(),
tpData,
utm: storage.getCookie(UTM_KEY),
page: {
title: title ? title.slice(0, 100) : undefined,
desc: desc ? desc.slice(0, 300) : undefined,
keywords: keywords ? keywords.slice(0, 100) : undefined,
hLen: topWindow.history?.length || undefined,
},
device: {
nbw: getConnectionDownLink(),
hc: topWindow.navigator?.hardwareConcurrency || undefined,
dm: topWindow.navigator?.deviceMemory || undefined,
}
},
ext,
user: {
buyeruid: storage.getCookie(COOKIE_KEY_MGUID) || undefined,
id: sharedid || pubcid,
Expand Down
74 changes: 73 additions & 1 deletion test/spec/modules/discoveryBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
THIRD_PARTY_COOKIE_ORIGIN,
COOKIE_KEY_MGUID,
getCurrentTimeToUTCString,
buildUTMTagData
buildUTMTagData,
getHLen,
getHC,
getDM,
} from 'modules/discoveryBidAdapter.js';
import * as utils from 'src/utils.js';

Expand Down Expand Up @@ -633,5 +636,74 @@ describe('discovery Bid Adapter Tests', function () {
expect(storage.setCookie.notCalled).to.be.true;
});
});
describe('getHLen', () => {
it('should return the correct length of history when accessible', () => {
const mockWindow = {
top: {
history: {
length: 3
}
}
};
const result = getHLen(mockWindow);
expect(result).to.equal(3);
});

it('should return undefined when accessing win.top.history.length throws an error', () => {
const mockWindow = {
get top() {
throw new Error('Access denied');
}
};
const result = getHLen(mockWindow);
expect(result).be.undefined;
});
});

describe('getHC', () => {
it('should return the correct value of hardwareConcurrency when accessible', () => {
const mockWindow = {
top: {
navigator: {
hardwareConcurrency: 4
}
}
};
const result = getHC(mockWindow);
expect(result).to.equal(4);
});
it('should return undefined when accessing win.top.navigator.hardwareConcurrency throws an error', () => {
const mockWindow = {
get top() {
throw new Error('Access denied');
}
};
const result = getHC(mockWindow);
expect(result).be.undefined;
});
});

describe('getDM', () => {
it('should return the correct value of deviceMemory when accessible', () => {
const mockWindow = {
top: {
navigator: {
deviceMemory: 4
}
}
};
const result = getDM(mockWindow);
expect(result).to.equal(4);
});
it('should return undefined when accessing win.top.navigator.deviceMemory throws an error', () => {
const mockWindow = {
get top() {
throw new Error('Access denied');
}
};
const result = getDM(mockWindow);
expect(result).be.undefined;
});
});
});
});