Skip to content

Commit

Permalink
Fix redirect error when req.url contains raw non-URL characters
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed May 10, 2016
1 parent a341f16 commit f281443
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* Fix redirect error when `req.url` contains raw non-URL characters
* deps: send@0.14.1

1.11.0 / 2016-06-07
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @private
*/

var encodeUrl = require('encodeurl')
var escapeHtml = require('escape-html')
var parseUrl = require('parseurl')
var resolve = require('path').resolve
Expand Down Expand Up @@ -172,7 +173,7 @@ function createRedirectDirectoryListener () {
originalUrl.pathname = collapseLeadingSlashes(originalUrl.pathname + '/')

// reformat the URL
var loc = url.format(originalUrl)
var loc = encodeUrl(url.format(originalUrl))
var msg = 'Redirecting to <a href="' + escapeHtml(loc) + '">' + escapeHtml(loc) + '</a>\n'
var res = this.res

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"repository": "expressjs/serve-static",
"dependencies": {
"encodeurl": "~1.0.1",
"escape-html": "~1.0.3",
"parseurl": "~1.3.1",
"send": "0.14.1"
Expand Down
Empty file added test/fixtures/snow ☃/.gitkeep
Empty file.
12 changes: 11 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,9 @@ describe('serveStatic()', function () {
describe('redirect', function () {
var server
before(function () {
server = createServer(fixtures)
server = createServer(fixtures, null, function (req, res) {
req.url = req.url.replace(/\/snow(\/|$)/, '/snow \u2603$1')
})
})

it('should redirect directories', function (done) {
Expand Down Expand Up @@ -466,6 +468,14 @@ describe('serveStatic()', function () {
.expect(301, done)
})

it('should ensure redirect URL is properly encoded', function (done) {
request(server)
.get('/snow')
.expect('Location', '/snow%20%E2%98%83/')
.expect('Content-Type', /html/)
.expect(301, 'Redirecting to <a href="/snow%20%E2%98%83/">/snow%20%E2%98%83/</a>\n', done)
})

it('should not redirect incorrectly', function (done) {
request(server)
.get('/')
Expand Down

0 comments on commit f281443

Please sign in to comment.