Skip to content

Commit b673c8d

Browse files
authored
Merge branch 'main' into dependabot/github_actions/github-actions-15fc6d9b70
2 parents c053926 + 63308c7 commit b673c8d

File tree

9 files changed

+236
-18
lines changed

9 files changed

+236
-18
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ci:
22
autoupdate_schedule: monthly
33
repos:
44
- repo: https://github.com/astral-sh/ruff-pre-commit
5-
rev: v0.4.7
5+
rev: v0.5.0
66
hooks:
77
- id: ruff
88
- id: ruff-format

docs/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ Name Description De
5555
==================================== ===================================== ==========================
5656
``DEBUG_TB_ENABLED`` Enable the toolbar? ``app.debug``
5757
``DEBUG_TB_HOSTS`` Whitelist of hosts to display toolbar any host
58+
``DEBUG_TB_ROUTES_HOST`` The host to associate with toolbar ``None``
59+
routes (where its assets are served
60+
from), or the sentinel value `*` to
61+
serve from the same host as the
62+
current request (ie any host). This
63+
is only required if Flask is
64+
configured to use `host_matching`.
5865
``DEBUG_TB_INTERCEPT_REDIRECTS`` Should intercept redirects? ``True``
5966
``DEBUG_TB_PANELS`` List of module/class names of panels enable all built-in panels
6067
``DEBUG_TB_PROFILER_ENABLED`` Enable the profiler on all requests ``False``, user-enabled

requirements/build.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ pyproject-hooks==1.1.0
1414
# via build
1515
tomli==2.0.1
1616
# via build
17-
zipp==3.18.1
17+
zipp==3.19.1
1818
# via importlib-metadata

requirements/dev.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ blinker==1.8.1
1919
# flask
2020
cachetools==5.3.3
2121
# via tox
22-
certifi==2024.2.2
22+
certifi==2024.7.4
2323
# via
2424
# -r docs.txt
2525
# requests
@@ -108,7 +108,7 @@ markupsafe==2.1.5
108108
# -r typing.txt
109109
# jinja2
110110
# werkzeug
111-
mypy==1.10.0
111+
mypy==1.11.0
112112
# via -r typing.txt
113113
mypy-extensions==1.0.0
114114
# via
@@ -150,9 +150,9 @@ pygments==2.18.0
150150
# sphinx
151151
pyproject-api==1.6.1
152152
# via tox
153-
pyright==1.1.365
153+
pyright==1.1.373
154154
# via -r typing.txt
155-
pytest==8.2.1
155+
pytest==8.3.2
156156
# via
157157
# -r tests.txt
158158
# -r typing.txt
@@ -214,7 +214,7 @@ tomli==2.0.1
214214
# pyproject-api
215215
# pytest
216216
# tox
217-
tox==4.15.0
217+
tox==4.15.1
218218
# via -r dev.in
219219
types-docutils==0.21.0.20240423
220220
# via
@@ -232,7 +232,7 @@ typing-extensions==4.11.0
232232
# -r typing.txt
233233
# mypy
234234
# sqlalchemy
235-
urllib3==2.2.1
235+
urllib3==2.2.2
236236
# via
237237
# -r docs.txt
238238
# requests
@@ -245,7 +245,7 @@ werkzeug==3.0.3
245245
# -r tests.txt
246246
# -r typing.txt
247247
# flask
248-
zipp==3.18.1
248+
zipp==3.19.1
249249
# via
250250
# -r docs.txt
251251
# -r tests.txt

requirements/docs.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ alabaster==0.7.13
88
# via sphinx
99
babel==2.14.0
1010
# via sphinx
11-
certifi==2024.2.2
11+
certifi==2024.7.4
1212
# via requests
1313
charset-normalizer==3.3.2
1414
# via requests
@@ -57,7 +57,7 @@ sphinxcontrib-qthelp==1.0.3
5757
# via sphinx
5858
sphinxcontrib-serializinghtml==1.1.5
5959
# via sphinx
60-
urllib3==2.2.1
60+
urllib3==2.2.2
6161
# via requests
62-
zipp==3.18.1
62+
zipp==3.19.1
6363
# via importlib-metadata

requirements/tests.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pluggy==1.5.0
3434
# via pytest
3535
pygments==2.18.0
3636
# via -r tests.in
37-
pytest==8.2.1
37+
pytest==8.3.2
3838
# via -r tests.in
3939
sqlalchemy==2.0.29
4040
# via flask-sqlalchemy
@@ -44,5 +44,5 @@ typing-extensions==4.11.0
4444
# via sqlalchemy
4545
werkzeug==3.0.3
4646
# via flask
47-
zipp==3.18.1
47+
zipp==3.19.1
4848
# via importlib-metadata

requirements/typing.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ markupsafe==2.1.5
2828
# via
2929
# jinja2
3030
# werkzeug
31-
mypy==1.10.0
31+
mypy==1.11.0
3232
# via -r typing.in
3333
mypy-extensions==1.0.0
3434
# via mypy
@@ -38,9 +38,9 @@ packaging==24.0
3838
# via pytest
3939
pluggy==1.5.0
4040
# via pytest
41-
pyright==1.1.365
41+
pyright==1.1.373
4242
# via -r typing.in
43-
pytest==8.2.1
43+
pytest==8.3.2
4444
# via -r typing.in
4545
sqlalchemy==2.0.29
4646
# via flask-sqlalchemy
@@ -60,7 +60,7 @@ typing-extensions==4.11.0
6060
# sqlalchemy
6161
werkzeug==3.0.3
6262
# via flask
63-
zipp==3.18.1
63+
zipp==3.19.1
6464
# via importlib-metadata
6565

6666
# The following packages are considered to be unsafe in a requirements file:

src/flask_debugtoolbar/__init__.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class DebugToolbarExtension:
5959

6060
def __init__(self, app: Flask | None = None) -> None:
6161
self.app = app
62+
self.toolbar_routes_host: str | None = None
63+
6264
# Support threads running `flask.copy_current_request_context` without
6365
# poping toolbar during `teardown_request`
6466
self.debug_toolbars_var: ContextVar[dict[Request, DebugToolbar]] = ContextVar(
@@ -97,6 +99,8 @@ def init_app(self, app: Flask) -> None:
9799
"var to be set"
98100
)
99101

102+
self._validate_and_configure_toolbar_routes_host(app)
103+
100104
DebugToolbar.load_panels(app)
101105

102106
app.before_request(self.process_request)
@@ -110,6 +114,7 @@ def init_app(self, app: Flask) -> None:
110114
"/_debug_toolbar/static/<path:filename>",
111115
"_debug_toolbar.static",
112116
self.send_static_file,
117+
host=self.toolbar_routes_host,
113118
)
114119

115120
app.register_blueprint(module, url_prefix="/_debug_toolbar/views")
@@ -118,6 +123,7 @@ def _default_config(self, app: Flask) -> dict[str, t.Any]:
118123
return {
119124
"DEBUG_TB_ENABLED": app.debug,
120125
"DEBUG_TB_HOSTS": (),
126+
"DEBUG_TB_ROUTES_HOST": None,
121127
"DEBUG_TB_INTERCEPT_REDIRECTS": True,
122128
"DEBUG_TB_PANELS": (
123129
"flask_debugtoolbar.panels.versions.VersionDebugPanel",
@@ -135,6 +141,61 @@ def _default_config(self, app: Flask) -> dict[str, t.Any]:
135141
"SQLALCHEMY_RECORD_QUERIES": app.debug,
136142
}
137143

144+
def _validate_and_configure_toolbar_routes_host(self, app: Flask) -> None:
145+
toolbar_routes_host = app.config["DEBUG_TB_ROUTES_HOST"]
146+
if app.url_map.host_matching and not toolbar_routes_host:
147+
import warnings
148+
149+
warnings.warn(
150+
"Flask-DebugToolbar requires DEBUG_TB_ROUTES_HOST to be set if Flask "
151+
"is running in `host_matching` mode. Static assets for the toolbar "
152+
"will not be served correctly unless this is set.",
153+
stacklevel=1,
154+
)
155+
156+
if toolbar_routes_host:
157+
if not app.url_map.host_matching:
158+
raise ValueError(
159+
"`DEBUG_TB_ROUTES_HOST` should only be set if your Flask app is "
160+
"using `host_matching`."
161+
)
162+
163+
if toolbar_routes_host.strip() == "*":
164+
toolbar_routes_host = "<toolbar_routes_host>"
165+
elif "<" in toolbar_routes_host and ">" in toolbar_routes_host:
166+
raise ValueError(
167+
"`DEBUG_TB_ROUTES_HOST` must either be a host name with no "
168+
"variables, to serve all Flask-DebugToolbar assets from a single "
169+
"host, or `*` to match the current request's host."
170+
)
171+
172+
# Automatically inject `toolbar_routes_host` into `url_for` calls for
173+
# the toolbar's `send_static_file` method.
174+
@app.url_defaults
175+
def inject_toolbar_routes_host_if_required(
176+
endpoint: str, values: dict[str, t.Any]
177+
) -> None:
178+
if app.url_map.is_endpoint_expecting(endpoint, "toolbar_routes_host"):
179+
values.setdefault("toolbar_routes_host", request.host)
180+
181+
# Automatically strip `toolbar_routes_host` from the endpoint values so
182+
# that the `send_static_host` method doesn't receive that parameter,
183+
# as it's not actually required internally.
184+
@app.url_value_preprocessor
185+
def strip_toolbar_routes_host_from_static_endpoint(
186+
endpoint: str | None, values: dict[str, t.Any] | None
187+
) -> None:
188+
if (
189+
endpoint
190+
and values
191+
and app.url_map.is_endpoint_expecting(
192+
endpoint, "toolbar_routes_host"
193+
)
194+
):
195+
values.pop("toolbar_routes_host", None)
196+
197+
self.toolbar_routes_host = toolbar_routes_host
198+
138199
def dispatch_request(self) -> t.Any:
139200
"""Modified version of ``Flask.dispatch_request`` to call
140201
:meth:`process_view`.

0 commit comments

Comments
 (0)