This is an automated email from the ASF dual-hosted git repository. deacon-mp pushed a commit to branch request-size-limits in repository https://gitbox.apache.org/repos/asf/caldera.git
commit e1eb1dd010ee1e307c1d6c6ef2ad5fe373f16b79 Author: Mark Perry <[email protected]> AuthorDate: Tue Nov 17 17:00:09 2026 -0500 Restore request size limits for large agent output and uploads Since the global client_max_size default became 1 MiB, the server rejects every request body over 1 MiB on the root application. That includes agent traffic carrying large command output and file uploads, which 5.3.0 accepted up to 25 MiB (5120**2 bytes). The api_upload_max_size_mb setting for /api/v2 never took effect either: aiohttp builds each request with the root application's client_max_size, so a limit passed to a sub-application is ignored. - Default client_max_size_mb to 25, the limit 5.3.0 applied to every request, both in conf/default.yml and when the key is missing from an existing config file. - Add request_size_middleware to the root application. It applies the limit recorded by the sub-application that serves the route, so api_upload_max_size_mb now controls /api/v2 request bodies. - Default api_upload_max_size_mb to 25 as well, so /api/v2 keeps the 5.3.0 limit unless an operator raises it. - Tests send real requests through both limits, including a 413 above each limit and a sub-application limit lower than the root one. --- 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 ef0075d9..a647784d 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..eefcabeb 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 +LIMIT_5_3_0 = 5120 ** 2 # the request size limit 5.3.0 applied to every route + 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_match_5_3_0(): + """Agent output and uploads up to the 5.3.0 limit 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 == LIMIT_5_3_0 + assert conf['api_upload_max_size_mb'] * MIB == LIMIT_5_3_0 + assert v2_module.DEFAULT_UPLOAD_MAX_SIZE_MB * MIB == LIMIT_5_3_0 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
