Skip to content

Commit 81fe656

Browse files
Paul Boocockpaulboocock
authored andcommitted
Fix SameSite cookie warning for storage Cookies (close #795)
1 parent 8b79a64 commit 81fe656

File tree

5 files changed

+47
-22
lines changed

5 files changed

+47
-22
lines changed

npm-shrinkwrap.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"name": "snowplow-tracker",
33
"version": "2.13.0",
44
"dependencies": {
5-
"browser-cookie-lite": "^1.0.4",
65
"jstimezonedetect": "1.0.5",
76
"sha1": "git://github.com/pvorb/node-sha1.git#910081c83f3661507d9d89e66e3f38d8b59d5559",
87
"snowplow-tracker-core": "^0.7.2",

src/js/lib/detectors.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
isFunction = require('lodash/isFunction'),
3939
isUndefined = require('lodash/isUndefined'),
4040
tz = require('jstimezonedetect').jstz.determine(),
41-
cookie = require('browser-cookie-lite'),
41+
helpers = require('./helpers'),
4242

4343
object = typeof exports !== 'undefined' ? exports : this, // For eventual node.js environment support
4444

@@ -99,8 +99,8 @@
9999
var cookieName = testCookieName || 'testcookie';
100100

101101
if (isUndefined(navigatorAlias.cookieEnabled)) {
102-
cookie.cookie(cookieName, '1');
103-
return cookie.cookie(cookieName) === '1' ? '1' : '0';
102+
helpers.cookie(cookieName, '1');
103+
return helpers.cookie(cookieName) === '1' ? '1' : '0';
104104
}
105105

106106
return navigatorAlias.cookieEnabled ? '1' : '0';

src/js/lib/helpers.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
isUndefined = require('lodash/isUndefined'),
4040
isObject = require('lodash/isObject'),
4141
map = require('lodash/map'),
42-
cookie = require('browser-cookie-lite'),
4342

4443
object = typeof exports !== 'undefined' ? exports : this; // For eventual node.js environment support
4544

@@ -391,8 +390,8 @@
391390
var position = split.length - 1;
392391
while (position >= 0) {
393392
var currentDomain = split.slice(position, split.length).join('.');
394-
cookie.cookie(cookieName, cookieValue, 0, '/', currentDomain);
395-
if (cookie.cookie(cookieName) === cookieValue) {
393+
object.cookie(cookieName, cookieValue, 0, '/', currentDomain);
394+
if (object.cookie(cookieName) === cookieValue) {
396395

397396
// Clean up created cookie(s)
398397
object.deleteCookie(cookieName, currentDomain);
@@ -433,7 +432,7 @@
433432
* @param domainName The domain the cookie is in
434433
*/
435434
object.deleteCookie = function (cookieName, domainName) {
436-
cookie.cookie(cookieName, '', -1, '/', domainName);
435+
object.cookie(cookieName, '', -1, '/', domainName);
437436
};
438437

439438
/**
@@ -453,6 +452,33 @@
453452
return cookieNames;
454453
};
455454

455+
/**
456+
* Get and set the cookies associated with the current document in browser
457+
* This implementation always returns a string, returns the cookie value if only name is specified
458+
*
459+
* @param name The cookie name (required)
460+
* @param value The cookie value
461+
* @param ttl The cookie Time To Live (seconds)
462+
* @param path The cookies path
463+
* @param domain The cookies domain
464+
* @param samesite The cookies samesite attribute
465+
* @param secure Boolean to specify if cookie should be secure
466+
* @return string The cookies value
467+
*/
468+
object.cookie = function(name, value, ttl, path, domain, samesite, secure) {
469+
470+
if (arguments.length > 1) {
471+
return document.cookie = name + "=" + encodeURIComponent(value) +
472+
(ttl ? "; Expires=" + new Date(+new Date()+(ttl*1000)).toUTCString() : "") +
473+
(path ? "; Path=" + path : "") +
474+
(domain ? "; Domain=" + domain : "") +
475+
(samesite ? "; SameSite=" + samesite : "") +
476+
(secure ? "; Secure" : "");
477+
}
478+
479+
return decodeURIComponent((("; "+document.cookie).split("; "+name+"=")[1]||"").split(";")[0]);
480+
}
481+
456482
/**
457483
* Parses an object and returns either the
458484
* integer or undefined.

src/js/tracker.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
map = require('lodash/map'),
4040
helpers = require('./lib/helpers'),
4141
proxies = require('./lib/proxies'),
42-
cookie = require('browser-cookie-lite'),
4342
detectors = require('./lib/detectors'),
4443
sha1 = require('sha1'),
4544
links = require('./links'),
@@ -202,6 +201,12 @@
202201
// Default is user agent defined.
203202
configCookiePath = '/',
204203

204+
// First-party cookie samesite attribute
205+
configCookieSameSite = argmap.hasOwnProperty('cookieSameSite') ? argmap.cookieSameSite : 'None',
206+
207+
// First-party cookie secure attribute
208+
configCookieSecure = argmap.hasOwnProperty('cookieSecure') ? argmap.cookieSecure : true,
209+
205210
// Do Not Track browser feature
206211
dnt = navigatorAlias.doNotTrack || navigatorAlias.msDoNotTrack || windowAlias.doNotTrack,
207212

@@ -509,7 +514,7 @@
509514
// Set to true if Opt-out cookie is defined
510515
var toOptoutByCookie;
511516
if (configOptOutCookie) {
512-
toOptoutByCookie = !!cookie.cookie(configOptOutCookie);
517+
toOptoutByCookie = !!helpers.cookie(configOptOutCookie);
513518
} else {
514519
toOptoutByCookie = false;
515520
}
@@ -536,7 +541,7 @@
536541
return helpers.attemptGetLocalStorage(fullName);
537542
} else if (configStateStorageStrategy == 'cookie' ||
538543
configStateStorageStrategy == 'cookieAndLocalStorage') {
539-
return cookie.cookie(fullName);
544+
return helpers.cookie(fullName);
540545
}
541546
}
542547

@@ -655,7 +660,7 @@
655660
helpers.attemptWriteLocalStorage(name, value, timeout);
656661
} else if (configStateStorageStrategy == 'cookie' ||
657662
configStateStorageStrategy == 'cookieAndLocalStorage') {
658-
cookie.cookie(name, value, timeout, configCookiePath, configCookieDomain);
663+
helpers.cookie(name, value, timeout, configCookiePath, configCookieDomain, configCookieSameSite, configCookieSecure);
659664
}
660665
}
661666

@@ -766,7 +771,7 @@
766771

767772
var toOptoutByCookie;
768773
if (configOptOutCookie) {
769-
toOptoutByCookie = !!cookie.cookie(configOptOutCookie);
774+
toOptoutByCookie = !!helpers.cookie(configOptOutCookie);
770775
} else {
771776
toOptoutByCookie = false;
772777
}
@@ -778,8 +783,8 @@
778783
helpers.attemptWriteLocalStorage(sesname, '');
779784
} else if (configStateStorageStrategy == 'cookie' ||
780785
configStateStorageStrategy == 'cookieAndLocalStorage') {
781-
cookie.cookie(idname, '', -1, configCookiePath, configCookieDomain);
782-
cookie.cookie(sesname, '', -1, configCookiePath, configCookieDomain);
786+
helpers.cookie(idname, '', -1, configCookiePath, configCookieDomain, configCookieSameSite, configCookieSecure);
787+
helpers.cookie(sesname, '', -1, configCookiePath, configCookieDomain, configCookieSameSite, configCookieSecure);
783788
}
784789
return;
785790
}
@@ -1452,7 +1457,7 @@
14521457
function getGaCookiesContext() {
14531458
var gaCookieData = {};
14541459
forEach(['__utma', '__utmb', '__utmc', '__utmv', '__utmz', '_ga'], function (cookieType) {
1455-
var value = cookie.cookie(cookieType);
1460+
var value = helpers.cookie(cookieType);
14561461
if (value) {
14571462
gaCookieData[cookieType] = value;
14581463
}
@@ -2176,7 +2181,7 @@
21762181
* @param string cookieName Name of the cookie whose value will be assigned to businessUserId
21772182
*/
21782183
apiMethods.setUserIdFromCookie = function(cookieName) {
2179-
businessUserId = cookie.cookie(cookieName);
2184+
businessUserId = helpers.cookie(cookieName);
21802185
};
21812186

21822187
/**

0 commit comments

Comments
 (0)