diff --git a/gunicorn/config.py b/gunicorn/config.py index 3c627934..a3ca0d05 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -425,6 +425,29 @@ class Keepalive(Setting): Generally set in the 1-5 seconds range. """ +class LimitRequestLine(Setting): + name = "limit_request_line" + section = "Security" + cli = ["--limit-request-line"] + meta = "INT" + validator = validate_pos_int + type = "int" + default = 4094 + desc = """\ + The maximum size of HTTP request line in bytes. + + This parameter is used to limit the allowed size of a client's + HTTP request-line. Since the request-line consists of the HTTP + method, URI, and protocol version, this directive places a + restriction on the length of a request-URI allowed for a request + on the server. A server needs this value to be large enough to + hold any of its resource names, including any information that + might be passed in the query part of a GET request. By default + this value is 4094 and can't be larger than 8190. + + This parameter can be used to prevent any DDOS attack. + """ + class Debug(Setting): name = "debug" section = "Debugging" diff --git a/gunicorn/http/errors.py b/gunicorn/http/errors.py index dc428420..4147781b 100644 --- a/gunicorn/http/errors.py +++ b/gunicorn/http/errors.py @@ -61,3 +61,12 @@ class ChunkMissingTerminator(ParseException): def __str__(self): return "Invalid chunk terminator is not '\\r\\n': %r" % self.term + + +class LimitRequestLine(ParseException): + def __init__(self, size, max_size): + self.size = size + self.max_size = max_size + + def __str__(self): + return "Request Line is too large (%s > %s)" % (self.size, self.max_size) diff --git a/gunicorn/http/message.py b/gunicorn/http/message.py index 3f14e882..e9b90235 100644 --- a/gunicorn/http/message.py +++ b/gunicorn/http/message.py @@ -13,7 +13,9 @@ except ImportError: from gunicorn.http.body import ChunkedReader, LengthReader, EOFReader, Body from gunicorn.http.errors import InvalidHeader, InvalidHeaderName, NoMoreData, \ -InvalidRequestLine, InvalidRequestMethod, InvalidHTTPVersion +InvalidRequestLine, InvalidRequestMethod, InvalidHTTPVersion, LimitRequestLine + +MAX_REQUEST_LINE = 8190 class Message(object): def __init__(self, cfg, unreader): @@ -107,6 +109,12 @@ class Request(Message): self.query = None self.fragment = None + # get max request line size + self.limit_request_line = max(cfg.limit_request_line, + MAX_REQUEST_LINE) + if self.limit_request_line <= 0: + self.limit_request_line = MAX_REQUEST_LINE + super(Request, self).__init__(cfg, unreader) @@ -123,32 +131,39 @@ class Request(Message): self.get_data(unreader, buf, stop=True) # Request line - idx = buf.getvalue().find("\r\n") - while idx < 0: + data = buf.getvalue() + while True: + idx = data.find("\r\n") + if idx >= 0: + break self.get_data(unreader, buf) - idx = buf.getvalue().find("\r\n") - self.parse_request_line(buf.getvalue()[:idx]) - rest = buf.getvalue()[idx+2:] # Skip \r\n - buf = StringIO() - buf.write(rest) + data = buf.getvalue() + if len(data) - 2 > self.limit_request_line: + raise LimitRequestLine(len(data), self.cfg.limit_request_line) + + self.parse_request_line(data[:idx]) + buf = StringIO() + buf.write(data[idx+2:]) # Skip \r\n # Headers - idx = buf.getvalue().find("\r\n\r\n") + data = buf.getvalue() + idx = data.find("\r\n\r\n") - done = buf.getvalue()[:2] == "\r\n" + done = data[:2] == "\r\n" while idx < 0 and not done: self.get_data(unreader, buf) - idx = buf.getvalue().find("\r\n\r\n") - done = buf.getvalue()[:2] == "\r\n" + data = buf.getvalue() + idx = data.find("\r\n\r\n") + done = data[:2] == "\r\n" if done: - self.unreader.unread(buf.getvalue()[2:]) + self.unreader.unread(data[2:]) return "" - self.headers = self.parse_headers(buf.getvalue()[:idx]) + self.headers = self.parse_headers(data[:idx]) - ret = buf.getvalue()[idx+4:] + ret = data[idx+4:] buf = StringIO() return ret diff --git a/gunicorn/workers/base.py b/gunicorn/workers/base.py index 893f4248..82f014d9 100644 --- a/gunicorn/workers/base.py +++ b/gunicorn/workers/base.py @@ -15,7 +15,8 @@ from gunicorn import util from gunicorn.workers.workertmp import WorkerTmp from gunicorn.http.errors import InvalidHeader, InvalidHeaderName, \ -InvalidRequestLine, InvalidRequestMethod, InvalidHTTPVersion +InvalidRequestLine, InvalidRequestMethod, InvalidHTTPVersion, \ +LimitRequestLine class Worker(object): @@ -147,6 +148,8 @@ class Worker(object): mesg = "

Invalid HTTP Version '%s'

" % str(exc) elif isinstance(exc, (InvalidHeaderName, InvalidHeader,)): mesg = "

Invalid Header '%s'

" % str(exc) + elif isinstance(exc, LimitRequestLine): + msg = str(exc) if self.debug: tb = traceback.format_exc() diff --git a/tests/002-test-request.py b/tests/002-test-request.py deleted file mode 100644 index 2b1a9c04..00000000 --- a/tests/002-test-request.py +++ /dev/null @@ -1,151 +0,0 @@ -# -*- coding: utf-8 - -# -# This file is part of gunicorn released under the MIT license. -# See the NOTICE for more information. - -import os -import t - -# from gunicorn.http import tee -# -# @t.http_request("001.http") -# def test_001(req): -# e = req.read() -# t.eq(e['CONTENT_LENGTH'], '14') -# t.eq(e['wsgi.version'], (1,0)) -# t.eq(e['REQUEST_METHOD'], 'PUT') -# t.eq(e['PATH_INFO'], '/stuff/here') -# t.eq(e['CONTENT_TYPE'], 'application/json') -# t.eq(e['QUERY_STRING'], 'foo=bar') -# -# t.eq(isinstance(e['wsgi.input'], tee.TeeInput), True) -# body = e['wsgi.input'].read() -# t.eq(body, '{"nom": "nom"}') -# -# @t.http_request("002.http") -# def test_002(req): -# e = req.read() -# t.eq(e['REQUEST_METHOD'], 'GET') -# t.eq(e['PATH_INFO'], "/test") -# t.eq(e['QUERY_STRING'], "") -# t.eq(e['HTTP_ACCEPT'], "*/*") -# t.eq(e['HTTP_HOST'], "0.0.0.0=5000") -# t.eq(e['HTTP_USER_AGENT'], "curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1") -# body = e['wsgi.input'].read() -# t.eq(body, '') -# -# @t.http_request("003.http") -# def test_003(req): -# e = req.read() -# -# t.eq(e['REQUEST_METHOD'], 'GET') -# t.eq(e['PATH_INFO'], "/favicon.ico") -# t.eq(e['QUERY_STRING'], "") -# t.eq(e['HTTP_ACCEPT'], "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") -# t.eq(e['HTTP_KEEP_ALIVE'], "300") -# -# body = e['wsgi.input'].read() -# t.eq(body, '') -# -# @t.http_request("004.http") -# def test_004(req): -# e = req.read() -# t.eq(e['REQUEST_METHOD'], 'GET') -# t.eq(e['PATH_INFO'], "/dumbfuck") -# t.eq(e['QUERY_STRING'], "") -# body = e['wsgi.input'].read() -# t.eq(body, '') -# -# -# @t.http_request("005.http") -# def test_005(req): -# e = req.read() -# t.eq(e['REQUEST_METHOD'], 'GET') -# t.eq(e['PATH_INFO'], "/forums/1/topics/2375") -# t.eq(e['QUERY_STRING'], "page=1") -# body = e['wsgi.input'].read() -# t.eq(body, '') -# -# -# @t.http_request("006.http") -# def test_006(req): -# e = req.read() -# t.eq(e['REQUEST_METHOD'], 'GET') -# t.eq(e['PATH_INFO'], "/get_no_headers_no_body/world") -# t.eq(e['QUERY_STRING'], "") -# body = e['wsgi.input'].read() -# t.eq(body, '') -# -# -# @t.http_request("007.http") -# def test_007(req): -# e = req.read() -# t.eq(e['REQUEST_METHOD'], 'GET') -# t.eq(e['PATH_INFO'], "/get_one_header_no_body") -# t.eq(e['QUERY_STRING'], "") -# t.eq(e['HTTP_ACCEPT'], "*/*") -# body = e['wsgi.input'].read() -# t.eq(body, '') -# -# -# @t.http_request("008.http") -# def test_008(req): -# e = req.read() -# t.eq(e['REQUEST_METHOD'], 'GET') -# t.eq(e['PATH_INFO'], "/get_funky_content_length_body_hello") -# t.eq(e['QUERY_STRING'], "") -# t.eq(e['CONTENT_LENGTH'], '5') -# body = e['wsgi.input'].read() -# t.eq(body, "HELLO") -# -# -# @t.http_request("009.http") -# def test_009(req): -# e = req.read() -# t.eq(e['REQUEST_METHOD'], 'POST') -# t.eq(e['PATH_INFO'], "/post_identity_body_world") -# t.eq(e['QUERY_STRING'], "q=search") -# t.eq(e['CONTENT_LENGTH'], '5') -# body = e['wsgi.input'].read() -# t.eq(body, "World") -# -# -# @t.http_request("010.http") -# def test_010(req): -# e = req.read() -# t.eq(e['REQUEST_METHOD'], 'POST') -# t.eq(e['PATH_INFO'], "/post_chunked_all_your_base") -# t.eq(e['HTTP_TRANSFER_ENCODING'], "chunked") -# t.eq(e['CONTENT_LENGTH'], '30') -# body = e['wsgi.input'].read() -# t.eq(body, "all your base are belong to us") -# -# -# @t.http_request("011.http") -# def test_011(req): -# e = req.read() -# t.eq(e['REQUEST_METHOD'], 'POST') -# t.eq(e['PATH_INFO'], "/two_chunks_mult_zero_end") -# t.eq(e['HTTP_TRANSFER_ENCODING'], "chunked") -# t.eq(e['CONTENT_LENGTH'], '11') -# body = e['wsgi.input'].read() -# t.eq(body, "hello world") -# -# @t.http_request("017.http") -# def test_017(req): -# e = req.read() -# t.eq(e['REQUEST_METHOD'], 'GET') -# t.eq(e['PATH_INFO'], "/stuff/here") -# t.eq(e["HTTP_IF_MATCH"], "bazinga!, large-sound") -# t.eq(e["wsgi.input"].read(), "") -# -# @t.http_request("017.http") -# def test_018(req): -# os.environ['SCRIPT_NAME'] = "/stuff" -# e = req.read() -# t.eq(e['REQUEST_METHOD'], 'GET') -# t.eq(e['SCRIPT_NAME'], "/stuff") -# t.eq(e['PATH_INFO'], "/here") -# t.eq(e["wsgi.input"].read(), "") -# - diff --git a/tests/requests/invalid/002.py b/tests/requests/invalid/002.py index 7dbd23be..5a4ca896 100644 --- a/tests/requests/invalid/002.py +++ b/tests/requests/invalid/002.py @@ -1,2 +1,2 @@ from gunicorn.http.errors import InvalidRequestLine -request = InvalidRequestLine \ No newline at end of file +request = InvalidRequestLine diff --git a/tests/requests/invalid/006.http b/tests/requests/invalid/006.http new file mode 100644 index 00000000..419a8c6f --- /dev/null +++ b/tests/requests/invalid/006.http @@ -0,0 +1,2 @@ +PUT /q=08aP8931Ltyl9nqyJvjMaRCOgDV3uONtAdHABjoZUG6KAP6h3Vh97O3GJjjovXYgNdrhxc7TriXoAmeehZMJx88EyhcPXO0f09Nvd128SZnxZ2r5jFDELkn26reKRysODSLBZLfjU3vxLzLXKWeFOFJKcZYRH9V7hC98DDS4ZsS7weUksBuK6m86aLNHHHB0Xbyxv1TiDbOWYIzKxV0eZKyk0CaDLDiR0CRuMOf4rwBe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z3X6YCF0lrcrQc0uhyr7gBGBs86nUBWFRp1LKjIRVTVXDipajqNDTQGNZtzvR9MUf1yJJV07inbrlPOENd7rHpKCrJtoZXOkDqInaIqoMCG3DVd353BGmZNJEKOa3DnL7fb9zwuHlvHAfCco7ZS4wAV87trWkp6skXux9v5WhkumbUyGq4ia6DM1PuqqnFfBTAWDzJsnggAJrzr8O7JbDtaXwcW9sqaOb0S6NvnUDZqiNdDQPMDOKvXRJJJQdf1FSrPCCSPEEWO1SeVwictj7rTbpWGRoukwhgJALys95pGGOQxCPzRGrtVFnGcsLN1CwI3wLbmDnNKUv3KpOLEOPRxQXeXuJRIiYCFum44c0wNr731DvHn3YEJMH4iwFONl1rolEL4w6KFUOCq7ekrE5iyUt1V32PNtuUshXRjOYjBval29JMH5GoqZlGhCczzHMA61cmuzqdFwiPCB9yzqvJTg8TqMNvwKJztFIQK4mc5Ev5rRVSozD796AVRKT8rZF39IA1kmCLdXqz7CCC8x4QjjDpxjKCXP5HkWf9mp2FNBjE3pAeaEc6Vk2ENLlW8WVCe HTTP/1.0\r\n +\r\n diff --git a/tests/requests/invalid/006.py b/tests/requests/invalid/006.py new file mode 100644 index 00000000..db7d6092 --- /dev/null +++ b/tests/requests/invalid/006.py @@ -0,0 +1,2 @@ +from gunicorn.http.errors import LimitRequestLine +request = LimitRequestLine diff --git a/tests/requests/valid/001.http b/tests/requests/valid/001.http index 43e389de..5e7980e8 100644 --- a/tests/requests/valid/001.http +++ b/tests/requests/valid/001.http @@ -3,4 +3,4 @@ Server: http://127.0.0.1:5984\r\n Content-Type: application/json\r\n Content-Length: 14\r\n \r\n -{"nom": "nom"} \ No newline at end of file +{"nom": "nom"} diff --git a/tests/treq.py b/tests/treq.py index ea99c3a3..8aeebcb8 100644 --- a/tests/treq.py +++ b/tests/treq.py @@ -12,6 +12,7 @@ import os import random import urlparse +from gunicorn.config import Config from gunicorn.http.errors import ParseException from gunicorn.http.parser import RequestParser @@ -78,27 +79,27 @@ class request(object): def send_bytes(self): for d in self.data: yield d - + def send_random(self): maxs = len(self.data) / 10 read = 0 while read < len(self.data): chunk = random.randint(1, maxs) yield self.data[read:read+chunk] - read += chunk + read += chunk # These functions define the sizes that the # read functions will read with. def size_all(self): return -1 - + def size_bytes(self): return 1 - + def size_small_random(self): return random.randint(0, 4) - + def size_random(self): return random.randint(1, 4096) @@ -130,7 +131,7 @@ class request(object): if len(body): raise AssertionError("Failed to read entire body: %r" % body) elif len(data): - raise AssertionError("Read beyond expected body: %r" % data) + raise AssertionError("Read beyond expected body: %r" % data) data = req.body.read(sizes()) if data: raise AssertionError("Read after body finished: %r" % data) @@ -152,7 +153,7 @@ class request(object): if len(body): raise AssertionError("Failed to read entire body: %r" % body) elif len(data): - raise AssertionError("Read beyond expected body: %r" % data) + raise AssertionError("Read beyond expected body: %r" % data) data = req.body.readline(sizes()) if data: raise AssertionError("Read data after body finished: %r" % data) @@ -174,7 +175,7 @@ class request(object): data = req.body.readlines(sizes()) if data: raise AssertionError("Read data after body finished: %r" % data) - + def match_iter(self, req, body, sizes): """\ This skips sizes because there's its not part of the iter api. @@ -196,7 +197,7 @@ class request(object): # Construct a series of test cases from the permutations of # send, size, and match functions. - + def gen_cases(self): def get_funs(p): return [v for k, v in inspect.getmembers(self) if k.startswith(p)] @@ -224,7 +225,7 @@ class request(object): def check(self, sender, sizer, matcher): cases = self.expect[:] - p = RequestParser(sender()) + p = RequestParser(Config(), sender()) for req in p: self.same(req, sizer, matcher, cases.pop(0)) t.eq(len(cases), 0) @@ -232,9 +233,6 @@ class request(object): def same(self, req, sizer, matcher, exp): t.eq(req.method, exp["method"]) t.eq(req.uri, exp["uri"]["raw"]) - t.eq(req.scheme, exp["uri"]["scheme"]) - t.eq(req.host, exp["uri"]["host"]) - t.eq(req.port, exp["uri"]["port"]) t.eq(req.path, exp["uri"]["path"]) t.eq(req.query, exp["uri"]["query"]) t.eq(req.fragment, exp["uri"]["fragment"]) @@ -263,58 +261,18 @@ class badrequest(object): while read < len(self.data): chunk = random.randint(1, maxs) yield self.data[read:read+chunk] - read += chunk - - def size(self): - return random.randint(0, 4) - - def match(self, req, body): - data = req.body.read(self.size()) - count = 1000 - while len(body): - if body[:len(data)] != data: - raise AssertionError("Invalid body data read: %r != %r" % ( - data, body[:len(data)])) - body = body[len(data):] - data = req.body.read(self.size()) - if not data: - count -= 1 - if count <= 0: - raise AssertionError("Unexpected apparent EOF") - - if len(body): - raise AssertionError("Failed to read entire body: %r" % body) - elif len(data): - raise AssertionError("Read beyond expected body: %r" % data) - data = req.body.read(sizes()) - if data: - raise AssertionError("Read after body finished: %r" % data) - - def same(self, req, sizer, matcher, exp): - t.eq(req.method, exp["method"]) - t.eq(req.uri, exp["uri"]["raw"]) - t.eq(req.scheme, exp["uri"]["scheme"]) - t.eq(req.host, exp["uri"]["host"]) - t.eq(req.port, exp["uri"]["port"]) - t.eq(req.path, exp["uri"]["path"]) - t.eq(req.query, exp["uri"]["query"]) - t.eq(req.fragment, exp["uri"]["fragment"]) - t.eq(req.version, exp["version"]) - t.eq(req.headers, exp["headers"]) - self.match(req, exp["body"]) - t.eq(req.trailers, exp.get("trailers", [])) + read += chunk def check(self): cases = self.expect[:] - p = RequestParser(self.send()) + p = RequestParser(Config(), self.send()) try: - for req in p: - self.same(req, cases.pop(0)) + [req for req in p] except Exception, inst: exp = cases.pop(0) if not issubclass(exp, Exception): raise TypeError("Test case is not an exception calss: %s" % exp) - if not isinstance(inst, exp): - raise TypeError("Invalid error result: %s: %s" % (exp, inst)) - t.eq(len(cases), 0) + t.istype(inst, exp) + return +