Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix $browser: change to encodeURIComponent and decodeURIComponent for co... #8125

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/ng/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,16 +280,16 @@ function Browser(window, document, $log, $sniffer) {
* @returns {Object} Hash of all cookies (if called without any parameter)
*/
self.cookies = function(name, value) {
/* global escape: false, unescape: false */
/* global encodeURIComponent: false, decodeURIComponent: false */
Copy link
Contributor

Choose a reason for hiding this comment

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

var cookieLength, cookieArray, cookie, i, index;

if (name) {
if (value === undefined) {
rawDocument.cookie = escape(name) + "=;path=" + cookiePath +
rawDocument.cookie = encodeURIComponent(name) + "=;path=" + cookiePath +
";expires=Thu, 01 Jan 1970 00:00:00 GMT";
} else {
if (isString(value)) {
cookieLength = (rawDocument.cookie = escape(name) + '=' + escape(value) +
cookieLength = (rawDocument.cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value) +
';path=' + cookiePath).length + 1;

// per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:
Expand All @@ -313,12 +313,12 @@ function Browser(window, document, $log, $sniffer) {
cookie = cookieArray[i];
index = cookie.indexOf('=');
if (index > 0) { //ignore nameless cookies
name = unescape(cookie.substring(0, index));
name = decodeURIComponent(cookie.substring(0, index));
// the first value that is seen for a cookie is the most
// specific one. values for the same cookie name that
// follow are for less specific paths.
if (lastCookies[name] === undefined) {
lastCookies[name] = unescape(cookie.substring(index + 1));
lastCookies[name] = decodeURIComponent(cookie.substring(index + 1));
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion test/ng/browserSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ describe('browser', function() {
var i, longVal = '', cookieStr;

for(i=0; i<4083; i++) {
longVal += '+';
longVal += 'x';
}

cookieStr = document.cookie;
Expand Down Expand Up @@ -323,6 +323,11 @@ describe('browser', function() {
expect(browser.cookies()[' cookie name ']).toEqual(' cookie value ');
expect(browser.cookies()['cookie name']).not.toBeDefined();
});

it('should unscape special characters in cookie values', function() {
document.cookie = 'cookie_name=cookie_value_%E2%82%AC';
expect(browser.cookies()['cookie_name']).toEqual('cookie_value_€');
});
});


Expand Down