Skip to content

Commit 3c8b926

Browse files
committed
feat(ngCookie): support SameSite option
Closes angular#16543 Closes angular#16544
1 parent 627180f commit 3c8b926

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

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}` - it will disable third-party usage for a specific cookie.
42+
* there are two possible values `lax` and `strict`. this attribute is still experimental and
43+
* is not supported by all browsers so it is better to use CSRF tokens to prevent CSRF attack.
44+
*
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)