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

Commit 56bd612

Browse files
committed
feat(ngCookie): support sameSite option
Closes #16543 Closes #16544
1 parent 627180f commit 56bd612

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

src/ng/cookieReader.js

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ function $$CookieReader($document) {
4343
cookie = cookieArray[i];
4444
index = cookie.indexOf('=');
4545
if (index > 0) { //ignore nameless cookies
46-
name = safeDecodeURIComponent(cookie.substring(0, index));
4746
// the first value that is seen for a cookie is the most
4847
// specific one. values for the same cookie name that
4948
// follow are for less specific paths.

src/ngCookies/cookieWriter.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function $$CookieWriter($document, $log, $browser) {
3333
str += options.domain ? ';domain=' + options.domain : '';
3434
str += expires ? ';expires=' + expires.toUTCString() : '';
3535
str += options.secure ? ';secure' : '';
36+
str += options.sameSite ? ';sameSite=' + options.sameSite : '';
3637

3738
// per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:
3839
// - 300 cookies

src/ngCookies/cookies.js

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ angular.module('ngCookies', ['ng']).
3838
* or a Date object indicating the exact date/time this cookie will expire.
3939
* - **secure** - `{boolean}` - If `true`, then the cookie will only be available through a
4040
* secured connection.
41+
* - **sameSite** - `{string}` - prevents the browser from sending the cookie along with cross-site requests.
42+
* Accepts the values `lax` and `strict`. See the [OWASP Wiki](https://www.owasp.org/index.php/SameSite)
43+
* for more info. Note that as of May 2018, not all browsers support `SameSite`,
44+
* so it cannot be used as a single measure against Cross-Site-Request-Forgery (CSRF) attacks.
4145
*
4246
* Note: By default, the address that appears in your `<base>` tag will be used as the path.
4347
* This is important so that cookies will be visible for all routes when html5mode is enabled.

test/ngCookies/cookieWriterSpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@ describe('cookie options', function() {
181181
expect(getLastCookieAssignment('secure')).toBe(true);
182182
});
183183

184+
it('should accept sameSite option when value is lax', function() {
185+
$$cookieWriter('name', 'value', {sameSite: 'lax'});
186+
expect(getLastCookieAssignment('sameSite')).toBe('lax');
187+
});
188+
189+
it('should accept sameSite option when value is strict', function() {
190+
$$cookieWriter('name', 'value', {sameSite: 'strict'});
191+
expect(getLastCookieAssignment('sameSite')).toBe('strict');
192+
});
193+
184194
it('should accept expires option on set', function() {
185195
$$cookieWriter('name', 'value', {expires: 'Fri, 19 Dec 2014 00:00:00 GMT'});
186196
expect(getLastCookieAssignment('expires')).toMatch(/^Fri, 19 Dec 2014 00:00:00 (UTC|GMT)$/);

0 commit comments

Comments
 (0)