Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Next 9.5.4] [ERR_INVALID_CHAR]: Invalid character in header content ["Location"] #17907

Closed
yk1979 opened this issue Oct 15, 2020 · 7 comments · Fixed by #18149
Closed

[Next 9.5.4] [ERR_INVALID_CHAR]: Invalid character in header content ["Location"] #17907

yk1979 opened this issue Oct 15, 2020 · 7 comments · Fixed by #18149
Assignees

Comments

@yk1979
Copy link

yk1979 commented Oct 15, 2020

Bug report

Describe the bug

internal server error occurs when redirect path includes query strings of non-ascii character(like Japanese).

in this PR, non-ascii character is passed to 'Location' header without encoding.
https://github.com/vercel/next.js/pull/17323/files

To Reproduce

  1. Set next.config.js with trailing slash config true.
<!-- next.config.js -->
module.exports = {
  trailingSlash: true
};
  1. Open link with query strings of non-ascii character
<!-- indes.jsx -->
export default function IndexPage() {
  return (
    <div>
      <a href="/about?tag=日本語">
        with query strings
      </a>
    </div>
  );
}
  1. Redirected to /about?tag=日本語 page, not /about/?tag=日本語. and 500 error occurs.
TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Location"]
    at ServerResponse.setHeader (_http_outgoing.js:488:3)
    at Object.fn (/node_modules/next/next-server/server/next-server.ts:569:15)
    at Router.execute (/node_modules/next/next-server/server/router.ts:200:40)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at DevServer.run (/node_modules/next/next-server/server/next-server.ts:855:23)

here is a demo.
https://codesandbox.io/s/non-ascii-query-error-c8esl

Expected behavior

redirected to /about/?tag=日本語, with trailing slash.
otherwise, encode japanese character.

@ka2jun8
Copy link
Contributor

ka2jun8 commented Oct 26, 2020

It seems to be fixed by this PR #18149 .
I checked my forked code sandbox using next.js canary release version (v9.5.6-canary.13).
It can be running without error.

https://codesandbox.io/s/non-ascii-query-error-forked-6p2ho

@PepijnSenders
Copy link
Contributor

Can confirm that this was fixed in the v10 release of next.js as well.

@ijjk
Copy link
Member

ijjk commented Nov 6, 2020

Closing as this should be resolved now per the above comments

@ijjk ijjk closed this as completed Nov 6, 2020
@ijjk ijjk added the kind: bug label Nov 6, 2020
@ijjk ijjk self-assigned this Nov 6, 2020
@ijjk ijjk added the point: 1 label Nov 6, 2020
@eduludi
Copy link

eduludi commented Apr 22, 2021

Looks like this problem persists somehow (or is related).

We are getting 500 errors when requests to our site include a slash before the url query, and due an invalid character in it node-server throws a 500 error:

TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Location"]

Here is a codesandbox to reproduce this issue:
https://codesandbox.io/s/nextjs-bug-errinvalidchar-invalid-character-in-header-content-location-ukhrh

Should I open a new issue? Thanks!

@hinsxd
Copy link

hinsxd commented Jun 7, 2021

We encountered this problem when trying to redirect to a url with non-ascii characters

Our solution: wrap the non-ascii characters with encodeURIComponent. Done.

@omar-dulaimi
Copy link

The error occurs in these lines:

const updatedDestination=(0,_url.format)(parsedDestination);
res.setHeader('Location',updatedDestination);
res.statusCode=(0,_loadCustomRoutes.getRedirectStatus)(redirectRoute);// Since IE11 doesn't support the 308 header add backwards
// compatibility using refresh header
if(res.statusCode===308){
   res.setHeader('Refresh',`0;url=${updatedDestination}`);
}

if you wrap updatedDestination with encodeURI then the problem gets solved.

I'm at v9.5.2 so I guess it was solved at later versions.

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants