Skip to content

feat(chart): check ingress-nginx body size - #787

Merged
uniqueg merged 5 commits into
devfrom
feat/785-proxy-body-size-check
Nov 18, 2026
Merged

uniqueg merged 5 commits into
devfrom
feat/785-proxy-body-size-check

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Summary

Moves proWES's body-size check (prowes.checkProxyBodySize, prowes#163) into the library chart, as proposed in #785:

  • foca-common.ingress now fails to render if the Ingress's annotation nginx.ingress.kubernetes.io/proxy-body-size is below foca-common.maxContentLength (the app's config.server.max_content_length), as ingress-nginx would answer the requests in between with its own 413 page instead of the app's problem details. The error names the value to set (set it to <n>m or more).
  • Sizes are nginx's: bytes, or with the suffix k, m or g (either case); 0 = no limit. At most 9 digits and no leading zeros, so that Sprig neither reads 010m as octal nor overflows; anything else fails with "must be a size of ingress-nginx".
  • Controller-neutral, no defaults: the library adds no annotation. Without the annotation (or with null), or without an app limit, nothing is checked. The check runs on the merged annotations, so it is unaffected by the cert-manager annotations of ingress.https.
  • Messages use <valuesKey>.ingress.annotations, so proWES's tests keep passing once it drops its own copy (follow-up drafted for proWES).
  • Docs: "Exposing the app" (Body size) and "ingress-nginx" (Body size) in docs/helm-chart.md; chart release notes docs/releases/chart/0.6.0.md. The chart version stays 0.6.0: it is unreleased (latest tag chart-v0.5.0), and the repo's convention is one version for all changes up to the next release.

Verification

  • New tests first (red: 15 failed), then green: pytest charts/foca-common/tests (Helm v3.18.6) -> 467 passed.
  • pytest tests/test_version_mentions.py tests/test_chart_gateway_keys.py -> 307 passed (the first CI run caught a hard-coded chart version in the docs, fixed in ff2782a).
  • helm lint --strict of charts/foca-common, the petstore chart with every ci/*-values.yaml, and the gateway chart: all pass.
  • ruff check / ruff format --check charts/foca-common/tests: pass. (mypy's configured paths don't include charts/.)
  • Snapshots unchanged.
  • After 434e4c5 (docs and a template comment): pytest charts/foca-common/tests tests/test_version_mentions.py -> 553 passed; helm lint --strict charts/foca-common passes.
  • CI: green on ff2782a after re-running one job that failed on an unrelated flaky test (tests/utils/test_http.py::test_address_fallback_bounded, Python 3.12 only; 5248 other tests passed there).

Declined review findings

  • Adversarial (low): for app limits of about a petabyte or more, the hint "set it to m or more" can name a value with more than 9 digits (which the check refuses), and near the int64 maximum the rounding overflows; pass/fail stays correct. Declined: unrealistic limits, and the hint is only a hint.

  • Quality (medium): a numeric (unquoted) annotation would be read as, e.g., 2e+07. Declined as a template change: the app values schema already refuses numbers ("got number, want null or string"); a test now pins that.

  • Quality (low): more rounding/boundary cases. Declined: the boundaries (10485759, 10239K, 11m, 1025m) are covered.

  • Sourcery (medium): reject the g suffix. Declined: nginx's client_max_body_size is an offset (ngx_parse_offset), which accepts g/G.

  • Sourcery (medium): skip the format check without an app limit. Kept the check (a malformed size breaks nginx's config anyway); fixed the docs, which said nothing was checked then (434e4c5).

Fixed from review: stale "the library doesn't use maxContentLength" in the helper comment and the Settings table; the Templates table now says that foca-common.ingress can fail; tests for the limit from the app's settings; release notes list the change under Breaking changes (renders that passed before can fail).

Open questions

None.

Closes #785

🤖 Generated with Claude Code

Summary by Sourcery

Add ingress-nginx body-size validation to prevent requests from being rejected by the ingress controller before reaching the application.

New Features:

  • Validate ingress-nginx request body-size annotations against the application’s configured maximum content length during chart rendering.
  • Support ingress-nginx size formats and provide actionable errors for invalid or insufficient limits.

Enhancements:

  • Keep the chart controller-neutral by applying validation only when the ingress-nginx annotation is explicitly provided, without adding a default annotation.

Documentation:

  • Document ingress-nginx body-size validation and its breaking-change behavior in the Helm chart documentation and release notes.

Tests:

  • Add coverage for valid, invalid, boundary, missing, and settings-derived body-size limits.

uniqueg and others added 2 commits November 18, 2026 09:52
The Ingress of foca-common fails to render if its annotation
nginx.ingress.kubernetes.io/proxy-body-size is below the app's limit
(config.server.max_content_length), as ingress-nginx would answer the
requests in between with its own 413 page instead of the app's problem
details. The error names the value to set. Sizes are those of nginx
(bytes, or with the suffix k, m or g; 0 for no limit), with at most 9
digits and no leading zeros, so that Sprig neither reads them as octal
nor overflows.

The library adds no annotation: without one, or without an app limit,
nothing is checked. Ported from prowes.checkProxyBodySize (prowes#163).

Refs #785

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Settings and Templates tables and the maxContentLength helper's
comment said that the library doesn't use the app's limit; the Ingress
now checks ingress-nginx's annotation against it. Adds tests for the
limit from the app's settings and for numeric annotations, which the
schema refuses.

Refs #785

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg uniqueg self-assigned this Nov 18, 2026
@sourcery-ai

sourcery-ai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Add a helper that validates an explicitly configured ingress-nginx body-size annotation against the app limit, and invoke it while rendering the Ingress.

  • charts/foca-common/templates/_helpers.tpl L670-673
  • charts/foca-common/templates/_ingress.tpl L6-8, L29-30, L64-99

Documentation

Explain the body-size check and its accepted formats, behavior, and remediation in the chart guide and release notes.

  • docs/helm-chart.md L391-402 (+3 small edits)
  • docs/releases/chart/0.6.0.md L1-5

Tests

Cover accepted, undersized, and invalid annotation values, including app settings and certificate annotations.

  • charts/foca-common/tests/test_templates.py L1381-1527

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

uniqueg and others added 2 commits November 18, 2026 10:02
test_chart_version_not_hard_coded refuses the current chart version in
the docs; the release notes say which version adds the check.

Refs #785

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (d881bcc) to head (434e4c5).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #787   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           75        75           
  Lines        10025     10025           
=========================================
  Hits         10025     10025           
Flag Coverage Δ
test_unit 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 18, 2026 09:35
@sourcery-ai

sourcery-ai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Linked issues

Checked at commit 434e4c5

#785 feat(chart): check ingress-nginx body size against the app limit · 5 of 5 met

# Requirement Status Notes
1 Fail rendering when the Ingress body-size annotation is below the app's limit, with a hint for what to set. ✅ Met The check compares the merged annotation against foca-common.maxContentLength and reports the minimum value to set; tests cover values below the app limit.
2 Handle nginx body-size values in bytes or with k, m or g suffixes; 0 means no limit. ✅ Met The helper accepts these formats, including either suffix case, and tests cover valid, unlimited, and invalid values.
3 Keep the library controller-neutral and do not add a default body-size annotation. ✅ Met The check runs only when the merged Ingress annotations contain the nginx annotation; the template adds no default, and the docs explain that app charts can set one.
4 Add tests in charts/foca-common/tests/test_templates.py. ✅ Met Tests cover below-limit failures, accepted sizes, invalid values, absent or null annotations, and the app's settings limit.
5 Document the check in the Helm chart guide's body-size sections. ✅ Met The guide updates both “Exposing the app” and “ingress-nginx” body-size guidance.

@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 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="charts/foca-common/templates/_ingress.tpl" line_range="87" />
<code_context>
+{{- $name := "nginx.ingress.kubernetes.io/proxy-body-size" -}}
+{{- if hasKey .annotations $name -}}
+{{- $size := toString (index .annotations $name) -}}
+{{- if not (regexMatch "^(0|[1-9][0-9]{0,8})[kKmMgG]?$" $size) -}}
+{{- fail (printf "%s.ingress.annotations: %s must be a size of ingress-nginx (at most 9 digits without leading zeros, bytes or with the suffix k, m or g, e.g., 10m; \"0\" for no limit), not %q" $key $name $size) -}}
+{{- end -}}
</code_context>
<issue_to_address>
**Gigabyte body limits are rejected**

When the `proxy-body-size` annotation uses a `g` or `G` suffix, `checkProxyBodySize` accepts and converts the suffix, but the annotation reaches ingress-nginx unchanged; NGINX rejects the generated configuration, so the intended limit is not applied and requests above the controller's existing limit receive 413 responses.

Reject `g` and `G` suffixes in the validation and remove the gigabyte conversion factor.
</issue_to_address>

### Comment 2
<location path="charts/foca-common/templates/_ingress.tpl" line_range="87-88" />
<code_context>
+{{- $name := "nginx.ingress.kubernetes.io/proxy-body-size" -}}
+{{- if hasKey .annotations $name -}}
+{{- $size := toString (index .annotations $name) -}}
+{{- if not (regexMatch "^(0|[1-9][0-9]{0,8})[kKmMgG]?$" $size) -}}
+{{- fail (printf "%s.ingress.annotations: %s must be a size of ingress-nginx (at most 9 digits without leading zeros, bytes or with the suffix k, m or g, e.g., 10m; \"0\" for no limit), not %q" $key $name $size) -}}
+{{- end -}}
+{{- $number := regexFind "^[0-9]+" $size -}}
</code_context>
<issue_to_address>
**Skip validation without app limit**

The issue says nothing is checked without an app limit. The helper validates the annotation's syntax before determining whether an app limit exists, so a malformed value still fails rendering in that case.
</issue_to_address>

Sourcery assessment

Approval pending. 2 findings to address first.

Blocking findings: charts/foca-common/templates/_ingress.tpl:87, charts/foca-common/templates/_ingress.tpl:88


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

Without an app limit, only the comparison is skipped: a value that is
not a size of ingress-nginx still fails to render, as nginx would
refuse it, too. The docs said nothing was checked then.

Refs #785

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg
uniqueg merged commit 9b24103 into dev Nov 18, 2026
83 checks passed
@uniqueg
uniqueg deleted the feat/785-proxy-body-size-check branch November 18, 2026 10:21
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.

feat(chart): check ingress-nginx body size against the app limit

1 participant