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

Add compatibility cookie for SameSite, with option to turn it off #1232

Merged
merged 2 commits into from
Jan 21, 2022
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
20 changes: 18 additions & 2 deletions src/helper/storage/cookie.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import Cookie from 'js-cookie';
import objectHelper from '../object';
import windowHandler from '../window';
function CookieStorage() {}

function buildCompatCookieKey(key) {
return '_' + key + '_compat';
}

function CookieStorage(options) {
this._options = options || {};
}

CookieStorage.prototype.getItem = function (key) {
return Cookie.get(key);
var cookie = Cookie.get(key);

return cookie || Cookie.get(buildCompatCookieKey(key));
};

CookieStorage.prototype.removeItem = function (key) {
Cookie.remove(key);
Cookie.remove(buildCompatCookieKey(key));
};

CookieStorage.prototype.setItem = function (key, value, options) {
Expand All @@ -22,6 +32,12 @@ CookieStorage.prototype.setItem = function (key, value, options) {
if (windowHandler.getWindow().location.protocol === 'https:') {
params.secure = true;
params.sameSite = 'none';

if (this._options.legacySameSiteCookie) {
// Save a compatibility cookie without sameSite='none' for browsers that don't support it.
var legacyOptions = objectHelper.blacklist(params, ['sameSite']);
Cookie.set(buildCompatCookieKey(key), value, legacyOptions);
}
}

Cookie.set(key, value, params);
Expand Down
12 changes: 7 additions & 5 deletions src/helper/storage/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import Warn from '../warn';

function StorageHandler(options) {
this.warn = new Warn({});
this.storage = new CookieStorage();
this.storage = new CookieStorage(options);

if (options.__tryLocalStorageFirst !== true) {
return;
}

try {
// some browsers throw an error when trying to access localStorage
// when localStorage is disabled.
Expand All @@ -22,7 +24,7 @@ function StorageHandler(options) {
}
}

StorageHandler.prototype.failover = function() {
StorageHandler.prototype.failover = function () {
if (this.storage instanceof DummyStorage) {
this.warn.warning('DummyStorage: ignore failover');
return;
Expand All @@ -35,7 +37,7 @@ StorageHandler.prototype.failover = function() {
}
};

StorageHandler.prototype.getItem = function(key) {
StorageHandler.prototype.getItem = function (key) {
try {
return this.storage.getItem(key);
} catch (e) {
Expand All @@ -45,7 +47,7 @@ StorageHandler.prototype.getItem = function(key) {
}
};

StorageHandler.prototype.removeItem = function(key) {
StorageHandler.prototype.removeItem = function (key) {
try {
return this.storage.removeItem(key);
} catch (e) {
Expand All @@ -55,7 +57,7 @@ StorageHandler.prototype.removeItem = function(key) {
}
};

StorageHandler.prototype.setItem = function(key, value, options) {
StorageHandler.prototype.setItem = function (key, value, options) {
try {
return this.storage.setItem(key, value, options);
} catch (e) {
Expand Down
Loading