From bebc77d01cfbeea91fa4a034984de32c7ada2aa6 Mon Sep 17 00:00:00 2001 From: Mark Conrad Date: Fri, 11 Dec 2020 10:54:38 -0500 Subject: [PATCH 1/8] Add the root domain check method as a global method added for use modules --- modules/userId/index.js | 55 ++++++++++++++++++++++++++++++++ test/spec/modules/userId_spec.js | 45 ++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/modules/userId/index.js b/modules/userId/index.js index f063fbc973b..38bbc364309 100644 --- a/modules/userId/index.js +++ b/modules/userId/index.js @@ -315,6 +315,60 @@ function hasGDPRConsent(consentData) { return true; } +/** + * Find the root domain + * @param {string|undefined} fullDomain + * @return {string} + */ +function findRootDomain(fullDomain = document.domain) { + if (!coreStorage.cookiesAreEnabled()) { + return fullDomain; + } + + const domainParts = fullDomain.split('.'); + if (domainParts.length == 2) { + return fullDomain; + } + let rootDomain; + let continueSearching; + let startIndex = -2; + const TEST_COOKIE_NAME = '_rdc'; + const TEST_COOKIE_VALUE = 'writeable'; + do { + rootDomain = domainParts.slice(startIndex).join('.'); + let expirationDate = new Date(utils.timestamp() + 10 * 1000).toUTCString(); + + // Write a test cookie + coreStorage.setCookie( + TEST_COOKIE_NAME, + TEST_COOKIE_VALUE, + expirationDate, + 'Lax', + rootDomain, + undefined + ); + + // See if the write was successful + const value = coreStorage.getCookie(TEST_COOKIE_NAME, undefined); + if (value === TEST_COOKIE_VALUE) { + continueSearching = false; + // Delete our test cookie + coreStorage.setCookie( + TEST_COOKIE_NAME, + '', + 'Thu, 01 Jan 1970 00:00:01 GMT', + undefined, + rootDomain, + undefined + ); + } else { + startIndex += -1; + continueSearching = Math.abs(startIndex) <= domainParts.length; + } + } while (continueSearching); + return rootDomain; +} + /** * @param {SubmoduleContainer[]} submodules * @param {function} cb - callback for after processing is done. @@ -727,6 +781,7 @@ export function init(config) { (getGlobal()).getUserIds = getUserIds; (getGlobal()).getUserIdsAsEids = getUserIdsAsEids; (getGlobal()).refreshUserIds = refreshUserIds; + (getGlobal()).findRootDomain = findRootDomain; } // init config update listener to start the application diff --git a/test/spec/modules/userId_spec.js b/test/spec/modules/userId_spec.js index 981ebb5f50e..ef9089dc142 100644 --- a/test/spec/modules/userId_spec.js +++ b/test/spec/modules/userId_spec.js @@ -410,6 +410,51 @@ describe('User ID', function () { }); }); + describe('findRootDomain', function () { + let sandbox; + + beforeEach(function () { + setSubmoduleRegistry([pubCommonIdSubmodule]); + init(config); + config.setConfig({ + userSync: { + syncDelay: 0, + userIds: [ + { + name: 'pubCommonId', + value: { pubcid: '11111' }, + }, + ], + }, + }); + sandbox = sinon.createSandbox() + sandbox + .stub(coreStorage, 'getCookie') + .onFirstCall() + .returns(null) // .co.uk + .onSecondCall() + .returns('writeable'); // realdomain.co.uk; + }); + + afterEach(function() { + sandbox.restore(); + }) + + it('should be a global function', function() { + expect(typeof (getGlobal()).findRootDomain).to.equal('function'); + }) + + it('should just find the root domain', function () { + var domain = getGlobal().findRootDomain('sub.realdomain.co.uk'); + expect(domain).to.be.eq('realdomain.co.uk'); + }); + + it('should find the full domain when no subdomain is present', function () { + var domain = getGlobal().findRootDomain('realdomain.co.uk'); + expect(domain).to.be.eq('realdomain.co.uk'); + }); + }); + describe('Opt out', function () { before(function () { coreStorage.setCookie(PBJS_USER_ID_OPTOUT_NAME, '1', (new Date(Date.now() + 5000).toUTCString())); From f32b59954351d29c869e75c79991da1175db6a05 Mon Sep 17 00:00:00 2001 From: Mark Conrad Date: Fri, 11 Dec 2020 14:58:04 -0500 Subject: [PATCH 2/8] Add a date suffix to keep the test cookie random --- modules/userId/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/userId/index.js b/modules/userId/index.js index 38bbc364309..5a5f45161c2 100644 --- a/modules/userId/index.js +++ b/modules/userId/index.js @@ -332,7 +332,7 @@ function findRootDomain(fullDomain = document.domain) { let rootDomain; let continueSearching; let startIndex = -2; - const TEST_COOKIE_NAME = '_rdc'; + const TEST_COOKIE_NAME = `_rdc${Date.now()}`; const TEST_COOKIE_VALUE = 'writeable'; do { rootDomain = domainParts.slice(startIndex).join('.'); From 0672f2bdd0d7cd296a5e806845ededa12d55fdc0 Mon Sep 17 00:00:00 2001 From: Mark Conrad Date: Wed, 16 Dec 2020 09:33:42 -0500 Subject: [PATCH 3/8] Per review, moved the findRootDomain method to utils out of the userId modules --- modules/userId/index.js | 55 ------------------------------- src/utils.js | 56 ++++++++++++++++++++++++++++++++ test/spec/modules/userId_spec.js | 45 ------------------------- test/spec/utils_spec.js | 28 ++++++++++++++++ 4 files changed, 84 insertions(+), 100 deletions(-) diff --git a/modules/userId/index.js b/modules/userId/index.js index 5a5f45161c2..f063fbc973b 100644 --- a/modules/userId/index.js +++ b/modules/userId/index.js @@ -315,60 +315,6 @@ function hasGDPRConsent(consentData) { return true; } -/** - * Find the root domain - * @param {string|undefined} fullDomain - * @return {string} - */ -function findRootDomain(fullDomain = document.domain) { - if (!coreStorage.cookiesAreEnabled()) { - return fullDomain; - } - - const domainParts = fullDomain.split('.'); - if (domainParts.length == 2) { - return fullDomain; - } - let rootDomain; - let continueSearching; - let startIndex = -2; - const TEST_COOKIE_NAME = `_rdc${Date.now()}`; - const TEST_COOKIE_VALUE = 'writeable'; - do { - rootDomain = domainParts.slice(startIndex).join('.'); - let expirationDate = new Date(utils.timestamp() + 10 * 1000).toUTCString(); - - // Write a test cookie - coreStorage.setCookie( - TEST_COOKIE_NAME, - TEST_COOKIE_VALUE, - expirationDate, - 'Lax', - rootDomain, - undefined - ); - - // See if the write was successful - const value = coreStorage.getCookie(TEST_COOKIE_NAME, undefined); - if (value === TEST_COOKIE_VALUE) { - continueSearching = false; - // Delete our test cookie - coreStorage.setCookie( - TEST_COOKIE_NAME, - '', - 'Thu, 01 Jan 1970 00:00:01 GMT', - undefined, - rootDomain, - undefined - ); - } else { - startIndex += -1; - continueSearching = Math.abs(startIndex) <= domainParts.length; - } - } while (continueSearching); - return rootDomain; -} - /** * @param {SubmoduleContainer[]} submodules * @param {function} cb - callback for after processing is done. @@ -781,7 +727,6 @@ export function init(config) { (getGlobal()).getUserIds = getUserIds; (getGlobal()).getUserIdsAsEids = getUserIdsAsEids; (getGlobal()).refreshUserIds = refreshUserIds; - (getGlobal()).findRootDomain = findRootDomain; } // init config update listener to start the application diff --git a/src/utils.js b/src/utils.js index acdf0f101ad..762f620d00c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -3,6 +3,7 @@ import { config } from './config.js'; import clone from 'just-clone'; import find from 'core-js-pure/features/array/find.js'; import includes from 'core-js-pure/features/array/includes.js'; +import { getCoreStorageManager } from './storageManager.js'; const CONSTANTS = require('./constants.json'); @@ -22,6 +23,7 @@ let consoleInfoExists = Boolean(consoleExists && window.console.info); let consoleWarnExists = Boolean(consoleExists && window.console.warn); let consoleErrorExists = Boolean(consoleExists && window.console.error); var events = require('./events.js'); +export const coreStorage = getCoreStorageManager('util'); // this allows stubbing of utility functions that are used internally by other utility functions export const internal = { @@ -1270,3 +1272,57 @@ export function cyrb53Hash(str, seed = 0) { h2 = imul(h2 ^ (h2 >>> 16), 2246822507) ^ imul(h1 ^ (h1 >>> 13), 3266489909); return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(); } + +/** + * Find the root domain + * @param {string|undefined} fullDomain + * @return {string} + */ +export function findRootDomain(fullDomain = window.location.hostname) { + if (!coreStorage.cookiesAreEnabled()) { + return fullDomain; + } + + const domainParts = fullDomain.split('.'); + if (domainParts.length == 2) { + return fullDomain; + } + let rootDomain; + let continueSearching; + let startIndex = -2; + const TEST_COOKIE_NAME = `_rdc${Date.now()}`; + const TEST_COOKIE_VALUE = 'writeable'; + do { + rootDomain = domainParts.slice(startIndex).join('.'); + let expirationDate = new Date(timestamp() + 10 * 1000).toUTCString(); + + // Write a test cookie + coreStorage.setCookie( + TEST_COOKIE_NAME, + TEST_COOKIE_VALUE, + expirationDate, + 'Lax', + rootDomain, + undefined + ); + + // See if the write was successful + const value = coreStorage.getCookie(TEST_COOKIE_NAME, undefined); + if (value === TEST_COOKIE_VALUE) { + continueSearching = false; + // Delete our test cookie + coreStorage.setCookie( + TEST_COOKIE_NAME, + '', + 'Thu, 01 Jan 1970 00:00:01 GMT', + undefined, + rootDomain, + undefined + ); + } else { + startIndex += -1; + continueSearching = Math.abs(startIndex) <= domainParts.length; + } + } while (continueSearching); + return rootDomain; +} diff --git a/test/spec/modules/userId_spec.js b/test/spec/modules/userId_spec.js index ef9089dc142..981ebb5f50e 100644 --- a/test/spec/modules/userId_spec.js +++ b/test/spec/modules/userId_spec.js @@ -410,51 +410,6 @@ describe('User ID', function () { }); }); - describe('findRootDomain', function () { - let sandbox; - - beforeEach(function () { - setSubmoduleRegistry([pubCommonIdSubmodule]); - init(config); - config.setConfig({ - userSync: { - syncDelay: 0, - userIds: [ - { - name: 'pubCommonId', - value: { pubcid: '11111' }, - }, - ], - }, - }); - sandbox = sinon.createSandbox() - sandbox - .stub(coreStorage, 'getCookie') - .onFirstCall() - .returns(null) // .co.uk - .onSecondCall() - .returns('writeable'); // realdomain.co.uk; - }); - - afterEach(function() { - sandbox.restore(); - }) - - it('should be a global function', function() { - expect(typeof (getGlobal()).findRootDomain).to.equal('function'); - }) - - it('should just find the root domain', function () { - var domain = getGlobal().findRootDomain('sub.realdomain.co.uk'); - expect(domain).to.be.eq('realdomain.co.uk'); - }); - - it('should find the full domain when no subdomain is present', function () { - var domain = getGlobal().findRootDomain('realdomain.co.uk'); - expect(domain).to.be.eq('realdomain.co.uk'); - }); - }); - describe('Opt out', function () { before(function () { coreStorage.setCookie(PBJS_USER_ID_OPTOUT_NAME, '1', (new Date(Date.now() + 5000).toUTCString())); diff --git a/test/spec/utils_spec.js b/test/spec/utils_spec.js index 6494ead78e7..9e6f83a2947 100644 --- a/test/spec/utils_spec.js +++ b/test/spec/utils_spec.js @@ -1197,5 +1197,33 @@ describe('Utils', function () { expect(typeof utils.cyrb53Hash(stringOne)).to.equal('string'); }); }); + + describe('findRootDomain', function () { + let sandbox; + + beforeEach(function () { + sandbox = sinon.createSandbox(); + sandbox + .stub(utils.coreStorage, 'getCookie') + .onFirstCall() + .returns(null) // .co.uk + .onSecondCall() + .returns('writeable'); // realdomain.co.uk; + }); + + afterEach(function () { + sandbox.restore(); + }); + + it('should just find the root domain', function () { + var domain = utils.findRootDomain('sub.realdomain.co.uk'); + expect(domain).to.be.eq('realdomain.co.uk'); + }); + + it('should find the full domain when no subdomain is present', function () { + var domain = utils.findRootDomain('realdomain.co.uk'); + expect(domain).to.be.eq('realdomain.co.uk'); + }); + }); }); }); From 47ec3b2ac158b8f2a2ff97ef021e4799a521ca90 Mon Sep 17 00:00:00 2001 From: Mark Conrad Date: Thu, 7 Jan 2021 15:45:46 -0500 Subject: [PATCH 4/8] More the domain method back the user module but attach it to the submodule so that userId modules can have access to it --- integrationExamples/gpt/userId_example.html | 3 +- modules/userId/index.js | 69 +++++++++++++++++++++ src/utils.js | 54 ---------------- test/spec/modules/userId_spec.js | 46 +++++++++++++- test/spec/utils_spec.js | 28 --------- 5 files changed, 116 insertions(+), 84 deletions(-) diff --git a/integrationExamples/gpt/userId_example.html b/integrationExamples/gpt/userId_example.html index e6f9255326a..fa9b1e3b5fd 100644 --- a/integrationExamples/gpt/userId_example.html +++ b/integrationExamples/gpt/userId_example.html @@ -109,6 +109,7 @@