Skip to content

Simplify Docker IP determination in middleware.py #2098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions debug_toolbar/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ def show_toolbar(request):
# This is a hack for docker installations. It attempts to look
# up the IP address of the docker host.
# This is not guaranteed to work.
docker_ip = (
# Convert the last segment of the IP address to be .1
".".join(socket.gethostbyname("host.docker.internal").rsplit(".")[:-1])
+ ".1"
)
docker_ip = socket.gethostbyname("gateway.docker.internal")
if request.META.get("REMOTE_ADDR") == docker_ip:
return True
except socket.gaierror:
Expand Down
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Pending
* Added a Makefile target (``make help``) to get a quick overview
of each target.
* Avoided reinitializing the staticfiles storage during instrumentation.
* Simplify Docker IP determination by using gateway IP.

5.0.1 (2025-01-13)
------------------
Expand Down
7 changes: 3 additions & 4 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@ def test_show_toolbar_INTERNAL_IPS(self):
with self.settings(INTERNAL_IPS=[]):
self.assertFalse(show_toolbar(self.request))

@patch("socket.gethostbyname", return_value="127.0.0.255")
@patch("socket.gethostbyname", return_value="192.168.65.1")
def test_show_toolbar_docker(self, mocked_gethostbyname):
with self.settings(INTERNAL_IPS=[]):
# Is true because REMOTE_ADDR is 127.0.0.1 and the 255
# is shifted to be 1.
self.request.META["REMOTE_ADDR"] = "192.168.65.1"
self.assertTrue(show_toolbar(self.request))
mocked_gethostbyname.assert_called_once_with("host.docker.internal")
mocked_gethostbyname.assert_called_once_with("gateway.docker.internal")

def test_not_iterating_over_INTERNAL_IPS(self):
"""Verify that the middleware does not iterate over INTERNAL_IPS in some way.
Expand Down
7 changes: 3 additions & 4 deletions tests/test_integration_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ async def test_show_toolbar_INTERNAL_IPS(self):
with self.settings(INTERNAL_IPS=[]):
self.assertFalse(show_toolbar(self.request))

@patch("socket.gethostbyname", return_value="127.0.0.255")
@patch("socket.gethostbyname", return_value="192.168.65.1")
async def test_show_toolbar_docker(self, mocked_gethostbyname):
with self.settings(INTERNAL_IPS=[]):
# Is true because REMOTE_ADDR is 127.0.0.1 and the 255
# is shifted to be 1.
self.request.META["REMOTE_ADDR"] = "192.168.65.1"
self.assertTrue(show_toolbar(self.request))
mocked_gethostbyname.assert_called_once_with("host.docker.internal")
mocked_gethostbyname.assert_called_once_with("gateway.docker.internal")

async def test_not_iterating_over_INTERNAL_IPS(self):
"""
Expand Down