Merge tag '3.7.0'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parents 31b2e2d, b91cd66 · Browse files · 19 files·+348 added−111 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
19 files+348 added−111 removed
- tests:+189 added−17 removed
- other:+159 added−94 removed
Provenance
- Commit
097cd0c- Tree
6ba4ab5- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent 1
31b2e2d- Parent 2
b91cd66
19 files changed+348 added−111 removed
modified:examples/auth/app.js+2 added-2 removed
76 unmodified lines7778798081818283848529 unmodified lines1151161171181191191201211221234 unmodified lines76 unmodified lines }}
app.get('/', function(req, res){ res.redirect('login'); res.redirect('/login');});
app.get('/restricted', restrict, function(req, res){ res.send('Wahoo! restricted area, click to <a href="/logout">logout</a>');29 unmodified lines } else { req.session.error = 'Authentication failed, please check your ' + ' username and password.' + ' (use "tj" and "foobar")'; res.redirect('login'); res.redirect('/login'); } });});
4 unmodified linesmodified:lib/application.js+8 added
9 unmodified lines10 var query = require('./middleware/query');11 var debug = require('debug')('express:application');12 var View = require('./view');13 var http = require('http');14+var compileTrust = require('./utils').compileTrust;15 var deprecate = require('./utils').deprecate;16 17 /**18 * Application prototype.31 unmodified lines50 this.enable('etag');51 var env = process.env.NODE_ENV || 'development';52 this.set('env', env);53 this.set('subdomain offset', 2);54+ this.set('trust proxy', false);55 56 debug('booting in %s mode', env);57 58 // inherit protos250 unmodified lines309 if (1 == arguments.length) {310 return this.settings[setting];311 } else {312 this.settings[setting] = val;313+314+ if (setting === 'trust proxy') {315+ debug('compile trust proxy %j', val);316+ this.set('trust proxy fn', compileTrust(val));317+ }318+319 return this;320 }321 };322 247 unmodified linesmodified:lib/request.js+41 added-22 removed
6 unmodified lines7 var http = require('http');8 var fresh = require('fresh');9 var parseRange = require('range-parser');10 var parse = require('parseurl');11+var proxyaddr = require('proxy-addr');12 13 /**14 * Request prototype.15 */223 unmodified lines239 240 /**241 * Return the protocol string "http" or "https"242 * when requested with TLS. When the "trust proxy"242- * setting is enabled the "X-Forwarded-Proto" header243- * field will be trusted. If you're running behind244- * a reverse proxy that supplies https for you this245- * may be enabled.243+ * setting trusts the socket address, the244+ * "X-Forwarded-Proto" header field will be trusted.245+ * If you're running behind a reverse proxy that246+ * supplies https for you this may be enabled.247 *248 * @return {String}249 * @api public250 */251 252 req.__defineGetter__('protocol', function(){252- var trustProxy = this.app.get('trust proxy');253- if (this.connection.encrypted) return 'https';254- if (!trustProxy) return 'http';253+ var trust = this.app.get('trust proxy fn');254+255+ if (!trust(this.connection.remoteAddress)) {256+ return this.connection.encrypted257+ ? 'https'258+ : 'http';259+ }260+261+ // Note: X-Forwarded-Proto is normally only ever a262+ // single value, but this is to be safe.263 var proto = this.get('X-Forwarded-Proto') || 'http';264 return proto.split(/\s*,\s*/)[0];265 });266 10 unmodified lines277 return 'https' == this.protocol;278 });279 280 /**273- * Return the remote address, or when274- * "trust proxy" is `true` return275- * the upstream addr.281+ * Return the remote address from the trusted proxy.282 *283+ * The is the remote address on the socket unless284+ * "trust proxy" is set.285+ *286 * @return {String}287 * @api public288 */289 290 req.__defineGetter__('ip', function(){282- return this.ips[0] || this.connection.remoteAddress;291+ var trust = this.app.get('trust proxy fn');292+ return proxyaddr(this, trust);293 });294 295 /**286- * When "trust proxy" is `true`, parse287- * the "X-Forwarded-For" ip address list.296+ * When "trust proxy" is set, trusted proxy addresses + client.297 *298 * For example if the value were "client, proxy1, proxy2"299 * you would receive the array `["client", "proxy1", "proxy2"]`291- * where "proxy2" is the furthest down-stream.300+ * where "proxy2" is the furthest down-stream and "proxy1" and301+ * "proxy2" were trusted.302 *303 * @return {Array}304 * @api public305 */306 307 req.__defineGetter__('ips', function(){298- var trustProxy = this.app.get('trust proxy');299- var val = this.get('X-Forwarded-For');300- return trustProxy && val301- ? val.split(/ *, */)302- : [];308+ var trust = this.app.get('trust proxy fn');309+ var addrs = proxyaddr.all(this, trust);310+ return addrs.slice(1).reverse();311 });312 313 /**314 * Return subdomains as an array.31 unmodified lines346 347 /**348 * Parse the "Host" header field hostname.349 *350+ * When the "trust proxy" setting trusts the socket351+ * address, the "X-Forwarded-Host" header field will352+ * be trusted.353+ *354 * @return {String}355 * @api public356 */357 358 req.__defineGetter__('host', function(){347- var trustProxy = this.app.get('trust proxy');348- var host = trustProxy && this.get('X-Forwarded-Host');349- host = host || this.get('Host');359+ var trust = this.app.get('trust proxy fn');360+ var host = this.get('X-Forwarded-Host');361+362+ if (!host || !trust(this.connection.remoteAddress)) {363+ host = this.get('Host');364+ }365+366 if (!host) return;367+368+ // IPv6 literal support369 var offset = host[0] === '['370 ? host.indexOf(']') + 1371 : 0;372 var index = host.indexOf(':', offset);373+374 return ~index375 ? host.substring(0, index)376 : host;377 });49 unmodified linesmodified:lib/utils.js+43 added-4 removed
4 unmodified lines5 var mime = require('send').mime;6 var crc32 = require('buffer-crc32');7 var basename = require('path').basename;8 var deprecate = require('util').deprecate;9+var proxyaddr = require('proxy-addr');10 11 /**11- * Deprecate function, like core `util.deprecate`12+ * Deprecate function, like core `util.deprecate`,13+ * but with NODE_ENV and color support.14 *15 * @param {Function} fn16 * @param {String} msg17 * @return {Function}18 * @api private19 */20 21 exports.deprecate = function(fn, msg){20- return 'test' !== process.env.NODE_ENV21- ? deprecate(fn, 'express: ' + msg)22- : fn;22+ if (process.env.NODE_ENV === 'test') return fn;23+24+ // prepend module name25+ msg = 'express: ' + msg;26+27+ if (process.stderr.isTTY) {28+ // colorize29+ msg = '\x1b[31;1m' + msg + '\x1b[0m';30+ }31+32+ return deprecate(fn, msg);33 };34 35 /**36 * Return ETag for `body`.120 unmodified lines157 }158 159 return ret;160 }161+162+/**163+ * Compile "proxy trust" value to function.164+ *165+ * @param {Boolean|String|Number|Array|Function} val166+ * @return {Function}167+ * @api private168+ */169+170+exports.compileTrust = function(val) {171+ if (typeof val === 'function') return val;172+173+ if (val === true) {174+ // Support plain true/false175+ return function(){ return true };176+ }177+178+ if (typeof val === 'number') {179+ // Support trusting hop count180+ return function(a, i){ return i < val };181+ }182+183+ if (typeof val === 'string') {184+ // Support comma-separated values185+ val = val.split(/ *, */);186+ }187+188+ return proxyaddr.compile(val || []);189+}modified:test/acceptance/auth.js+76 added-16 removed
1 var app = require('../../examples/auth/app')2- , request = require('supertest');2+var request = require('supertest')3 4-function redirects(to, fn){5- return function(err, res){6- res.statusCode.should.equal(302)7- res.headers.should.have.property('location').match(to);8- fn()9- }10-}11-4 function getCookie(res) {5 return res.headers['set-cookie'][0].split(';')[0];6 }7 1 unmodified line9 describe('GET /',function(){10 it('should redirect to /login', function(done){11 request(app)12 .get('/')21- .end(redirects(/login$/, done))13+ .expect('Location', '/login')14+ .expect(302, done)15 })16 })17 25- describe('GET /restricted (w/o cookie)',function(){26- it('should redirect to /login', function(done){18+ describe('GET /login',function(){19+ it('should render login form', function(done){20 request(app)21+ .get('/login')22+ .expect(200, /<form/, done)23+ })24+25+ it('should display login error', function(done){26+ request(app)27+ .post('/login')28+ .type('urlencoded')29+ .send('username=not-tj&password=foobar')30+ .expect('Location', '/login')31+ .expect(302, function(err, res){32+ if (err) return done(err)33+ request(app)34+ .get('/login')35+ .set('Cookie', getCookie(res))36+ .expect(200, /Authentication failed/, done)37+ })38+ })39+ })40+41+ describe('GET /logout',function(){42+ it('should redirect to /', function(done){43+ request(app)44+ .get('/logout')45+ .expect('Location', '/')46+ .expect(302, done)47+ })48+ })49+50+ describe('GET /restricted',function(){51+ it('should redirect to /login without cookie', function(done){52+ request(app)53 .get('/restricted')29- .end(redirects(/login$/,done))54+ .expect('Location', '/login')55+ .expect(302, done)56 })57+58+ it('should succeed with proper cookie', function(done){59+ request(app)60+ .post('/login')61+ .type('urlencoded')62+ .send('username=tj&password=foobar')63+ .expect('Location', '/')64+ .expect(302, function(err, res){65+ if (err) return done(err)66+ request(app)67+ .get('/restricted')68+ .set('Cookie', getCookie(res))69+ .expect(200, done)70+ })71+ })72 })73 74 describe('POST /login', function(){34- it('should fail without proper credentials', function(done){75+ it('should fail without proper username', function(done){76 request(app)77 .post('/login')78 .type('urlencoded')79 .send('username=not-tj&password=foobar')39- .end(redirects(/login$/, done))80+ .expect('Location', '/login')81+ .expect(302, done)82 })83+84+ it('should fail without proper password', function(done){85+ request(app)86+ .post('/login')87+ .type('urlencoded')88+ .send('username=tj&password=baz')89+ .expect('Location', '/login')90+ .expect(302, done)91+ })92+93+ it('should succeed with proper credentials', function(done){94+ request(app)95+ .post('/login')96+ .type('urlencoded')97+ .send('username=tj&password=foobar')98+ .expect('Location', '/')99+ .expect(302, done)100+ })101 })42-})102+})added:test/support/env.js+2 added
12
process.env.NODE_ENV = 'test';modified:test/middleware.basic.js+1 added-1 removed
39 unmodified lines40414243444439 unmodified lines done(); }) }) })})No newline at end of file})modified:test/req.host.js+65 added
67 unmodified lines68 .post('/')69 .set('Host', '[::1]:3000')70 .expect('[::1]', done);71 })72+73+ describe('when "trust proxy" is enabled', function(){74+ it('should respect X-Forwarded-Host', function(done){75+ var app = express();76+77+ app.enable('trust proxy');78+79+ app.use(function(req, res){80+ res.end(req.host);81+ });82+83+ request(app)84+ .get('/')85+ .set('Host', 'localhost')86+ .set('X-Forwarded-Host', 'example.com')87+ .expect('example.com', done);88+ })89+90+ it('should ignore X-Forwarded-Host if socket addr not trusted', function(done){91+ var app = express();92+93+ app.set('trust proxy', '10.0.0.1');94+95+ app.use(function(req, res){96+ res.end(req.host);97+ });98+99+ request(app)100+ .get('/')101+ .set('Host', 'localhost')102+ .set('X-Forwarded-Host', 'example.com')103+ .expect('localhost', done);104+ })105+106+ it('should default to Host', function(done){107+ var app = express();108+109+ app.enable('trust proxy');110+111+ app.use(function(req, res){112+ res.end(req.host);113+ });114+115+ request(app)116+ .get('/')117+ .set('Host', 'example.com')118+ .expect('example.com', done);119+ })120+ })121+122+ describe('when "trust proxy" is disabled', function(){123+ it('should ignore X-Forwarded-Host', function(done){124+ var app = express();125+126+ app.use(function(req, res){127+ res.end(req.host);128+ });129+130+ request(app)131+ .get('/')132+ .set('Host', 'localhost')133+ .set('X-Forwarded-Host', 'evil')134+ .expect('localhost', done);135+ })136+ })137 })138 })modified:test/req.ip.js+15 added
18 unmodified lines19 .get('/')20 .set('X-Forwarded-For', 'client, p1, p2')21 .expect('client', done);22 })23+24+ it('should return the addr after trusted proxy', function(done){25+ var app = express();26+27+ app.set('trust proxy', 2);28+29+ app.use(function(req, res, next){30+ res.send(req.ip);31+ });32+33+ request(app)34+ .get('/')35+ .set('X-Forwarded-For', 'client, p1, p2')36+ .expect('p1', done);37+ })38 })39 40 describe('when "trust proxy" is disabled', function(){41 it('should return the remote address', function(done){31 unmodified linesmodified:test/req.ips.js+15 added
18 unmodified lines19 .get('/')20 .set('X-Forwarded-For', 'client, p1, p2')21 .expect('["client","p1","p2"]', done);22 })23+24+ it('should stop at first untrusted', function(done){25+ var app = express();26+27+ app.set('trust proxy', 2);28+29+ app.use(function(req, res, next){30+ res.send(req.ips);31+ });32+33+ request(app)34+ .get('/')35+ .set('X-Forwarded-For', 'client, p1, p2')36+ .expect('["p1","p2"]', done);37+ })38 })39 40 describe('when "trust proxy" is disabled', function(){41 it('should return an empty array', function(done){29 unmodified linesmodified:test/req.protocol.js+15 added
30 unmodified lines31 .set('X-Forwarded-Proto', 'https')32 .expect('https', done);33 })34 35+ it('should ignore X-Forwarded-Proto if socket addr not trusted', function(done){36+ var app = express();37+38+ app.set('trust proxy', '10.0.0.1');39+40+ app.use(function(req, res){41+ res.end(req.protocol);42+ });43+44+ request(app)45+ .get('/')46+ .set('X-Forwarded-Proto', 'https')47+ .expect('http', done);48+ })49+50 it('should default to http', function(done){51 var app = express();52 53 app.enable('trust proxy');27 unmodified linesmodified:.gitignore+1 added-4 removed
1-coverage.html1+coverage/2 .DS_Store3-lib-cov3 *.seed4 *.log5 *.csv6 *.dat4 unmodified lines11 benchmarks/graphs12 testing13 node_modules/14 testing16-.coverage_data17-cover_html15 test.js16 .ideamodified:.npmignore+1 added-2 removed
1 .git*2 benchmarks/3+coverage/4 docs/5 examples/6 support/7 test/8 testing.js9 .DS_Store10 .travis.yml10-coverage.html11-lib-cov11 Contributing.mdmodified:.travis.yml+1 added
4 unmodified lines5 matrix:6 allow_failures:7 - node_js: "0.11"8 fast_finish: true9+script: "npm run-script test-travis"modified:History.md+31 added
1 unreleased2 ==========3 4 * fix behavior of multiple `app.VERB` for the same path5+ * proper proxy trust with `app.set('trust proxy', trust)`6+ - `app.set('trust proxy', 1)` trust first hop7+ - `app.set('trust proxy', 'loopback')` trust loopback addresses8+ - `app.set('trust proxy', '10.0.0.1')` trust single IP9+ - `app.set('trust proxy', '10.0.0.1/16')` trust subnet10+ - `app.set('trust proxy', '10.0.0.1, 10.0.0.2')` trust list11+ - `app.set('trust proxy', false)` turn off12+ - `app.set('trust proxy', true)` trust everything13 * update type-is to 1.2.014 - support suffix matching15 16 4.2.0 / 2014-05-1191 unmodified lines108 - `app.router()` - returns the app Router instance109 - `app.route()` - Proxy to the app's `Router#route()` method to create a new route110 - Router & Route - public API111 112+3.7.0 / 2014-05-18113+==================114+115+ * proper proxy trust with `app.set('trust proxy', trust)`116+ - `app.set('trust proxy', 1)` trust first hop117+ - `app.set('trust proxy', 'loopback')` trust loopback addresses118+ - `app.set('trust proxy', '10.0.0.1')` trust single IP119+ - `app.set('trust proxy', '10.0.0.1/16')` trust subnet120+ - `app.set('trust proxy', '10.0.0.1, 10.0.0.2')` trust list121+ - `app.set('trust proxy', false)` turn off122+ - `app.set('trust proxy', true)` trust everything123+ * update connect to 2.16.2124+ - deprecate `res.headerSent` -- use `res.headersSent`125+ - deprecate `res.on("header")` -- use on-headers module instead126+ - fix edge-case in `res.appendHeader` that would append in wrong order127+ - json: use body-parser128+ - urlencoded: use body-parser129+ - dep: bytes@1.0.0130+ - dep: cookie-parser@1.1.0131+ - dep: csurf@1.2.0132+ - dep: express-session@1.1.0133+ - dep: method-override@1.0.1134+135 3.6.0 / 2014-05-09136 ==================137 138 * deprecate `app.del()` -- use `app.delete()` instead1312 unmodified linesmodified:index.js+1 added-3 removed
1 2-module.exports = process.env.EXPRESS_COV3- ? require('./lib-cov/express')4- : require('./lib/express');2+module.exports = require('./lib/express');deleted:Makefile-34 removed
1-2-MOCHA_OPTS= --check-leaks3-REPORTER = dot4-5-check: test6-7-test: test-unit test-acceptance8-9-test-unit:10- @NODE_ENV=test ./node_modules/.bin/mocha \11- --reporter $(REPORTER) \12- --globals setImmediate,clearImmediate \13- $(MOCHA_OPTS)14-15-test-acceptance:16- @NODE_ENV=test ./node_modules/.bin/mocha \17- --reporter $(REPORTER) \18- --bail \19- test/acceptance/*.js20-21-test-cov: lib-cov22- @EXPRESS_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html23-24-lib-cov:25- @jscoverage lib lib-cov26-27-bench:28- @$(MAKE) -C benchmarks29-30-clean:31- rm -f coverage.html32- rm -fr lib-cov33-34-.PHONY: test test-unit test-acceptance bench cleanmodified:package.json+26 added-21 removed
31 unmodified lines32 "name": "Roman Shtylman",33 "email": "shtylman+expressjs@gmail.com"34 }35 ],36+ "keywords": [37+ "express",38+ "framework",39+ "sinatra",40+ "web",41+ "rest",42+ "restful",43+ "router",44+ "app",45+ "api"46+ ],47+ "repository": "git://github.com/visionmedia/express",48+ "license": "MIT",49 "dependencies": {37- "parseurl": "1.0.1",50 "accepts": "1.0.1",39- "type-is": "1.2.0",51+ "parseurl": "1.0.1",52+ "proxy-addr": "1.0.0",53 "range-parser": "1.0.0",54+ "type-is": "1.2.0",55 "cookie": "0.1.2",56 "buffer-crc32": "0.2.1",57 "fresh": "0.2.2",58 "methods": "1.0.0",7 unmodified lines66 "path-to-regexp": "0.1.2",67 "debug": "0.8.1"68 },69 "devDependencies": {70+ "coveralls": "2.10.0",71+ "istanbul": "0.2.10",72 "mocha": "~1.18.2",73+ "should": "~3.3.1",74+ "supertest": "~0.12.0",75 "body-parser": "~1.1.2",76 "connect-redis": "~2.0.0",77 "ejs": "~1.0.0",78 "jade": "~0.35.0",79 "marked": "0.3.2",80 "multiparty": "~3.2.4",81 "hjs": "~0.0.6",64- "should": "~3.3.1",65- "supertest": "~0.12.0",82 "method-override": "1.0.0",83 "cookie-parser": "1.0.1",84 "express-session": "1.0.4",85 "morgan": "1.0.1",86 "vhost": "1.0.0"87 },72- "keywords": [73- "express",74- "framework",75- "sinatra",76- "web",77- "rest",78- "restful",79- "router",80- "app",81- "api"82- ],83- "repository": "git://github.com/visionmedia/express",84- "scripts": {85- "prepublish": "npm prune",86- "test": "make test"87- },88 "engines": {89 "node": ">= 0.10.0"90 },91- "license": "MIT"91+ "scripts": {92+ "prepublish": "npm prune",93+ "test": "mocha --require test/support/env --reporter dot --check-leaks test/ test/acceptance/",94+ "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/",95+ "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/ && cat ./coverage/lcov.info | coveralls"96+ }97 }modified:Readme.md+4 added-2 removed
1 [](http://expressjs.com/)2 3 Fast, unopinionated, minimalist web framework for [node](http://nodejs.org).4 5- [](https://travis-ci.org/visionmedia/express) [](https://www.gittip.com/visionmedia/) [](http://badge.fury.io/js/express)5+ [](http://badge.fury.io/js/express) [](https://travis-ci.org/visionmedia/express) [](https://coveralls.io/r/visionmedia/express) [](https://www.gittip.com/visionmedia/)6 7 ```js8 var express = require('express');9 var app = express();84 unmodified lines94 $ npm install95 96 Then run the tests:97 98+```sh98- $ make test99+$ npm test100+```101 102 ## Contributors103 104 Author: [TJ Holowaychuk](http://github.com/visionmedia) 6 unmodified lines