Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent XSS in the cookie attributes #400

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could write

stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];

and save some more bytes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bildschirmfoto 2018-02-02 um 09 46 58

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, unless attributes[attributeName] were needed afterwards for some other processing... but it doesn't look like it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Done.

}
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