-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AMX ID System: allow cookie storage (#9761)
* Update AMXIdSystem logic, allow non-html5 storage, refactor sharedId domainOverride function into library * Fix failing test, bad invocation of getStorageManager
- Loading branch information
Showing
7 changed files
with
163 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Create a domainOverride callback for an ID module, closing over | ||
* an instance of StorageManager. | ||
* | ||
* The domainOverride function, given document.domain, will return | ||
* the topmost domain we are able to set a cookie on. For example, | ||
* given subdomain.example.com, it would return example.com. | ||
* | ||
* @param {StorageManager} storage e.g. from getStorageManager() | ||
* @param {string} moduleName the name of the module using this function | ||
* @returns {function(): string} | ||
*/ | ||
export function domainOverrideToRootDomain(storage, moduleName) { | ||
return function() { | ||
const domainElements = document.domain.split('.'); | ||
const cookieName = `_gd${Date.now()}_${moduleName}`; | ||
|
||
for (let i = 0, topDomain, testCookie; i < domainElements.length; i++) { | ||
const nextDomain = domainElements.slice(i).join('.'); | ||
|
||
// write test cookie | ||
storage.setCookie(cookieName, '1', undefined, undefined, nextDomain); | ||
|
||
// read test cookie to verify domain was valid | ||
testCookie = storage.getCookie(cookieName); | ||
|
||
// delete test cookie | ||
storage.setCookie(cookieName, '', 'Thu, 01 Jan 1970 00:00:01 GMT', undefined, nextDomain); | ||
|
||
if (testCookie === '1') { | ||
// 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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
test/spec/libraries/domainOverrideToRootDomain/index_spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import {domainOverrideToRootDomain} from 'libraries/domainOverrideToRootDomain/index.js'; | ||
import {getStorageManager} from 'src/storageManager.js'; | ||
import {MODULE_TYPE_UID} from '../../../../src/activities/modules'; | ||
|
||
const storage = getStorageManager({ moduleName: 'test', moduleType: MODULE_TYPE_UID }); | ||
const domainOverride = domainOverrideToRootDomain(storage, 'test'); | ||
|
||
describe('domainOverride', () => { | ||
let sandbox, domain, cookies, rejectCookiesFor; | ||
let setCookieStub; | ||
|
||
beforeEach(() => { | ||
sandbox = sinon.createSandbox(); | ||
sandbox.stub(document, 'domain').get(() => domain); | ||
cookies = {}; | ||
sandbox.stub(storage, 'getCookie').callsFake((key) => cookies[key]); | ||
rejectCookiesFor = null; | ||
setCookieStub = sandbox.stub(storage, 'setCookie').callsFake((key, value, expires, sameSite, domain) => { | ||
if (domain !== rejectCookiesFor) { | ||
if (expires != null) { | ||
expires = new Date(expires); | ||
} | ||
if (expires == null || expires > Date.now()) { | ||
cookies[key] = value; | ||
} else { | ||
delete cookies[key]; | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
afterEach(() => sandbox.restore()) | ||
|
||
it('test cookies include the module name', () => { | ||
domain = 'greatpublisher.com' | ||
rejectCookiesFor = 'greatpublisher.com' | ||
|
||
// stub Date.now() to return a constant value | ||
sandbox.stub(Date, 'now').returns(1234567890) | ||
|
||
const randomName = `adapterV${(Math.random() * 1e8).toString(16)}` | ||
const localDomainOverride = domainOverrideToRootDomain(storage, randomName) | ||
|
||
const time = Date.now(); | ||
localDomainOverride(); | ||
|
||
sandbox.assert.callCount(setCookieStub, 2) | ||
sandbox.assert.calledWith(setCookieStub, `_gd${time}_${randomName}`, '1', undefined, undefined, 'greatpublisher.com') | ||
}); | ||
|
||
it('will return the root domain when given a subdomain', () => { | ||
const test_domains = [ | ||
'deeply.nested.subdomain.for.greatpublisher.com', | ||
'greatpublisher.com', | ||
'subdomain.greatpublisher.com', | ||
'a-subdomain.greatpublisher.com', | ||
]; | ||
|
||
test_domains.forEach((testDomain) => { | ||
domain = testDomain | ||
rejectCookiesFor = 'com' | ||
expect(domainOverride()).to.equal('greatpublisher.com'); | ||
}); | ||
}); | ||
|
||
it(`If we can't set cookies on the root domain, we'll return the subdomain`, () => { | ||
domain = 'subdomain.greatpublisher.com' | ||
rejectCookiesFor = 'greatpublisher.com' | ||
expect(domainOverride()).to.equal('subdomain.greatpublisher.com'); | ||
}); | ||
|
||
it('Will return undefined if we can\'t set cookies on the root domain or the subdomain', () => { | ||
domain = 'subdomain.greatpublisher.com' | ||
rejectCookiesFor = 'subdomain.greatpublisher.com' | ||
expect(domainOverride()).to.equal(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.