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

Allow setting httpOnly #194

Merged
merged 1 commit into from
Oct 22, 2018
Merged

Allow setting httpOnly #194

merged 1 commit into from
Oct 22, 2018

Conversation

vincent99
Copy link
Contributor

There is currently no support for setting httpOnly on cookies in write(), with an explicit assert saying it's not useful.

While what it says is true and you won't be able to read them from inside Ember, there are still useful reasons you might want to set a httpOnly cookie, e.g. if it is generated by the client but only needs to be readable by a backend service/API.

(If you prefer to keep the assert, it could instead be an option like yesReallyHttpOnly passed into write() that causes it to allow writing one, I just need some way to do it)

Copy link
Member

@marcoow marcoow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point actually. Is it even possible to set a HTTP-only cookie in the browser via document.cookie though?

Also, it might make sense to replace the assertion with a debug log.


if ( !replaced ) {
responseHeaders.append('set-cookie', serializedCookie);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what this change does. Is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't really intend to put this in the same PR because I haven't added tests for it yet. So I can pull it out into a separate PR if you like.

But basically every time something writes a cookie right now it gets added to the header array, even if you've previously written a cookie of the same name. ember-simple-auth in particular does this several times during a fastboot request, which results in sending a bunch of redundant info to the client.

So if you do

cookies.write('key', 'value1');
cookies.write('key', 'value2');

The response headers are sent as

Set-Cookie: key=value1
Set-Cookie: key=value2

This change updates/replaces the existing header entry if there's already one for the name instead of always appending a new entry.

@vincent99
Copy link
Contributor Author

It does appear that you can't set httpOnly in a browser, which makes sense. We're using this inside fastboot (copying the cookies from a login fetch() response back to the fastboot response so that the client gets them). So I could lave the assert but wrap it in a check for !isFastBoot?

@vincent99 vincent99 force-pushed the master branch 2 times, most recently from 874592b to 45354e8 Compare September 24, 2018 18:24
@vincent99
Copy link
Contributor Author

Updated to allow httpOnly only in fastboot, show the assert in a browser, test both sides appropriately, and pull out the redundant header stuff into a separate PR.

Copy link
Member

@marcoow marcoow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍 I'd just restructure the tests a bit for simplicity.

expect(() => {
this.subject().write(COOKIE_NAME, 'test', { httpOnly: true });
}).to.throw();
it('throws when the HTTP only option is set in a browser', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's dedicated describes for both FastBoot and the browser actually: here and here. Can this be split up and moved into the respective describes?

@vincent99
Copy link
Contributor Author

I was just trying to minimize the change keeping the options checking in itValidatesWriteOptions(), but sure, moved to 2 separate tests in "in the browser" and "in the FastBoot server".

@marcoow marcoow merged commit af4df61 into mainmatter:master Oct 22, 2018
@marcoow
Copy link
Member

marcoow commented Oct 22, 2018

thanks 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants