Remove problematic customization of session callback #435
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes the customization on the Authjs session callback.
The customization was suppose to only add
id
to theuser
object of the session callback response (exposed via/api/auth/session
), but when the previous customization, the callback also started returning otherwise secret information - namely the "sessionToken". This is really unexpected and poses a security risk.It is especially a problem because the session token is suppose to only be stored in an
HttpOnly
cookie in the browser and on the server side (database), making it inaccessible to JavaScript. But with the/api/auth/session
endpoint returning the session token it is easily accessible from JavaScript by doing a network request.Reading the session token from JavaScript is problematic because it can be used as part of a session hijacking attack using XSS.
With this fix the session object is explicitly constructed to follow the
DefaultSession
(source) with only theuser.id
added.Motivation and Context
With this seemingly innocent modification to the session callback (which is clearly documented in Authjs documentation)
the
GET /api/auth/session
endpoint starts returning a lot more session information:Without the modification the response looks like this (which is safe and expected):
The root cause is likely that with the modification the callback starts returning
AdapterSession
(source) instead ofDefault Session
(source).With this PR only the
user.id
and property is added:Screenshots (if appropriate):
From the Authjs documentation (https://authjs.dev/guides/extending-the-session#with-database).
Types of changes