Skip to content

feat(res.redirect): add validation for url and status arguments#6404

Open
bjohansebas wants to merge 2 commits intomasterfrom
throw-redirect
Open

feat(res.redirect): add validation for url and status arguments#6404
bjohansebas wants to merge 2 commits intomasterfrom
throw-redirect

Conversation

@bjohansebas
Copy link
Member

Validations are being added, this would ideally be for Express 6. Maybe sending a deprecation message would be great, what do you think?

closes #6391

@UlisesGascon UlisesGascon added the semver-major This change is a semver major label Mar 18, 2025
Copy link
Member

@UlisesGascon UlisesGascon left a comment

Choose a reason for hiding this comment

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

I think is a good idea, but we will need to introduce the deprecation notice in 5.x before merging this PR 🤔

var app = express();

app.use(function (req, res) {
res.redirect("300", 'http://google.com')
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
res.redirect("300", 'http://google.com')
res.redirect("300", 'https://google.com')

http -> https

Copy link
Member

Choose a reason for hiding this comment

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

Not bad, but also just to be clear it is not necessary in the tests.

Comment on lines 836 to 839
if (typeof status !== 'number') {
deprecate('Status must be a number');
throw new TypeError('res.redirect: status must be a number');
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Status is set using this.status(status), which performs type validation (#4212). Earlier status is used to get a status message for body, but passing something invalid there is not a problem if an error is thrown before it's sent.

Suggested change
if (typeof status !== 'number') {
deprecate('Status must be a number');
throw new TypeError('res.redirect: status must be a number');
}

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

Labels

6.x enhancement semver-major This change is a semver major

Projects

None yet

Development

Successfully merging this pull request may close these issues.

res.redirect(undefined) sends invalid Location: undefined header Throw when the path is undefined in res.redirect.

5 participants