Skip to content

Commit 81e202a

Browse files
Do not fail if there is a malformed cookie name
Closes js-cookiegh-198. Closes js-cookiegh-196.
1 parent 0769556 commit 81e202a

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/js.cookie.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@
9393

9494
for (; i < cookies.length; i++) {
9595
var parts = cookies[i].split('=');
96-
var name = parts[0].replace(rdecode, decodeURIComponent);
9796
var cookie = parts.slice(1).join('=');
9897

9998
if (cookie.charAt(0) === '"') {
10099
cookie = cookie.slice(1, -1);
101100
}
102101

103102
try {
103+
var name = parts[0].replace(rdecode, decodeURIComponent);
104104
cookie = converter.read ?
105105
converter.read(cookie, name) : converter(cookie, name) ||
106106
cookie.replace(rdecode, decodeURIComponent);

test/tests.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ QUnit.test('RFC 6265 - reading cookie-octet enclosed in DQUOTE', function (asser
9898
assert.strictEqual(Cookies.get('c'), 'v', 'should simply ignore quoted strings');
9999
});
100100

101+
// github.com/js-cookie/js-cookie/issues/196
102+
QUnit.test('Call to read cookie when there is another unrelated cookie with malformed encoding in the name', function (assert) {
103+
assert.expect(2);
104+
document.cookie = 'BS%BS=1';
105+
document.cookie = 'c=v';
106+
assert.strictEqual(Cookies.get('c'), 'v', 'should not throw a URI malformed exception when retrieving a single cookie');
107+
assert.deepEqual(Cookies.get(), { c: 'v' }, 'should not throw a URI malformed exception when retrieving all cookies');
108+
document.cookie = 'BS%BS=1; expires=Thu, 01 Jan 1970 00:00:00 GMT';
109+
});
110+
101111
// github.com/js-cookie/js-cookie/pull/62
102112
QUnit.test('Call to read cookie when there is another unrelated cookie with malformed encoding in the value', function (assert) {
103113
assert.expect(2);

0 commit comments

Comments
 (0)