Skip to content

Commit

Permalink
fix (and test) limit for request.BODY (#1143)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-maurer committed Jul 14, 2023
1 parent f62d0c9 commit 18a1748
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/ZPublisher/HTTPRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,8 +1354,6 @@ def sane_environment(env):

class ValueDescriptor:
"""(non data) descriptor to compute `value` from `file`."""
VALUE_LIMIT = FORM_MEMORY_LIMIT

def __get__(self, inst, owner=None):
if inst is None:
return self
Expand All @@ -1365,9 +1363,13 @@ def __get__(self, inst, owner=None):
except Exception:
fpos = None
try:
v = file.read()
if self.VALUE_LIMIT and file.read(1):
raise BadRequest("data exceeds memory limit")
limit = inst.VALUE_LIMIT
if limit:
v = file.read(limit)
if file.read(1):
raise BadRequest("data exceeds memory limit")
else:
v = file.read()
if fpos is None:
# store the value as we cannot read it again
inst.value = v
Expand All @@ -1377,7 +1379,19 @@ def __get__(self, inst, owner=None):
file.seek(fpos)


class Global:
"""(non data) descriptor to access a (modul) global attribute."""
def __init__(self, name):
"""access global *name*."""
self.name = name

def __get__(self, inst, owner=None):
return globals()[self.name]


class ValueAccessor:
VALUE_LIMIT = None

value = ValueDescriptor()


Expand Down Expand Up @@ -1410,6 +1424,8 @@ class FormField(SimpleNamespace, ValueAccessor):


class ZopeFieldStorage(ValueAccessor):
VALUE_LIMIT = Global("FORM_MEMORY_LIMIT")

def __init__(self, fp, environ):
self.file = fp
method = environ.get("REQUEST_METHOD", "GET").upper()
Expand Down
21 changes: 21 additions & 0 deletions src/ZPublisher/tests/testHTTPRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,27 @@ def test_put_with_body_and_query_string(self):
self.assertEqual(req.BODY, b"foo")
self.assertEqual(req.form["bar"], "bar")

def test_put_with_body_limit(self):
req_factory = self._getTargetClass()
req = req_factory(
BytesIO(b"foo"),
{
"SERVER_NAME": "localhost",
"SERVER_PORT": "8080",
"REQUEST_METHOD": "PUT",
},
None,
)
from ZPublisher import HTTPRequest
saved_form_memory_limit = HTTPRequest.FORM_MEMORY_LIMIT
HTTPRequest.FORM_MEMORY_LIMIT = 1
try:
req.processInputs()
with self.assertRaises(BadRequest):
req.BODY
finally:
HTTPRequest.FORM_MEMORY_LIMIT = saved_form_memory_limit

def test_issue_1095(self):
body = TEST_ISSUE_1095_DATA
env = self._makePostEnviron(body)
Expand Down

0 comments on commit 18a1748

Please sign in to comment.