Skip to content

Commit

Permalink
Merge branch 'main' into improve-e001
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-schilling authored Jul 3, 2024
2 parents 385262b + c54f898 commit 11ef8f5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repos:
hooks:
- id: doc8
- repo: https://github.com/adamchainz/django-upgrade
rev: 1.17.0
rev: 1.19.0
hooks:
- id: django-upgrade
args: [--target-version, "4.2"]
Expand All @@ -32,7 +32,7 @@ repos:
args:
- --trailing-comma=es5
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v9.3.0
rev: v9.6.0
hooks:
- id: eslint
additional_dependencies:
Expand All @@ -44,7 +44,7 @@ repos:
args:
- --fix
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.4.5'
rev: 'v0.5.0'
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
15 changes: 12 additions & 3 deletions debug_toolbar/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ def show_toolbar(request):
"""
Default function to determine whether to show the toolbar on a given page.
"""
internal_ips = list(settings.INTERNAL_IPS)
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 @@ -31,11 +37,14 @@ def show_toolbar(request):
".".join(socket.gethostbyname("host.docker.internal").rsplit(".")[:-1])
+ ".1"
)
internal_ips.append(docker_ip)
if request.META.get("REMOTE_ADDR") == docker_ip:
return True
except socket.gaierror:
# It's fine if the lookup errored since they may not be using docker
pass
return settings.DEBUG and request.META.get("REMOTE_ADDR") in internal_ips

# No test passed
return False


@lru_cache(maxsize=None)
Expand Down
6 changes: 4 additions & 2 deletions debug_toolbar/static/debug_toolbar/css/toolbar.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* Variable definitions */
:root,
#djDebug[data-theme="light"] {
:root {
/* Font families are the same as in Django admin/css/base.css */
--djdt-font-family-primary: "Segoe UI", system-ui, Roboto, "Helvetica Neue",
Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji",
Expand All @@ -10,7 +9,10 @@
"Source Code Pro", "Fira Mono", "Droid Sans Mono", "Courier New",
monospace, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol",
"Noto Color Emoji";
}

:root,
#djDebug[data-theme="light"] {
--djdt-font-color: black;
--djdt-background-color: white;
--djdt-panel-content-background-color: #eee;
Expand Down
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Change log
Pending
-------

* Fixed overriding font-family for both light and dark themes.
* Restored compatibility with ``iptools.IpRangeList``.
* Limit ``E001`` check to likely error cases when the
``SHOW_TOOLBAR_CALLBACK`` has changed, but the toolbar's URL
paths aren't installed.
Expand Down
22 changes: 22 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ def test_show_toolbar_docker(self, mocked_gethostbyname):
self.assertTrue(show_toolbar(self.request))
mocked_gethostbyname.assert_called_once_with("host.docker.internal")

def test_not_iterating_over_INTERNAL_IPS(self):
"""Verify that the middleware does not iterate over INTERNAL_IPS in some way.
Some people use iptools.IpRangeList for their INTERNAL_IPS. This is a class
that can quickly answer the question if the setting contain a certain IP address,
but iterating over this object will drain all performance / blow up.
"""

class FailOnIteration:
def __iter__(self):
raise RuntimeError(
"The testcase failed: the code should not have iterated over INTERNAL_IPS"
)

def __contains__(self, x):
return True

with self.settings(INTERNAL_IPS=FailOnIteration()):
response = self.client.get("/regular/basic/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, "djDebug") # toolbar

def test_should_render_panels_RENDER_PANELS(self):
"""
The toolbar should force rendering panels on each request
Expand Down

0 comments on commit 11ef8f5

Please sign in to comment.