Skip to content

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

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,18 @@ res.redirect = function redirect(url) {
address = arguments[1]
}

if (!address) {
throw new TypeError('url argument is required to res.redirect');
}

if (typeof address !== 'string') {
throw new TypeError('res.redirect: url must be a string');
}

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

// Set location header
address = this.location(address).get('Location');

Expand Down
36 changes: 36 additions & 0 deletions test/res.redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,42 @@ describe('res', function(){
.expect(302, done)
})

it('should throw an error if the url is missing', function(done){
var app = express();

app.use(function (req, res) {
res.redirect(undefined)
})

request(app)
.get('/')
.expect(500, /url argument is required to res.redirect/, done)
})

it('should throw an error if the url is not a string', function(done){
var app = express();

app.use(function (req, res) {
res.redirect(['http://google.com'])
})

request(app)
.get('/')
.expect(500, /res.redirect: url must be a string/, done)
})

it('should throw an error if the status is not a number', function(done){
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.

})

request(app)
.get('/')
.expect(500, /res.redirect: status must be a number/, done)
})

it('should encode "url"', function (done) {
var app = express()

Expand Down