Skip to content

Fix show_toolbar_with_docker on runtimes with unrelated host address - #2422

Merged
tim-schilling merged 7 commits into
django-commons:mainfrom
Sanjays2402:fix/docker-host-gateway-fallback
Aug 8, 2026
Merged

Fix show_toolbar_with_docker on runtimes with unrelated host address#2422
tim-schilling merged 7 commits into
django-commons:mainfrom
Sanjays2402:fix/docker-host-gateway-fallback

Conversation

@Sanjays2402

Copy link
Copy Markdown
Contributor

Description

show_toolbar_with_docker derives the Docker host address by resolving
host.docker.internal and replacing the last segment with .1. On OrbStack
that name resolves to 0.250.250.254, which is outside the container network,
so the derived address never matches REMOTE_ADDR and the toolbar stays
hidden. When that probe does not match, the host address is now derived from
the container's own addresses instead.

Fixes #2419

Checklist:

  • I have added the relevant tests for this change.
  • I have added an item to the Pending section of docs/changes.rst.

AI/LLM Usage

  • This PR includes code generated with the help of an AI/LLM

show_toolbar_with_docker derived the Docker host address by resolving
host.docker.internal and replacing the last segment with .1. Runtimes
such as OrbStack resolve that name to an address outside the container
network (0.250.250.254), so the derived address never matches
REMOTE_ADDR and the toolbar stays hidden.

Fall back to deriving the gateway from the container's own addresses
when the host.docker.internal probe does not match.

Closes django-commons#2419
@tim-schilling

Copy link
Copy Markdown
Member

I tweaked the solution to be more amenable to follow-up docker tests. If this new check fails, it shouldn't automatically return false. It ideally should fall through to the last return false statement.

@github-actions

github-actions Bot commented Jul 31, 2026

Copy link
Copy Markdown

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  debug_toolbar
  middleware.py 45, 48, 63, 77-81
Project Total  

This report was generated by python-coverage-comment-action

@tim-schilling tim-schilling left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you round out the tests to have coverage of the missing lines identified here: #2422 (comment)?

@tim-schilling

Copy link
Copy Markdown
Member

Let's also update the documentation warning around docker with a note to include something like:

When the Docker host lookup fails, the show_toolbar_with_docker callback guesses the host's address from the container's own network. If run outside a container that guess resolve to another machine on your network, granting it access to the toolbar's data. The show_toolbar_with_docker callback should only be used inside a container.

@Sanjays2402

Copy link
Copy Markdown
Contributor Author

Added that note to the Docker warning in docs/installation.rst (64ad00a), wording close to yours, just softened "resolve" to "can resolve":

When the Docker host lookup fails, the show_toolbar_with_docker callback guesses the host's address from the container's own network. If run outside a container that guess can resolve to another machine on your network, granting it access to the toolbar's data. The show_toolbar_with_docker callback should only be used inside a container.

Thanks for the fall-through rework, that reads better than the nested version I had.

@tim-schilling
tim-schilling merged commit e367e1b into django-commons:main Aug 8, 2026
28 checks passed
@tim-schilling

Copy link
Copy Markdown
Member

As I went to merge it I got cold-feet that someone could use this improperly. However, the check on DEBUG is solid, so not super worried about that, but I was a little worried someone may copy it without understand the implications. So I added an extra comment to explain what it's doing and where the risk would be if it were misused. Thank you @Sanjays2402 for this!

@alastair

Copy link
Copy Markdown

Thank you @tim-schilling and @Sanjays2402!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

show_toolbar_with_docker doesn't work on orbstack

3 participants