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

fix handling of = in cookie values #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,18 @@ function parse(str) {
var obj = {};
var pairs = str.split(/ *; */);
var pair;
if ('' == pairs[0]) return obj;
var eqidx;
var name;
var value;
for (var i = 0; i < pairs.length; ++i) {
pair = pairs[i].split('=');
obj[decode(pair[0])] = decode(pair[1]);
pair = pairs[i];
eqidx = pair.indexOf("=");
if (eqidx === -1) {
eqidx = pair.length;
}
name = decode(pair.substr(0, eqidx));
value = decode(pair.substr(eqidx + 1)); // +1 because we don't want the =
obj[name] = value;
}
return obj;
}
Expand Down
9 changes: 9 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ describe('cookie()', function(){
assert('ferret' == obj.species, '.species failed');
assert(null == obj.bad);
});

it('should properly handle equal signs in the value', function(){
// see https://github.com/matthewmueller/next-cookies/issues/20
var values = ['=', '==', '===', 'a=b', 'a==b', 'a=', 'SEnxqTgooNWEFfQ9gUipwdhUrZm1VejMLDQ=='];
values.forEach(function(value) {
document.cookie = 'name=' + value;
assert.equal(value, cookie('name'));
});
});
})