Prevent open redirect allow list bypass due to encodeurl
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by FDrag0n · committed by Wes Todd · parent 567c9c6 · Browse files · 2 files·+64 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Jon Church <me@jonchurch.com>
Diff stats
2 files+64 added−2 removed
- tests:+45 added−1 removed
- other:+19 added−1 removed
Provenance
- Commit
0867302- Tree
783be98- Author
FDrag0n <34733637+FDrag0n@users.noreply.github.com>- Committer
Wes Todd <wes@wesleytodd.com>- Parent
567c9c6
2 files changed+64 added−2 removed
modified:lib/response.js+19 added-1 removed
32 unmodified lines33 var extname = path.extname;34 var mime = send.mime;35 var resolve = path.resolve;36 var vary = require('vary');37+var urlParse = require('url').parse;38 39 /**40 * Response prototype.41 * @public869 unmodified lines911 if (url === 'back') {912 loc = this.req.get('Referrer') || '/';913 }914 915+ var lowerLoc = loc.toLowerCase();916+ var encodedUrl = encodeUrl(loc);917+ if (lowerLoc.indexOf('https://') === 0 || lowerLoc.indexOf('http://') === 0) {918+ try {919+ var parsedUrl = urlParse(loc);920+ var parsedEncodedUrl = urlParse(encodedUrl);921+ // Because this can encode the host, check that we did not change the host922+ if (parsedUrl.host !== parsedEncodedUrl.host) {923+ // If the host changes after encodeUrl, return the original url924+ return this.set('Location', loc);925+ }926+ } catch (e) {927+ // If parse fails, return the original url928+ return this.set('Location', loc);929+ }930+ }931+932 // set location915- return this.set('Location', encodeUrl(loc));933+ return this.set('Location', encodedUrl);934 };935 936 /**937 * Redirect to the given `url` with optional response `status`250 unmodified linesmodified:test/res.location.js+45 added-1 removed
1234456789101112131415161718192021222324252627282914 unmodified lines444546474849505152535455565758596061626364656667686970717273747576777879808168 unmodified lines'use strict'
var express = require('../') , request = require('supertest'); , request = require('supertest') , url = require('url');
describe('res', function(){ describe('.location(url)', function(){ it('should set the header', function(done){ var app = express();
app.use(function(req, res){ res.location('http://google.com/').end(); });
request(app) .get('/') .expect('Location', 'http://google.com/') .expect(200, done) })
it('should preserve trailing slashes when not present', function(done){ var app = express();
app.use(function(req, res){ res.location('http://google.com').end(); });
request(app)14 unmodified lines .expect('Location', 'https://google.com?q=%E2%98%83%20%C2%A710') .expect(200, done) })
it('should not encode bad "url"', function (done) { var app = express()
app.use(function (req, res) { // This is here to show a basic check one might do which // would pass but then the location header would still be bad if (url.parse(req.query.q).host !== 'google.com') { res.status(400).end('Bad url'); } res.location(req.query.q).end(); });
request(app) .get('/?q=http://google.com\\@apple.com') .expect(200) .expect('Location', 'http://google.com\\@apple.com') .end(function (err) { if (err) { throw err; }
// This ensures that our protocol check is case insensitive request(app) .get('/?q=HTTP://google.com\\@apple.com') .expect(200) .expect('Location', 'HTTP://google.com\\@apple.com') .end(done) }); });
it('should not touch already-encoded sequences in "url"', function (done) { var app = express()
app.use(function (req, res) {68 unmodified lines