🍪 Cookie API: Async Function to Rotate Secrets #9933
SpencerDuball
started this conversation in
Proposals
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This proposal is to update the Cookie API to accept
secrets: string[] | (() => Promise<string[]>)
as an option instead of onlysecrets: string[]
. This would allow for programmatically retrieving the secrets used for signing and signature verification.Problem
In my app, I would like to automatically rotate the secrets that sessions are signed with. I can rotate these secrets without issue, but to my knowledge there is no way to update the secrets that the cookie factory uses to sign/verify cookies. In the example below from the Singing cookies documentation we can see that the cookie factory is imported and used as a variable:
The secrets are statically passed upon the initialization of the cookie factory, however even if using an environment variable these strings would be passed by value into the
secrets
array, ex:secrets: [ENV.SECRET_1, ENV.SECRET_2]
.Proposal
This proposal is to update the Cookie API to also allow accepting an async function for the
secrets
option. This would allow users to supply any logic to retrieve the secrets they wish. This should have pretty minimal impact to the Cookie API as well, we already use async methods to.parse
and.serialize
(which use the secrets). There is one more method that accesses thesecrets
parameter,get isSigned()
that might need a breaking change or could just assume the cookie is signed if a custom secrets function is passed. If we assume isSigned when a function is passed it would be good to raise an error if thesecrets
function did not return a secret to sign the cookie with.Here is a sample of the majority of the API changes, reference the current implementation to compare/constrast. Search for
// <-- CHANGE HERE
to see the changes, there are only 4 and they are relatively minimal.Workarounds
createCookieFactory
to potentially supply custom functions for thesign
andunsign
. This could technically be done, but you would need to pass in dummy values for thesecrets
when creating a cookie factory. You would also be discarding the passed secret to thesign
andunsign
functions when doing so, see the node example here for reference: remix-node/implementations.ts.Beta Was this translation helpful? Give feedback.
All reactions