Skip to content

fix: throw TypeError for invalid arguments in res.redirect()#7049

Closed
erdinccurebal wants to merge 1 commit intoexpressjs:masterfrom
erdinccurebal:fix/redirect-invalid-url
Closed

fix: throw TypeError for invalid arguments in res.redirect()#7049
erdinccurebal wants to merge 1 commit intoexpressjs:masterfrom
erdinccurebal:fix/redirect-invalid-url

Conversation

@erdinccurebal
Copy link

Summary

  • res.redirect(undefined) previously sent a malformed HTTP response with Location: undefined header. While a deprecation warning was emitted, the invalid response was still sent to the client.
  • This converts the deprecated behavior into thrown TypeErrors, consistent with how res.status() already validates its input (see response.js#L64-L72).
  • Validates both the url (must be a string) and status (must be a number) arguments.

Before

res.redirect(undefined)
// => 302 with "Location: undefined" header (malformed HTTP response)

res.redirect(null)
// => 302 with "Location: null" header (malformed HTTP response)

After

res.redirect(undefined)
// => TypeError: res.redirect() requires a URL string, got undefined

res.redirect(null)
// => TypeError: res.redirect() requires a URL string, got object

Test plan

  • Added test: res.redirect(undefined) throws (500)
  • Added test: res.redirect(null) throws (500)
  • Added test: res.redirect(123) throws (500)
  • Added test: res.redirect(301, undefined) throws (500)
  • Added test: res.redirect('301', '/foo') throws (500)
  • All 1249 existing tests pass
  • 5 new tests pass (18 total in res.redirect suite)

Fixes #6941

Previously, res.redirect() with non-string url (e.g. undefined, null)
would only emit a deprecation warning but still send a malformed HTTP
response with an invalid Location header (e.g. "Location: undefined").
This converts the deprecated behavior into thrown TypeErrors, consistent
with how res.status() validates its input. This prevents sending
invalid HTTP responses and gives developers clear, actionable errors.

Fixes expressjs#6941
@bjohansebas bjohansebas reopened this Feb 21, 2026
@bjohansebas
Copy link
Member

duplicated of #6404

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

res.redirect(undefined) sends invalid Location: undefined header

2 participants