Skip to content

Commit 7bc2135

Browse files
RFC 6265 do not allow double quotes for cookie octet value
1 parent a0ca45a commit 7bc2135

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎src/js.cookie.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
window.Cookies = factory();
1818
}
1919
}(function () {
20+
function encode(s) {
21+
return s
22+
.replace(/"/g, '%22');
23+
}
24+
25+
function decode(s) {
26+
return s
27+
.replace(/%22/g, '"');
28+
}
29+
2030
function parseCookieValue(s) {
2131
var value = s;
2232
if (s.indexOf('"') === 0) {
@@ -64,7 +74,7 @@
6474
}
6575

6676
return (document.cookie = [
67-
key, '=', api.json ? JSON.stringify(value) : String(value),
77+
key, '=', encode(api.json ? JSON.stringify(value) : String(value)),
6878
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
6979
options.path ? '; path=' + options.path : '',
7080
options.domain ? '; domain=' + options.domain : '',
@@ -85,7 +95,7 @@
8595
for (; i < l; i++) {
8696
var parts = cookies[i].split('='),
8797
name = parts.shift(),
88-
cookie = parts.join('=');
98+
cookie = decode(parts.join('='));
8999

90100
if (key === name) {
91101
// If second argument (value) is a function it's a converter...

‎test/tests.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ test('json = true', function () {
256256

257257
if ('JSON' in window) {
258258
Cookies.set('c', { foo: 'bar' });
259-
strictEqual(document.cookie, 'c=' + JSON.stringify({ foo: 'bar' }), 'should stringify JSON');
259+
strictEqual(document.cookie, 'c=' + JSON.stringify({ foo: 'bar' }).replace(/"/g, '%22'), 'should stringify JSON');
260260
} else {
261261
ok(true);
262262
}

0 commit comments

Comments
 (0)