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

How do I get the real ip? #599

Closed
thelinuxlich opened this issue Nov 19, 2015 · 16 comments
Closed

How do I get the real ip? #599

thelinuxlich opened this issue Nov 19, 2015 · 16 comments

Comments

@thelinuxlich
Copy link

I set app.proxy = true but sometimes I still get someone with ctx.ip = "127.0.0.x"

@alsotang
Copy link
Contributor

sometimes or always?

2015-11-19 11:38 GMT+08:00 Alisson Cavalcante Agiani <
[email protected]>:

I set app.proxy = true but sometimes I still get someone with ctx.ip =
"127.0.0.x"


Reply to this email directly or view it on GitHub
#599.

@thelinuxlich
Copy link
Author

always when someone is behind a NAT

@jonathanong
Copy link
Member

this is what https://github.com/jshttp/proxy-addr is for, which i haven't had to implement. PR would be welcomed. #281

@thelinuxlich
Copy link
Author

So getting the last ip of ctx.ips is not enough?

@thelinuxlich
Copy link
Author

This seems to be working for returning the real ip:

const current_ip = ctx.ips.length > 0 ? ctx.ips[ctx.ips.length - 1] : ctx.ip;

@tj
Copy link
Member

tj commented Nov 19, 2015

the client's ip should be first, check out https://en.wikipedia.org/wiki/X-Forwarded-For

@thelinuxlich
Copy link
Author

I know, I want the last ip because it is returning LAN ips

@jgdev
Copy link

jgdev commented Feb 27, 2016

There's a problem using koa over nginx server, when I force X-Forwarded-For in the request.

An X-Forwarded-For header requested by client overwrites this.request.ip. This is a critical security issue.

Found the problem in line 381 of koa/lib/request.js
https://github.com/koajs/koa/blob/master/lib/request.js#L381

It's better use X-Real-IP over X-Forwarded-For.

@tj
Copy link
Member

tj commented Feb 27, 2016

real_ip_header X-Forwarded-For? supporting two sounds wonky to me, why not use the more common one? As far as security goes that's what app.proxy = true is for, if you don't trust the fields then app.proxy = false

@jgdev
Copy link

jgdev commented Feb 27, 2016

Thats because NginX appends external request's X-Forwarded-For value to final header but clears external X-Real-IP header and gets it right with far end's value.

@tj
Copy link
Member

tj commented Feb 27, 2016

hmm yea I suppose it depends how nginx is configured, though if you have X-Real-IP you might as well just clobber X-Forwarded-For to ensure it's correct and not spoofed

@fengmk2
Copy link
Member

fengmk2 commented Feb 28, 2016

If your koa app behind a nginx, you must add 'proxy_set_header X-Forwarded-For $remote_addr' to the nginx proxy conf.

@pepkin88
Copy link

I just found that you can spoof an IP address on the standard configuration of nginx. And I happen to whitelist payment servers on my webhook by an IP address.
I agree with @jgdev, this is a critical security issue and should be fixed.

@runhwguo
Copy link

runhwguo commented Sep 2, 2017

in nginx:
location / {
proxy_pass http://test; proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

in node koa:
app.proxy = true;
ip = ctx.ip;

@ProMasoud
Copy link

in nginx:
location / {
proxy_pass http://test; proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

in node koa:
app.proxy = true;
ip = ctx.ip;

for me this worked plus

ip = ctx.request.ip;

@gherciu
Copy link

gherciu commented Jul 24, 2021

I had the same problem but resolved it by using this module found on NPM request-ip

in koa it can be simply used userIp = requestIp.getClientIp(ctx.request)

The user ip is determined by the following order:

X-Client-IP
X-Forwarded-For (Header may return multiple IP addresses in the format: "client IP, proxy 1 IP, proxy 2 IP", so we take the the first one.)
CF-Connecting-IP (Cloudflare)
Fastly-Client-Ip (Fastly CDN and Firebase hosting header when forwared to a cloud function)
True-Client-Ip (Akamai and Cloudflare)
X-Real-IP (Nginx proxy/FastCGI)
X-Cluster-Client-IP (Rackspace LB, Riverbed Stingray)
X-Forwarded, Forwarded-For and Forwarded (Variations of #2)
req.connection.remoteAddress
req.socket.remoteAddress
req.connection.socket.remoteAddress
req.info.remoteAddress

If an IP address cannot be found, it will return null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants