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
Provenance
- Commit
7b21d43- Tree
33c2a44- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
10bdf61- Parent 2
4f7156f
4 files changed+40 added−0 removed
modified:docs/config.rst+15 added
256 unmodified lines257 preferable.258 259 Default: ``None``260 261+.. py:data:: TRUSTED_HOSTS262+263+ 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.268+269+ Validation is done during routing against this value. ``before_request`` and270+ ``after_request`` callbacks will still be called.271+272+ Default: ``None``273+274+ .. versionadded:: 3.1275+276 .. py:data:: SERVER_NAME277 278 Inform the application what host and port it is bound to.279 557 unmodified linesmodified:src/flask/app.py+7 added
22 unmodified lines23 from werkzeug.routing import RoutingException24 from werkzeug.routing import Rule25 from werkzeug.serving import is_running_from_reloader26 from werkzeug.wrappers import Response as BaseResponse27+from werkzeug.wsgi import get_host28 29 from . import cli30 from . import typing as ft31 from .ctx import AppContext151 unmodified lines183 "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 lines442 443 .. 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_hosts448+449+ # 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"]453 454 if self.url_map.host_matching:1082 unmodified linesmodified:tests/test_request.py+16 added
50 unmodified lines51 with app.app_context():52 assert r.max_content_length == 9053 assert r.max_form_memory_size == 3054 assert r.max_form_parts == 455+56+57+def test_trusted_hosts_config(app: Flask) -> None:58+ app.config["TRUSTED_HOSTS"] = ["example.test", ".other.test"]59+60+ @app.get("/")61+ def index() -> str:62+ return ""63+64+ 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 == 400modified:CHANGES.rst+2 added
24 unmodified lines25 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`31 32 33 Version 3.0.334 -------------1563 unmodified lines