2019-03-21 16:41:22 +00:00
|
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
2015-11-30 14:32:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class CustomProxyFix(object):
|
|
|
|
|
def __init__(self, app, forwarded_proto):
|
Update call to proxy fix to use new method signature
Old method:
```python
ProxyFix(app, num_proxies=1)
```
https://werkzeug.palletsprojects.com/en/0.14.x/contrib/fixers/#werkzeug.contrib.fixers.ProxyFix
This uses forwarded values for `REMOTE_ADDR` and `HTTP_HOST`.
New method:
```python
ProxyFix(app, num_proxies=None, x_for=1, x_proto=0, x_host=0, x_port=0, x_prefix=0)
```
https://werkzeug.palletsprojects.com/en/0.15.x/middleware/proxy_fix/#module-werkzeug.middleware.proxy_fix
Setting `x_for=1` preserves the same behaviour as `num_proxies=1`.
Setting `x_proto=1` and `x_host=1` will cause `REMOTE_ADDR`,
`HTTP_HOST`, `SERVER_NAME` and `SERVER_PORT` to be forwarded.
So we will be forwarding `SERVER_NAME` and `SERVER_PORT` which we
weren’t before, but we think this is OK.
2019-03-25 10:52:21 +00:00
|
|
|
self.app = ProxyFix(app, x_for=1, x_proto=1, x_host=1, x_port=0, x_prefix=0)
|
2015-11-30 14:32:58 +00:00
|
|
|
self.forwarded_proto = forwarded_proto
|
|
|
|
|
|
|
|
|
|
def __call__(self, environ, start_response):
|
2023-08-25 09:12:23 -07:00
|
|
|
environ.update({"HTTP_X_FORWARDED_PROTO": self.forwarded_proto})
|
2015-11-30 14:32:58 +00:00
|
|
|
return self.app(environ, start_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_app(app):
|
2023-08-25 09:12:23 -07:00
|
|
|
app.wsgi_app = CustomProxyFix(app.wsgi_app, app.config.get("HTTP_PROTOCOL", "http"))
|