Environment
See repro, Nitro v2.13.4
Reproduction
https://stackblitz.com/edit/github-r2z2zxky?file=nitro.config.ts
nitro.config.ts:
routeRules: {
'/news/**': {
redirect: {
to: '/blog/**',
statusCode: 301,
},
},
},
Navigate to /news/%E2%80%98broken-url%E2%80%99
Describe the bug
When using routeRules to redirect a wildcard path, if a request URL contains URL-encoded special characters (such as %E2%80%98 for ‘), Nitro appears to decode the path segments before constructing the destination URL.
When sendRedirect is then called with this decoded string, Node.js throws an ERR_INVALID_CHAR error (Invalid character in header content ["location"]) because HTTP headers cannot contain unencoded non-ASCII characters.
Additional context
This was originally reported in Nuxt (nuxt/nuxt#35281) but it should be tracked in Nitro since routeRules routing and header handling happen here.
Logs
ERROR [request error] [unhandled] [GET] http://localhost:3000/news/%E2%80%98broken-url%E2%80%99
ℹ Error: Invalid character in header content ["location"]
⁃ at wrappedFn (https://jvmipzvolvgithub-tm3f.w-corp-staticblitz.com/builtins.4a5e7736.js:109:5032)
⁃ at setHeader (https://jvmipzvolvgithub-tm3f.w-corp-staticblitz.com/builtins.4a5e7736.js:6:9450)
⁃ at sendRedirect (node_modules/h3/dist/index.mjs:906:18)
⁃ at <anonymous> (node_modules/nitropack/dist/runtime/internal/route-rules.mjs:52:56)
⁃ at <anonymous> (node_modules/h3/dist/index.mjs:2142:31)
[CAUSE]
TypeError {
fileName: 'https://jvmipzvolvgithub-tm3f.w-corp-staticblitz.com/blitz.bd880c29.js',
lineNumber: 31,
columnNumber: 24835,
stack: 'Invalid character in header content ["location"]\n' +
'at wrappedFn (https://jvmipzvolvgithub-tm3f.w-corp-staticblitz.com/builtins.4a5e7736.js:109:5032)\n' +
'at setHeader (https://jvmipzvolvgithub-tm3f.w-corp-staticblitz.com/builtins.4a5e7736.js:6:9450)\n' +
'at sendRedirect (./node_modules/h3/dist/index.mjs:906:18)\n' +
'at <anonymous> (./node_modules/nitropack/dist/runtime/internal/route-rules.mjs:52:56)\n' +
'at <anonymous> (./node_modules/h3/dist/index.mjs:2142:31)',
code: 'ERR_INVALID_CHAR',
message: 'Invalid character in header content ["location"]',
[Symbol(kInternalError)]: true,
[Symbol(fixed)]: true,
}
Environment
See repro, Nitro v2.13.4
Reproduction
https://stackblitz.com/edit/github-r2z2zxky?file=nitro.config.ts
nitro.config.ts:Navigate to
/news/%E2%80%98broken-url%E2%80%99Describe the bug
When using
routeRulesto redirect a wildcard path, if a request URL contains URL-encoded special characters (such as%E2%80%98for‘), Nitro appears to decode the path segments before constructing the destination URL.When
sendRedirectis then called with this decoded string, Node.js throws anERR_INVALID_CHARerror (Invalid character in header content ["location"]) because HTTP headers cannot contain unencoded non-ASCII characters.Additional context
This was originally reported in Nuxt (nuxt/nuxt#35281) but it should be tracked in Nitro since
routeRulesrouting and header handling happen here.Logs