This is an automated email from the ASF dual-hosted git repository.
deacon-mp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/caldera.git
The following commit(s) were added to refs/heads/master by this push:
new 2312687c Restore request size limits for large agent output and
uploads (#3433)
2312687c is described below
commit 2312687cbda370fa4205081e2ecb32fe57c2708b
Author: deacon-mp <[email protected]>
AuthorDate: Wed Nov 18 22:02:53 2026 -0500
Restore request size limits for large agent output and uploads (#3433)
Since the global client_max_size_mb default became 1 MiB (#3268), the
server rejected every request body over 1 MiB on the root application,
including agent traffic carrying large command output and file uploads,
which 5.3.0 accepted up to 25 MiB. api_upload_max_size_mb never took
effect for /api/v2 either, because aiohttp builds each request with the
root application's client_max_size.
- Default client_max_size_mb to 25, in conf/default.yml and when the key
is missing from an existing config file.
- Add request_size_middleware to the root application so the limit
recorded by the sub-application that serves a route applies;
api_upload_max_size_mb now controls /api/v2 request bodies.
- Default api_upload_max_size_mb to 25 as well.
- Tests send real requests through both limits.
Co-authored-by: Ricky Chen <[email protected]>
---
app/api/v2/__init__.py | 29 +++++++++-
conf/default.yml | 4 +-
server.py | 6 +-
tests/security/test_client_max_size.py | 101 +++++++++++++++++++++++++++------
4 files changed, 115 insertions(+), 25 deletions(-)
diff --git a/app/api/v2/__init__.py b/app/api/v2/__init__.py
index dd7e9fd5..93fcaffa 100644
--- a/app/api/v2/__init__.py
+++ b/app/api/v2/__init__.py
@@ -1,15 +1,37 @@
from aiohttp import web
+DEFAULT_UPLOAD_MAX_SIZE_MB = 25
-def make_app(services, upload_max_size_mb=100):
+# Request body limit, in bytes, for the routes of a sub-application.
+CLIENT_MAX_SIZE = web.AppKey('client_max_size', int)
+
+
[email protected]
+async def request_size_middleware(request, handler):
+ """Apply the request body limit of the sub-application that serves the
route.
+
+ aiohttp creates every request with the root application's client_max_size,
so a
+ limit given to a sub-application has no effect on its own. Install this on
the
+ root application ahead of any middleware that reads the request body.
+ """
+ for app in reversed(request.match_info.apps):
+ limit = app.get(CLIENT_MAX_SIZE)
+ if limit:
+ if limit != request.client_max_size:
+ request = request.clone(client_max_size=limit)
+ break
+ return await handler(request)
+
+
+def make_app(services, upload_max_size_mb=DEFAULT_UPLOAD_MAX_SIZE_MB):
from .responses import json_request_validation_middleware
from .security import authentication_required_middleware_factory,
pass_option_middleware
try:
max_size = int(upload_max_size_mb)
- max_size = max_size if max_size > 0 else 100
+ max_size = max_size if max_size > 0 else DEFAULT_UPLOAD_MAX_SIZE_MB
except (TypeError, ValueError):
- max_size = 100
+ max_size = DEFAULT_UPLOAD_MAX_SIZE_MB
app = web.Application(
client_max_size=max_size * 1024 * 1024,
@@ -19,6 +41,7 @@ def make_app(services, upload_max_size_mb=100):
json_request_validation_middleware
]
)
+ app[CLIENT_MAX_SIZE] = max_size * 1024 * 1024
from .handlers.health_api import HealthApi
HealthApi(services).add_routes(app)
diff --git a/conf/default.yml b/conf/default.yml
index 654c7118..0bd8247e 100644
--- a/conf/default.yml
+++ b/conf/default.yml
@@ -1,6 +1,6 @@
ability_refresh: 60
-client_max_size_mb: 1
-api_upload_max_size_mb: 100
+client_max_size_mb: 25
+api_upload_max_size_mb: 25
api_key_blue: BLUEADMIN123
api_key_red: ADMIN123
app.contact.dns.domain: mycaldera.caldera
diff --git a/server.py b/server.py
index ad55e41e..1097cdc2 100644
--- a/server.py
+++ b/server.py
@@ -302,13 +302,13 @@ if __name__ == "__main__":
app_svc = AppService(
application=web.Application(
- client_max_size=_get_size_mb('client_max_size_mb', 1) * 1024 *
1024,
- middlewares=[pass_option_middleware]
+ client_max_size=_get_size_mb('client_max_size_mb', 25) * 1024 *
1024,
+ middlewares=[pass_option_middleware,
app.api.v2.request_size_middleware]
)
)
app_svc.register_subapp("/api/v2", app.api.v2.make_app(
app_svc.get_services(),
- upload_max_size_mb=_get_size_mb('api_upload_max_size_mb', 100)
+ upload_max_size_mb=_get_size_mb('api_upload_max_size_mb',
app.api.v2.DEFAULT_UPLOAD_MAX_SIZE_MB)
))
init_swagger_documentation(app_svc.application)
if args.uiDevHost:
diff --git a/tests/security/test_client_max_size.py
b/tests/security/test_client_max_size.py
index 8b8619bf..0290dc47 100644
--- a/tests/security/test_client_max_size.py
+++ b/tests/security/test_client_max_size.py
@@ -1,11 +1,17 @@
-import pytest
+from pathlib import Path
from unittest.mock import MagicMock
-from aiohttp import web
+
+import pytest
+import yaml
+from aiohttp import FormData, web
import app.api.v2 as v2_module
from app.api.v2.security import pass_option_middleware
from app.utility.base_world import BaseWorld
+MIB = 1024 * 1024
+DEFAULT_REQUEST_LIMIT = 25 * MIB # the limit every release before 5.4.0
hardcoded
+
def _make_app(upload_max_size_mb):
"""Call the real make_app with mock services and return the resulting
app."""
@@ -25,8 +31,9 @@ def test_valid_integer_config(mb, expected_bytes):
@pytest.mark.parametrize('bad_val', [None, '', 'abc', '1MB', -1, 0])
def test_invalid_config_falls_back_to_default(bad_val):
- """None, strings, and non-positive values must fall back to 100MB
default."""
- assert _make_app(bad_val)._client_max_size == 100 * 1024 * 1024
+ """None, strings, and non-positive values must fall back to the 25MB
default."""
+ assert _make_app(bad_val)._client_max_size == 25 * MIB
+ assert _make_app(bad_val)[v2_module.CLIENT_MAX_SIZE] == 25 * MIB
def test_string_integer_coerces_correctly():
@@ -34,21 +41,17 @@ def test_string_integer_coerces_correctly():
assert _make_app('50')._client_max_size == 50 * 1024 * 1024
-def test_api_upload_limit_larger_than_global_default():
- """API v2 upload limit (100MB default) must be larger than the global 1MB
default."""
- api_app = _make_app(100)
- assert api_app._client_max_size == 100 * 1024 * 1024
- assert api_app._client_max_size > 1 * 1024 * 1024
+def test_make_app_records_upload_limit():
+ """The v2 app records its limit so the root app can apply it to v2
routes."""
+ assert _make_app(100)[v2_module.CLIENT_MAX_SIZE] == 100 * MIB
-def test_global_default_tighter_than_old_hardcoded():
- """New 1MB global default must be tighter than the old hardcoded 5120**2
(~26MB)."""
- # Verify the v2 app can be created with the old value, and 1MB is
genuinely smaller
- old_hardcoded = 5120 ** 2
- new_default_bytes = 1 * 1024 * 1024
- assert new_default_bytes < old_hardcoded
- # Confirm the app created with 1MB actually applies that limit
- assert _make_app(1)._client_max_size == new_default_bytes
+def test_default_limits_are_25_mib():
+ """Agent output and uploads up to 25 MiB must still be accepted by
default."""
+ conf = yaml.safe_load((Path(__file__).parents[2] / 'conf' /
'default.yml').read_text())
+ assert conf['client_max_size_mb'] * MIB == DEFAULT_REQUEST_LIMIT
+ assert conf['api_upload_max_size_mb'] * MIB == DEFAULT_REQUEST_LIMIT
+ assert v2_module.DEFAULT_UPLOAD_MAX_SIZE_MB * MIB == DEFAULT_REQUEST_LIMIT
def test_root_and_subapp_limits():
@@ -63,3 +66,67 @@ def test_root_and_subapp_limits():
assert root_app._client_max_size == 1 * 1024 * 1024
assert v2_app._client_max_size == 100 * 1024 * 1024
assert v2_app._client_max_size > root_app._client_max_size
+
+
+async def _body_size(request):
+ return web.json_response({'size': len(await request.read())})
+
+
+async def _upload_size(request):
+ form = await request.post()
+ return web.json_response({'size': len(form['file'].file.read())})
+
+
[email protected]
+async def _read_form_early(request, handler):
+ """Stands in for root middleware that parses the body before the handler
runs."""
+ if request.path == '/api/v2/payloads':
+ await request.post()
+ return await handler(request)
+
+
+def _limited_app(root_limit, api_limit):
+ root = web.Application(client_max_size=root_limit,
+ middlewares=[pass_option_middleware,
v2_module.request_size_middleware, _read_form_early])
+ root.router.add_post('/report', _body_size)
+ api = web.Application(client_max_size=api_limit)
+ api[v2_module.CLIENT_MAX_SIZE] = api_limit
+ api.router.add_post('/payloads', _upload_size)
+ api.router.add_post('/echo', _body_size)
+ root.add_subapp('/api/v2', api)
+ return root
+
+
+def _upload(size):
+ data = FormData()
+ data.add_field('file', b'x' * size, filename='payload.bin',
content_type='application/octet-stream')
+ return data
+
+
+async def test_subapp_limit_applies_to_subapp_routes(aiohttp_client):
+ client = await aiohttp_client(_limited_app(root_limit=1024, api_limit=16 *
1024))
+
+ resp = await client.post('/api/v2/payloads', data=_upload(8 * 1024))
+ assert resp.status == 200
+ assert (await resp.json())['size'] == 8 * 1024
+
+ resp = await client.post('/api/v2/echo', data=b'x' * 8 * 1024)
+ assert resp.status == 200
+
+ assert (await client.post('/api/v2/payloads', data=_upload(32 *
1024))).status == 413
+ assert (await client.post('/api/v2/echo', data=b'x' * 32 * 1024)).status
== 413
+
+
+async def test_root_limit_still_applies_to_root_routes(aiohttp_client):
+ client = await aiohttp_client(_limited_app(root_limit=1024, api_limit=16 *
1024))
+
+ assert (await client.post('/report', data=b'x' * 512)).status == 200
+ assert (await client.post('/report', data=b'x' * 8 * 1024)).status == 413
+
+
+async def test_subapp_limit_can_be_lower_than_root(aiohttp_client):
+ client = await aiohttp_client(_limited_app(root_limit=16 * 1024,
api_limit=1024))
+
+ assert (await client.post('/report', data=b'x' * 8 * 1024)).status == 200
+ assert (await client.post('/api/v2/echo', data=b'x' * 8 * 1024)).status ==
413
+ assert (await client.post('/api/v2/payloads', data=_upload(8 *
1024))).status == 413