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

Commit 3952d35

Browse files
committed
fix($browser): should use first value for a cookie.
With this change, $browser.cookies()["foo"] will behave like docCookies.getItem("foo") where docCookies is defined at https://developer.mozilla.org/en-US/docs/DOM/document.cookie This fixes the issue where, if there's a value for the XSRF-TOKEN cookie value with the path /, then that value is used for all applications in the domain even if they set path specific values for XSRF-TOKEN. Closes #2635
1 parent bffe6fa commit 3952d35

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/ng/browser.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,13 @@ function Browser(window, document, $log, $sniffer) {
297297
cookie = cookieArray[i];
298298
index = cookie.indexOf('=');
299299
if (index > 0) { //ignore nameless cookies
300-
lastCookies[unescape(cookie.substring(0, index))] = unescape(cookie.substring(index + 1));
300+
var name = unescape(cookie.substring(0, index));
301+
// the first value that is seen for a cookie is the most
302+
// specific one. values for the same cookie name that
303+
// follow are for less specific paths.
304+
if (lastCookies[name] === undefined) {
305+
lastCookies[name] = unescape(cookie.substring(index + 1));
306+
}
301307
}
302308
}
303309
}

test/ng/browserSpecs.js

+7
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ describe('browser', function() {
304304
expect(browser.cookies().foo).toEqual('bar=baz');
305305
});
306306

307+
it('should return the the first value provided for a cookie', function() {
308+
// For a cookie that has different values that differ by path, the
309+
// value for the most specific path appears first. browser.cookies()
310+
// should provide that value for the cookie.
311+
document.cookie = 'foo="first"; foo="second"';
312+
expect(browser.cookies()['foo']).toBe('"first"');
313+
});
307314

308315
it ('should unescape cookie values that were escaped by puts', function() {
309316
document.cookie = "cookie2%3Dbar%3Bbaz=val%3Due;path=/";

0 commit comments

Comments
 (0)