gpt4 book ai didi

python - 如何在 Flask 中安全获取用户的真实 IP 地址(使用 mod_wsgi)?

转载 作者:太空狗 更新时间:2023-10-29 18:17:19 25 4
gpt4 key购买 nike

我在 mod_wsgi/Apache 上安装了一个 flask 应用程序,需要记录用户的 IP 地址。 request.remote_addr 返回“127.0.0.1”和this fix试图纠正它,但我发现 Django 出于安全原因删除了类似的代码。

有没有更好的方法可以安全获取用户的真实IP地址?

编辑:也许我遗漏了一些明显的东西。我申请了werkzeug's/Flask's fix但是当我尝试使用更改的 header 进行请求时,它似乎没有什么不同:

运行.py:

    from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
app.run()

View .py:

for ip in request.access_route:
print ip # prints "1.2.3.4" and "my.ip.address"

无论是否启用 ProxyFix,都会出现同样的结果。我觉得我错过了一些非常明显的东西

最佳答案

您可以使用 request.access_route attribute仅当您定义了一个可信代理列表。

access_route 属性使用 X-Forwarded-For header , 返回到 REMOTE_ADDR WSGI 变量;后者很好,因为您的服务器确定了这一点; X-Forwarded-For 几乎可以由任何人设置,但如果您信任代理来正确设置值,则使用第一个(从末尾开始)不是 信任:

trusted_proxies = {'127.0.0.1'}  # define your own set
route = request.access_route + [request.remote_addr]

remote_addr = next((addr for addr in reversed(route)
if addr not in trusted_proxies), request.remote_addr)

这样,即使有人用 fake_ip1,fake_ip2 欺骗了 X-Forwarded-For header ,代理服务器也会添加 ,spoof_machine_ip到最后,上面的代码会将 remote_addr 设置为 spoof_machine_ip,无论除​​了你的最外层代理之外还有多少受信任的代理。

这是您的链接文章中谈到的白名单方法(简单地说,Rails 使用它),以及什么 Zope implemented over 11 years ago .

您的 ProxyFix 方法工作得很好,但您误解了它的作用。它设置request.remote_addrrequest.access_route 属性未更改(X-Forwarded-For header 由中间件调整)。 但是,我会非常谨慎地避免盲目计算代理数。

将相同的白名单方法应用于中间件看起来像:

class WhitelistRemoteAddrFix(object):
"""This middleware can be applied to add HTTP proxy support to an
application that was not designed with HTTP proxies in mind. It
only sets `REMOTE_ADDR` from `X-Forwarded` headers.

Tests proxies against a set of trusted proxies.

The original value of `REMOTE_ADDR` is stored in the WSGI environment
as `werkzeug.whitelist_remoteaddr_fix.orig_remote_addr`.

:param app: the WSGI application
:param trusted_proxies: a set or sequence of proxy ip addresses that can be trusted.
"""

def __init__(self, app, trusted_proxies=()):
self.app = app
self.trusted_proxies = frozenset(trusted_proxies)

def get_remote_addr(self, remote_addr, forwarded_for):
"""Selects the new remote addr from the given list of ips in
X-Forwarded-For. Picks first non-trusted ip address.
"""

if remote_addr in self.trusted_proxies:
return next((ip for ip in reversed(forwarded_for)
if ip not in self.trusted_proxies),
remote_addr)

def __call__(self, environ, start_response):
getter = environ.get
remote_addr = getter('REMOTE_ADDR')
forwarded_for = getter('HTTP_X_FORWARDED_FOR', '').split(',')
environ.update({
'werkzeug.whitelist_remoteaddr_fix.orig_remote_addr': remote_addr,
})
forwarded_for = [x for x in [x.strip() for x in forwarded_for] if x]
remote_addr = self.get_remote_addr(remote_addr, forwarded_for)
if remote_addr is not None:
environ['REMOTE_ADDR'] = remote_addr
return self.app(environ, start_response)

明确地说:这个中间件也是,设置request.remote_addrrequest.access_route 不受影响。

关于python - 如何在 Flask 中安全获取用户的真实 IP 地址(使用 mod_wsgi)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22868900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com