amannn / next-intl

🌐 Internationalization (i18n) for Next.js
https://next-intl-docs.vercel.app
MIT License
2.58k stars 236 forks source link

Malformed URLs crash the entire app with a 500 Error #1351

Closed cmaerz closed 1 month ago

cmaerz commented 1 month ago

Description

When I use a malformed URL like /soga/ibiza% the app crashed with a

 Error [URIError]: URI malformed
    at decodeURI (<anonymous>)
    at middleware (webpack-internal:///(middleware)/./node_modules/next-intl/dist/development/middleware/middleware.js:24:36)
    at Object.middleware$1 (webpack-internal:///(middleware)/./src/middleware.ts:66:12)

I worked it around with the following portion in middleware.ts

try {
    decodeURI(req.nextUrl.pathname);
  } catch (e) {
    return new Response(new Blob(), { status: 404 });
  }

It also reproducible on the demo Repos :D

Verifications

Mandatory reproduction URL

https://next-intl-bug-repro-app-router.vercel.app/en/asdafa%

Reproduction description

Steps to reproduce:

  1. Enter a Url Like /en/ibiza%
  2. See the Error

Expected behaviour

Returns a 404, like NextJS normally does.

amannn commented 1 month ago

Thanks for the report! After some local testing I found that Next.js might also return a 400 status code, maybe depending on if you have a segment like /[locale] where it tries to extract a segment from the pathname.

I've set up https://github.com/amannn/next-intl/pull/1353/ where the invalid request is simply forwarded, letting Next.js handle it. As far as I can tell, in invalid pathname never reaches the app, but is caught by the error handling of Next.js at some level.

Does that sound reasonable to you?

cmaerz commented 1 month ago

Sounds good! Thanks!

Schöne Grüße in die Berge :)

amannn commented 1 month ago

Will be out in a minute, schöne Grüße retour! 😄