Do not enable subdomain matching by default
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Updated tests for new subdomain matching Added a test to validate matching behavior
authored by Armin Ronacher · committed by David Lord · parent 4a7db66 · Browse files · 4 files·+52 added−7 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+52 added−7 removed
- tests:+35 added−6 removed
- other:+17 added−1 removed
Provenance
- Commit
8cec201- Tree
9beb2d6- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
David Lord <davidism@gmail.com>- Parent
4a7db66
4 files changed+52 added−7 removed
modified:flask/app.py+15 added-1 removed
124 unmodified lines125 126 .. versionadded:: 0.13127 The `host_matching` and `static_host` parameters were added.128 129+ .. versionadded:: 1.0130+ The `subdomain_matching` parameter was added. Subdomain matching131+ needs to be enabled manually now. Setting `SERVER_NAME` does not132+ 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.213 unmodified lines351 static_url_path=None,352 static_folder='static',353 static_host=None,354 host_matching=False,355+ subdomain_matching=False,356 template_folder='templates',357 instance_path=None,358 instance_relative_config=False,359 root_path=None175 unmodified lines535 #: app.url_map.converters['list'] = ListConverter536 self.url_map = Map()537 538 self.url_map.host_matching = host_matching539+ self.subdomain_matching = subdomain_matching540 541 # tracks internally if the application already handled at least one542 # request.543 self._got_first_request = False1450 unmodified lines1994 This can now also be called without a request object when the1995 URL adapter is created for the application context.1996 """1997 if request is not None:1991- return self.url_map.bind_to_environ(request.environ,1998+ rv = self.url_map.bind_to_environ(request.environ,1999 server_name=self.config['SERVER_NAME'])2000+ # If subdomain matching is not enabled (which is the default2001+ # we put back the default subdomain in all cases. This really2002+ # should be the default in Werkzeug but it currently does not2003+ # have that feature.2004+ if not self.subdomain_matching:2005+ rv.subdomain = self.url_map.default_subdomain2006+ return rv2007 # We need at the very least the server name to be set for this2008 # to work.2009 if self.config['SERVER_NAME'] is not None:2010 return self.url_map.bind(243 unmodified linesmodified:tests/test_basic.py+29 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+def test_subdomain_matching_behavior():1835+ for matching in False, True: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 'matched without subdomain'1843+1844+ rv = client.get('/', 'http://127.0.0.1:3000/')1845+ if matching:1846+ assert rv.status_code == 4041847+ else:1848+ assert rv.data == b'matched without subdomain'1849+1850+1851 def test_multi_route_rules(app, client):1852 @app.route('/')1853 @app.route('/<test>/')1854 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+2 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 now. It can be turned on by143+ passing ``subdomain_matching=True`` to the Flask constructor.144 145 .. _pallets/meta#24: https://github.com/pallets/meta/issues/24146 .. _#1421: https://github.com/pallets/flask/issues/1421147 .. _#1489: https://github.com/pallets/flask/pull/1489660 unmodified lines