]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Avoid uncaught exception when _xsrf argument could not be decoded. 1689/head
authorharboecp <harboecp@users.noreply.github.com>
Tue, 12 Apr 2016 13:14:01 +0000 (15:14 +0200)
committerharboecp <harboecp@users.noreply.github.com>
Tue, 12 Apr 2016 17:09:02 +0000 (19:09 +0200)
This is primarily to avoid "TypeError: a bytes-like object is required, not 'NoneType" in "check_xsrf_cookie"

tornado/test/web_test.py
tornado/web.py

index fac23a21fd82ec9e65062312bd29c3f74b40a3a6..1beb9486c9d9015ea5f8693a6d16d02b675a934d 100644 (file)
@@ -2490,6 +2490,22 @@ class XSRFTest(SimpleHandlerTestCase):
                 body=urllib_parse.urlencode(dict(_xsrf=self.xsrf_token)))
         self.assertEqual(response.code, 403)
 
+    def test_xsrf_fail_argument_invalid_format(self):
+        with ExpectLog(gen_log, ".*'_xsrf' argument has invalid format"):
+            response = self.fetch(
+                "/", method="POST",
+                headers=self.cookie_headers(),
+                body=urllib_parse.urlencode(dict(_xsrf='3|')))
+        self.assertEqual(response.code, 403)
+
+    def test_xsrf_fail_cookie_invalid_format(self):
+        with ExpectLog(gen_log, ".*XSRF cookie does not match POST"):
+            response = self.fetch(
+                "/", method="POST",
+                headers=self.cookie_headers(token='3|'),
+                body=urllib_parse.urlencode(dict(_xsrf=self.xsrf_token)))
+        self.assertEqual(response.code, 403)
+
     def test_xsrf_fail_cookie_no_body(self):
         with ExpectLog(gen_log, ".*'_xsrf' argument missing"):
             response = self.fetch(
index 8f2acfcc93f23c235489191a0a1b4f987b647109..7380c814948954f459f06d89b048e25f634cd1e1 100644 (file)
@@ -1265,6 +1265,8 @@ class RequestHandler(object):
             raise HTTPError(403, "'_xsrf' argument missing from POST")
         _, token, _ = self._decode_xsrf_token(token)
         _, expected_token, _ = self._get_raw_xsrf_token()
+        if not token:
+            raise HTTPError(403, "'_xsrf' argument has invalid format")
         if not _time_independent_equals(utf8(token), utf8(expected_token)):
             raise HTTPError(403, "XSRF cookie does not match POST argument")