nitro icon indicating copy to clipboard operation
nitro copied to clipboard

getRouterParams(event) is always empty in nitro middlewares

Open cosbgn opened this issue 1 year ago • 7 comments

Environment

nuxt@latest

Reproduction

https://stackblitz.com/edit/github-fxp3q3?file=middleware%2Fexample.js

Describe the bug

const params = getRouterParams(event) is always empty in nitro middlewares

Additional context

No response

Logs

No response

cosbgn avatar Feb 05 '24 13:02 cosbgn

To me, this behavior makes sense as the middleware runs before the route handlers, meaning that the middleware might not have any knowledge about possible router params.

TheAlexLichter avatar Feb 05 '24 13:02 TheAlexLichter

@manniL I'm not sure, in theory I agree, but we do have access to the URL, even the nuxt example is:

// Example nuxt middleware from docs

export default defineEventHandler((event) => {
  console.log('New request: ' + getRequestURL(event))
})

I have a lot of use cases where if I have [_book_id] in the URL I want to make sure the user has access to the the book, the middleware would be a perfect place to do this, so I can be sure any route which has [_book_id] is automatically validated.

If you do have access to the URL, you should have also access to the params, no?

cosbgn avatar Feb 05 '24 13:02 cosbgn

Access to the URL makes totally sense, but as middleware is triggered before any router matching logic happens (AFAIK), the event is not aware of any route parameters yet (when using global middleware)

TheAlexLichter avatar Feb 05 '24 15:02 TheAlexLichter

So would there be a problem if the middleware was given knowledge of the URL that made it to run, and of course the previous one?

Access to the URL makes totally sense, but as middleware is triggered before any router matching logic happens (AFAIK), the event is not aware of any route parameters yet (when using global middleware)

chibx avatar Feb 05 '24 21:02 chibx

The middleware already knows the URL of the request :)

TheAlexLichter avatar Feb 05 '24 22:02 TheAlexLichter

@manniL Should this be resolved, and router params are able to be read from the event parameter in nitro middlewares, would it impose any security vulnerability?

chibx avatar Feb 06 '24 07:02 chibx

Any updates?

samenick avatar Oct 10 '24 15:10 samenick