clear KeyError in production for Werkzeug 0.15

This commit is contained in:
David Lord 2019-01-05 12:49:59 -08:00
parent f7e7f2ab11
commit 49efc44233
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
2 changed files with 12 additions and 11 deletions

View file

@ -1698,16 +1698,17 @@ class Flask(_PackageBoundObject):
# we cannot prevent users from trashing it themselves in a custom # we cannot prevent users from trashing it themselves in a custom
# trap_http_exception method so that's their fault then. # trap_http_exception method so that's their fault then.
# MultiDict passes the key to the exception, but that's ignored if isinstance(e, BadRequestKeyError):
# when generating the response message. Set an informative if self.debug or self.config["TRAP_BAD_REQUEST_ERRORS"]:
# description for key errors in debug mode or when trapping errors. # Werkzeug < 0.15 doesn't add the KeyError to the 400
if ( # message, add it in manually.
(self.debug or self.config['TRAP_BAD_REQUEST_ERRORS']) description = e.get_description()
and isinstance(e, BadRequestKeyError)
# only set it if it's still the default description if e.args[0] not in description:
and e.description is BadRequestKeyError.description e.description = "KeyError: '{}'".format(*e.args)
): else:
e.description = "KeyError: '{0}'".format(*e.args) # Werkzeug >= 0.15 does add it, remove it in production
e.args = ()
if isinstance(e, HTTPException) and not self.trap_http_exception(e): if isinstance(e, HTTPException) and not self.trap_http_exception(e):
return self.handle_http_exception(e) return self.handle_http_exception(e)

View file

@ -1045,7 +1045,7 @@ def test_trapping_of_bad_request_key_errors(app, client):
with pytest.raises(KeyError) as e: with pytest.raises(KeyError) as e:
client.get("/key") client.get("/key")
assert e.errisinstance(BadRequest) assert e.errisinstance(BadRequest)
assert 'missing_key' in e.value.description assert 'missing_key' in e.value.get_description()
rv = client.get('/abort') rv = client.get('/abort')
assert rv.status_code == 400 assert rv.status_code == 400