Skip to content
Myr

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

5 files+63 added−10 removed

modified:lib/response.js+4 added-5 removed
12 unmodified lines
13 */1415var contentDisposition = require('content-disposition');16var deprecate = require('depd')('express');17var encodeUrl = require('encodeurl');18var escapeHtml = require('escape-html');19var http = require('http');20var isAbsolute = require('./utils').isAbsolute;21var onFinished = require('on-finished');
810 unmodified lines
832 loc = this.req.get('Referrer') || '/';833 }834835 // set location835 this.set('Location', loc);836 return this;836 return this.set('Location', encodeUrl(loc));837};838839/**840 * Redirect to the given `url` with optional response `status`
29 unmodified lines
870 }871 }872873 // Set location header874 this.location(address);875 address = this.get('Location');874 address = this.location(address).get('Location');875876 // 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 },881882 html: function(){883 var u = escapeHtml(address);
182 unmodified lines
modified:test/res.location.js+26 added
modified:test/res.redirect.js+31 added-5 removed
16 unmodified lines
17 .get('/')18 .expect('location', 'http://google.com')19 .expect(302, done)20 })2122 it('should encode "url"', function (done) {23 var app = express()2425 app.use(function (req, res) {26 res.redirect('https://google.com?q=\u2603 §10')27 })2829 request(app)30 .get('/')31 .expect('Location', 'https://google.com?q=%E2%98%83%20%C2%A710')32 .expect(302, done)33 })3435 it('should not touch already-encoded sequences in "url"', function (done) {36 var app = express()3738 app.use(function (req, res) {39 res.redirect('https://google.com?q=%A710')40 })4142 request(app)43 .get('/')44 .expect('Location', 'https://google.com?q=%A710')45 .expect(302, done)46 })47 })4849 describe('.redirect(status, url)', function(){50 it('should set the response status', function(done){
59 unmodified lines
110 it('should escape the url', function(done){111 var app = express();112113 app.use(function(req, res){88 res.redirect('<lame>');114 res.redirect('<la\'me>');115 });116117 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="&lt;lame&gt;">&lt;lame&gt;</a></p>', done);122 .expect('Location', '%3Cla\'me%3E')123 .expect(302, '<p>' + http.STATUS_CODES[302] + '. Redirecting to <a href="%3Cla&#39;me%3E">%3Cla&#39;me%3E</a></p>', done)124 })125126 it('should include the redirect type', function(done){127 var app = express();
38 unmodified lines
166 .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 })173174 it('should include the redirect type', function(done){175 var app = express();
32 unmodified lines
modified:History.md+1 added
3 unmodified lines
4 * 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 cases
3124 unmodified lines
modified:package.json+1 added