Skip to content
Prev Previous commit
Next Next commit
Reworked show_toolbar_with_docker to support falling through
  • Loading branch information
tim-schilling authored Jul 31, 2026
commit f5589ea66b6bc3ba52d1eda69a3b47669d301410
13 changes: 6 additions & 7 deletions debug_toolbar/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ def show_toolbar_with_docker(request: HttpRequest) -> bool:
if not settings.DEBUG:
return False

# Test: settings
if request.META.get("REMOTE_ADDR") in settings.INTERNAL_IPS:
return True

# Test: Docker
try:
# This is a hack for docker installations. It attempts to look
# up the IP address of the docker host.
Expand All @@ -68,18 +66,19 @@ def show_toolbar_with_docker(request: HttpRequest) -> bool:
# It's fine if the lookup errored since they may not be using docker
pass

# Test: Docker, falling back to the gateway of the container's own network.
# Some Docker runtimes (for example OrbStack) resolve host.docker.internal
# to an address that is unrelated to the container network, so the check
# above cannot derive the host address from it.
try:
container_ips = socket.gethostbyname_ex(socket.gethostname())[2]
gateways = {ip.rsplit(".", 1)[0] + ".1" for ip in container_ips}
if request.META.get("REMOTE_ADDR") in gateways:
return True
except socket.gaierror:
# It's fine if the lookup errored since they may not be using docker
return False

gateways = {ip.rsplit(".", 1)[0] + ".1" for ip in container_ips}
return request.META.get("REMOTE_ADDR") in gateways
pass

return False


@cache
Expand Down