Skip to content

Commit 8a44fe6

Browse files
committed
Fix redirect error when path contains raw non-URL characters
1 parent 6428fb4 commit 8a44fe6

File tree

6 files changed

+17
-1
lines changed

6 files changed

+17
-1
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* Fix redirect error when `path` contains raw non-URL characters
45
* Fix redirect when `path` starts with multiple forward slashes
56

67
0.14.0 / 2016-06-06

index.html

Whitespace-only changes.

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var createError = require('http-errors')
1616
var debug = require('debug')('send')
1717
var deprecate = require('depd')('send')
1818
var destroy = require('destroy')
19+
var encodeUrl = require('encodeurl')
1920
var escapeHtml = require('escape-html')
2021
var etag = require('etag')
2122
var EventEmitter = require('events').EventEmitter
@@ -444,7 +445,7 @@ SendStream.prototype.redirect = function redirect (path) {
444445
return
445446
}
446447

447-
var loc = collapseLeadingSlashes(path + '/')
448+
var loc = encodeUrl(collapseLeadingSlashes(path + '/'))
448449
var msg = 'Redirecting to <a href="' + escapeHtml(loc) + '">' + escapeHtml(loc) + '</a>\n'
449450
var res = this.res
450451

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"debug": "~2.2.0",
1818
"depd": "~1.1.0",
1919
"destroy": "~1.0.4",
20+
"encodeurl": "~1.0.1",
2021
"escape-html": "~1.0.3",
2122
"etag": "~1.7.0",
2223
"fresh": "0.3.0",

test/fixtures/snow ☃/index.html

Whitespace-only changes.

test/send.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,19 @@ describe('send(file).pipe(res)', function () {
349349
.expect('Location', '/pets/')
350350
.expect(301, done)
351351
})
352+
353+
it('should respond with an HTML redirect', function (done) {
354+
var app = http.createServer(function (req, res) {
355+
send(req, req.url.replace('/snow', '/snow ☃'), {root: 'test/fixtures'})
356+
.pipe(res)
357+
})
358+
359+
request(app)
360+
.get('/snow')
361+
.expect('Location', '/snow%20%E2%98%83/')
362+
.expect('Content-Type', /html/)
363+
.expect(301, 'Redirecting to <a href="/snow%20%E2%98%83/">/snow%20%E2%98%83/</a>\n', done)
364+
})
352365
})
353366

354367
describe('when no "error" listeners are present', function () {

0 commit comments

Comments
 (0)