setting `SERVER_NAME` does not restrict routing for both `subdomain_matching` and `host_matching` (#5634)
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 07c7d57, 4995a77 · Browse files · 5 files·+98 added−43 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+98 added−43 removed
- tests:+57 added−22 removed
- other:+41 added−21 removed
Provenance
- Commit
10bdf61- Tree
b628cf2- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
07c7d57- Parent 2
4995a77
5 files changed+98 added−43 removed
modified:docs/config.rst+13 added-4 removed
258 unmodified lines259 Default: ``None``260 261 .. py:data:: SERVER_NAME262 263- Inform the application what host and port it is bound to. Required264- for subdomain route matching support.263+ Inform the application what host and port it is bound to.264 266- If set, ``url_for`` can generate external URLs with only an application267- context instead of a request context.265+ Must be set if ``subdomain_matching`` is enabled, to be able to extract the266+ subdomain from the request.267 268+ Must be set for ``url_for`` to generate external URLs outside of a269+ request context.270+271 Default: ``None``272 273+ .. versionchanged:: 3.1274+ Does not restrict requests to only this domain, for both275+ ``subdomain_matching`` and ``host_matching``.276+277+ .. versionchanged:: 1.0278+ Does not implicitly enable ``subdomain_matching``.279+280 .. versionchanged:: 2.3281 Does not affect ``SESSION_COOKIE_DOMAIN``.282 283 .. py:data:: APPLICATION_ROOT538 unmodified linesmodified:src/flask/app.py+25 added-17 removed
423 unmodified lines424 """Creates a URL adapter for the given request. The URL adapter425 is created at a point where the request context is not yet set426 up so the request is passed explicitly.427 428- .. versionadded:: 0.6428+ .. versionchanged:: 3.1429+ If :data:`SERVER_NAME` is set, it does not restrict requests to430+ only that domain, for both ``subdomain_matching`` and431+ ``host_matching``.432 430- .. versionchanged:: 0.9431- This can now also be called without a request object when the432- URL adapter is created for the application context.433-433 .. versionchanged:: 1.0434 :data:`SERVER_NAME` no longer implicitly enables subdomain435 matching. Use :attr:`subdomain_matching` instead.436+437+ .. versionchanged:: 0.9438+ This can be called outside a request when the URL adapter is created439+ for an application context.440+441+ .. versionadded:: 0.6442 """443 if request is not None:439- # If subdomain matching is disabled (the default), use the440- # default subdomain in all cases. This should be the default441- # in Werkzeug but it currently does not have that feature.442- if not self.subdomain_matching:443- subdomain = self.url_map.default_subdomain or None444- else:445- subdomain = None444+ subdomain = None445+ server_name = self.config["SERVER_NAME"]446 447+ if self.url_map.host_matching:448+ # Don't pass SERVER_NAME, otherwise it's used and the actual449+ # host is ignored, which breaks host matching.450+ server_name = None451+ elif not self.subdomain_matching:452+ # Werkzeug doesn't implement subdomain matching yet. Until then,453+ # disable it by forcing the current subdomain to the default, or454+ # the empty string.455+ subdomain = self.url_map.default_subdomain or ""456+457 return self.url_map.bind_to_environ(448- request.environ,449- server_name=self.config["SERVER_NAME"],450- subdomain=subdomain,458+ request.environ, server_name=server_name, subdomain=subdomain459 )452- # We need at the very least the server name to be set for this453- # to work.460+461+ # Need at least SERVER_NAME to match/build outside a request.462 if self.config["SERVER_NAME"] is not None:463 return self.url_map.bind(464 self.config["SERVER_NAME"],465 script_name=self.config["APPLICATION_ROOT"],1064 unmodified linesmodified:tests/test_basic.py+43 added
2 unmodified lines3 import typing as t4 import uuid5 import warnings6 import weakref7+from contextlib import nullcontext8 from datetime import datetime9 from datetime import timezone10 from platform import python_implementation11 1471 unmodified lines1483 assert repr(flask.g) == "<LocalProxy unbound>"1484 assert not flask.g1485 1486 1487+@pytest.mark.parametrize(1488+ ("subdomain_matching", "host_matching", "expect_base", "expect_abc", "expect_xyz"),1489+ [1490+ (False, False, "default", "default", "default"),1491+ (True, False, "default", "abc", "<invalid>"),1492+ (False, True, "default", "abc", "default"),1493+ ],1494+)1495+def test_server_name_matching(1496+ subdomain_matching: bool,1497+ host_matching: bool,1498+ expect_base: str,1499+ expect_abc: str,1500+ expect_xyz: str,1501+) -> None:1502+ app = flask.Flask(1503+ __name__,1504+ subdomain_matching=subdomain_matching,1505+ host_matching=host_matching,1506+ static_host="example.test" if host_matching else None,1507+ )1508+ app.config["SERVER_NAME"] = "example.test"1509+1510+ @app.route("/", defaults={"name": "default"}, host="<name>")1511+ @app.route("/", subdomain="<name>", host="<name>.example.test")1512+ def index(name: str) -> str:1513+ return name1514+1515+ client = app.test_client()1516+1517+ r = client.get(base_url="http://example.test")1518+ assert r.text == expect_base1519+1520+ r = client.get(base_url="http://abc.example.test")1521+ assert r.text == expect_abc1522+1523+ with pytest.warns() if subdomain_matching else nullcontext():1524+ r = client.get(base_url="http://xyz.other.test")1525+1526+ assert r.text == expect_xyz1527+1528+1529 def test_server_name_subdomain():1530 app = flask.Flask(__name__, subdomain_matching=True)1531 client = app.test_client()1532 405 unmodified linesmodified:tests/test_blueprints.py+14 added-22 removed
949 unmodified lines950 assert response.status_code == 200951 952 953 def test_nesting_subdomains(app, client) -> None:954- subdomain = "api"954+ app.subdomain_matching = True955+ app.config["SERVER_NAME"] = "example.test"956+ client.allow_subdomain_redirects = True957+958 parent = flask.Blueprint("parent", __name__)959 child = flask.Blueprint("child", __name__)960 961 @child.route("/child/")962 def index():963 return "child"964 965 parent.register_blueprint(child)963- app.register_blueprint(parent, subdomain=subdomain)966+ app.register_blueprint(parent, subdomain="api")967 965- client.allow_subdomain_redirects = True966-967- domain_name = "domain.tld"968- app.config["SERVER_NAME"] = domain_name969- response = client.get("/child/", base_url="http://api." + domain_name)968+ response = client.get("/child/", base_url="http://api.example.test")970-969 assert response.status_code == 200970 971 972 def test_child_and_parent_subdomain(app, client) -> None:975- child_subdomain = "api"976- parent_subdomain = "parent"973+ app.subdomain_matching = True974+ app.config["SERVER_NAME"] = "example.test"975+ client.allow_subdomain_redirects = True976+977 parent = flask.Blueprint("parent", __name__)978- child = flask.Blueprint("child", __name__, subdomain=child_subdomain)978+ child = flask.Blueprint("child", __name__, subdomain="api")979 980 @child.route("/")981 def index():982 return "child"983 984 parent.register_blueprint(child)985- app.register_blueprint(parent, subdomain=parent_subdomain)985+ app.register_blueprint(parent, subdomain="parent")986 987- client.allow_subdomain_redirects = True988-989- domain_name = "domain.tld"990- app.config["SERVER_NAME"] = domain_name991- response = client.get(992- "/", base_url=f"http://{child_subdomain}.{parent_subdomain}.{domain_name}"993- )994-987+ response = client.get("/", base_url="http://api.parent.example.test")988 assert response.status_code == 200989 997- response = client.get("/", base_url=f"http://{parent_subdomain}.{domain_name}")998-990+ response = client.get("/", base_url="http://parent.example.test")991 assert response.status_code == 404992 993 994 def test_unique_blueprint_names(app, client) -> None:52 unmodified linesmodified:CHANGES.rst+3 added
21 unmodified lines22 ``load_defaults=False`` is passed. :issue:`5628`23 - Support key rotation with the ``SECRET_KEY_FALLBACKS`` config, a list of old24 secret keys that can still be used for unsigning. Extensions will need to25 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 30 31 Version 3.0.332 -------------1563 unmodified lines