Encode URL in res.location/res.redirect if not already encoded
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixes #2897 fixes #3003
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent c12cc88 · Browse files · 5 files·+63 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+63 added−10 removed
- tests:+57 added−5 removed
- other:+6 added−5 removed
Provenance
- Commit
76eaa32- Tree
c7ec7a1- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
c12cc88
5 files changed+63 added−10 removed
modified:lib/response.js+4 added-5 removed
12 unmodified lines13 */14 15 var contentDisposition = require('content-disposition');16 var deprecate = require('depd')('express');17+var encodeUrl = require('encodeurl');18 var escapeHtml = require('escape-html');19 var http = require('http');20 var isAbsolute = require('./utils').isAbsolute;21 var onFinished = require('on-finished');810 unmodified lines832 loc = this.req.get('Referrer') || '/';833 }834 835 // set location835- this.set('Location', loc);836- return this;836+ return this.set('Location', encodeUrl(loc));837 };838 839 /**840 * Redirect to the given `url` with optional response `status`29 unmodified lines870 }871 }872 873 // Set location header874- this.location(address);875- address = this.get('Location');874+ address = this.location(address).get('Location');875 876 // Support text/{plain,html} by default877 this.format({878 text: function(){880- body = statusCodes[status] + '. Redirecting to ' + encodeURI(address);879+ body = statusCodes[status] + '. Redirecting to ' + address;880 },881 882 html: function(){883 var u = escapeHtml(address);182 unmodified linesmodified:test/res.location.js+26 added
14 unmodified lines151617181920212223242526272829303132333435363738394041424344454614 unmodified lines .get('/') .expect('Location', 'http://google.com') .expect(200, done) })
it('should encode "url"', function (done) { var app = express()
app.use(function (req, res) { res.location('https://google.com?q=\u2603 §10').end() })
request(app) .get('/') .expect('Location', 'https://google.com?q=%E2%98%83%20%C2%A710') .expect(200, done) })
it('should not touch already-encoded sequences in "url"', function (done) { var app = express()
app.use(function (req, res) { res.location('https://google.com?q=%A710').end() })
request(app) .get('/') .expect('Location', 'https://google.com?q=%A710') .expect(200, done) }) })})modified:test/res.redirect.js+31 added-5 removed
16 unmodified lines17 .get('/')18 .expect('location', 'http://google.com')19 .expect(302, done)20 })21+22+ it('should encode "url"', function (done) {23+ var app = express()24+25+ app.use(function (req, res) {26+ res.redirect('https://google.com?q=\u2603 §10')27+ })28+29+ request(app)30+ .get('/')31+ .expect('Location', 'https://google.com?q=%E2%98%83%20%C2%A710')32+ .expect(302, done)33+ })34+35+ it('should not touch already-encoded sequences in "url"', function (done) {36+ var app = express()37+38+ app.use(function (req, res) {39+ res.redirect('https://google.com?q=%A710')40+ })41+42+ request(app)43+ .get('/')44+ .expect('Location', 'https://google.com?q=%A710')45+ .expect(302, done)46+ })47 })48 49 describe('.redirect(status, url)', function(){50 it('should set the response status', function(done){59 unmodified lines110 it('should escape the url', function(done){111 var app = express();112 113 app.use(function(req, res){88- res.redirect('<lame>');114+ res.redirect('<la\'me>');115 });116 117 request(app)118 .get('/')119 .set('Host', 'http://example.com')120 .set('Accept', 'text/html')121 .expect('Content-Type', /html/)96- .expect('Location', '<lame>')97- .expect(302, '<p>' + http.STATUS_CODES[302] + '. Redirecting to <a href="<lame>"><lame></a></p>', done);122+ .expect('Location', '%3Cla\'me%3E')123+ .expect(302, '<p>' + http.STATUS_CODES[302] + '. Redirecting to <a href="%3Cla'me%3E">%3Cla'me%3E</a></p>', done)124 })125 126 it('should include the redirect type', function(done){127 var app = express();38 unmodified lines166 .get('/')167 .set('Host', 'http://example.com')168 .set('Accept', 'text/plain, */*')169 .expect('Content-Type', /plain/)144- .expect('Location', 'http://example.com/?param=<script>alert("hax");</script>')145- .expect(302, http.STATUS_CODES[302] + '. Redirecting to http://example.com/?param=%3Cscript%3Ealert(%22hax%22);%3C/script%3E', done);170+ .expect('Location', 'http://example.com/?param=%3Cscript%3Ealert(%22hax%22);%3C/script%3E')171+ .expect(302, http.STATUS_CODES[302] + '. Redirecting to http://example.com/?param=%3Cscript%3Ealert(%22hax%22);%3C/script%3E', done)172 })173 174 it('should include the redirect type', function(done){175 var app = express();32 unmodified linesmodified:History.md+1 added
3 unmodified lines4 * Add `acceptRanges` option to `res.sendFile`/`res.sendfile`5 * Add `cacheControl` option to `res.sendFile`/`res.sendfile`6 * Add `options` argument to `req.range`7 - Includes the `combine` option8+ * Encode URL in `res.location`/`res.redirect` if not already encoded9 * Fix some redirect handling in `res.sendFile`/`res.sendfile`10 * Fix Windows absolute path check using forward slashes11 * Improve error with invalid arguments to `req.get()`12 * Improve performance for `res.json`/`res.jsonp` in most cases3124 unmodified linesmodified:package.json+1 added
33 unmodified lines34353637383940414250 unmodified lines33 unmodified lines "cookie": "0.3.1", "cookie-signature": "1.0.6", "debug": "~2.2.0", "depd": "~1.1.0", "encodeurl": "~1.0.1", "escape-html": "~1.0.3", "etag": "~1.7.0", "finalhandler": "0.4.1", "fresh": "0.3.0",50 unmodified lines