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

Set sameSite to 'none' for cookies when using HTTPS #1229

Merged
merged 1 commit into from
Jan 14, 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
7 changes: 4 additions & 3 deletions src/helper/storage/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import objectHelper from '../object';
import windowHandler from '../window';
function CookieStorage() {}

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

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

CookieStorage.prototype.setItem = function(key, value, options) {
CookieStorage.prototype.setItem = function (key, value, options) {
var params = objectHelper.extend(
{
expires: 1 // 1 day
Expand All @@ -21,6 +21,7 @@ CookieStorage.prototype.setItem = function(key, value, options) {

if (windowHandler.getWindow().location.protocol === 'https:') {
params.secure = true;
params.sameSite = 'none';
}

Cookie.set(key, value, params);
Expand Down
38 changes: 19 additions & 19 deletions test/helper/storage.cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,40 @@ var cookieStorage = new CookieStorage();
const KEY = 'foo';
const VALUE = 'bar';

describe('storage.cookies', function() {
beforeEach(function() {
sinon.stub(CookieLibrary, 'get').callsFake(function(key) {
describe('storage.cookies', function () {
beforeEach(function () {
sinon.stub(CookieLibrary, 'get').callsFake(function (key) {
expect(key).to.be(KEY);
return VALUE;
});
sinon.stub(CookieLibrary, 'set').callsFake(function(key, value) {
sinon.stub(CookieLibrary, 'set').callsFake(function (key, value) {
expect(key).to.be(KEY);
expect(value).to.be(VALUE);
});
sinon.stub(CookieLibrary, 'remove').callsFake(function(key) {
sinon.stub(CookieLibrary, 'remove').callsFake(function (key) {
expect(key).to.be(KEY);
});
});
afterEach(function() {
afterEach(function () {
CookieLibrary.get.restore();
CookieLibrary.set.restore();
CookieLibrary.remove.restore();
});
describe('getItem', function() {
it('calls Cookie.get', function() {
describe('getItem', function () {
it('calls Cookie.get', function () {
const value = cookieStorage.getItem(KEY);
expect(value).to.be(VALUE);
});
});
describe('removeItem', function() {
it('calls Cookie.remove', function(done) {
describe('removeItem', function () {
it('calls Cookie.remove', function (done) {
cookieStorage.removeItem(KEY);
done();
});
});
describe('setItem', function() {
beforeEach(function() {
sinon.stub(windowHandler, 'getWindow').callsFake(function() {
describe('setItem', function () {
beforeEach(function () {
sinon.stub(windowHandler, 'getWindow').callsFake(function () {
return {
location: {
protocol: 'http:'
Expand All @@ -51,11 +51,11 @@ describe('storage.cookies', function() {
});
});

afterEach(function() {
afterEach(function () {
windowHandler.getWindow.restore();
});

it('calls Cookie.set with default values', function() {
it('calls Cookie.set with default values', function () {
cookieStorage.setItem(KEY, VALUE);

expect(CookieLibrary.set.firstCall.args).to.be.eql([
Expand All @@ -65,7 +65,7 @@ describe('storage.cookies', function() {
]);
});

it('calls Cookie.set with custom values', function() {
it('calls Cookie.set with custom values', function () {
cookieStorage.setItem(KEY, VALUE, { expires: 2, test: true });

expect(CookieLibrary.set.firstCall.args).to.be.eql([
Expand All @@ -75,9 +75,9 @@ describe('storage.cookies', function() {
]);
});

it('sets the secure flag on cookies when using the https protocol', function() {
it('sets the secure flag on cookies when using the https protocol', function () {
windowHandler.getWindow.restore();
sinon.stub(windowHandler, 'getWindow').callsFake(function() {
sinon.stub(windowHandler, 'getWindow').callsFake(function () {
return {
location: {
protocol: 'https:'
Expand All @@ -90,7 +90,7 @@ describe('storage.cookies', function() {
expect(CookieLibrary.set.firstCall.args).to.be.eql([
'foo',
'bar',
{ expires: 2, test: true, secure: true }
{ expires: 2, test: true, secure: true, sameSite: 'none' }
]);
});
});
Expand Down