fix(server/authn): Re-order auth flow to prevent side effects on handleCookieAuth#3923
Conversation
|
@gregfurman is attempting to deploy a commit to the Hatchet Team on Vercel. A member of the Team first needs to authorize it. |
|
❓ Should we be including a migration to clean-up orphaned session entries (i.e those with |
|
|
||
| if _, err := c.Cookie(store.GetName()); err != nil { | ||
| return forbidden | ||
| } |
There was a problem hiding this comment.
Are we sure this is correct? There are some endpoints where we would like to call Set-Cookie, for example when beginning an oauth flow, we need to store the oauth state parameter.
It's quite possible we don't hit this case on any such endpoints, in which case I wonder why we call SaveUnauthenticated later on (which we do a few lines later). Though it's possible that the error block is there so that we can invalidate a cookie which can no longer be decrypted.
There was a problem hiding this comment.
Are we sure this is correct?
Not entirely 🫠 Since the bug here is that we shouldn't be creating a new UserSession entry when there is no session to be persisted -- which will always occur regardless of the auth type since we assume cookie auth whose fallback has the aforementioned side-effect of a new row being added.
I could rejig the logic here to instead start off assuming bearer auth (which has no side-effects) i.e from cookie -> bearer -> custom to bearer -> custom -> cookie
It's quite possible we don't hit this case on any such endpoints, in which case I wonder why we call SaveUnauthenticated later on (which we do a few lines later).
At a glance, I couldn't really figure out how to induce a fallback to SaveUnauthenticated which makes me think this handler logic deserves a revisit.
For this PR, let me not make any changes to the individual auth handler logic and instead try the aforementioned handler re-ordering. Wdyt?
I think a cron job to clean up those entries is not unreasonable, though note that not every session which has
|
0172fe1 to
5920247
Compare
Ah yes. I omitted the hatchet/pkg/auth/cookie/sessionstore.go Lines 218 to 226 in 26a44c2 Granted, there are probably some edge-cases here like a user never logging back in after their session expired which would probably just leave the expired session in the table. Since there isn't really a way of determining whether a session entry was incorrectly included on a token-authenticated request, perhaps a cron makes sense. Do we have a preference on whether to manage cleanups in the Go code or would a Also, I re-worked the logic here based on #3923 (comment) -- lmk if we're happy with the approach! |
Definitely would prefer to manage this in Go code, we don't want to depend on postgres extensions if we can avoid them. I think a simple |
Description
Currently, any HTTP request to a route that accepts cookie-based auth creates a new entry in the
UserSessiontable, even when the request is ultimately authenticated via a different strategy (e.g. bearer auth usingHATCHET_CLIENT_API_TOKEN).This happens because
handleCookieAuthsaves an unauthenticated session as a side effect before falling through to the next auth strategy in the chain. For programmatic clients making many bearer-auth requests, this produces an unauthenticatedUserSessionrow per request that is never referenced again and never cleaned up.Hence, we should only create new
UserSessionentries when the request is actually attempting cookie-based auth.Partial fix for #3913. See original issue for reproduction steps.
Type of change
What's Changed
cookieAuthoccurs last.