nitrojs / nitrojs/nitro

Invalid character in header content ["location"]

Open
#4,323 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug v2
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

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 (https://github.com/nuxt/nuxt/issues/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,
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction in nitro.config.ts and the failing request to /news/%E2%80%98broken-url%E2%80%99. Then inspect runtime/internal/route-rules.mjs at the redirect call and the h3 sendRedirect entry point shown in the stack trace. Done means wildcard redirects preserve a valid Location header for URL-encoded special characters without ERR_INVALID_CHAR.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.