]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Fix reponse header sanitization.
authorBen Darnell <ben@bendarnell.com>
Tue, 24 Apr 2012 04:55:05 +0000 (21:55 -0700)
committerBen Darnell <ben@bendarnell.com>
Tue, 24 Apr 2012 04:55:05 +0000 (21:55 -0700)
tornado/test/web_test.py
tornado/web.py

index 9f4c860ebf91bc0c1da984c38ef0ff590ce50f07..5312304f54ca27a86eef61fac4f3905e45cc42df 100644 (file)
@@ -335,6 +335,16 @@ class RedirectHandler(RequestHandler):
             raise Exception("didn't get permanent or status arguments")
 
 
+class HeaderInjectionHandler(RequestHandler):
+    def get(self):
+        try:
+            self.set_header("X-Foo", "foo\r\nX-Bar: baz")
+            raise Exception("Didn't get expected exception")
+        except ValueError, e:
+            assert "Unsafe header value" in str(e)
+            self.finish(b("ok"))
+
+
 class WebTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         loader = DictLoader({
@@ -359,6 +369,7 @@ class WebTest(AsyncHTTPTestCase, LogTrapTestCase):
             url("/flow_control", FlowControlHandler),
             url("/multi_header", MultiHeaderHandler),
             url("/redirect", RedirectHandler),
+            url("/header_injection", HeaderInjectionHandler),
             ]
         return Application(urls,
                            template_loader=loader,
@@ -452,6 +463,10 @@ js_embed()
         response = self.fetch("/redirect?status=307", follow_redirects=False)
         self.assertEqual(response.code, 307)
 
+    def test_header_injection(self):
+        response = self.fetch("/header_injection")
+        self.assertEqual(response.body, b("ok"))
+
 
 class ErrorResponseTest(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
index c31eb674b2aa760bf9e6c17a74a2988cc5c144de..76392b75c1ced47ff524f0c20a510409ba8b2e26 100644 (file)
@@ -275,7 +275,7 @@ class RequestHandler(object):
         # If \n is allowed into the header, it is possible to inject
         # additional headers or split the request. Also cap length to
         # prevent obviously erroneous values.
-        if len(value) > 4000 or re.match(b(r"[\x00-\x1f]"), value):
+        if len(value) > 4000 or re.search(b(r"[\x00-\x1f]"), value):
             raise ValueError("Unsafe header value %r", value)
         return value