Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SameSite attribute support #269

Merged
merged 4 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ The `cookies` service has methods for reading and writing cookies:
URL-decoding the value).
* `write(name, value, options = {})`: writes a cookie with the given name and
value; options can be used to set `domain`, `expires` (Date), `maxAge` (time
in seconds), `path`, `secure`, and `raw` (boolean, disables URL-encoding the
value).
in seconds), `path`, `secure`, `raw` (boolean, disables URL-encoding the
value) and `sameSite` (can be either `'strict'` or `'lax'`).
* `clear(name, options = {})`: clears the cookie so that future reads do not
return a value; options can be used to specify `domain`, `path` or `secure`.
* `exists(name)`: checks whether a cookie exists at all (even with a falsy
Expand Down
3 changes: 3 additions & 0 deletions addon/utils/serialize-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export const serializeCookie = (name, value, options = {}) => {
if (!isEmpty(options.path)) {
cookie = `${cookie}; path=${options.path}`;
}
if (!isEmpty(options.sameSite)) {
cookie = `${cookie}; SameSite=${options.sameSite}`;
}

return cookie;
};
19 changes: 19 additions & 0 deletions tests/unit/services/cookies-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,16 @@ describe('CookiesService', function() {
this.subject().write(COOKIE_NAME, 'test', { httpOnly: true });
}).to.throw();
});

it('sets the sameSite flag', function() {
defineProperty(this.fakeDocument, 'cookie', {
set(value) {
expect(value).to.include('; SameSite=Strict');
}
});

this.subject().write(COOKIE_NAME, 'test', { sameSite: 'Strict' });
});
});

describe('clearing a cookie', function() {
Expand Down Expand Up @@ -723,6 +733,15 @@ describe('CookiesService', function() {
this.subject().write(COOKIE_NAME, 'test', { httpOnly: true });
}).to.not.throw();
});

it('sets the sameSite flag', function() {
this.fakeFastBoot.response.headers.append = function(headerName, headerValue) {
expect(headerName).to.equal('set-cookie');
expect(headerValue).to.equal(`${COOKIE_NAME}=test; SameSite=Strict`);
};

this.subject().write(COOKIE_NAME, 'test', { sameSite: 'Strict' });
});
});

describe('clearing a cookie', function() {
Expand Down