You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 23, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Oct 23, 2023. It is now read-only.
IP address lost when using express behind a proxy and 'trust proxy' is enabled #360
varhttp=require('http')varexpress=require('express')varapp=express()app.enable('trust proxy')varraven=require('raven')vardsn='';raven.config(dsn,{autoBreadcrumbs: true}).install();app.use(raven.requestHandler());app.get('/',function(req,res){res.send('hello world '+req.ip)})app.get('/err',function(req,res){thrownewError('test')})app.use(raven.errorHandler());http.createServer(app).listen(8888);
Set up a proxy to forward to the node app, I used nginx with the following config:
Visit / via the proxy and you'll see hello world and the IP printed
Visit /err via the proxy, it'll throw an error, check this in sentry it won't show the IP. I checked that the line mentioned above is resulting in req.ip undefined.
I've not had time to debug why req.ip is undefined but since it's not undefined when accessed outside raven I'm assuming it's a bug in raven.
I found Object.prototype.hasOwnProperty.call(req, 'ip')) return false, which leads ip field does not assigned in extend and req.ip remains undefined in Raven:
module.exports.extend = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
Though I'm not sure this is really a bug in Raven, I came across this issue discussing X-Forwarded-For, and thought the little bit of code I posted over on this issue could be helpful:
Activity
goorockey commentedon Aug 21, 2017
+1
I found
Object.prototype.hasOwnProperty.call(req, 'ip'))return false, which leads ip field does not assigned inextendandreq.ipremains undefined in Raven:client.js
utils.js
rwky commentedon Aug 25, 2017
As a work around I'm using
mergeContextto merge{ user: { ip: req.ip }}I'll see if I can make a PR at some point to fix it properly though if someone else wants to do it that'd be great.
wbyoung commentedon Aug 27, 2017
Though I'm not sure this is really a bug in Raven, I came across this issue discussing
X-Forwarded-For, and thought the little bit of code I posted over on this issue could be helpful:kamilogorek commentedon Sep 22, 2017
Addressed in #379 Let's keep the conversation in the PR.
ipis indeed non-enumerable when using with proxy feature.