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

Commit 164d546

Browse files
committed
feat(ngCookies): support sameSite option
1 parent 627180f commit 164d546

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/ngCookies/cookieWriter.js

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
function $$CookieWriter($document, $log, $browser) {
1515
var cookiePath = $browser.baseHref();
1616
var rawDocument = $document[0];
17+
var sameSiteValidValues = ['lax', 'strict'];
18+
19+
function isSameSiteValueValid(val) {
20+
return sameSiteValidValues.indexOf(val) >= 0;
21+
}
1722

1823
function buildCookieString(name, value, options) {
1924
var path, expires;
@@ -33,6 +38,7 @@ function $$CookieWriter($document, $log, $browser) {
3338
str += options.domain ? ';domain=' + options.domain : '';
3439
str += expires ? ';expires=' + expires.toUTCString() : '';
3540
str += options.secure ? ';secure' : '';
41+
str += options.sameSite && isSameSiteValueValid(options.sameSite) ? ';SameSite=' + options.sameSite : '';
3642

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

test/ngCookies/cookieWriterSpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,21 @@ 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+
194+
it('should not accept sameSite option when value is invalid', function() {
195+
$$cookieWriter('name', 'value', {sameSite: 'test'});
196+
expect(getLastCookieAssignment('sameSite')).toBeUndefined();
197+
});
198+
184199
it('should accept expires option on set', function() {
185200
$$cookieWriter('name', 'value', {expires: 'Fri, 19 Dec 2014 00:00:00 GMT'});
186201
expect(getLastCookieAssignment('expires')).toMatch(/^Fri, 19 Dec 2014 00:00:00 (UTC|GMT)$/);

0 commit comments

Comments
 (0)