Skip to content
Myr

configure and check `request.trusted_hosts` (#5637)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by GitHub · parents 10bdf61, 4f7156f · Browse files · 4 files+40 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+40 added−0 removed

  • tests:+16 added−0 removed
  • other:+24 added−0 removed

4 files+40 added−0 removed

modified:docs/config.rst+15 added
256 unmodified lines
257 preferable.258259 Default: ``None``260261.. py:data:: TRUSTED_HOSTS262263 Validate :attr:`.Request.host` and other attributes that use it against264 these trusted values. Raise a :exc:`~werkzeug.exceptions.SecurityError` if265 the host is invalid, which results in a 400 error. If it is ``None``, all266 hosts are valid. Each value is either an exact match, or can start with267 a dot ``.`` to match any subdomain.268269 Validation is done during routing against this value. ``before_request`` and270 ``after_request`` callbacks will still be called.271272 Default: ``None``273274 .. versionadded:: 3.1275276.. py:data:: SERVER_NAME277278 Inform the application what host and port it is bound to.279
557 unmodified lines
modified:src/flask/app.py+7 added
22 unmodified lines
23from werkzeug.routing import RoutingException24from werkzeug.routing import Rule25from werkzeug.serving import is_running_from_reloader26from werkzeug.wrappers import Response as BaseResponse27from werkzeug.wsgi import get_host2829from . import cli30from . import typing as ft31from .ctx import AppContext
151 unmodified lines
183 "SECRET_KEY": None,184 "SECRET_KEY_FALLBACKS": None,185 "PERMANENT_SESSION_LIFETIME": timedelta(days=31),186 "USE_X_SENDFILE": False,187 "TRUSTED_HOSTS": None,188 "SERVER_NAME": None,189 "APPLICATION_ROOT": "/",190 "SESSION_COOKIE_NAME": "session",191 "SESSION_COOKIE_DOMAIN": None,
250 unmodified lines
442443 .. versionadded:: 0.6444 """445 if request is not None:446 if (trusted_hosts := self.config["TRUSTED_HOSTS"]) is not None:447 request.trusted_hosts = trusted_hosts448449 # Check trusted_hosts here until bind_to_environ does.450 request.host = get_host(request.environ, request.trusted_hosts) # pyright: ignore451 subdomain = None452 server_name = self.config["SERVER_NAME"]453454 if self.url_map.host_matching:
1082 unmodified lines
modified:tests/test_request.py+16 added
50 unmodified lines
51 with app.app_context():52 assert r.max_content_length == 9053 assert r.max_form_memory_size == 3054 assert r.max_form_parts == 4555657def test_trusted_hosts_config(app: Flask) -> None:58 app.config["TRUSTED_HOSTS"] = ["example.test", ".other.test"]5960 @app.get("/")61 def index() -> str:62 return ""6364 client = app.test_client()65 r = client.get(base_url="http://example.test")66 assert r.status_code == 20067 r = client.get(base_url="http://a.other.test")68 assert r.status_code == 20069 r = client.get(base_url="http://bad.test")70 assert r.status_code == 400
modified:CHANGES.rst+2 added
24 unmodified lines
25 add support. :issue:`5621`26- Fix how setting ``host_matching=True`` or ``subdomain_matching=False``27 interacts with ``SERVER_NAME``. Setting ``SERVER_NAME`` no longer restricts28 requests to only that domain. :issue:`5553`29- ``Request.trusted_hosts`` is checked during routing, and can be set through30 the ``TRUSTED_HOSTS`` config. :issue:`5636`313233Version 3.0.334-------------
1563 unmodified lines