Skip to content

feat: @fastify/cookie responsible for the signing and unsigning#125

Closed
Uzlopak wants to merge 19 commits into
fastify:masterfrom
Uzlopak:integrate-cookie
Closed

feat: @fastify/cookie responsible for the signing and unsigning#125
Uzlopak wants to merge 19 commits into
fastify:masterfrom
Uzlopak:integrate-cookie

Conversation

@Uzlopak
Copy link
Copy Markdown
Contributor

@Uzlopak Uzlopak commented Aug 15, 2022

This PR couples @fastify/cookie stronger to @fastify/session. So instead of duplicating signing and unsigning logic in @fastify/session, we expect that setting the signing related stuff in @fastify/cookie and we just consume the logic in @fastify/session.

@rclmenezes
I think before we merge other patches, we should first merge this.

To review this PR i think, it is easier by going through the commits. I could not break it down into much smaller PRs as it is basically one big change.

Checklist

Comment thread test/session.test.js
@Uzlopak Uzlopak requested review from Fdawgs and darkgl0w August 15, 2022 14:23
@Uzlopak Uzlopak linked an issue Aug 15, 2022 that may be closed by this pull request
2 tasks
@Uzlopak Uzlopak changed the title @fastify/cookie responsible for the signing and unsigning feat: @fastify/cookie responsible for the signing and unsigning Aug 15, 2022
rclmenezes and others added 4 commits August 15, 2022 21:14
…-session (#113)

* Add cookiePrefix as an option to allow for compatibility with express-session

* Add docs

* Fix lint

* improve solution

* Apply suggestions from code review

Co-authored-by: uzlopak <aras.abbasi@googlemail.com>
Copy link
Copy Markdown
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

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

By comparing express-session, the secret option should stay in here.

Comment thread package.json Outdated
"license": "MIT",
"dependencies": {
"cookie-signature": "^1.1.0",
"@fastify/cookie": "^7.4.0",
Copy link
Copy Markdown
Member

@climba03003 climba03003 Aug 16, 2022

Choose a reason for hiding this comment

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

I would move it back to devDependency.

The user should explicitly install and register @fastify/cookie in their application.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can fastify-plugin ensure the version of @fastify/cookie is at least 7.4? or is that out of scope for that module, and it might fail at runtime when accessing non-existent methods?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can require for a decorator to exist: https://github.com/fastify/fastify-plugin#dependencies

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, right! should add that 👍

@Uzlopak
Copy link
Copy Markdown
Contributor Author

Uzlopak commented Aug 16, 2022

I think my approach is much cleaner, as we just need to setup fastify-cookie correct and then can use the actual methods instead of taking also care of the secrect management.

@climba03003
Copy link
Copy Markdown
Member

climba03003 commented Aug 16, 2022

I think my approach is much cleaner, as we just need to setup fastify-cookie correct and then can use the actual methods instead of taking also care of the secrect management.

It require the user to be clear that they shouldn't forget the secret in registering @fastify-cookie.
There is no way to block them and remind them to add one before application startup.

It is totally different than the way before and may expose user in vulnerability.

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Here is a few questions:

  1. What happens if there is no secret set to both @fastify/cookie and @fastify/session? Let's make sure an error is thrown at some point.

  2. Make sure to throw if secret is passed to @fastify/session.

@Uzlopak
Copy link
Copy Markdown
Contributor Author

Uzlopak commented Aug 16, 2022

@mcollina
Created fastify/fastify-cookie#203

So when no secret is set, we can feature detect if the decorators exist. If they dont we can throw a secret not set error. Obviously I can only write corresponding unit tests if the PR is merged and a new release of @fastify/cookie was published.

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina
Copy link
Copy Markdown
Member

@Uzlopak please wait for @climba03003 and @SimenB

@Uzlopak
Copy link
Copy Markdown
Contributor Author

Uzlopak commented Aug 16, 2022

will do

@Uzlopak
Copy link
Copy Markdown
Contributor Author

Uzlopak commented Aug 16, 2022

@mcollina

I still have to add that the plugin throws an error if a secret was provided.

@climba03003
Copy link
Copy Markdown
Member

climba03003 commented Aug 16, 2022

It does not convince me to provide secret in @fastify/cookie.

  1. It leaks of length check, user can use weak secret which can't currently.
  2. User can not switch from express-session to @fastify/session with just config copy and paste.

Point 2. is not as important as 1..
If it the concern is code de-duplication, why can't we use the signer factory from @fastify/cookie?

Comment thread lib/session.js Outdated
Comment thread package.json Outdated
Uzlopak and others added 2 commits August 16, 2022 14:43
Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
@rclmenezes
Copy link
Copy Markdown
Contributor

rclmenezes commented Aug 16, 2022

My last few patches have been all about making @fastify/session compatible with express-session.

This PR creates a new incompatibility: what if someone provides a different secret to both express-cookie and express-session? They probably shouldn't, but the effect in express would be that non-session-related cookies have one secret and sessions have another.

But honestly, I can't think of a real use case to have differing secrets here. And I think good code / practices is more important than express compatibility.

I suspect we should tell users in this scenario that they should migrate to one secret. They can do so by setting Express's cookieParser to: cookieParser([sessionSecret, oldSecret])

@Uzlopak
Copy link
Copy Markdown
Contributor Author

Uzlopak commented Aug 16, 2022

Dont worry. Working on a different branch.

@Uzlopak
Copy link
Copy Markdown
Contributor Author

Uzlopak commented Aug 17, 2022

Closing in favor of #129

@Uzlopak Uzlopak closed this Aug 17, 2022
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.

Custom sign/unsign

5 participants