Skip to content

Commit

Permalink
Prevent samesite cookie warning from triggering (close #886)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Boocock authored and paulboocock committed Feb 3, 2021
1 parent 45b934b commit a5630d1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 26 deletions.
17 changes: 4 additions & 13 deletions src/js/lib/detectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

import isUndefined from 'lodash/isUndefined';
import { jstz } from 'jstimezonedetect';
import { isFunction, cookie } from '../lib/helpers'
import { isFunction } from '../lib/helpers'

var windowAlias = window,
navigatorAlias = navigator,
Expand Down Expand Up @@ -89,14 +89,7 @@ export function localStorageAccessible() {
/*
* Does browser have cookies enabled (for this site)?
*/
export function hasCookies(testCookieName) {
var cookieName = testCookieName || 'testcookie';

if (isUndefined(navigatorAlias.cookieEnabled)) {
cookie(cookieName, '1');
return cookie(cookieName) === '1' ? '1' : '0';
}

export function hasCookies() {
return navigatorAlias.cookieEnabled ? '1' : '0';
}

Expand Down Expand Up @@ -154,7 +147,7 @@ export function detectDocumentSize() {
* @param string testCookieName Name to use for the test cookie
* @return Object containing browser features
*/
export function detectBrowserFeatures(useCookies, testCookieName) {
export function detectBrowserFeatures() {
var i,
mimeType,
pluginMap = {
Expand Down Expand Up @@ -206,9 +199,7 @@ export function detectBrowserFeatures(useCookies, testCookieName) {
// Other browser features
features.res = screenAlias.width + 'x' + screenAlias.height;
features.cd = screenAlias.colorDepth;
if (useCookies) {
features.cookie = hasCookies(testCookieName);
}
features.cookie = hasCookies();

return features;
}
12 changes: 6 additions & 6 deletions src/js/lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ export function attemptWriteSessionStorage(key, value) {
/**
* Finds the root domain
*/
export function findRootDomain() {
export function findRootDomain(sameSite, secure) {
var cookiePrefix = '_sp_root_domain_test_';
var cookieName = cookiePrefix + new Date().getTime();
var cookieValue = '_test_value_' + new Date().getTime();
Expand All @@ -410,13 +410,13 @@ export function findRootDomain() {
var position = split.length - 1;
while (position >= 0) {
var currentDomain = split.slice(position, split.length).join('.');
cookie(cookieName, cookieValue, 0, '/', currentDomain);
cookie(cookieName, cookieValue, 0, '/', currentDomain, sameSite, secure);
if (cookie(cookieName) === cookieValue) {
// Clean up created cookie(s)
deleteCookie(cookieName, currentDomain);
deleteCookie(cookieName, currentDomain, sameSite, secure);
var cookieNames = getCookiesWithPrefix(cookiePrefix);
for (var i = 0; i < cookieNames.length; i++) {
deleteCookie(cookieNames[i], currentDomain);
deleteCookie(cookieNames[i], currentDomain, sameSite, secure);
}

return currentDomain;
Expand Down Expand Up @@ -450,8 +450,8 @@ export function isValueInArray(val, array) {
* @param cookieName The name of the cookie to delete
* @param domainName The domain the cookie is in
*/
export function deleteCookie(cookieName, domainName) {
cookie(cookieName, '', -1, '/', domainName);
export function deleteCookie(cookieName, domainName, sameSite, secure) {
cookie(cookieName, '', -1, '/', domainName, sameSite, secure);
}

/**
Expand Down
11 changes: 4 additions & 7 deletions src/js/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,7 @@ export function Tracker(functionName, namespace, version, mutSnowplowState, argm
// Browser language (or Windows language for IE). Imperfect but CloudFront doesn't log the Accept-Language header
browserLanguage = navigatorAlias.userLanguage || navigatorAlias.language,
// Browser features via client-side data collection
browserFeatures = detectBrowserFeatures(
configStateStorageStrategy == 'cookie' || configStateStorageStrategy == 'cookieAndLocalStorage',
getSnowplowCookieName('testcookie')
),
browserFeatures = detectBrowserFeatures(),
// Unique ID for the tracker instance used to mark links which are being tracked
trackerId = functionName + '_' + namespace,
// Last activity timestamp
Expand Down Expand Up @@ -353,7 +350,7 @@ export function Tracker(functionName, namespace, version, mutSnowplowState, argm
let gdprBasisData = {};

if (argmap.hasOwnProperty('discoverRootDomain') && argmap.discoverRootDomain) {
configCookieDomain = findRootDomain();
configCookieDomain = findRootDomain(configCookieSameSite, configCookieSecure);
}

if (autoContexts.gaCookies) {
Expand Down Expand Up @@ -691,8 +688,8 @@ export function Tracker(functionName, namespace, version, mutSnowplowState, argm
const sesname = getSnowplowCookieName('ses');
attemptDeleteLocalStorage(idname);
attemptDeleteLocalStorage(sesname);
deleteCookie(idname);
deleteCookie(sesname);
deleteCookie(idname, configCookieDomain, configCookieSameSite, configCookieSecure);
deleteCookie(sesname, configCookieDomain, configCookieSameSite, configCookieSecure);
}

/*
Expand Down

0 comments on commit a5630d1

Please sign in to comment.