diff options
| author | Dan Prince <dprince@redhat.com> | 2013-01-12 22:22:42 -0500 |
|---|---|---|
| committer | Dan Prince <dprince@redhat.com> | 2013-01-21 19:54:29 -0500 |
| commit | 7691276b869a86c2b75631d5bede9f61e030d9d8 (patch) | |
| tree | 42da4e3aec16d1473f66a4f6463e3d8248f4207c /keystone/common | |
| parent | 8748cfa3a6b7573550e7ec8ced87e6fd2096a628 (diff) | |
Limit the size of HTTP requests.
Adds a new RequestBodySizeLimiter middleware to guard against
really large HTTP requests. The default max request size is 112k
although this limit is configurable via the 'max_request_body_size'
config parameter.
Fixes LP Bug #1099025.
Change-Id: Id51be3d9a0d829d63d55a92dca61a39a17629785
Diffstat (limited to 'keystone/common')
| -rw-r--r-- | keystone/common/utils.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/keystone/common/utils.py b/keystone/common/utils.py index d74da5b5..2c194db5 100644 --- a/keystone/common/utils.py +++ b/keystone/common/utils.py @@ -311,3 +311,37 @@ def setup_remote_pydev_debug(): except: LOG.exception(_(error_msg)) raise + + +class LimitingReader(object): + """Reader to limit the size of an incoming request.""" + def __init__(self, data, limit): + """ + :param data: Underlying data object + :param limit: maximum number of bytes the reader should allow + """ + self.data = data + self.limit = limit + self.bytes_read = 0 + + def __iter__(self): + for chunk in self.data: + self.bytes_read += len(chunk) + if self.bytes_read > self.limit: + raise exception.RequestTooLarge() + else: + yield chunk + + def read(self, i): + result = self.data.read(i) + self.bytes_read += len(result) + if self.bytes_read > self.limit: + raise exception.RequestTooLarge() + return result + + def read(self): + result = self.data.read() + self.bytes_read += len(result) + if self.bytes_read > self.limit: + raise exception.RequestTooLarge() + return result |
