Skip to content

Commit 5ac2a2a

Browse files
Test characters allowed in the cookie-name
1 parent 5235b01 commit 5ac2a2a

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

‎src/js.cookie.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
value = value.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
8787

8888
key = encodeURIComponent(String(key));
89+
key = key.replace(/%(23|24|26|2B|5E|60|7C)/, decodeURIComponent);
8990
key = key.replace(/[\(\)]/g, escape);
9091

9192
return (document.cookie = [

‎test/encoding.js‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,66 @@ test('RFC 6265 - character not allowed in the cookie-name "\\t"', function () {
350350
test('RFC 6265 - character not allowed in the cookie-name " "', function () {
351351
expect(2);
352352
Cookies.set(' ', 'v');
353-
strictEqual(Cookies.get(' '), 'v', 'should handle whitespace character');
353+
strictEqual(Cookies.get(' '), 'v', 'should handle the whitespace character');
354354
strictEqual(document.cookie, '%20=v', 'whitespace is not allowed, need to encode');
355355
});
356356

357+
test('RFC 6265 - character allowed in the cookie-name "#"', function () {
358+
expect(2);
359+
Cookies.set('#', 'v');
360+
strictEqual(Cookies.get('#'), 'v', 'should handle the sharp character');
361+
strictEqual(document.cookie, '#=v', 'sharp is allowed, should not encode');
362+
});
363+
364+
test('RFC 6265 - character allowed in the cookie-name "$"', function () {
365+
expect(2);
366+
Cookies.set('$', 'v');
367+
strictEqual(Cookies.get('$'), 'v', 'should handle the dollar sign character');
368+
strictEqual(document.cookie, '$=v', 'dollar sign is allowed, should not encode');
369+
});
370+
371+
test('RFC 6265 - character allowed in cookie-name "%"', function () {
372+
expect(2);
373+
Cookies.set('%', 'v');
374+
strictEqual(Cookies.get('%'), 'v', 'should handle the percent character');
375+
strictEqual(document.cookie, '%25=v', 'percent is allowed, but need to be escaped');
376+
});
377+
378+
test('RFC 6265 - character allowed in the cookie-name "&"', function () {
379+
expect(2);
380+
Cookies.set('&', 'v');
381+
strictEqual(Cookies.get('&'), 'v', 'should handle the ampersand character');
382+
strictEqual(document.cookie, '&=v', 'ampersand is allowed, should not encode');
383+
});
384+
385+
test('RFC 6265 - character allowed in the cookie-name "+"', function () {
386+
expect(2);
387+
Cookies.set('+', 'v');
388+
strictEqual(Cookies.get('+'), 'v', 'should handle the plus character');
389+
strictEqual(document.cookie, '+=v', 'plus is allowed, should not encode');
390+
});
391+
392+
test('RFC 6265 - character allowed in the cookie-name "^"', function () {
393+
expect(2);
394+
Cookies.set('^', 'v');
395+
strictEqual(Cookies.get('^'), 'v', 'should handle the caret character');
396+
strictEqual(document.cookie, '^=v', 'caret is allowed, should not encode');
397+
});
398+
399+
test('RFC 6265 - character allowed in the cookie-name "`"', function () {
400+
expect(2);
401+
Cookies.set('`', 'v');
402+
strictEqual(Cookies.get('`'), 'v', 'should handle the grave accent character');
403+
strictEqual(document.cookie, '`=v', 'grave accent is allowed, should not encode');
404+
});
405+
406+
test('RFC 6265 - character allowed in the cookie-name "|"', function () {
407+
expect(2);
408+
Cookies.set('|', 'v');
409+
strictEqual(Cookies.get('|'), 'v', 'should handle the pipe character');
410+
strictEqual(document.cookie, '|=v', 'pipe is allowed, should not encode');
411+
});
412+
357413
test('cookie-name - 2 bytes characters', function () {
358414
expect(2);
359415

0 commit comments

Comments
 (0)