How to securely get the IP address of a user in Flask with a proxy server?

I am using Flask and should get the IP address of the user. This is usually done using request.remote_addr, but since this application is hosted on a third party (and uses cloudflare), it simply returns localhost.

Flask suggests getting an X-Forwarded-Host , but then they immediately say that it is a security risk. Is there a safe way to get the real IP address of the client?

+3
source share
1 answer

Problem

, ProxyFix , , ProxyFix , - , .

, ProxyFix, REMOTE_ADDR, , IP- TCP-. , IP- TCP- . , IP- , REMOTE_ADDR - ; , , , request.remote_addr.

, , -, TCP- ; TCP- -, - TCP- -. , request.remote_addr IP- , .

, ProxyFix , request.remote_addr IP- , . , HTTP-, (, Apache Nginx) HTTP- (X-Forwarded-For) IP- , . , Cloudflare HTTP-, ProxyFix, , ; , request.remote_addr IP- . "ProxyFix".

, , . , , TCP , HTTP ; - , , , HTTP-. , IP- HTTP- IP- ! IP- , . IP- HTML, SQL Javascript , SQL XSS.

, ; ProxyFix IP- TCP- - HTTP-.

ProxyFix : , , . , (, Nginx Apache) , ProxyFix .

, , ProxyFix ( ). , ProxyFix, , num_proxies.

, A, Cloudflare (B), ( C). Cloudflare IP- A CF-Connecting-IP header.

, IP- C, HTTP- C, , . ProxyFix , IP- HTTP-, , , , , , , .

, - mod_cloudflare, - Apache mod, , HTTP- IP- Cloudflare ( TCP IP). Cloudflare. . IP- , (, Nginx).

. , "": , , IP- . TCP IP- Cloudflare, . , , DDOS , Cloudflare. , , , . , , .

+6

All Articles