Skip to content
Myr

Flask will now give you an error in debug mode if a post request caused a redirect by the routing system.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent ce70131 · Browse files · 3 files+68 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+68 added−2 removed

  • tests:+24 added−0 removed
  • other:+44 added−2 removed

3 files+68 added−2 removed

modified:flask/app.py+18 added-2 removed
16 unmodified lines
17from itertools import chain18from functools import update_wrapper1920from werkzeug.datastructures import ImmutableDict21from werkzeug.routing import Map, Rule21from werkzeug.routing import Map, Rule, RequestRedirect22from werkzeug.exceptions import HTTPException, InternalServerError, \23 MethodNotAllowed, BadRequest2425from .helpers import _PackageBoundObject, url_for, get_flashed_messages, \
1107 unmodified lines
1133 if handler is None:1134 return InternalServerError()1135 return handler(e)11361137 def raise_routing_exception(self, request):1138 """Exceptions that are recording during routing are reraised with1139 this method. During debug we are not reraising redirect requests1140 for non ``GET``, ``HEAD``, or ``OPTIONS`` requests and we're raising1141 a different error instead to help debug situations.11421143 :internal:1144 """1145 if not self.debug \1146 or not isinstance(request.routing_exception, RequestRedirect) \1147 or request.method in ('GET', 'HEAD', 'OPTIONS'):1148 raise request.routing_exception11491150 from .debughelpers import FormDataRoutingRedirect1151 raise FormDataRoutingRedirect(request)11521153 def dispatch_request(self):1154 """Does the request dispatching. Matches the URL and returns the1155 return value of the view or error handler. This does not have to1156 be a response object. In order to convert the return value to a
4 unmodified lines
1161 moved to the new :meth:`full_dispatch_request`.1162 """1163 req = _request_ctx_stack.top.request1164 if req.routing_exception is not None:1149 raise req.routing_exception1165 self.raise_routing_exception(req)1166 rule = req.url_rule1167 # if we provide automatic options for this URL and the1168 # request came with the OPTIONS method, reply automatically1169 if getattr(rule, 'provide_automatic_options', False) \
280 unmodified lines
modified:flask/debughelpers.py+26 added
31 unmodified lines
32 def __str__(self):33 return self.msg343536class FormDataRoutingRedirect(AssertionError):37 """This exception is raised by Flask in debug mode if it detects a38 redirect caused by the routing system when the request method is not39 GET, HEAD or OPTIONS. Reasoning: form data will be dropped.40 """4142 def __init__(self, request):43 exc = request.routing_exception44 buf = ['A request was sent to this URL (%s) but a redirect was '45 'issued automatically by the routing system to "%s".'46 % (request.url, exc.new_url)]4748 # In case just a slash was appended we can be extra helpful49 if request.base_url + '/' == exc.new_url.split('?')[0]:50 buf.append(' The URL was defined with a trailing slash so '51 'Flask will automatically redirect to the URL '52 'with the trailing slash if it was accessed '53 'without one.')5455 buf.append(' Make sure to directly send your %s-request to this URL '56 'since we can\'t make browsers or HTTP clients redirect '57 'with form data.' % request.method)58 buf.append('\n\nNote: this exception is only raised in debug mode')59 AssertionError.__init__(self, ''.join(buf).encode('utf-8'))606162def attach_enctype_error_multidict(request):63 """Since Flask 0.8 we're monkeypatching the files object in case a64 request is detected that does not use multipart form data but the files65 object is accessed.
13 unmodified lines
modified:tests/flask_tests.py+24 added
979 unmodified lines
980 c.get('/')981 self.assertEqual(got, [42])982 self.assert_(app.got_first_request)983984 def test_routing_redirect_debugging(self):985 app = flask.Flask(__name__)986 app.debug = True987 @app.route('/foo/', methods=['GET', 'POST'])988 def foo():989 return 'success'990 with app.test_client() as c:991 try:992 c.post('/foo', data={})993 except AssertionError, e:994 self.assert_('http://localhost/foo/' in str(e))995 self.assert_('Make sure to directly send your POST-request '996 'to this URL' in str(e))997 else:998 self.fail('Expected exception')9991000 rv = c.get('/foo', data={}, follow_redirects=True)1001 self.assertEqual(rv.data, 'success')10021003 app.debug = False1004 with app.test_client() as c:1005 rv = c.post('/foo', data={}, follow_redirects=True)1006 self.assertEqual(rv.data, 'success')100710081009class JSONTestCase(unittest.TestCase):10101011 def test_json_bad_requests(self):1012 app = flask.Flask(__name__)
1097 unmodified lines