Skip to content
Myr

fix subdomain_matching=False behavior

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

authored by David Lord · committed by David Lord · parent 07c7d57 · 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

5 files+98 added−43 removed

modified:docs/config.rst+13 added-4 removed
258 unmodified lines
259 Default: ``None``260261.. py:data:: SERVER_NAME262263 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.264266 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.267268 Must be set for ``url_for`` to generate external URLs outside of a269 request context.270271 Default: ``None``272273 .. versionchanged:: 3.1274 Does not restrict requests to only this domain, for both275 ``subdomain_matching`` and ``host_matching``.276277 .. versionchanged:: 1.0278 Does not implicitly enable ``subdomain_matching``.279280 .. versionchanged:: 2.3281 Does not affect ``SESSION_COOKIE_DOMAIN``.282283.. py:data:: APPLICATION_ROOT
538 unmodified lines
modified:src/flask/app.py+25 added-17 removed
423 unmodified lines
424 """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.427428 .. 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``.432430 .. versionchanged:: 0.9431 This can now also be called without a request object when the432 URL adapter is created for the application context.433433 .. versionchanged:: 1.0434 :data:`SERVER_NAME` no longer implicitly enables subdomain435 matching. Use :attr:`subdomain_matching` instead.436437 .. versionchanged:: 0.9438 This can be called outside a request when the URL adapter is created439 for an application context.440441 .. 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"]446447 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 ""456457 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.460461 # 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 lines
modified:tests/test_basic.py+43 added
2 unmodified lines
3import typing as t4import uuid5import warnings6import weakref7from contextlib import nullcontext8from datetime import datetime9from datetime import timezone10from platform import python_implementation11
1471 unmodified lines
1483 assert repr(flask.g) == "<LocalProxy unbound>"1484 assert not flask.g148514861487@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)1495def 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"15091510 @app.route("/", defaults={"name": "default"}, host="<name>")1511 @app.route("/", subdomain="<name>", host="<name>.example.test")1512 def index(name: str) -> str:1513 return name15141515 client = app.test_client()15161517 r = client.get(base_url="http://example.test")1518 assert r.text == expect_base15191520 r = client.get(base_url="http://abc.example.test")1521 assert r.text == expect_abc15221523 with pytest.warns() if subdomain_matching else nullcontext():1524 r = client.get(base_url="http://xyz.other.test")15251526 assert r.text == expect_xyz152715281529def test_server_name_subdomain():1530 app = flask.Flask(__name__, subdomain_matching=True)1531 client = app.test_client()1532
405 unmodified lines
modified:tests/test_blueprints.py+14 added-22 removed
949 unmodified lines
950 assert response.status_code == 200951952953def 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 = True957958 parent = flask.Blueprint("parent", __name__)959 child = flask.Blueprint("child", __name__)960961 @child.route("/child/")962 def index():963 return "child"964965 parent.register_blueprint(child)963 app.register_blueprint(parent, subdomain=subdomain)966 app.register_blueprint(parent, subdomain="api")967965 client.allow_subdomain_redirects = True966967 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")970969 assert response.status_code == 200970971972def 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 = True976977 parent = flask.Blueprint("parent", __name__)978 child = flask.Blueprint("child", __name__, subdomain=child_subdomain)978 child = flask.Blueprint("child", __name__, subdomain="api")979980 @child.route("/")981 def index():982 return "child"983984 parent.register_blueprint(child)985 app.register_blueprint(parent, subdomain=parent_subdomain)985 app.register_blueprint(parent, subdomain="parent")986987 client.allow_subdomain_redirects = True988989 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 )994987 response = client.get("/", base_url="http://api.parent.example.test")988 assert response.status_code == 200989997 response = client.get("/", base_url=f"http://{parent_subdomain}.{domain_name}")998990 response = client.get("/", base_url="http://parent.example.test")991 assert response.status_code == 404992993994def test_unique_blueprint_names(app, client) -> None:
52 unmodified lines
modified:CHANGES.rst+3 added
21 unmodified lines
22 ``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`293031Version 3.0.332-------------
1563 unmodified lines