Skip to content

Invalid character in header content ["location"] #4323

@matsn0w

Description

@matsn0w

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,
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions