Skip to content

Commit f41d4d8

Browse files
Test for all unallowed characters in the cookie-name
1 parent 1208729 commit f41d4d8

2 files changed

Lines changed: 84 additions & 18 deletions

File tree

‎src/js.cookie.js‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,30 @@
1717
window.Cookies = factory();
1818
}
1919
}(function () {
20-
var chars = {
21-
',': '%2C',
20+
var unallowedChars = {
2221
';': '%3B',
23-
' ': '%20',
22+
',': '%2C',
2423
'"': '%22'
2524
};
26-
function encode(value) {
27-
for (var character in chars) {
28-
value = value.replace(new RegExp(character, 'g'), chars[character]);
25+
var unallowedCharsInName = extend(unallowedChars, {
26+
'=': '%3D',
27+
'\t': '%09'
28+
});
29+
var unallowedCharsInValue = extend(unallowedChars, {
30+
' ': '%20'
31+
});
32+
function encode(value, charmap) {
33+
for (var character in charmap) {
34+
value = value
35+
.replace(new RegExp(character, 'g'), charmap[character]);
2936
}
3037
return value;
3138
}
3239

33-
function decode(value) {
34-
for (var character in chars) {
35-
value = value.replace(new RegExp(chars[character], 'g'), character);
40+
function decode(value, charmap) {
41+
for (var character in charmap) {
42+
value = value
43+
.replace(new RegExp(charmap[character], 'g'), character);
3644
}
3745
return value;
3846
}
@@ -43,7 +51,7 @@
4351
value = value.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
4452
}
4553

46-
value = decode(value);
54+
value = decode(value, unallowedCharsInValue);
4755

4856
try {
4957
return api.json ? JSON.parse(value) : value;
@@ -85,7 +93,7 @@
8593
}
8694

8795
return (document.cookie = [
88-
key, '=', encode(api.json ? JSON.stringify(value) : String(value)),
96+
encode(key, unallowedCharsInName), '=', encode(api.json ? JSON.stringify(value) : String(value), unallowedCharsInValue),
8997
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
9098
options.path ? '; path=' + options.path : '',
9199
options.domain ? '; domain=' + options.domain : '',
@@ -105,7 +113,7 @@
105113

106114
for (; i < l; i++) {
107115
var parts = cookies[i].split('='),
108-
name = parts.shift(),
116+
name = decode(parts.shift(), unallowedCharsInName),
109117
cookie = parts.join('=');
110118

111119
if (key === name) {

‎test/tests.js‎

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,25 +288,83 @@ test('Quote as the first character in the cookie value', function () {
288288
strictEqual(Cookies.get('c'), '"', 'should handle the quote character');
289289
});
290290

291-
test('RFC 6265 special characters', function () {
291+
test('RFC 6265 disallowed characters in cookie-octet', function () {
292292
expect(5);
293-
293+
294294
Cookies.set('whitespace', ' ');
295295
strictEqual(Cookies.get('whitespace'), ' ', 'should handle the whitespace character');
296-
296+
297297
Cookies.set('comma', ',');
298298
strictEqual(Cookies.get('comma'), ',', 'should handle the comma character');
299-
299+
300300
Cookies.set('semicolon', ';');
301301
strictEqual(Cookies.get('semicolon'), ';', 'should handle the semicolon character');
302-
302+
303303
Cookies.set('backslash', '\\');
304304
strictEqual(Cookies.get('backslash'), '\\', 'should handle the backslash character');
305-
305+
306306
Cookies.set('multiple', '" ,;\\" ,;\\');
307307
strictEqual(Cookies.get('multiple'), '" ,;\\" ,;\\', 'should handle multiple special characters');
308308
});
309309

310+
test('RFC 6265 disallowed characters in cookie-name', function () {
311+
expect(18);
312+
313+
Cookies.set('(', 'v');
314+
strictEqual(Cookies.get('('), 'v', 'should handle the opening parens character');
315+
316+
Cookies.set(')', 'v');
317+
strictEqual(Cookies.get(')'), 'v', 'should handle the closing parens character');
318+
319+
Cookies.set('<', 'v');
320+
strictEqual(Cookies.get('<'), 'v', 'should handle the less-than character');
321+
322+
Cookies.set('>', 'v');
323+
strictEqual(Cookies.get('>'), 'v', 'should handle the greater-than character');
324+
325+
Cookies.set('@', 'v');
326+
strictEqual(Cookies.get('@'), 'v', 'should handle the at character');
327+
328+
Cookies.set(',', 'v');
329+
strictEqual(Cookies.get(','), 'v', 'should handle the comma character');
330+
331+
Cookies.set(';', 'v');
332+
strictEqual(Cookies.get(';'), 'v', 'should handle the semicolon character');
333+
334+
Cookies.set(':', 'v');
335+
strictEqual(Cookies.get(':'), 'v', 'should handle the colon character');
336+
337+
Cookies.set('\\', 'v');
338+
strictEqual(Cookies.get('\\'), 'v', 'should handle the backslash character');
339+
340+
Cookies.set('"', 'v');
341+
strictEqual(Cookies.get('"'), 'v', 'should handle the double quote character');
342+
343+
Cookies.set('/', 'v');
344+
strictEqual(Cookies.get('/'), 'v', 'should handle the slash character');
345+
346+
Cookies.set('[', 'v');
347+
strictEqual(Cookies.get('['), 'v', 'should handle the opening square brackets character');
348+
349+
Cookies.set(']', 'v');
350+
strictEqual(Cookies.get(']'), 'v', 'should handle the closing square brackets character');
351+
352+
Cookies.set('?', 'v');
353+
strictEqual(Cookies.get('?'), 'v', 'should handle the question mark character');
354+
355+
Cookies.set('=', 'v');
356+
strictEqual(Cookies.get('='), 'v', 'should handle the equal sign character');
357+
358+
Cookies.set('{', 'v');
359+
strictEqual(Cookies.get('{'), 'v', 'should handle the opening curly brackets character');
360+
361+
Cookies.set('}', 'v');
362+
strictEqual(Cookies.get('}'), 'v', 'should handle the closing curly brackets character');
363+
364+
Cookies.set(' ', 'v');
365+
strictEqual(Cookies.get(' '), 'v', 'should handle the horizontal tab character');
366+
});
367+
310368
module('removeCookie', lifecycle);
311369

312370
test('deletion', function () {

0 commit comments

Comments
 (0)