Skip to content

fix: register body parser for text/plain to stream payloads - #494

Open
abdul-karim-mia wants to merge 2 commits into
fastify:mainfrom
abdul-karim-mia:fix/stream-text-plain-payloads
Open

abdul-karim-mia wants to merge 2 commits into
fastify:mainfrom
abdul-karim-mia:fix/stream-text-plain-payloads

Conversation

@abdul-karim-mia

Copy link
Copy Markdown

Fixes #493

Context & Problem

As documented in the README:

Note that this plugin is fully encapsulated, and non-JSON payloads will be streamed directly to the destination.

Fastify core registers default content-type parsers for both application/json and text/plain. Previously, @fastify/http-proxy registered its pass-through bodyParser for application/json and wildcard *.

Because Fastify routes exact content-type matches before wildcard parsers, requests with content-type: text/plain matched Fastify's built-in parser instead of being streamed to the destination. Consequently:

  1. text/plain payloads in non-UTF-8 encodings (e.g. ISO-8859-1) were decoded as UTF-8 strings and corrupted.
  2. text/plain payloads larger than the Fastify bodyLimit (default 1 MiB) were rejected with 413 FST_ERR_CTP_BODY_TOO_LARGE instead of streaming.

Solution

Register the pass-through bodyParser for text/plain alongside application/json when opts.proxyPayloads !== false.

Validation

  • Added a test in test/test.js verifying that non-UTF-8 bytes and bodies larger than 1 MiB are streamed to upstream without corruption or rejection.
  • All 145 tests pass with 100% code coverage (borp -C --check-coverage).
  • ESLint and TSTyche type checks pass cleanly (npm test).

)

Signed-off-by: Abdul karim mia <97598943+abdul-karim-mia@users.noreply.github.com>
@SameRainbows

This comment was marked as spam.

This branch has not been deployed

No deployments
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.

text/plain request bodies are parsed instead of streamed: non-UTF-8 bytes corrupted, bodies over 1 MiB rejected with 413

3 participants