-
-
Notifications
You must be signed in to change notification settings - Fork 168
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
getServerSession mutates event argument #523
Comments
I have a similar problem caused by this behavior. I have a Nitro middleware that calls So, calling The problem is caused by modifying the response headers. Even calling Examples:With no calls to
|
I did a small research and discovered where this header is set. This line createas a session response. This line uses the response data to update the I don't know the internals of
In the first case, modifying the In the second case, the Looks like a flaw of the
|
Hi @xak2000, thanks for your investigation, it genuinely helped me a lot! I linked a Pull Request above which fixes the header and cookie duplication when calling For a context: I was considering not modifying the event, however this is too much effort to properly test for me. |
Hello @phoenix-ru, thank you for you response! However, I reviewed your commits and I think that maybe you don't fully understand the problem. The main problem is not headers/cookies duplication, but the fact that the Example: Let's say the client calls import { getServerSession } from '#auth'
export default defineEventHandler(async (event) => {
setResponseHeader(event, 'Content-Type', 'text/plain')
console.log('On the server headers', event.node.res.getHeaders())
await getServerSession(event)
console.log('On the server headers', event.node.res.getHeaders())
return 'Hello, World!'
}) The client could look like this: const { data } = await useFetch('/api/text', {
onResponse(context) {
console.log(`On the client content-type: ${context.response.headers.get('content-type')}`)
},
})
console.log(`On the client response: ${data.value}`) In this situation, even after de-duplication, the An example of console output from the example above (when the client-side is running on SSR):
Also, as the So, I consider your implementation as a workaround, that is better than nothing, as it will still help to overcome the consequences of this |
Without digging into this too much: What about cloning and restoring headers and cookies before / after this line? If not mutating the event isn't easily accomplished, this might work around the entire problem rather than parts of it. edit: Don't get me wrong, thanks for working on this @phoenix-ru |
@j2L4e Interesting idea, actually! I think both headers/cookies are just JS arrays? In this case cloning/restoring them should not cause any troubles. And, of course, I agree, @phoenix-ru, thank you for your work! |
Hey everyone! |
Hi @warflash , I am currently busy on #673. Seeing as it has high potential to resolve some of the other issues, I would do an investigation after that. In the meantime, it would be great if you could provide a reproduction to what you're experiencing, as reproducing these edge-cases takes a noticeable effort from our side which could've been dedicated to fixing instead. upd: I have found a way to fix it during the authjs migration. Not sure if we should back-port it or simply release after the migration is complete |
Hey @phoenix-ru, first off, #673 sounds fantastic, so thanks for that!
I'd highly vote in favor of a backport, given that #673 will be a new major version with breaking changes. If for whatever reason people can not update to ^1.0.0 right away (or potentially never) they'll be stuck with a security issue later down the road. Thanks! |
Giving an update - me and @zoey-kaiser decided to postpone #673 due to some stability issues with |
@warflash I am getting To verify the fix, I tested headers on both server handle and client using method from @xak2000 : // Client
const { data: statusData } = await useFetch('/api/status', {
onResponse (context) {
console.log(`On the client content-type: ${context.response.headers.get('content-type')}`)
}
})
// Server
const session = await getServerSession(event)
console.log('On the server content-type:', event.node.res.getHeader('content-type')) And in both places content type is Thank you very much for doing investigations and providing a good reproduction |
@phoenix-ru Nice! Just did a very quick test on the feature branch and it indeed looks like the api response sent by the server has the proper headers now and crucially also no longer includes the Set-Cookies header 🚀 Slightly unrelated, but just noticed while investigating this issue: |
Being fair, |
Hmm thinking more about it - it sort of needs to return the session as otherwise there's no way to do anything auth related on the server at all :/ If possible |
Environment
Reproduction
Call getServerSession() multiple times from within any api route handler
Describe the bug
Calling
getServerSession
internally calls the auth handler. This has side-effects because cookies and headers are set.In my case, having multiple
Content-Type
headers set, caused errors in client-side content type detection.$fetch
wouldn't automatically parseapplication/json
responses correctly, but return a blob instead.Some component of the nuxt dev setup seems to be deduping or resetting
Content-Type
headers, effectively mitigating this problem in development. an observable error only occurs in production builds.The cause of this can be observed in dev mode, though.
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: