Skip to content

fix!: limit request bodies by default - #813

Merged
uniqueg merged 8 commits into
devfrom
issue-798-body-size-limit
Nov 21, 2026
Merged

uniqueg merged 8 commits into
devfrom
issue-798-body-size-limit

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

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 default MAX_CONTENT_LENGTH that the issue asks for in the same fix:

  • server.max_content_length now defaults to 10 MiB; before, there was no limit. Setting it to null removes the limit.
  • The new option server.max_form_memory_size defaults to 500 kB, as in Flask, and is set as Flask's MAX_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.
    • A body whose declared size is over the limit gets 413 (problem details) without being read.
    • A chunked body is read up to the limit plus one byte.
    • Both hold for every operation and before authentication.
  • Werkzeug's RequestEntityTooLarge was already mapped to problem details with status 413.
  • Helm library chart (unreleased 0.6.0): the values schema now allows null for both limits and checks max_form_memory_size. The docs tell app charts to set maxContentLength: 10485760 if their base config sets no limit, so that the Ingress check applies. The petstore example chart now does this.
  • Docs: a new "Request body size" section in 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, hence fix!.

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:
    • the defaults, null, and validation of both options;
    • URL-encoded bodies over the form limit, declared and chunked, including mixed-case media types and media types with parameters, with no other limit set;
    • the exact boundary;
    • bodies that are not URL-encoded;
    • the lower of the two limits winning;
    • the multipart field limit;
    • a limit added to the Flask config later.
    • A regression test checks that an anonymous URL-encoded body over the limit sent to a secured operation gets 413 with Werkzeug's _parse_urlencoded patched to fail, and that neither the security handler nor the operation runs. Without the fix, the same request was parsed and then answered with 401.
  • Full unit suite with coverage, run locally: 5291 passed. The only failures and errors are tests that need a real MongoDB (none in the local sandbox; CI provides one). The failures are identical with and without this branch's changes. foca/factories/connexion_app.py and foca/models/config.py are at 100% coverage.
  • ruff format --check, ruff check, mypy, ast-grep scan, mkdocs build --strict: all pass.
  • Charts with Helm 3.22.1: helm lint --strict passes on the library chart and on the petstore and gateway charts with all CI values, and pytest charts/foca-common/tests gives 510 passed.
  • New integration tests in tests/integration_tests.py cover the petstore with the default limits: a URL-encoded body over 500 kB and a body over 10 MiB both get 413. 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: all checks pass (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

  • Make the chart helper foca-common.maxContentLength fall 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 set proxy-body-size below 10m. Instead, the default is documented in docs/helm-chart.md, in the helper comment and in the 0.6.0 chart release notes, and app charts are told to set maxContentLength.
  • Warn at startup if the WES attachment max_size (default 32 MiB) is above server.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:

  • Reject oversized request bodies with HTTP 413 before parsing or authentication, including URL-encoded form payloads and chunked requests.

Enhancements:

  • Add configurable request and form-data size limits, defaulting to 10 MiB and 500 kB respectively, with support for disabling either limit.
  • Update Helm chart validation and examples to account for the new body-size defaults and nullable settings.
  • Document request-body limits, deployment considerations, plugin interactions, and migration guidance.

Deployment:

  • Align Helm ingress body-size checks and the petstore chart with FOCA's default request-body limit.

Documentation:

  • Document the new request-body and form-data limits across server configuration, deployment, Helm, plugin, and migration documentation.

Tests:

  • Add coverage for default and nullable limits, content types, declared and chunked bodies, boundary conditions, multipart fields, authentication ordering, and integration behavior.

uniqueg and others added 7 commits November 21, 2026 16:58
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]>
@sourcery-ai

sourcery-ai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Data structures

Add default request-body and form-memory limits to the server configuration model, including their validation and field documentation.

  • foca/models/config.py L336-348, L369, L388-389

API

Update chart values schemas to allow null body-size limits and validate the new form-memory setting.

  • charts/foca-common/app-values.schema.json L513-520
  • examples/petstore/chart/values.schema.json L513-520

Implementation

Enforce the lower applicable limit for URL-encoded bodies before request handling and configure Flask with both server limits.

  • foca/factories/connexion_app.py L110-119, L151-156 (+3 small edits)

Configuration

Update the Helm helper and example chart settings so Ingress checks account for FOCA's default body-size limit.

  • charts/foca-common/templates/_helpers.tpl L711-718
  • examples/petstore/chart/templates/_helpers.tpl L8-10

Set and describe the default request-body and form-data limits in the configuration template and Gunicorn deployment settings.

  • foca/gunicorn_config.py L108-110
  • templates/config.yaml L18-24

Documentation

Document the request-body and form-data limits, their operational implications, and the migration steps for existing configurations.

  • docs/configuration/plugins.md L335-338
  • docs/configuration/server.md L11-12, L17-19, L32-74
  • docs/deployment.md L93, L140-144
  • docs/migration-0.23.md L22-27, L305-329
  • docs/plugins.md L596-599

Explain the new chart limit defaults and the explicit app-chart setting needed for ingress validation.

  • docs/helm-chart.md L88, L248-251, L400-403
  • docs/releases/chart/0.6.0.md L12

Tests

Add unit coverage for default and nullable limits, URL-encoded and multipart enforcement, boundary cases, and rejection before authentication.

  • tests/factories/test_connexion_app.py L225-258, L555-677, L784-832 (+2 small edits)

Update chart tests for the default limit, explicit null override, and validation of the form-memory setting.

  • charts/foca-common/tests/test_templates.py L1395-1398, L1557-1586 (+2 small edits)

Add petstore integration checks that oversized URL-encoded and general request bodies receive 413 responses under the defaults.

  • tests/integration_tests.py L27-29, L200-233

Other changes

Files not grouped.

  • examples/petstore/config.yaml L1-2

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (267d8f3) to head (aafc558).

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     
Flag Coverage Δ
test_unit 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review November 21, 2026 21:02
@sourcery-ai

sourcery-ai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Linked issues

Checked at commit aafc558

#798 fix: don't parse request bodies before auth · 4 of 4 met

# Requirement Status Notes
1 Reject oversized URL-encoded bodies with 413 before request handling, for all operations, using a configurable limit. ✅ Met The before-request hook applies the lower of the content and form limits to URL-encoded bodies; the form limit defaults to 500 kB and can be set to null.
2 Test that an anonymous oversized URL-encoded request gets a response without parsing the form. ✅ Met The regression test patches Werkzeug's URL-encoded parser, then verifies a 413 and that neither authentication nor the operation runs.
3 Document the security implications and configuration of the limit. ✅ Met The server configuration docs explain that bodies are parsed before authentication, describe both limits, and show how to raise or remove them.
4 Add a configurable default request-body limit and return problem details with status 413 for oversized bodies. ✅ Met The content limit defaults to 10 MiB, and Werkzeug's RequestEntityTooLarge is mapped to FOCA's PayloadTooLarge error.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread templates/config.yaml Resolved
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]>

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery assessment

Approved.

@uniqueg
uniqueg merged commit e0822b9 into dev Nov 21, 2026
58 checks passed
@uniqueg
uniqueg deleted the issue-798-body-size-limit branch November 21, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: don't parse request bodies before auth

1 participant