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

Apply secure flag to cookies when running on https protocol #1158

Merged
merged 1 commit into from
Mar 18, 2021
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
6 changes: 6 additions & 0 deletions src/helper/storage/cookie.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Cookie from 'js-cookie';
import objectHelper from '../object';
import windowHandler from '../window';
function CookieStorage() {}

CookieStorage.prototype.getItem = function(key) {
Expand All @@ -17,6 +18,11 @@ CookieStorage.prototype.setItem = function(key, value, options) {
},
options
);

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

Cookie.set(key, value, params);
};

Expand Down
7 changes: 6 additions & 1 deletion test/helper/storage-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ describe('helpers storage handler', function() {
beforeEach(function() {
sinon.stub(windowHandler, 'getWindow').callsFake(function(message) {
return {
localStorage: new MockLocalStorage()
localStorage: new MockLocalStorage(),
location: {
protocol: 'http:'
}
};
});
});
Expand All @@ -40,6 +43,7 @@ describe('helpers storage handler', function() {
let setItemSpy;
let getItemStub;
let removeItemSpy;

beforeEach(function() {
windowHandler.getWindow.restore();

Expand Down Expand Up @@ -119,6 +123,7 @@ describe('helpers storage handler', function() {
var handler = new StorageHandler({ __tryLocalStorageFirst: true });

expect(handler.storage).to.be.a(MockLocalStorage);

handler.setItem('some', 'value', { options: true });

expect(handler.storage).to.be.a(CookieStorage);
Expand Down
38 changes: 38 additions & 0 deletions test/helper/storage.cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import expect from 'expect.js';
import sinon from 'sinon';

import CookieStorage from '../../src/helper/storage/cookie';
import windowHandler from '../../src/helper/window';

var cookieStorage = new CookieStorage();
const KEY = 'foo';
const VALUE = 'bar';
Expand Down Expand Up @@ -39,21 +41,57 @@ describe('storage.cookies', function() {
});
});
describe('setItem', function() {
beforeEach(function() {
sinon.stub(windowHandler, 'getWindow').callsFake(function() {
return {
location: {
protocol: 'http:'
}
};
});
});

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

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

expect(CookieLibrary.set.firstCall.args).to.be.eql([
'foo',
'bar',
{ expires: 1 }
]);
});

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

expect(CookieLibrary.set.firstCall.args).to.be.eql([
'foo',
'bar',
{ expires: 2, test: true }
]);
});

it('sets the secure flag on cookies when using the https protocol', function() {
windowHandler.getWindow.restore();
sinon.stub(windowHandler, 'getWindow').callsFake(function() {
return {
location: {
protocol: 'https:'
}
};
});

cookieStorage.setItem(KEY, VALUE, { expires: 2, test: true });

expect(CookieLibrary.set.firstCall.args).to.be.eql([
'foo',
'bar',
{ expires: 2, test: true, secure: true }
]);
});
});
});
18 changes: 18 additions & 0 deletions test/web-auth/cross-origin-authentication.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,19 @@ describe('auth0.WebAuth.crossOriginAuthentication', function() {
});
global.window = {};
});

beforeEach(function() {
sinon.spy(Storage.prototype, 'setItem');

sinon.stub(windowHelper, 'getWindow').callsFake(function() {
return {
location: {
protocol: 'http:'
}
};
});
});

afterEach(function() {
request.post.restore();
Storage.prototype.setItem.restore();
Expand All @@ -38,6 +48,10 @@ describe('auth0.WebAuth.crossOriginAuthentication', function() {
if (WebMessageHandler.prototype.run.restore) {
WebMessageHandler.prototype.run.restore();
}

if (windowHelper.getWindow.restore) {
windowHelper.getWindow.restore();
}
});
it('should call /co/authenticate and redirect to /authorize with login_ticket using `username`', function() {
sinon.stub(request, 'post').callsFake(function(url) {
Expand All @@ -63,16 +77,20 @@ describe('auth0.WebAuth.crossOriginAuthentication', function() {
}
});
});

this.co.login({
username: '[email protected]',
password: '123456',
anotherOption: 'foobar'
});

expect(this.webAuthSpy.authorize.getCall(0).args[0]).to.be.eql({
username: '[email protected]',
loginTicket: 'a_login_ticket',
anotherOption: 'foobar'
});

windowHelper.getWindow.restore();
});
it('should call /co/authenticate and redirect to /authorize with login_ticket using `email`', function() {
sinon.stub(request, 'post').callsFake(function(url) {
Expand Down