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

Allow for cookies to be secure #285

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions src/store-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ var storeAPI = {

// set will store the given value at key and returns value.
// Calling set with value === undefined is equivalent to calling remove.
set: function(key, value) {
set: function(key, value, isSecure = false) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using default parameter values will break compatibility with a bunch of browsers that Store.js is supposed to support.

Copy link

@Lawlez Lawlez Aug 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about something like this:

set: function(key, value, options) { ...

this.storage.write(this._namespacePrefix + key, this._serialize(value), options)

and in cookieStorage.js:
function write(key, data, options) {
if(!key) { return }
var opts = options ? options : ""
doc.cookie = escape(key) + "=" + escape(data) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/" + options
} }`

the user could then set a cookie like:
store.set('myName', 'data', ';Secure;SameSite=strict')

if (value === undefined) {
return this.remove(key)
}
this.storage.write(this._namespacePrefix + key, this._serialize(value))
this.storage.write(this._namespacePrefix + key, this._serialize(value), isSecure)
return value
},

Expand Down
5 changes: 3 additions & 2 deletions storages/cookieStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ function each(callback) {
}
}

function write(key, data) {
function write(key, data, isSecure = false) {
if(!key) { return }
doc.cookie = escape(key) + "=" + escape(data) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/"
var secure = isSecure ? ";secure" : ""
doc.cookie = escape(key) + "=" + escape(data) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/" + secure
}

function remove(key) {
Expand Down