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

Commit 9c99590

Browse files
shahataIgorMinar
authored andcommitted
fix($browser): do not decode cookies that do not appear encoded
Closes #9211 Closes #9225
1 parent 2691668 commit 9c99590

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/ng/browser.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,14 @@ function Browser(window, document, $log, $sniffer) {
297297
var lastCookieString = '';
298298
var cookiePath = self.baseHref();
299299

300+
function safeDecodeURIComponent(str) {
301+
try {
302+
return decodeURIComponent(str);
303+
} catch (e) {
304+
return str;
305+
}
306+
}
307+
300308
/**
301309
* @name $browser#cookies
302310
*
@@ -350,12 +358,12 @@ function Browser(window, document, $log, $sniffer) {
350358
cookie = cookieArray[i];
351359
index = cookie.indexOf('=');
352360
if (index > 0) { //ignore nameless cookies
353-
name = decodeURIComponent(cookie.substring(0, index));
361+
name = safeDecodeURIComponent(cookie.substring(0, index));
354362
// the first value that is seen for a cookie is the most
355363
// specific one. values for the same cookie name that
356364
// follow are for less specific paths.
357365
if (lastCookies[name] === undefined) {
358-
lastCookies[name] = decodeURIComponent(cookie.substring(index + 1));
366+
lastCookies[name] = safeDecodeURIComponent(cookie.substring(index + 1));
359367
}
360368
}
361369
}

test/ng/browserSpecs.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ describe('browser', function() {
283283
expect(oldVal).not.toBeDefined();
284284
});
285285

286-
it('should escape both name and value', function() {
286+
it('should encode both name and value', function() {
287287
browser.cookies('cookie1=', 'val;ue');
288288
browser.cookies('cookie2=bar;baz', 'val=ue');
289289

@@ -359,7 +359,7 @@ describe('browser', function() {
359359
expect(browser.cookies()['foo']).toBe('"first"');
360360
});
361361

362-
it ('should unescape cookie values that were escaped by puts', function() {
362+
it ('should decode cookie values that were encoded by puts', function() {
363363
document.cookie = "cookie2%3Dbar%3Bbaz=val%3Due;path=/";
364364
expect(browser.cookies()['cookie2=bar;baz']).toEqual('val=ue');
365365
});
@@ -371,10 +371,16 @@ describe('browser', function() {
371371
expect(browser.cookies()['cookie name']).not.toBeDefined();
372372
});
373373

374-
it('should unscape special characters in cookie values', function() {
374+
it('should decode special characters in cookie values', function() {
375375
document.cookie = 'cookie_name=cookie_value_%E2%82%AC';
376376
expect(browser.cookies()['cookie_name']).toEqual('cookie_value_€');
377377
});
378+
379+
it('should not decode cookie values that do not appear to be encoded', function() {
380+
// see #9211 - sometimes cookies contain a value that causes decodeURIComponent to throw
381+
document.cookie = 'cookie_name=cookie_value_%XX';
382+
expect(browser.cookies()['cookie_name']).toEqual('cookie_value_%XX');
383+
});
378384
});
379385

380386

0 commit comments

Comments
 (0)