Merge pull request #2994 from jarek/werkzeug-640-exceptions-during-bind
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Handle errors during create_url_adapter
authored by David Lord · committed by GitHub · parents 3394191, a5709e7 · Browse files · 3 files·+65 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+65 added−2 removed
- tests:+54 added−0 removed
- other:+11 added−2 removed
Provenance
- Commit
ca23b7b- Tree
c1b4e33- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
3394191- Parent 2
a5709e7
3 files changed+65 added−2 removed
modified:flask/ctx.py+7 added-2 removed
280 unmodified lines281 self.app = app282 if request is None:283 request = app.request_class(environ)284 self.request = request285+ self.url_adapter = None286+ try:285- self.url_adapter = app.create_url_adapter(self.request)287+ self.url_adapter = app.create_url_adapter(self.request)288+ except HTTPException as e:289+ self.request.routing_exception = e290 self.flashes = None291 self.session = session292 293 # Request contexts can be pushed multiple times and interleaved with14 unmodified lines308 # object. These will be called before the regular "after_request"309 # functions.310 self._after_request_functions = []311 312+ if self.url_adapter is not None:308- self.match_request()313+ self.match_request()314 315 def _get_g(self):316 return _app_ctx_stack.top.g317 def _set_g(self, value):151 unmodified linesmodified:tests/test_reqctx.py+54 added
227 unmodified lines228 response = app.test_client().get('/')229 assert response.status_code == 500230 assert not flask.request231 assert not flask.current_app232+233+234+def test_bad_environ_raises_bad_request():235+ app = flask.Flask(__name__)236+237+ # We cannot use app.test_client() for the Unicode-rich Host header,238+ # because werkzeug enforces latin1 on Python 2.239+ # However it works when actually passed to the server.240+241+ from flask.testing import make_test_environ_builder242+ builder = make_test_environ_builder(app)243+ environ = builder.get_environ()244+245+ # use a non-printable character in the Host - this is key to this test246+ environ['HTTP_HOST'] = u'\x8a'247+248+ with app.request_context(environ):249+ response = app.full_dispatch_request()250+ assert response.status_code == 400251+252+253+def test_environ_for_valid_idna_completes():254+ app = flask.Flask(__name__)255+256+ @app.route('/')257+ def index():258+ return 'Hello World!'259+260+ # We cannot use app.test_client() for the Unicode-rich Host header,261+ # because werkzeug enforces latin1 on Python 2.262+ # However it works when actually passed to the server.263+264+ from flask.testing import make_test_environ_builder265+ builder = make_test_environ_builder(app)266+ environ = builder.get_environ()267+268+ # these characters are all IDNA-compatible269+ environ['HTTP_HOST'] = u'ąśźäüжŠßя.com'270+271+ with app.request_context(environ):272+ response = app.full_dispatch_request()273+274+ assert response.status_code == 200275+276+277+def test_normal_environ_completes():278+ app = flask.Flask(__name__)279+280+ @app.route('/')281+ def index():282+ return 'Hello World!'283+284+ response = app.test_client().get('/', headers={'host': 'xn--on-0ia.com'})285+ assert response.status_code == 200modified:CHANGES.rst+4 added
10 unmodified lines11 12 - :meth:`flask.RequestContext.copy` includes the current session13 object in the request context copy. This prevents ``flask.session`` 14 pointing to an out-of-date object. (`#2935`)15+- Using built-in RequestContext, unprintable Unicode characters in Host16+ header will result in a HTTP 400 response and not HTTP 500 as previously.17+ (`#2994`)18 19 .. _#2935: https://github.com/pallets/flask/issues/293520+.. _#2994: https://github.com/pallets/flask/pull/299421 22 23 Version 1.0.324 -------------926 unmodified lines