Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy-addr + forwarded #666

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var debug = require('debug')('koa:application');
var Emitter = require('events').EventEmitter;
var compose_es7 = require('composition');
var onFinished = require('on-finished');
var proxyAddr = require('proxy-addr');
var response = require('./response');
var compose = require('koa-compose');
var isJSON = require('koa-is-json');
Expand Down Expand Up @@ -184,6 +185,24 @@ app.onerror = function(err){
console.error();
};

/**
* Compile trust function for proxy-addr.
*/

Object.defineProperty(app, 'proxy', {
get(){
return this._proxy;
},
set(val){
this._proxy = val;
if ('boolean' == typeof val) {
this._proxyTrust = function () { return val };
} else {
this._proxyTrust = proxyAddr.compile(val);
}
}
});

/**
* Response helper.
*/
Expand Down
11 changes: 6 additions & 5 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/

var contentType = require('content-type');
var proxyAddr = require('proxy-addr');
var stringify = require('url').format;
var forwarded = require('forwarded');
var parse = require('parseurl');
var qs = require('querystring');
var typeis = require('type-is');
Expand Down Expand Up @@ -371,14 +373,14 @@ module.exports = {
/**
* Return the remote address, or when
* `app.proxy` is `true` return
* the upstream addr.
* the most upstream trusted addr.
*
* @return {String}
* @api public
*/

get ip() {
return this.ips[0] || this.socket.remoteAddress || '';
return proxyAddr(this.req, this.app._proxyTrust) || '';
},

/**
Expand All @@ -395,9 +397,8 @@ module.exports = {

get ips() {
var proxy = this.app.proxy;
var val = this.get('X-Forwarded-For');
return proxy && val
? val.split(/\s*,\s*/)
return proxy
? forwarded(this.req)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on what you want ips to be, ideally they should get cut off at the trust endpoint. You can remove a dependency with using https://github.com/jshttp/proxy-addr#proxyaddrallreq-trust

return proxyAddr.all(this,req, this.app._proxyTrust);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh shoot. nice.

: [];
},

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"destroy": "^1.0.3",
"error-inject": "~1.0.0",
"escape-html": "~1.0.1",
"forwarded": "^0.1.0",
"fresh": "^0.3.0",
"http-assert": "^1.1.0",
"http-errors": "^1.2.8",
Expand All @@ -38,6 +39,7 @@
"on-finished": "^2.1.0",
"only": "0.0.2",
"parseurl": "^1.3.0",
"proxy-addr": "^1.0.10",
"statuses": "^1.2.0",
"type-is": "^1.5.5",
"vary": "^1.0.0"
Expand Down
5 changes: 3 additions & 2 deletions test/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ var koa = require('..');

exports = module.exports = function(req, res){
var socket = new Stream.Duplex();
req = req || { headers: {}, socket: socket, __proto__: Stream.Readable.prototype };
res = res || { _headers: {}, socket: socket, __proto__: Stream.Writable.prototype };
socket.remoteAddress = '127.0.0.1';
req = req || { headers: {}, socket: socket, connection: socket, __proto__: Stream.Readable.prototype };
res = res || { _headers: {}, socket: socket, connection: socket, __proto__: Stream.Writable.prototype };
res.getHeader = function(k){ return res._headers[k.toLowerCase()] };
res.setHeader = function(k, v){ res._headers[k.toLowerCase()] = v };
res.removeHeader = function(k, v){ delete res._headers[k.toLowerCase()] };
Expand Down
4 changes: 2 additions & 2 deletions test/request/ips.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('req.ips', function(){
it('should be ignored', function(){
var req = request();
req.app.proxy = false;
req.header['x-forwarded-for'] = '127.0.0.1,127.0.0.2';
req.header['x-forwarded-for'] = '127.0.0.2';
req.ips.should.eql([]);
})
})
Expand All @@ -18,7 +18,7 @@ describe('req.ips', function(){
it('should be used', function(){
var req = request();
req.app.proxy = true;
req.header['x-forwarded-for'] = '127.0.0.1,127.0.0.2';
req.header['x-forwarded-for'] = '127.0.0.2';
req.ips.should.eql(['127.0.0.1', '127.0.0.2']);
})
})
Expand Down