Skip to content
Myr

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

5 files+82 added−25 removed

modified:docs/config.rst+8 added-7 removed
179 unmodified lines
180181.. py:data:: SESSION_COOKIE_DOMAIN182183 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.186187 Default: ``None``188189.. py:data:: SESSION_COOKIE_PATH
66 unmodified lines
256 Default: ``timedelta(hours=12)`` (``43200`` seconds)257258.. py:data:: SERVER_NAME259260 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.262263 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. ::268269 127.0.0.1 localhost.dev270271 If set, ``url_for`` can generate external URLs with only an application
379 unmodified lines
modified:flask/app.py+32 added-12 removed
121 unmodified lines
122123 .. versionadded:: 0.11124 The `root_path` parameter was added.125126 .. 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.128129 .. 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.133134 :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 lines
353 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=None
175 unmodified lines
537 #: app.url_map.converters['list'] = ListConverter538 self.url_map = Map()539540 self.url_map.host_matching = host_matching541 self.subdomain_matching = subdomain_matching542543 # tracks internally if the application already handled at least one544 # request.545 self._got_first_request = False
1439 unmodified lines
19851986 return rv19871988 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.19921993 .. versionadded:: 0.619941995 .. versionchanged:: 0.91996 This can now also be called without a request object when the1997 URL adapter is created for the application context.19981999 .. 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 lines
modified:tests/test_basic.py+31 added-4 removed
1427 unmodified lines
1428 assert repr(flask.g) == '<LocalProxy unbound>'1429 assert not flask.g143014311432def test_test_app_proper_environ(app, client):1432def 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()14381439 @app.route('/')1440 def index():1441 return 'Foo'
342 unmodified lines
1784 assert 'foos' not in flask.g1785 assert sorted(flask.g) == ['bar', 'foo']178617871786def test_subdomain_basic_support(app, client):1788def test_subdomain_basic_support():1789 app = flask.Flask(__name__, subdomain_matching=True)1790 app.config['SERVER_NAME'] = 'localhost.localdomain'1791 client = app.test_client()17921793 @app.route('/')1794 def normal_index():1795 return 'normal index'
8 unmodified lines
1804 rv = client.get('/', 'http://test.localhost.localdomain/')1805 assert rv.data == b'test index'180618071804def test_subdomain_matching(app, client):1808def test_subdomain_matching():1809 app = flask.Flask(__name__, subdomain_matching=True)1810 client = app.test_client()1811 app.config['SERVER_NAME'] = 'localhost.localdomain'18121813 @app.route('/', subdomain='<user>')1814 def index(user):
2 unmodified lines
1817 rv = client.get('/', 'http://mitsuhiko.localhost.localdomain/')1818 assert rv.data == b'index for mitsuhiko'181918201815def test_subdomain_matching_with_ports(app, client):1821def 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()18251826 @app.route('/', subdomain='<user>')1827 def index(user):1828 return 'index for %s' % user
1 unmodified line
1830 rv = client.get('/', 'http://mitsuhiko.localhost.localdomain:3000/')1831 assert rv.data == b'index for mitsuhiko'183218331834@pytest.mark.parametrize('matching', (False, True))1835def 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()18391840 @app.route('/')1841 def index():1842 return '', 20418431844 # ip address can't match name1845 rv = client.get('/', 'http://127.0.0.1:3000/')1846 assert rv.status_code == 404 if matching else 20418471848 # allow all subdomains if matching is disabled1849 rv = client.get('/', 'http://www.localhost.localdomain:3000/')1850 assert rv.status_code == 404 if matching else 204185118521853def test_multi_route_rules(app, client):1854 @app.route('/')1855 @app.route('/<test>/')1856 def index(test='a'):
63 unmodified lines
modified:tests/test_testing.py+6 added-2 removed
112 unmodified lines
113 assert eb.script_root == ''114 assert eb.path == '/'115116117def test_blueprint_with_subdomain(app, client):117def 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()122123 bp = flask.Blueprint('company', __name__, subdomain='xxx')124125 @bp.route('/')
179 unmodified lines
305 assert rv.is_json306 assert rv.get_json() == json_data307308307def test_subdomain(app, client):309def test_subdomain():310 app = flask.Flask(__name__, subdomain_matching=True)311 app.config['SERVER_NAME'] = 'example.com'312 client = app.test_client()313314 @app.route('/', subdomain='<company_id>')315 def view(company_id):316 return company_id
70 unmodified lines
modified:CHANGES.rst+5 added
137 unmodified lines
138- 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`_)146147.. _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/1489
34 unmodified lines
184.. _#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/2635189190191Version 0.12.2192--------------
618 unmodified lines