Guard header copy against committed response in NettyRoutingFilter - #4272
Guard header copy against committed response in NettyRoutingFilter#4272adityaanikam wants to merge 2 commits into
Conversation
NettyRoutingFilter unconditionally called response.getHeaders() .remove(...) and .addAll(...) when copying the filtered response headers onto the client response. Once a response is committed, AbstractServerHttpResponse#getHeaders() returns a cached ReadOnlyHttpHeaders wrapper, and mutating it throws UnsupportedOperationException. Guard the header-copy step with response.isCommitted(), matching the same check already used elsewhere in this codebase (see BaseWebClientTests#modifyResponseFilter). Headers can no longer reach the client past that point regardless, so skipping is safe. Fixes spring-cloudgh-4270 Signed-off-by: adityaanikam <adityanikam9502@gmail.com>
HDPark95
left a comment
There was a problem hiding this comment.
filteredResponseHeaders.remove(TRANSFER_ENCODING) (L182) is a no-op: the enclosing if already requires !filteredResponseHeaders.containsHeader(TRANSFER_ENCODING). The old line mutated response.getHeaders(), so this drops the scrub rather than guarding it.
Checked on main: a probe that presets Transfer-Encoding on the response and routes to a Content-Length upstream passes on main and fails with this patch, leaving both headers set. Keeping response.getHeaders().remove(...) inside your else branch preserves it.
Prior art on the same guard: #4115.
The previous commit moved the Transfer-Encoding removal from response.getHeaders() onto filteredResponseHeaders. That branch is only entered when filteredResponseHeaders does not contain Transfer-Encoding, so removing it there is a no-op, and the scrub of a stale Transfer-Encoding already set on the downstream response was lost. Restore the original response.getHeaders().remove(...) and move the whole block inside the not-committed branch, where the headers are still mutable. When the response is committed nothing is written either way, so the scrub is not needed there. Add a test that presets Transfer-Encoding on the response and routes to an upstream answering with Content-Length. It fails without this change, leaving both headers set. Signed-off-by: adityaanikam <adityanikam9502@gmail.com>
|
Good catch, thanks you're right on both counts. The enclosing Pushed a fix: I also added a test matching the probe you described it presets |
Fixes gh-4270
Problem
NettyRoutingFiltercopies filtered response headers back onto theclient response near the end of
filter():If the response has already been committed elsewhere in the filter
chain before routing completes,
AbstractServerHttpResponse#getHeaders()returns a cached
ReadOnlyHttpHeaderswrapper instead of the mutableheaders map. Calling
remove(...)oraddAll(...)on that wrapperthrows
UnsupportedOperationException, which surfaces as an errorsignal on the filter chain instead of the response completing
normally.
Fix
Guard the header-copy step with
response.isCommitted(). Once aresponse is committed, headers can no longer reach the client
regardless, so skipping the copy is safe and matches an existing
precedent already in the test codebase
(
BaseWebClientTests#modifyResponseFilter, which uses the sameisCommitted()check before touching response headers).Testing
NettyRoutingFilterTests#routingAfterResponseAlreadyCommittedDoesNotThrow,which pre-commits a
MockServerWebExchangeand assertsnettyRoutingFilter.filter(...)completes normally instead oferroring.
NettyRoutingFilterTestssuite passes.the new test) reproduces the exact reported
UnsupportedOperationExceptionatReadOnlyHttpHeaders.remove(ReadOnlyHttpHeaders.java:166), matchingthe original issue's stack trace. Restoring the fix passes cleanly.