Repository navigation
fix!: limit request bodies by default - #813
Conversation
Connexion parses the body of every request, also as a form, before it checks the request's token, and Werkzeug parses URL-encoded bodies at once without applying Flask's limits of form data. Anonymous clients could thus make the app parse bodies of any size. Limit request bodies to 10 MiB by default (server.max_content_length) and URL-encoded bodies to 500 kB (the new server.max_form_memory_size, set as Flask's MAX_FORM_MEMORY_SIZE), checked by the body size hook before requests are handled, so that larger ones get 413 unparsed. BREAKING CHANGE: request bodies over 10 MiB and URL-encoded bodies over 500 kB are rejected with 413 unless the limits are raised or set to null. Refs #798 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Describe the default limits of request bodies and form data in the server configuration, the deployment notes, the plugin docs and the config template, and how to adapt apps in the migration guide. Refs #798 Co-Authored-By: Claude Opus 5.5 <[email protected]>
FOCA now limits request bodies by default, so app values may remove the limits with null: allow it for config.server.max_content_length and check the new config.server.max_form_memory_size in the values schema. Document that app charts set maxContentLength to FOCA's default if their base config sets no limit, so that the Ingress check applies. Refs #798 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Check that non-file fields of multipart bodies over the form limit get 413 and files don't, that the lower of both limits applies to URL-encoded bodies, and name the default limits in the tests. Refs #798 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Name both limits in the Gunicorn settings and the petstore example, list how bodies are checked, and fix the punctuation of the migration guide's list. Refs #798 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The petstore relies on FOCA's default limit of request bodies, so its chart settings name it (maxContentLength), as the docs now ask of app charts, and the Ingress check applies by default. Test that a null limit reaches the overlay and overrides the settings, and re-wrap the helper's comment. Refs #798 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Send a URL-encoded body between both limits, and say in the migration guide that the form limit applies whatever max_content_length is. Refs #798 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideData structuresAdd default request-body and form-memory limits to the server configuration model, including their validation and field documentation.
APIUpdate chart values schemas to allow null body-size limits and validate the new form-memory setting.
ImplementationEnforce the lower applicable limit for URL-encoded bodies before request handling and configure Flask with both server limits.
ConfigurationUpdate the Helm helper and example chart settings so Ingress checks account for FOCA's default body-size limit.
Set and describe the default request-body and form-data limits in the configuration template and Gunicorn deployment settings.
DocumentationDocument the request-body and form-data limits, their operational implications, and the migration steps for existing configurations.
Explain the new chart limit defaults and the explicit app-chart setting needed for ingress validation.
TestsAdd unit coverage for default and nullable limits, URL-encoded and multipart enforcement, boundary cases, and rejection before authentication.
Update chart tests for the default limit, explicit null override, and validation of the form-memory setting.
Add petstore integration checks that oversized URL-encoded and general request bodies receive 413 responses under the defaults.
Other changesFiles not grouped.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #813 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 76 76
Lines 10634 10640 +6
=========================================
+ Hits 10634 10640 +6
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Linked issuesChecked at commit aafc558 #798 fix: don't parse request bodies before auth · 4 of 4 met
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="templates/config.yaml" line_range="20" />
<code_context>
port: 8080
debug: true
testing: false
- max_content_length: 10485760
+ max_content_length: 1048576
+ max_form_memory_size: 100000
</code_context>
<issue_to_address>
**Existing configs stay unlimited**
When an existing deployment retains a config file generated from the pre-PR template, `ConfigParser` validates the retained `max_content_length: null` as an explicit opt-out, so `ServerConfig` does not apply the new default and `_check_body_size` leaves request bodies unlimited. Existing deployments using a config generated from the old template therefore remain exposed to unbounded bodies.
Tell users upgrading from the old template to replace its explicit `null` with `10485760` or remove the key to adopt the new default.
</issue_to_address>Sourcery assessment
Approval pending. 1 finding to address first.
Blocking findings: templates/config.yaml:20
Configs made from the earlier template set max_content_length to null and keep unlimited bodies after the upgrade: say so in the migration guide, and how to adopt the new default. Refs #798 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes #798
Summary
Connexion 2 parses the body of every request, including as a form, before it checks the token. Werkzeug also parses URL-encoded bodies in one go and applies neither of Flask's form limits. So far FOCA set no body limit by default. This PR implements the first option of the issue, the configurable size limit with
413, plus the defaultMAX_CONTENT_LENGTHthat the issue asks for in the same fix:server.max_content_lengthnow defaults to 10 MiB; before, there was no limit. Setting it tonullremoves the limit.server.max_form_memory_sizedefaults to 500 kB, as in Flask, and is set as Flask'sMAX_FORM_MEMORY_SIZE. Werkzeug applies it to the non-file fields of multipart bodies. FOCA now also applies it to whole URL-encoded bodies (application/x-www-form-urlencoded), in the existing body-size hook (_check_body_size), which runs before Connexion's handling. For URL-encoded bodies the lower of the two limits applies.413(problem details) without being read.RequestEntityTooLargewas already mapped to problem details with status413.nullfor both limits and checksmax_form_memory_size. The docs tell app charts to setmaxContentLength: 10485760if their base config sets no limit, so that the Ingress check applies. The petstore example chart now does this.configuration/server.md, the deployment notes, the plugin and WES attachment docs, the config template, the Helm chart docs and the chart release notes. The migration guide for 0.23.0 gets a new section, "Request bodies are limited by default". This is a breaking change, hencefix!.The other option in the issue, validating tokens before Connexion parses the body, is a separate follow-up issue (decision of 2026-11-21).
Verification
pytest tests/factories/test_connexion_app.py: 88 passed. New tests cover:null, and validation of both options;413with Werkzeug's_parse_urlencodedpatched to fail, and that neither the security handler nor the operation runs. Without the fix, the same request was parsed and then answered with401.foca/factories/connexion_app.pyandfoca/models/config.pyare at 100% coverage.ruff format --check,ruff check,mypy,ast-grep scan,mkdocs build --strict: all pass.helm lint --strictpasses on the library chart and on the petstore and gateway charts with all CI values, andpytest charts/foca-common/testsgives 510 passed.tests/integration_tests.pycover the petstore with the default limits: a URL-encoded body over 500 kB and a body over 10 MiB both get413. Locally these were checked only in-process, through the petstore app's Flask test client, with the same results. The docker-compose integration jobs run in CI.CI passed, including unit tests on Python 3.11 to 3.15 with MongoDB 7.0 and 8.0, lowest dependencies, the petstore and gateway integration tests, Chart, Chart install, image scans, CodeQL, codecov/patch and codecov/project, and Read the Docs).Declined review findings
foca-common.maxContentLengthfall back to 10485760 when no limit is set. The library chart can't know the FOCA version of the app it renders. A fallback would fail renders of apps on older FOCA that setproxy-body-sizebelow 10m. Instead, the default is documented indocs/helm-chart.md, in the helper comment and in the 0.6.0 chart release notes, and app charts are told to setmaxContentLength.max_size(default 32 MiB) is aboveserver.max_content_length. This is new runtime logic beyond this issue. It is documented in the plugin docs and in the migration guide.🤖 Generated with Claude Code
Summary by Sourcery
Limit request bodies by default and add configurable form-data protection before request parsing and authentication.
Bug Fixes:
Enhancements:
Deployment:
Documentation:
Tests: