Merge pull request #2635 from pallets/feature/server-name-routing
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Require opt-in for subdomain matching
authored by David Lord · committed by GitHub · parents 4a7db66, 82f0d12 · Browse files · 5 files·+82 added−25 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+82 added−25 removed
- tests:+37 added−6 removed
- other:+45 added−19 removed
Provenance
- Commit
f808c20- Tree
40c32f7- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
4a7db66- Parent 2
82f0d12
5 files changed+82 added−25 removed
modified:docs/config.rst+8 added-7 removed
179 unmodified lines180 181 .. py:data:: SESSION_COOKIE_DOMAIN182 183 The domain match rule that the session cookie will be valid for. If not184- set, the cookie will be valid for all subdomains of ``SERVER_NAME``. If185- ``False``, the cookie's domain will not be set.184+ set, the cookie will be valid for all subdomains of :data:`SERVER_NAME`.185+ If ``False``, the cookie's domain will not be set.186 187 Default: ``None``188 189 .. py:data:: SESSION_COOKIE_PATH66 unmodified lines256 Default: ``timedelta(hours=12)`` (``43200`` seconds)257 258 .. py:data:: SERVER_NAME259 260- Inform the application what host and port it is bound to. Required for261- subdomain route matching support.260+ Inform the application what host and port it is bound to. Required261+ for subdomain route matching support.262 263 If set, will be used for the session cookie domain if264- ``SESSION_COOKIE_DOMAIN`` is not set. Modern web browsers will not allow265- setting cookies for domains without a dot. To use a domain locally,266- add any names that should route to the app to your ``hosts`` file. ::264+ :data:`SESSION_COOKIE_DOMAIN` is not set. Modern web browsers will265+ not allow setting cookies for domains without a dot. To use a domain266+ locally, add any names that should route to the app to your267+ ``hosts`` file. ::268 269 127.0.0.1 localhost.dev270 271 If set, ``url_for`` can generate external URLs with only an application379 unmodified linesmodified:flask/app.py+32 added-12 removed
121 unmodified lines122 123 .. versionadded:: 0.11124 The `root_path` parameter was added.125 126- .. versionadded:: 0.13127- The `host_matching` and `static_host` parameters were added.126+ .. versionadded:: 1.0127+ The ``host_matching`` and ``static_host`` parameters were added.128 129+ .. versionadded:: 1.0130+ The ``subdomain_matching`` parameter was added. Subdomain131+ matching needs to be enabled manually now. Setting132+ :data:`SERVER_NAME` does not implicitly enable it.133+134 :param import_name: the name of the application package135 :param static_url_path: can be used to specify a different path for the136 static files on the web. Defaults to the name137 of the `static_folder` folder.138 :param static_folder: the folder with static files that should be served139 at `static_url_path`. Defaults to the ``'static'``140 folder in the root path of the application.136- :param host_matching: sets the app's ``url_map.host_matching`` to the given137- value. Defaults to False.138- :param static_host: the host to use when adding the static route. Defaults139- to None. Required when using ``host_matching=True``140- with a ``static_folder`` configured.141+ :param static_host: the host to use when adding the static route.142+ Defaults to None. Required when using ``host_matching=True``143+ with a ``static_folder`` configured.144+ :param host_matching: set ``url_map.host_matching`` attribute.145+ Defaults to False.146+ :param subdomain_matching: consider the subdomain relative to147+ :data:`SERVER_NAME` when matching routes. Defaults to False.148 :param template_folder: the folder that contains the templates that should149 be used by the application. Defaults to150 ``'templates'`` folder in the root path of the151 application.201 unmodified lines353 static_url_path=None,354 static_folder='static',355 static_host=None,356 host_matching=False,357+ subdomain_matching=False,358 template_folder='templates',359 instance_path=None,360 instance_relative_config=False,361 root_path=None175 unmodified lines537 #: app.url_map.converters['list'] = ListConverter538 self.url_map = Map()539 540 self.url_map.host_matching = host_matching541+ self.subdomain_matching = subdomain_matching542 543 # tracks internally if the application already handled at least one544 # request.545 self._got_first_request = False1439 unmodified lines1985 1986 return rv1987 1988 def create_url_adapter(self, request):1980- """Creates a URL adapter for the given request. The URL adapter1981- is created at a point where the request context is not yet set up1982- so the request is passed explicitly.1989+ """Creates a URL adapter for the given request. The URL adapter1990+ is created at a point where the request context is not yet set1991+ up so the request is passed explicitly.1992 1993 .. versionadded:: 0.61994 1995 .. versionchanged:: 0.91996 This can now also be called without a request object when the1997 URL adapter is created for the application context.1998+1999+ .. versionchanged:: 1.02000+ :data:`SERVER_NAME` no longer implicitly enables subdomain2001+ matching. Use :attr:`subdomain_matching` instead.2002 """2003 if request is not None:2004+ # If subdomain matching is disabled (the default), use the2005+ # default subdomain in all cases. This should be the default2006+ # in Werkzeug but it currently does not have that feature.2007+ subdomain = ((self.url_map.default_subdomain or None)2008+ if not self.subdomain_matching else None)2009+ return self.url_map.bind_to_environ(1991- return self.url_map.bind_to_environ(request.environ,1992- server_name=self.config['SERVER_NAME'])2010+ request.environ,2011+ server_name=self.config['SERVER_NAME'],2012+ subdomain=subdomain)2013 # We need at the very least the server name to be set for this2014 # to work.2015 if self.config['SERVER_NAME'] is not None:2016 return self.url_map.bind(243 unmodified linesmodified:tests/test_basic.py+31 added-4 removed
1427 unmodified lines1428 assert repr(flask.g) == '<LocalProxy unbound>'1429 assert not flask.g1430 1431 1432-def test_test_app_proper_environ(app, client):1432+def test_test_app_proper_environ():1433+ app = flask.Flask(__name__, subdomain_matching=True)1434 app.config.update(1435 SERVER_NAME='localhost.localdomain:5000'1436 )1437+ client = app.test_client()1438 1439 @app.route('/')1440 def index():1441 return 'Foo'342 unmodified lines1784 assert 'foos' not in flask.g1785 assert sorted(flask.g) == ['bar', 'foo']1786 1787 1786-def test_subdomain_basic_support(app, client):1788+def test_subdomain_basic_support():1789+ app = flask.Flask(__name__, subdomain_matching=True)1790 app.config['SERVER_NAME'] = 'localhost.localdomain'1791+ client = app.test_client()1792 1793 @app.route('/')1794 def normal_index():1795 return 'normal index'8 unmodified lines1804 rv = client.get('/', 'http://test.localhost.localdomain/')1805 assert rv.data == b'test index'1806 1807 1804-def test_subdomain_matching(app, client):1808+def test_subdomain_matching():1809+ app = flask.Flask(__name__, subdomain_matching=True)1810+ client = app.test_client()1811 app.config['SERVER_NAME'] = 'localhost.localdomain'1812 1813 @app.route('/', subdomain='<user>')1814 def index(user):2 unmodified lines1817 rv = client.get('/', 'http://mitsuhiko.localhost.localdomain/')1818 assert rv.data == b'index for mitsuhiko'1819 1820 1815-def test_subdomain_matching_with_ports(app, client):1821+def test_subdomain_matching_with_ports():1822+ app = flask.Flask(__name__, subdomain_matching=True)1823 app.config['SERVER_NAME'] = 'localhost.localdomain:3000'1824+ client = app.test_client()1825 1826 @app.route('/', subdomain='<user>')1827 def index(user):1828 return 'index for %s' % user1 unmodified line1830 rv = client.get('/', 'http://mitsuhiko.localhost.localdomain:3000/')1831 assert rv.data == b'index for mitsuhiko'1832 1833 1834+@pytest.mark.parametrize('matching', (False, True))1835+def test_subdomain_matching_other_name(matching):1836+ app = flask.Flask(__name__, subdomain_matching=matching)1837+ app.config['SERVER_NAME'] = 'localhost.localdomain:3000'1838+ client = app.test_client()1839+1840+ @app.route('/')1841+ def index():1842+ return '', 2041843+1844+ # ip address can't match name1845+ rv = client.get('/', 'http://127.0.0.1:3000/')1846+ assert rv.status_code == 404 if matching else 2041847+1848+ # allow all subdomains if matching is disabled1849+ rv = client.get('/', 'http://www.localhost.localdomain:3000/')1850+ assert rv.status_code == 404 if matching else 2041851+1852+1853 def test_multi_route_rules(app, client):1854 @app.route('/')1855 @app.route('/<test>/')1856 def index(test='a'):63 unmodified linesmodified:tests/test_testing.py+6 added-2 removed
112 unmodified lines113 assert eb.script_root == ''114 assert eb.path == '/'115 116 117-def test_blueprint_with_subdomain(app, client):117+def test_blueprint_with_subdomain():118+ app = flask.Flask(__name__, subdomain_matching=True)119 app.config['SERVER_NAME'] = 'example.com:1234'120 app.config['APPLICATION_ROOT'] = '/foo'121+ client = app.test_client()122 123 bp = flask.Blueprint('company', __name__, subdomain='xxx')124 125 @bp.route('/')179 unmodified lines305 assert rv.is_json306 assert rv.get_json() == json_data307 308 307-def test_subdomain(app, client):309+def test_subdomain():310+ app = flask.Flask(__name__, subdomain_matching=True)311 app.config['SERVER_NAME'] = 'example.com'312+ client = app.test_client()313 314 @app.route('/', subdomain='<company_id>')315 def view(company_id):316 return company_id70 unmodified linesmodified:CHANGES.rst+5 added
137 unmodified lines138 - Added :data:`SESSION_COOKIE_SAMESITE` to control the ``SameSite``139 attribute on the session cookie. (`#2607`_)140 - Added :meth:`~flask.Flask.test_cli_runner` to create a Click runner141 that can invoke Flask CLI commands for testing. (`#2636`_)142+- Subdomain matching is disabled by default and setting143+ :data:`SERVER_NAME` does not implicily enable it. It can be enabled by144+ passing ``subdomain_matching=True`` to the ``Flask`` constructor.145+ (`#2635`_)146 147 .. _pallets/meta#24: https://github.com/pallets/meta/issues/24148 .. _#1421: https://github.com/pallets/flask/issues/1421149 .. _#1489: https://github.com/pallets/flask/pull/148934 unmodified lines184 .. _#2581: https://github.com/pallets/flask/pull/2581185 .. _#2606: https://github.com/pallets/flask/pull/2606186 .. _#2607: https://github.com/pallets/flask/pull/2607187 .. _#2636: https://github.com/pallets/flask/pull/2636188+.. _#2635: https://github.com/pallets/flask/pull/2635189 190 191 Version 0.12.2192 --------------618 unmodified lines