Skip to content

Commit

Permalink
Prevent XSS in the Path attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
FagnerMartinsBrack committed Feb 1, 2018
1 parent 73476a4 commit 7804f21
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -203,7 +203,9 @@ Cookies.remove('name', { path: '' });
(From [Internet Explorer Cookie Internals (FAQ)](http://blogs.msdn.com/b/ieinternals/archive/2009/08/20/wininet-ie-cookie-internals-faq.aspx))

This means one cannot set a path using `path: window.location.pathname` in case such pathname contains a filename like so: `/check.html` (or at least, such cookie cannot be read correctly).
This means one cannot set a path using `window.location.pathname` in case such pathname contains a filename like so: `/check.html` (or at least, such cookie cannot be read correctly).

In fact, you should never allow untrusted input to set the cookie attributes or you might be exposed to a [XSS attack](https://github.com/js-cookie/js-cookie/issues/396).

### domain

Expand Down
10 changes: 10 additions & 0 deletions src/js.cookie.js
Expand Up @@ -87,6 +87,16 @@
if (attributes[attributeName] === true) {
continue;
}

// Considers RFC 6265 section 5.2:
// ...
// 3. If the remaining unparsed-attributes contains a %x3B (";")
// character:
// Consume the characters of the unparsed-attributes up to,
// not including, the first %x3B (";") character.
// ...
attributes[attributeName] = attributes[attributeName].split(';')[0];

stringifiedAttributes += '=' + attributes[attributeName];
}
return (document.cookie = key + '=' + value + stringifiedAttributes);
Expand Down
10 changes: 10 additions & 0 deletions test/tests.js
Expand Up @@ -294,6 +294,16 @@ QUnit.test('undefined attribute value', function (assert) {
}), 'c=v; path=/', 'should not write undefined unofficial attribute');
});

// github.com/js-cookie/js-cookie/issues/396
QUnit.test('sanitization of attributes to prevent XSS from untrusted input', function (assert) {
assert.expect(1);
assert.strictEqual(Cookies.set('c', 'v', {
path: '/;domain=sub.domain.com',
domain: 'site.com;remove_this',
customAttribute: 'value;;remove_this'
}), 'c=v; path=/; domain=site.com; customAttribute=value', 'should not allow semicolon in a cookie attribute');
});

QUnit.module('remove', lifecycle);

QUnit.test('deletion', function (assert) {
Expand Down

0 comments on commit 7804f21

Please sign in to comment.