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

fix: skip top domain when cookie is disabled #580

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import utils from './utils';
import UUID from './uuid';
import base64Id from './base64Id';
import DEFAULT_OPTIONS from './options';
import getHost from './get-host';
import baseCookie from './base-cookie';
import { AmplitudeServerZone, getEventLogApi } from './server-zone';
import ConfigManager from './config-manager';
Expand Down Expand Up @@ -309,7 +308,7 @@ AmplitudeClient.prototype._runNewSessionStartCallbacks = function () {
};

AmplitudeClient.prototype.deleteLowerLevelDomainCookies = function () {
const host = getHost();
const host = utils.getHost();

const cookieHost =
this.options.domain && this.options.domain[0] === '.' ? this.options.domain.slice(1) : this.options.domain;
Expand Down
3 changes: 1 addition & 2 deletions src/base-cookie.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Constants from './constants';
import base64Id from './base64Id';
import utils from './utils';

const get = (name) => {
Expand Down Expand Up @@ -94,7 +93,7 @@ const sortByEventTime = (cookies) => {

// test that cookies are enabled - navigator.cookiesEnabled yields false positives in IE, need to test directly
const areCookiesEnabled = (opts = {}) => {
const cookieName = Constants.COOKIE_TEST_PREFIX + base64Id();
const cookieName = Constants.COOKIE_TEST_PREFIX;
if (typeof document === 'undefined') {
return false;
}
Expand Down
3 changes: 1 addition & 2 deletions src/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import Base64 from './base64';
import utils from './utils';
import getLocation from './get-location';
import baseCookie from './base-cookie';
import topDomain from './top-domain';

Expand All @@ -31,7 +30,7 @@ var options = function (opts) {
_options.secure = opts.secure;
_options.sameSite = opts.sameSite;

var domain = !utils.isEmptyString(opts.domain) ? opts.domain : '.' + topDomain(getLocation().href);
var domain = !utils.isEmptyString(opts.domain) ? opts.domain : '.' + topDomain(utils.getLocation().href);
var token = Math.random();
_options.domain = domain;
set('amplitude_test', token);
Expand Down
19 changes: 0 additions & 19 deletions src/get-host.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/get-location.js

This file was deleted.

5 changes: 2 additions & 3 deletions src/metadata-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import Base64 from './base64';
import baseCookie from './base-cookie';
import Constants from './constants';
import getLocation from './get-location';
import ampLocalStorage from './localstorage';
import topDomain from './top-domain';
import utils from './utils';
Expand Down Expand Up @@ -35,8 +34,8 @@ class MetadataStorage {
this.expirationDays = expirationDays;

this.cookieDomain = '';
const loc = getLocation() ? getLocation().href : undefined;
const writableTopDomain = topDomain(loc);
const loc = utils.getLocation() ? utils.getLocation().href : undefined;
const writableTopDomain = !disableCookies ? topDomain(loc) : '';
this.cookieDomain = domain || (writableTopDomain ? '.' + writableTopDomain : null);

if (storageOptionExists[storage]) {
Expand Down
3 changes: 1 addition & 2 deletions src/top-domain.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import baseCookie from './base-cookie';
import base64Id from './base64Id';
import getHost from './get-host';
import utils from './utils';

// Utility that finds top level domain to write to
const topDomain = (url) => {
const host = getHost(url);
const host = utils.getHost(url);
const parts = host.split('.');
const levels = [];
const cname = '_tldtest_' + base64Id();
Expand Down
22 changes: 22 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,26 @@ const validateSessionId = (sessionId) => {
return false;
};

const getLocation = () => {
return GlobalScope.location;
};

const getHost = (url) => {
const defaultHostname = GlobalScope.location ? GlobalScope.location.hostname : '';
if (url) {
if (typeof document !== 'undefined') {
const a = document.createElement('a');
a.href = url;
return a.hostname || defaultHostname;
}
if (typeof URL === 'function') {
const u = new URL(url);
return u.hostname || defaultHostname;
}
}
return defaultHostname;
};

export default {
setLogLevel,
getLogLevel,
Expand All @@ -303,4 +323,6 @@ export default {
validateDeviceId,
validateTransport,
validateSessionId,
getLocation,
getHost,
};
41 changes: 36 additions & 5 deletions test/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2739,23 +2739,54 @@ describe('AmplitudeClient', function () {
});
});

it('should not create any cookies if disabledCookies = true', function () {
it('should not use any cookies with simple host if disabledCookies = true', function () {
deleteAllCookies();
clock.tick(20);

var cookieArray = getAllCookies();
assert.equal(cookieArray.length, 0);

var deviceId = 'test_device_id';
var amplitude2 = new AmplitudeClient();
var amplitude = new AmplitudeClient();

amplitude2.init(apiKey, null, {
amplitude.init(apiKey, null, {
deviceId: deviceId,
disableCookies: true,
});

cookieArray = getAllCookies();
assert.equal(cookieArray.length, 0);
});

it('should not use any cookies with multiple domains in host if disabledCookies = true', function () {
const stubbedGetLocation = sinon.stub(utils, 'getLocation').returns({ href: 'https://abc.def.xyz/test' });
const spiedSet = sinon.spy(baseCookie, 'set');
const spiedGet = sinon.spy(baseCookie, 'get');
deleteAllCookies();
clock.tick(20);

var cookieArray = getAllCookies();
assert.equal(cookieArray.length, 0);

var deviceId = 'test_device_id';
var amplitude = new AmplitudeClient();

amplitude.init(apiKey, null, {
deviceId: deviceId,
disableCookies: true,
});

cookieArray = getAllCookies();
assert.equal(cookieArray.length, 0);

// spied cookie operations should not be fired
assert.equal(spiedSet.called, false);
assert.equal(spiedGet.called, false);

// restore stub, spy
stubbedGetLocation.restore();
spiedSet.restore();
spiedGet.restore();
});

it('should create cookies if disabledCookies = false', function () {
Expand All @@ -2766,9 +2797,9 @@ describe('AmplitudeClient', function () {
assert.equal(cookieArray.length, 0);

var deviceId = 'test_device_id';
var amplitude2 = new AmplitudeClient();
var amplitude = new AmplitudeClient();

amplitude2.init(apiKey, null, {
amplitude.init(apiKey, null, {
deviceId: deviceId,
disableCookies: false,
});
Expand Down
18 changes: 18 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sinon from 'sinon';
import utils from '../src/utils.js';
import constants from '../src/constants.js';
import GlobalScope from '../src/global-scope';

describe('utils', function () {
describe('isEmptyString', function () {
Expand Down Expand Up @@ -280,4 +281,21 @@ describe('utils', function () {
assert.isFalse(utils.validateSessionId(false));
});
});

describe('getHost', function () {
it('should return hostname for url', function () {
const url = 'https://amplitude.is.good.com/test';
assert.equal(utils.getHost(url), 'amplitude.is.good.com');
});

it('should return current hostname if no url is provided', function () {
assert.equal(utils.getHost(), GlobalScope.location.hostname);
});
});

describe('getLocation', function () {
it('should return global location', function () {
assert.equal(utils.getLocation(), GlobalScope.location);
});
});
});