Skip to content

fix(divide): enforce requestMaxSize on chunked request bodies - #8033

Merged
yuluo-yx merged 2 commits into
apache:masterfrom
Sean-Walker0:fix/7816-divide-request-max-size-chunked-rebuilt
Oct 24, 2026
Merged

yuluo-yx merged 2 commits into
apache:masterfrom
Sean-Walker0:fix/7816-divide-request-max-size-chunked-rebuilt

Conversation

@Sean-Walker0

Copy link
Copy Markdown
Contributor

Fixes #7816. Supersedes #7820 (approved, but it conflicted with master after #7857 moved the selection/forwarding into forwardSelected with canary routing — this is the same fix rebased onto that structure, as requested in the review follow-up).

Modifications

A positive divide rule requestMaxSize was only compared against the Content-Length header. Chunked or unknown-length requests report -1, so the comparison succeeded without examining a single body byte — oversized streaming bodies were forwarded.

For unknown-length requests the limit is enforced on the actual bytes: the request is decorated with a counting body that resets per subscription (Flux.defer) and aborts the stream with ResponseStatusException(PAYLOAD_TOO_LARGE) (which GlobalErrorHandler already honours) once the forwarded size exceeds the maximum. The known-length fast path is unchanged; the decorated exchange is threaded through the forwardSelected call so canary selection, attributes and circuit-breaker recording all operate on it.

Verifying this change

  • doExecuteRejectsChunkedBodyExceedingRequestMaxSize — 2-byte chunked body under a 1-byte limit; the downstream drains the body and the stream must abort with ResponseStatusException carrying PAYLOAD_TOO_LARGE. Red on the un-fixed code (verified by temporarily bypassing the decorator), green after.
  • ./mvnw -pl shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide -am test -B — full module 103/103 green (all canary/circuit-breaker suites from [ISSUE #7826][ISSUE #7760] Reuse Divide keys and enforce one recovery probe #7857 included), checkstyle clean.

Notes

Make sure that:

  • You have read the contribution guidelines.
  • You submit test cases (unit or integration tests) that back your changes.
  • Your local test passed ./mvnw clean install -Dmaven.javadoc.skip=true (module-scoped: shenyu-plugin-divide with -am, 103 tests + checkstyle green).

A positive divide rule requestMaxSize was only compared against the
Content-Length header. Chunked or unknown-length requests report -1
for that header, so the comparison succeeded without examining a
single body byte and oversized streaming bodies were forwarded.

For unknown-length requests the limit is now enforced on the actual
bytes: the request is decorated with a counting body that resets per
subscription and aborts the stream with 413 (PAYLOAD_TOO_LARGE, which
GlobalErrorHandler already honours) once the forwarded size exceeds
the configured maximum. The known-length fast path is unchanged.

Rebased onto current master, which moved the selection/forwarding into
forwardSelected with canary routing and circuit-breaker candidates;
the decorated exchange is threaded through the forwardSelected call.
Regression test posts a 2-byte chunked body under a 1-byte limit and
asserts a PAYLOAD_TOO_LARGE ResponseStatusException (red on the
previous code, which forwarded it).

@Aias00 Aias00 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The streaming guard is the right shape: it only engages when requestMaxSize > 0 and Content-Length is absent, so known-length bodies keep the existing fast path and there is no double accounting with the header check above it.

Two things to consider, neither blocking:

  1. forwarded is a mutable field on the anonymous decorator, reset inside Flux.defer. That is correct for the sequential (and retry) case, because each subscription restarts the count. A concurrent second subscription of getBody() on the same exchange would race on it, though. If that can happen through the retry strategy, an AtomicLong removes the doubt; otherwise a comment stating the single-subscription assumption is enough.

  2. The 413 is raised from inside the body Flux, so it surfaces from chain.execute(...) rather than from the plugin's return value. The test proves the error type reaches the subscriber, but please confirm it reaches the client as a 413 response body rather than a bare stream error — the header-based path above returns a normal WebFluxResultUtils result, so the two rejection paths would otherwise behave differently at the edge.

Approving; the above can be follow-ups.

@yuluo-yx

Copy link
Copy Markdown
Member

/retest

@yuluo-yx
yuluo-yx merged commit a0b196a into apache:master Oct 24, 2026
29 checks passed
dmsolr pushed a commit that referenced this pull request Oct 25, 2026
Sync to current master to resolve the stable shenyu-integrated-test-http CI failure (CachePluginTest.testMemoryModle/testRedisModle). Root cause: this branch predated #8079, whose pre-fix CachedResponse.capture() refused to store any response carrying a Vary header, so the cached endpoint was never reused and both requests hit the backend. #8079 keys Vary responses on the selecting request headers instead. Also pulls in the divide/loadbalancer work (#8076, #7172, #7986, #8005, #7821, #8033, #8090).

#7172 removeStaleUpstreams reconciles via removeFromMap/triggerRemoveOne, which republish the immutable healthy snapshot under the same lock, so the read-only snapshot model stays correct with no stale retained instances.
@Aias00 Aias00 added this to the 2.7.2 milestone Nov 13, 2026
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.

[BUG] Divide requestMaxSize is not enforced for chunked or unknown-length request bodies

3 participants