Skip to content

fix: throw TypeError for invalid url in res.redirect#6957

Closed
MirrorDNA-Reflection-Protocol wants to merge 1 commit intoexpressjs:masterfrom
MirrorDNA-Reflection-Protocol:fix/redirect-undefined-6941
Closed

fix: throw TypeError for invalid url in res.redirect#6957
MirrorDNA-Reflection-Protocol wants to merge 1 commit intoexpressjs:masterfrom
MirrorDNA-Reflection-Protocol:fix/redirect-undefined-6941

Conversation

@MirrorDNA-Reflection-Protocol

Summary

Fixes #6941res.redirect(undefined) was sending a malformed Location: undefined header instead of throwing an error.

Changes

When res.redirect is called with:

  • undefined, null, or empty URL
  • Non-string URL
  • Non-number status

It now throws a TypeError instead of sending a malformed HTTP response.

Before:

HTTP/1.1 302 Found
Location: undefined

After:

TypeError: url argument is required to res.redirect

The deprecation warnings are preserved for backwards compatibility tracking, but the response is no longer sent.

Testing

This change may require updating existing tests that expect deprecated behavior. The fix ensures HTTP responses are always valid per spec.

When res.redirect is called with undefined/null or non-string URL,
it now throws a TypeError instead of sending a malformed Location
header with 'undefined' as the value.

The deprecation warnings are preserved for backwards compatibility
tracking, but the function now throws immediately after the warning
to prevent the malformed HTTP response.

Fixes expressjs#6941
Copy link
Contributor

@krzysdz krzysdz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate of #6404.

Other than this, here are a couple comments for you:

  • Avoid making unrelated formatting changes, as this makes PRs harder to review.
  • #6404 has not landed yet, because there is no branch for 6.x and this is a breaking change.
  • It doesn't make sense to print a deprecation notice and throw an error at the same time. The deprecation is there to signal that the behaviour will change in a future major version.
  • Such changes should be covered by tests.

@MirrorDNA-Reflection-Protocol MirrorDNA-Reflection-Protocol deleted the fix/redirect-undefined-6941 branch December 27, 2025 04:44
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

3 participants