Repository navigation
fix: limit request body size - #119
Conversation
Request bodies were read into memory without limit before the access
token was checked, so unauthenticated clients could exhaust the pod's
memory.
- Set Flask's MAX_CONTENT_LENGTH from the new config option
`custom.max_content_length` (default 1 MiB), in a new `create_app()`
used by `main()` and the tests. Werkzeug raises 413 when Connexion
first accesses the request stream (building its request, outside the
security decorator), so before authentication and parsing; declared
sizes are rejected without reading the body, chunked bodies are read
up to the limit only (and truncated, so larger JSON bodies are 400).
- Map RequestEntityTooLarge to problem details ("Content too large").
- Limit the length of strings in the registration schemas of
`additions.openapi.yaml` (URLs 2048, names 1024, description 4096,
other fields 256); the vendored GA4GH specifications are unchanged.
- Helm chart: default Ingress annotation
`nginx.ingress.kubernetes.io/proxy-body-size: "1m"` (via
`ingress.annotations`, removable with `null`), and `tmpSizeLimit`
(default 64Mi) for the `/tmp` emptyDirs of the app and MongoDB.
- Document the limits in the README and the deployment README.
Closes #114
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Bring in #118 (HTTP(S)-only URLs). Conflicts in the README and `additions.openapi.yaml` are resolved by keeping both changes: the URL rules and descriptions of #118, and the `maxLength` limits and request size notes of this branch. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Werkzeug reads bodies of unknown size (chunked) up to MAX_CONTENT_LENGTH only and silently drops the rest, so a valid body padded to the limit and followed by junk was registered. A `before_request` hook now reads one byte more than the limit for requests without `Content-Length` and returns 413 if the body is larger; bodies of exactly the limit pass. Also, from the reviews: - Title 413 responses "Content Too Large" (RFC 9110). - Keep the table of length limits in the README only, mention that they apply to verification bodies, and note that services stored with longer values can't be updated or verified unchanged. - Tests: import the default limit, drop a redundant test, slim down the 413 tests and parametrize the length limits explicitly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The ingress-nginx annotation `proxy-body-size` was hard-coded as "1m" in the values, apart from the app's limit. The Ingress now sets it from `config.custom.max_content_length` (in bytes, default 1048576); annotations in `cloud_registry.ingress.annotations` still take precedence, and `null` removes it. Also, from the reviews: - `tmpSizeLimit` accepts Kubernetes quantities (checked by a pattern) or numbers of bytes; empty strings and 0 are rejected. Numbers from values files (floats) are rendered as integers. - Render the `emptyDir` volumes of both pods with one helper. - Deployment README: describe the derived limit, and point to the documentation of other ingress controllers instead of naming their settings. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A null, zero or non-numeric `config.custom.max_content_length` gave `proxy-body-size: "0"`, i.e., no limit in ingress-nginx, and `config.custom: null` failed with a Go error. - The values schema requires a positive integer, and the helper fails with a clear message otherwise (also without schema validation); floats from values files are accepted if they are integers. - `config.custom: null` is like no custom config: the generated custom config and the default limit are kept. - Define `tmpSizeLimit` once in the schema, and reject all-zero quantities (with `not`, as Go regular expressions have no lookahead). - Note that `check_streamed_size` relies on the cached body, drop an unused argument of a test helper and re-wrap the deployment README. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThe PR adds defense-in-depth request-size controls from application configuration through ingress and pod temporary storage, ensuring oversized declared or chunked bodies are rejected before authentication with consistent 413 problem details. It also adds schema-level limits for registration data, validates Helm configuration and override behavior, and documents and tests the new limits. Sequence diagram for request body size enforcementsequenceDiagram
participant Client
participant App as Flask/Connexion App
participant Auth as Access Token Check
participant Parser as Request Parser
Client->>App: HTTP request
alt Content-Length exceeds MAX_CONTENT_LENGTH
App-->>Client: 413 Content Too Large
else Chunked body exceeds MAX_CONTENT_LENGTH
App->>App: check_streamed_size()
App-->>Client: 413 Content Too Large
else Body within limit
App->>Auth: Check access token
Auth->>Parser: Parse request body
Parser-->>Client: API response
end
Entity relationship diagram for registration field limitserDiagram
SERVICE_REGISTRATION {
string url_2048
string name_1024
string description_4096
string version_256
}
ORGANIZATION {
string name_1024
string url_2048
}
SERVICE_TYPE {
string group_256
string artifact_256
string version_256
}
SERVICE_REGISTRATION ||--o| ORGANIZATION : contains
SERVICE_REGISTRATION ||--|| SERVICE_TYPE : has
File-Level Changes
Assessment against linked issues
Possibly linked issues
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 #119 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 9 9
Lines 654 676 +22
=========================================
+ Hits 654 676 +22 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="cloud_registry/app.py" line_range="28-29" />
<code_context>
+ werkzeug.exceptions.RequestEntityTooLarge: The body is larger than
+ the size limit.
+ """
+ if request.content_length is not None:
+ return # checked by Werkzeug, by the declared size
+ limit = current_app.config["MAX_CONTENT_LENGTH"]
+ request.max_content_length = limit + 1
</code_context>
<issue_to_address>
**Declared bodies bypass limit on unread routes**
When a request declares a body larger than the limit but targets an operation that does not read its request stream, `check_streamed_size` returns for every request with `Content-Length`, relying on Werkzeug to reject the size only when the stream is accessed. A request to an operation that does not consume a body can therefore proceed without a 413 (and a protected operation can check authentication first), contrary to the documented rejection of oversized requests before authentication.
Reject a declared `request.content_length` above the configured limit in the hook before returning.
</issue_to_address>
### Comment 2
<location path="README.md" line_range="197-198" />
<code_context>
+| `description` | 4096 |
+| `version`, `environment`, `type.group`, `type.artifact`, `type.version` | 256 |
+
+Services stored before these limits were introduced may have longer values;
+they can't be updated or verified unchanged, only with the values shortened.
+
See [Access control](#access-control) for the roles and [Verified
</code_context>
<issue_to_address>
**Over-limit services get verified**
When an admin sends an empty or null verification body for a legacy service with over-limit fields, `putServiceVerification` treats the empty or null body as verification of the stored record and writes `VERIFIED` without checking its field lengths, so an over-limit legacy service is verified unchanged despite the README restriction.
Validate the stored service against the new field limits before verification and reject it if any values exceed them.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 2 findings to address first, and the new 64 MiB /tmp limits can cause Kubernetes to evict the app or MongoDB pod if temporary-file use exceeds the cap, creating an outage before the limit is reverted. The request and string limits also reject requests that previously worked, but those effects stop after a revert.
Blocking findings: cloud_registry/app.py:29, README.md:198
From Sourcery's review: declared sizes (`Content-Length`) over the limit were only rejected by Werkzeug when the request stream was read, which Connexion does for the current operations but which isn't guaranteed for operations that don't read their body. The `before_request` hook now rejects them itself. Also correct the README: services stored with longer values can be verified unchanged (with an empty body), only not updated unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Limits the size of request bodies, which were read into memory without limit before the access token was checked. Unauthenticated clients could therefore exhaust the pod's memory (pre-release review finding, #114).
custom.max_content_length(default 1 MiB), set as Flask'sMAX_CONTENT_LENGTHin a newcreate_app(). Bodies with a larger declared size get 413 before authentication and parsing.before_requesthook also rejects chunked bodies larger than the limit with 413. Without it, Werkzeug silently truncated them.additions.openapi.yamlare limited (URLs 2048, names 1024, description 4096, others 256). The vendored GA4GH specs are unchanged.proxy-body-sizeannotation is derived fromconfig.custom.max_content_length. Explicit annotations take precedence, andnullremoves it.config.custom: nullnow behaves like no custom config.tmpSizeLimit(default 64Mi) for the/tmpemptyDirs of the app and MongoDB. All-zero quantities are rejected.Closes #114
Verification
coverage run --source=cloud_registry -m pytest(with helm on PATH): 572 passed, 100 % coverageruff check,ruff format --check,mypy: passhelm lint --strict(default values, CI values, CI values with auth off) andhelm template: passast-grep scan: cleanCI: all checks pass (tests on Python 3.10 and 3.12, Helm chart, Compose, static analysis, codecov, Read the Docs).
Open questions
None.
🤖 Generated with Claude Code
Summary by Sourcery
Protect the application from oversized requests by enforcing configurable body and field limits across the app, API, and Helm deployment.
New Features:
Bug Fixes:
Enhancements:
Deployment:
Documentation:
Tests: