Skip to content

Commit ff9dfc6

Browse files
committed
Handle RFC 2068 quoted cookie values properly, closes carhartl#57
1 parent 3ec7c95 commit ff9dfc6

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ HEAD
1212
- Removing licensing under GPL Version 2, the plugin is now released under MIT License only
1313
(following the jQuery library itself here).
1414

15+
- Properly handle RFC 2068 quoted cookie values.
16+
1517
1.2.0
1618
-----
1719
- Adding `$.removeCookie('foo')` for deleting a cookie, using `$.cookie('foo', null)` is now deprecated.

jquery.cookie.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@
1414
}
1515

1616
function decoded(s) {
17-
return decodeURIComponent(s.replace(pluses, ' '));
17+
return unRfc2068(decodeURIComponent(s.replace(pluses, ' ')));
1818
}
19+
20+
function unRfc2068(value) {
21+
if (value.indexOf('"') === 0) {
22+
// This is a quoted cookie as according to RFC2068, unescape
23+
value = value.slice(1, -1).replace('\\"', '"').replace('\\\\', '\\');
24+
}
25+
return value;
26+
};
1927

2028
var config = $.cookie = function (key, value, options) {
2129

test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ test('not existing', function () {
3333
equal($.cookie('whatever'), null, 'should return null');
3434
});
3535

36+
test('rfc2068 quoted string', function () {
37+
expect(1);
38+
document.cookie = 'c="v@address.com\\"\\\\"';
39+
equal($.cookie('c'), 'v@address.com"\\', 'should decode rfc2068 quoted string');
40+
});
41+
3642
test('decode', function () {
3743
expect(1);
3844
document.cookie = encodeURIComponent(' c') + '=' + encodeURIComponent(' v');

0 commit comments

Comments
 (0)