Skip to content

feat: allow overriding HTTP parser body length via env var - #265

Merged
Clivern merged 4 commits into
Clivern:mainfrom
ngdangdat:feat/configurable-parser-length
Apr 29, 2026
Merged

feat: allow overriding HTTP parser body length via env var#265
Clivern merged 4 commits into
Clivern:mainfrom
ngdangdat:feat/configurable-parser-length

Conversation

@ngdangdat

@ngdangdat ngdangdat commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Make Plug.Parsers's :length configurable via APP_HTTP_MAX_BODY_LENGTH (defaults to the current 8 MB).

Reference: Plug.Parsers — Options:length sets the maximum body length the parser accepts; it defaults to 8_000_000 bytes.

Why

Pushing a Terraform state larger than 8 MB fails with:

Acquiring state lock. This may take a few moments...
Failed to persist state: HTTP error: 413
ls -lah errored.tfstate
-rw-rw-r-- 1 michael michael 8.0M Apr 29 06:24 errored.tfstate

The errored.tfstate is exactly 8.0M, which trips Plug.Parsers's default :length of 8_000_000 bytes in lib/lynx_web/endpoint.ex:52. State files in real-world projects routinely grow past that, so the cap needs to be tunable per-deployment.

Change

Wired the limit through application config (sitting next to the existing LynxWeb.Endpoint config keys, matching the APP_HOST pattern already there):

# config/config.exs
config :lynx, LynxWeb.Endpoint,
  ...
  http_max_body_length: String.to_integer(System.get_env("APP_HTTP_MAX_BODY_LENGTH") || "8000000")
# lib/lynx_web/endpoint.ex
@max_body_length Application.compile_env(
                   :lynx,
                   [__MODULE__, :http_max_body_length],
                   8_000_000
                 )

plug Plug.Parsers,
  parsers: [:urlencoded, :multipart, :json],
  pass: ["*/*"],
  length: @max_body_length,
  json_decoder: Phoenix.json_library()

Default behavior is unchanged. Operators bumping into 413s can set e.g. APP_HTTP_MAX_BODY_LENGTH=104857600 for 100 MB before building/booting the app.

Note: because Application.compile_env/3 resolves at compile time, the env var needs to be set when the release is built — same lifecycle as the other env-driven config keys here (APP_HOST, APP_SECRET).

Tests

test/lynx_web/endpoint_test.exs covers:

  • :http_max_body_length defaults to 8_000_000 so existing deployments are unaffected.
  • The configured value is accepted by Plug.Parsers.init/1 as :length.

Test plan

  • mix test test/lynx_web/endpoint_test.exs is green.
  • Boot without the env var — uploads up to 8 MB still work, >8 MB returns 413 (current behavior).
  • Set APP_HTTP_MAX_BODY_LENGTH=33554432 and confirm a ~16 MB tfstate push succeeds.

@ngdangdat
ngdangdat marked this pull request as ready for review April 29, 2026 09:07
@ngdangdat
ngdangdat requested a review from Clivern as a code owner April 29, 2026 09:07
@ngdangdat

ngdangdat commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

Hi @Clivern,

Please help to review the PR when you have time. I'm facing this issue when using Lynx in my production environment 🙇

tofu apply
...
Acquiring state lock. This may take a few moments...
Failed to persist state: HTTP error: 413

Thank you in advance!

clv0x
clv0x approved these changes Apr 29, 2026

@Clivern Clivern left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

lgtm

@Clivern
Clivern merged commit a0a56bc into Clivern:main Apr 29, 2026
8 checks passed
Clivern added a commit that referenced this pull request Apr 29, 2026
@Clivern

Clivern commented Apr 29, 2026

Copy link
Copy Markdown
Owner

Created a new docker image https://hub.docker.com/layers/clivern/lynx/0.12.10/images/sha256-80a0def2cc281d450052ba9d5bbdc229cfdebef009d680d5e299ea5f395731b9

@ngdangdat
ngdangdat deleted the feat/configurable-parser-length branch April 29, 2026 10:40
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.

3 participants