]> granicus.if.org Git - python/commitdiff
simply ignore headers with no name (#19996)
authorBenjamin Peterson <benjamin@python.org>
Mon, 26 Jan 2015 04:34:42 +0000 (23:34 -0500)
committerBenjamin Peterson <benjamin@python.org>
Mon, 26 Jan 2015 04:34:42 +0000 (23:34 -0500)
Patch by Cory Benfield.

Lib/httplib.py
Lib/rfc822.py
Lib/test/test_httplib.py
Lib/test/test_rfc822.py
Misc/NEWS

index 194e130f1705947da23728973b28a41a782b4c23..8a6ad814b798b0be4501f74d0aa0f14c3030287e 100644 (file)
@@ -313,6 +313,11 @@ class HTTPMessage(mimetools.Message):
                 hlist.append(line)
                 self.addheader(headerseen, line[len(headerseen)+1:].strip())
                 continue
+            elif headerseen is not None:
+                # An empty header name. These aren't allowed in HTTP, but it's
+                # probably a benign mistake. Don't add the header, just keep
+                # going.
+                continue
             else:
                 # It's not a header line; throw it back and stop here.
                 if not self.dict:
index b65d8da0d25babd71cd52720e1e422524e38d921..c1d0865bbb4851b98257446cb51d436d394cdec2 100644 (file)
@@ -179,6 +179,11 @@ class Message:
                 lst.append(line)
                 self.dict[headerseen] = line[len(headerseen)+1:].strip()
                 continue
+            elif headerseen is not None:
+                # An empty header name. These aren't allowed in HTTP, but it's
+                # probably a benign mistake. Don't add the header, just keep
+                # going.
+                continue
             else:
                 # It's not a header line; throw it back and stop here.
                 if not self.dict:
@@ -202,7 +207,7 @@ class Message:
         data in RFC 2822-like formats with special header formats.
         """
         i = line.find(':')
-        if i > 0:
+        if i > -1:
             return line[:i].lower()
         return None
 
index 0892d5ab63bbab770b990b53d384b2a52edfd406..c071411f58cb31e189376f4f324d34bd11fc7e41 100644 (file)
@@ -164,6 +164,16 @@ class HeaderTests(TestCase):
         conn.request('GET', '/foo')
         self.assertTrue(sock.data.startswith(expected))
 
+    def test_malformed_headers_coped_with(self):
+        # Issue 19996
+        body = "HTTP/1.1 200 OK\r\nFirst: val\r\n: nval\r\nSecond: val\r\n\r\n"
+        sock = FakeSocket(body)
+        resp = httplib.HTTPResponse(sock)
+        resp.begin()
+
+        self.assertEqual(resp.getheader('First'), 'val')
+        self.assertEqual(resp.getheader('Second'), 'val')
+
 
 class BasicTest(TestCase):
     def test_status_lines(self):
index d8a0280f51e24f6eb07199a093ea5c5070335773..cdd8c9c5a22272db465942333e2536109272bfbe 100644 (file)
@@ -248,6 +248,12 @@ A test message.
         eq(rfc822.quote('foo\\wacky"name'), 'foo\\\\wacky\\"name')
         eq(rfc822.unquote('"foo\\\\wacky\\"name"'), 'foo\\wacky"name')
 
+    def test_invalid_headers(self):
+        eq = self.assertEqual
+        msg = self.create_message("First: val\n: otherval\nSecond: val2\n")
+        eq(msg.getheader('First'), 'val')
+        eq(msg.getheader('Second'), 'val2')
+
 
 def test_main():
     test_support.run_unittest(MessageTestCase)
index f21dbe83f78c3911f77f4f9c510f4ca1cf4633e8..6582bab86893b5f19df0911ca6a02868d97304ae 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #19996: Make :mod:`httplib` ignore headers with no name rather than
+  assuming the body has started.
+
 - Issue #20188: Support Application-Layer Protocol Negotiation (ALPN) in the ssl
   module.