Skip to content

Commit 42d102a

Browse files
encode all unallowed chars in cookie name and value
Also create a TODO to fix the problem of the "[]" in the cookie-name
1 parent 655bd68 commit 42d102a

2 files changed

Lines changed: 27 additions & 26 deletions

File tree

‎src/js.cookie.js‎

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,24 @@
1717
window.Cookies = factory();
1818
}
1919
}(function () {
20-
var unallowedChars = {
21-
';': '%3B',
22-
',': '%2C',
23-
'"': '%22'
20+
var unallowedCharsInName = {
21+
'\\(': '%28',
22+
'\\)': '%29'
2423
};
25-
var unallowedCharsInName = extend(unallowedChars, {
26-
'=': '%3D',
27-
'\t': '%09'
28-
});
29-
var unallowedCharsInValue = extend(unallowedChars, {
30-
' ': '%20'
31-
});
32-
3324
function encode (value, charmap) {
34-
for (var character in charmap) {
25+
value = encodeURIComponent(value);
26+
for ( var character in charmap ) {
3527
value = value
3628
.replace(new RegExp(character, 'g'), charmap[character]);
3729
}
3830
return value;
3931
}
4032

41-
function decode (value, charmap) {
42-
for (var character in charmap) {
43-
value = value
44-
.replace(new RegExp(charmap[character], 'g'), character);
33+
function decode (value) {
34+
var matches = value.match(/(%[0-9A-Z]{2})+/g);
35+
while ( matches && matches.length ) {
36+
var match = matches.shift();
37+
value = value.replace(new RegExp(match, 'g'), decodeURIComponent(match));
4538
}
4639
return value;
4740
}
@@ -51,7 +44,7 @@
5144
value = value.slice(1, -1);
5245
}
5346

54-
value = decode(value, unallowedCharsInValue);
47+
value = decode(value);
5548

5649
if (json) {
5750
try {
@@ -101,7 +94,7 @@
10194
}
10295
} catch(e) {}
10396

104-
value = encode(String(value), unallowedCharsInValue);
97+
value = encode(String(value));
10598

10699
return (document.cookie = [
107100
encode(key, unallowedCharsInName), '=', value,
@@ -126,7 +119,7 @@
126119

127120
for (; i < cookies.length; i++) {
128121
var parts = cookies[i].split('='),
129-
name = decode(parts.shift(), unallowedCharsInName),
122+
name = decode(parts.shift()),
130123
cookie = parts.join('=');
131124

132125
if (key === name) {

‎test/tests.js‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ test('percent character in cookie value', function () {
9191
strictEqual(Cookies.get('bad'), 'foo%', 'should read the percent character');
9292
});
9393

94+
test('percent character in cookie value mixed with encoded values', function () {
95+
expect(1);
96+
document.cookie = 'bad=foo%bar%22baz%bax%3D';
97+
strictEqual(Cookies.get('bad'), 'foo%bar"baz%bax=', 'should read the percent character');
98+
});
99+
94100
asyncTest('malformed cookie value in IE (#88, #117)', function () {
95101
expect(1);
96102
// Sandbox in an iframe so that we can poke around with document.cookie.
@@ -483,12 +489,14 @@ test('passing options reference', function () {
483489
deepEqual(options, { path: '/' }, "won't alter options object");
484490
});
485491

486-
test('[] used in name', function () {
487-
expect(1);
488-
document.cookie = 'c[1]=foo';
489-
Cookies.remove('c[1]');
490-
strictEqual(document.cookie, '', 'delete the cookie');
491-
});
492+
// TODO: Need to handle this without the raw option because [] are not allowed in cookie-name
493+
// github.com/carhartl/jquery-cookie/issues/155
494+
//test('[] used in name', function () {
495+
// expect(1);
496+
// document.cookie = 'c[1]=foo';
497+
// Cookies.remove('c[1]');
498+
// strictEqual(document.cookie, '', 'delete the cookie');
499+
//});
492500

493501
module('converters', lifecycle);
494502

0 commit comments

Comments
 (0)