Skip to content

Commit 7185983

Browse files
authored
Merge pull request js-cookie#418 from carhartl/fix-getjson-accidentally-writing-cookie-with-too-many-arguments
Fix getJSON() accidentally writing cookie with too many arguments
2 parents 8b2a880 + d7534cd commit 7185983

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ Read all visible cookies:
9595
Cookies.get(); // => { name: 'value' }
9696
```
9797

98+
*Note: It is not possible to read a particular cookie by passing one of the cookie attributes (which may or may not
99+
have been used when writing the cookie in question):*
100+
101+
```javascript
102+
Cookies.get('foo', domain: { 'sub.example.com' }); // `domain` won't have any effect...!
103+
```
104+
105+
The cookie with the name `foo` will only be available on `.get()` if it's visible from where the
106+
code is called; the domain and/or path attribute will not have an effect when reading.
107+
98108
Delete cookie:
99109

100110
```javascript

src/js.cookie.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@
140140
api.get = function (key) {
141141
return api.call(api, key);
142142
};
143-
api.getJSON = function () {
144-
return api.apply({
143+
api.getJSON = function (key) {
144+
return api.call({
145145
json: true
146-
}, arguments);
146+
}, key);
147147
};
148148
api.remove = function (key, attributes) {
149149
api(key, '', extend(attributes, {

test/tests.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,11 @@ QUnit.test('Cookies with escaped quotes in json using raw converters', function
474474
assert.strictEqual(Cookies.get('c'), '{ \\"foo\\": \\"bar\\" }', 'returns unparsed cookie with enclosing quotes removed');
475475
});
476476

477+
QUnit.test('Prevent accidentally writing cookie when passing unexpected argument', function (assert) {
478+
Cookies.getJSON('c', { foo: 'bar' });
479+
assert.strictEqual(Cookies.get('c'), undefined, 'should not write any cookie');
480+
});
481+
477482
QUnit.module('noConflict', lifecycle);
478483

479484
QUnit.test('do not conflict with existent globals', function (assert) {

0 commit comments

Comments
 (0)