From 65ee024b1f753c7de214b1660f9a5fd2e5f9900e Mon Sep 17 00:00:00 2001 From: idettman Date: Wed, 15 Jul 2020 13:28:30 -0700 Subject: [PATCH 1/4] update formatting --- modules/pubCommonIdSystem.js | 30 ++++++++++++++++++++++++++++++ modules/userId/index.js | 18 ++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/modules/pubCommonIdSystem.js b/modules/pubCommonIdSystem.js index 8e2be1207f5..a00b80d8c52 100644 --- a/modules/pubCommonIdSystem.js +++ b/modules/pubCommonIdSystem.js @@ -7,11 +7,14 @@ import * as utils from '../src/utils.js'; import {submodule} from '../src/hook.js'; +import {getCoreStorageManager} from '../src/storageManager.js'; const PUB_COMMON_ID = 'PublisherCommonId'; const MODULE_NAME = 'pubCommonId'; +const coreStorage = getCoreStorageManager('userid'); + /** @type {Submodule} */ export const pubCommonIdSubmodule = { /** @@ -90,6 +93,33 @@ export const pubCommonIdSubmodule = { const callback = this.makeCallback(pixelUrl, storedId); return callback ? {callback: callback} : {id: storedId}; } + }, + + /** + * @param {string} domain + * @param {HTMLDocument} document + * @return {(string|undefined)} + */ + domainOverride: function () { + const domainElements = document.domain.split('.'); + const cookieName = `_gd${Date.now()}`; + for (let i = 0, topDomain; i < domainElements.length; i++) { + const nextDomain = domainElements.slice(i).join('.'); + + // write test cookie + coreStorage.setCookie(cookieName, '1', undefined, undefined, nextDomain); + + // read test cookie to verify domain was valid + if (coreStorage.getCookie(cookieName) === '1') { + // delete test cookie + coreStorage.setCookie(cookieName, '', 'Thu, 01 Jan 1970 00:00:01 GMT', undefined, nextDomain); + // cookie was written successfully using test domain so the topDomain is updated + topDomain = nextDomain; + } else { + // cookie failed to write using test domain so exit by returning the topDomain + return topDomain; + } + } } }; diff --git a/modules/userId/index.js b/modules/userId/index.js index 88a0a636caf..aa3cff82f30 100644 --- a/modules/userId/index.js +++ b/modules/userId/index.js @@ -159,17 +159,23 @@ export function setSubmoduleRegistry(submodules) { } /** - * @param {SubmoduleStorage} storage + * @param {SubmoduleContainer} submodule * @param {(Object|string)} value */ -function setStoredValue(storage, value) { +function setStoredValue(submodule, value) { + /** + * @type {SubmoduleStorage} + */ + const storage = submodule.config.storage; + const domainOverride = (typeof submodule.submodule.domainOverride === 'function') ? submodule.submodule.domainOverride() : null; + try { const valueStr = utils.isPlainObject(value) ? JSON.stringify(value) : value; const expiresStr = (new Date(Date.now() + (storage.expires * (60 * 60 * 24 * 1000)))).toUTCString(); if (storage.type === COOKIE) { - coreStorage.setCookie(storage.name, valueStr, expiresStr, 'Lax'); + coreStorage.setCookie(storage.name, valueStr, expiresStr, 'Lax', domainOverride); if (typeof storage.refreshInSeconds === 'number') { - coreStorage.setCookie(`${storage.name}_last`, new Date().toUTCString(), expiresStr); + coreStorage.setCookie(`${storage.name}_last`, new Date().toUTCString(), expiresStr, 'Lax', domainOverride); } } else if (storage.type === LOCAL_STORAGE) { coreStorage.setDataInLocalStorage(`${storage.name}_exp`, expiresStr); @@ -246,7 +252,7 @@ function processSubmoduleCallbacks(submodules, cb) { // if valid, id data should be saved to cookie/html storage if (idObj) { if (submodule.config.storage) { - setStoredValue(submodule.config.storage, idObj); + setStoredValue(submodule, idObj); } // cache decoded value (this is copied to every adUnit bid) submodule.idObj = submodule.submodule.decode(idObj); @@ -437,7 +443,7 @@ function initSubmodules(submodules, consentData) { if (utils.isPlainObject(response)) { if (response.id) { // A getId/extendId result assumed to be valid user id data, which should be saved to users local storage or cookies - setStoredValue(submodule.config.storage, response.id); + setStoredValue(submodule, response.id); storedId = response.id; } From bd0f009af3d06a907f7e95e9a05d1e6d5ed0ad2c Mon Sep 17 00:00:00 2001 From: idettman Date: Thu, 16 Jul 2020 09:34:32 -0700 Subject: [PATCH 2/4] update formatting --- modules/userId/index.js | 2 +- test/spec/modules/userId_spec.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/modules/userId/index.js b/modules/userId/index.js index aa3cff82f30..71643887b73 100644 --- a/modules/userId/index.js +++ b/modules/userId/index.js @@ -162,7 +162,7 @@ export function setSubmoduleRegistry(submodules) { * @param {SubmoduleContainer} submodule * @param {(Object|string)} value */ -function setStoredValue(submodule, value) { +export function setStoredValue(submodule, value) { /** * @type {SubmoduleStorage} */ diff --git a/test/spec/modules/userId_spec.js b/test/spec/modules/userId_spec.js index 8ce81ea85b0..77b11387d47 100644 --- a/test/spec/modules/userId_spec.js +++ b/test/spec/modules/userId_spec.js @@ -5,7 +5,8 @@ import { requestBidsHook, setSubmoduleRegistry, syncDelay, - coreStorage + coreStorage, + setStoredValue } from 'modules/userId/index.js'; import {createEidsArray} from 'modules/userId/eids.js'; import {config} from 'src/config.js'; @@ -1460,4 +1461,30 @@ describe('User ID', function() { expect(server.requests[0].url).to.equal('https://match.adsrvr.org/track/rid?ttd_pid=rubicon&fmt=json'); }); }); + + describe('Set cookie behavior', function() { + let coreStorageSpy; + beforeEach(function() { + coreStorageSpy = sinon.spy(coreStorage, 'setCookie'); + }); + afterEach(function() { + coreStorageSpy.restore(); + }); + it('should allow submodules to override the domain', function () { + const submodule = { + submodule: { + domainOverride: function() { + return 'foo.com' + } + }, + config: { + storage: { + type: 'cookie' + } + } + } + setStoredValue(submodule, 'bar'); + expect(coreStorage.setCookie.getCall(0).args[4]).to.equal('foo.com'); + }); + }); }); From 21f536a924e91404004a7d67fecc398b754c24f9 Mon Sep 17 00:00:00 2001 From: idettman Date: Thu, 16 Jul 2020 09:37:33 -0700 Subject: [PATCH 3/4] requested changes implemented --- modules/pubCommonIdSystem.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/pubCommonIdSystem.js b/modules/pubCommonIdSystem.js index a00b80d8c52..9516934de42 100644 --- a/modules/pubCommonIdSystem.js +++ b/modules/pubCommonIdSystem.js @@ -7,13 +7,13 @@ import * as utils from '../src/utils.js'; import {submodule} from '../src/hook.js'; -import {getCoreStorageManager} from '../src/storageManager.js'; +import {getStorageManager} from '../src/storageManager.js'; const PUB_COMMON_ID = 'PublisherCommonId'; const MODULE_NAME = 'pubCommonId'; -const coreStorage = getCoreStorageManager('userid'); +const storage = getStorageManager(null, 'pubCommonId'); /** @type {Submodule} */ export const pubCommonIdSubmodule = { @@ -107,12 +107,12 @@ export const pubCommonIdSubmodule = { const nextDomain = domainElements.slice(i).join('.'); // write test cookie - coreStorage.setCookie(cookieName, '1', undefined, undefined, nextDomain); + storage.setCookie(cookieName, '1', undefined, undefined, nextDomain); // read test cookie to verify domain was valid - if (coreStorage.getCookie(cookieName) === '1') { + if (storage.getCookie(cookieName) === '1') { // delete test cookie - coreStorage.setCookie(cookieName, '', 'Thu, 01 Jan 1970 00:00:01 GMT', undefined, nextDomain); + storage.setCookie(cookieName, '', 'Thu, 01 Jan 1970 00:00:01 GMT', undefined, nextDomain); // cookie was written successfully using test domain so the topDomain is updated topDomain = nextDomain; } else { From 1e2b77c9d67d44bfb398392528db1ff2528ffca1 Mon Sep 17 00:00:00 2001 From: idettman Date: Thu, 16 Jul 2020 09:42:53 -0700 Subject: [PATCH 4/4] add unit test --- test/spec/modules/userId_spec.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/spec/modules/userId_spec.js b/test/spec/modules/userId_spec.js index 77b11387d47..f8f10c72a79 100644 --- a/test/spec/modules/userId_spec.js +++ b/test/spec/modules/userId_spec.js @@ -1486,5 +1486,20 @@ describe('User ID', function() { setStoredValue(submodule, 'bar'); expect(coreStorage.setCookie.getCall(0).args[4]).to.equal('foo.com'); }); + + it('should pass null for domain if submodule does not override the domain', function () { + const submodule = { + submodule: { + + }, + config: { + storage: { + type: 'cookie' + } + } + } + setStoredValue(submodule, 'bar'); + expect(coreStorage.setCookie.getCall(0).args[4]).to.equal(null); + }); }); });