Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
middleware: If there are no realms yet, show proxy errors to end-user.
Alex Vandiver6219e518188c
diff --git a/templates/zerver/config_error/container.html b/templates/zerver/config_error/container.htmlindex 05aa2860984..9ebe2763877 100644--- a/templates/zerver/config_error/container.html+++ b/templates/zerver/config_error/container.html@@ -15,16 +15,18 @@<div class="white-box"><div class="errorbox config-error"><div class="errorcontent">+ {% block error_body %}<p>{% block error_content %}{% endblock %}</p>- <p>After making your changes, remember to restart- the Zulip server.</p>- <p><a href=""> Refresh</a> to try again or <a href="{{ go_back_to_url }}">go back to {{ go_back_to_url_name }}</a>.</p>- </div>+ <p>After making your changes, remember to restart the Zulip server.</p>+ <p><a href="">Refresh</a> to try again or+ <a href="{{ go_back_to_url }}">go back to {{ go_back_to_url_name }}</a>.</p>+ {% endblock %}+ </div></div></div></div>diff --git a/templates/zerver/config_error/proxy.html b/templates/zerver/config_error/proxy.htmlnew file mode 100644index 00000000000..7fab86494b3--- /dev/null+++ b/templates/zerver/config_error/proxy.html@@ -0,0 +1,90 @@+{% extends "zerver/config_error/container.html" %}+{% macro setting() -%}+ {%- if docker_config -%}+ <code>LOADBALANCER_IPS</code> in your Docker image's environment+ {%- else -%}+ <code>ips</code> in the <code>loadbalancers</code> section of <code>/etc/zulip/zulip.conf</code>+ {%- endif -%}+{%- endmacro %}++{% block error_body %}++{% if not current_proxies and not x_forwarded_for %}++<p>+ You have not configured any reverse proxies in {{ setting() }},+ and an HTTP request was received without any reverse proxy+ headers. Zulip requires that all client traffic to it be over+ HTTPS. Since you have configured Zulip itself to be served over+ HTTP, it must be placed behind a proxy which does TLS termination.+</p>++<p>+ You must configure a reverse proxy in front of Zulip which serves+ traffic over HTTPS, and configure Zulip to trust that proxy, by+ adding its IP to {{ setting() }}. See our <a href="https://zulip.readthedocs.io/en/stable/production/reverse-proxies.html"+ >documentation about deploying behind reverse proxies</a> for more+ details.+</p>++{% elif not current_proxies %}++<p>+ You have not configured any reverse proxies in {{ setting() }},+ but reverse proxy headers were detected in a request from+ <code>{{ remote_addr }}</code>.+</p>++<p>+ Add <code>{{ remote_addr }}</code> to {{ setting() }} and restart+ your Zulip server. See+ our <a href="https://zulip.readthedocs.io/en/stable/production/reverse-proxies.html"+ >documentation about deploying behind reverse proxies</a> for more+ details.+</p>++{% elif not x_forwarded_proto %}++<p>+ You have configured reverse proxies (<code>{{ current_proxies }}</code>),+ and traffic is being served through them, but the remote proxy did+ not send an <code>X-Forwarded-Proto</code> header.+</p>++<p>+ Please read our <a href="https://zulip.readthedocs.io/en/stable/production/reverse-proxies.html"+ >documentation about configuring your reverse proxy</a>, and+ configure your proxy to send an <code>X-Forwarded-Proto</code>+ header.+</p>++{% else %}++<p>+ You have configured reverse proxies (<code>{{ current_proxies }}</code>),+ but this request did not come from a matching IP address -- it+ came from <code>{{ remote_addr }}</code>.+</p>++<p>+ You should update {{ setting() }} to include <code>{{ remote_addr }}</code>,+ and restart your Zulip server. See our <a href="https://zulip.readthedocs.io/en/stable/production/reverse-proxies.html"+ >documentation about deploying behind reverse proxies</a> for more+ details.+</p>++{% endif %}++<hr />++<h2>Request headers:</h2>++<pre>+{% for item in all_headers -%}+{% if item[1] != "" -%}+{{ item[0] }}: {{ item[1] }}+{% endif -%}+{% endfor -%}+</pre>++{% endblock %}diff --git a/zerver/middleware.py b/zerver/middleware.pyindex 7d35b3922dc..b30eecbe6a5 100644--- a/zerver/middleware.py+++ b/zerver/middleware.py@@ -1,5 +1,6 @@import cProfileimport logging+import osimport tempfileimport timefrom collections.abc import Callable, MutableMapping@@ -41,11 +42,13 @@ from zerver.lib.response import (json_response_from_error,json_unauthorized,)+from zerver.lib.server_initialization import server_initializedfrom zerver.lib.subdomains import get_subdomainfrom zerver.lib.typed_endpoint import INTENTIONALLY_UNDOCUMENTED, ApiParamConfig, typed_endpointfrom zerver.lib.user_agent import parse_user_agentfrom zerver.models import Realmfrom zerver.models.realms import get_realm+from zproject.config import get_configParamT = ParamSpec("ParamT")logger = logging.getLogger("zulip.requests")@@ -650,7 +653,7 @@ class DetectProxyMisconfiguration(MiddlewareMixin):view_func: Callable[Concatenate[HttpRequest, ParamT], HttpResponseBase],args: list[object],kwargs: dict[str, Any],- ) -> None:+ ) -> HttpResponse | None:proxy_state_header = request.headers.get("X-Proxy-Misconfiguration", "")# Our nginx configuration sets this header if:# 1. the request came in over HTTP with no proxy headers set@@ -686,7 +689,21 @@ class DetectProxyMisconfiguration(MiddlewareMixin):and not request.is_secure()and request.META["REMOTE_ADDR"] not in ("127.0.0.1", "::1")):- raise ProxyMisconfigurationError(proxy_state_header)+ if server_initialized():+ raise ProxyMisconfigurationError(proxy_state_header)++ context = {+ "current_proxies": get_config("loadbalancer", "ips"),+ "x_forwarded_for": request.headers.get("X-Forwarded-For"),+ "x_forwarded_proto": request.headers.get("X-Forwarded-Proto"),+ "remote_addr": request.META["REMOTE_ADDR"],+ "docker_config": settings.RUNNING_IN_DOCKER+ and os.environ.get("MANUAL_CONFIGURATION") != "True",+ "all_headers": list(request.headers.items()),+ "env": os.environ,+ }+ return render(request, "zerver/config_error/proxy.html", status=500, context=context)+ return Nonedef validate_scim_bearer_token(request: HttpRequest) -> bool: