You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Prevent servlet snippet injection from corrupting recycled responses - #21104
copilot/task-fix-snippet-injection-issueopen-telemetry/opentelemetry-java-instrumentation:copilot/task-fix-snippet-injection-issueCopy head branch name to clipboard
copilot/task-fix-snippet-injection-issueopen-telemetry/opentelemetry-java-instrumentation:copilot/task-fix-snippet-injection-issueCopy head branch name to clipboard
Prevent recycled Jetty output streams from carrying stale snippet state into later requests. A cached five-byte Content-Length could truncate a 228-byte JSP to 37 bytes.
Scope stream state to the request lifetime in both javax and jakarta instrumentation, while keeping stream and writer matching independent and preserving late content-type support.
Waiting on the author · refreshed 2026-11-24 11:37 UTC
Move out of draft to request review.
Status above doesn't look right?
Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
Anything look wrong?Report it with what you expected; it helps us improve the dashboard.
CopilotAI
changed the title
[WIP] Fix advice failure with snippet injection in Jetty smoke tests
Prevent servlet snippet injection from corrupting recycled responses
Oct 31, 2026
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Mock lifetime and synchronous keep-alive tests do not establish shared-body lifecycle behavior when forwards or async code reset the raw container response, so installed-agent validation is needed.
No open issues 🎉
What changed in this PR
This PR fixes servlet snippet injection state leaking across recycled responses, preventing stale Content-Length values from truncating later responses in both javax and jakarta instrumentation.
Changes:
Bind injection state to the request and response body while preserving async redispatch and late-content-type support.
Share writer and stream tracking, and reset abandoned body state without double-adjusting Content-Length.
Add lifecycle regression tests and a Jetty smoke test that reuses one HTTP/1.1 connection.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Both servlet response wrappers disable stream injection before writer creation succeeds, causing snippet injection to be lost when callers catch a getWriter() failure and continue using the output stream.
[Testing] The new ERROR-dispatch behavior lacks an installed-agent regression: the unit test constructs the wrapper directly, leaving its interaction with the existing-wrapper guard and reset advice untested. Add a regression for both Servlet 3 and Servlet 5 that writes an uncommitted fixed-length body, dispatches to an HTML error servlet, and checks the complete replacement body, exactly one snippet, and matching Content-Length when present. The same gap applies to Servlet5SnippetInjectingResponseWrapper.java:66.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Replacing the request attribute during async redispatch can allow an earlier stream state to be collected, disabling injection on a still-used stream in both servlet wrappers.
The reason will be displayed to describe this comment to others. Learn more.
Note
This is an experimental automated review from the OpenTelemetry PR review workflow. It can miss problems or flag things that are fine, so treat its findings as suggestions.
The reason will be displayed to describe this comment to others. Learn more.
Finding: With snippet injection enabled, the StreamStates request attribute can be read by a wrapper whose class loader differs from the one that stored it. The (StreamStates) cast then throws ClassCastException from getOutputStream() into application code. The same cast exists at Servlet5SnippetInjectingResponseWrapper.java:158.
Why:
StreamStates is a nested helper class in the javaagent module, not a bootstrap or JDK type. All servlet advice is inline = false, so helpers are isolated. Each instrumented class loader gets its own InstrumentationModuleClassLoader, which defines injected helpers self-first (registry, loader).
Wrapping happens in any Filter.doFilter/Servlet.service. A WAR-loaded filter and the container-loaded HttpServlet.service therefore wrap through different helper loaders.
The existing containsHeader comment notes that async dispatch keeps request attributes but not the wrapped response (link). Suppose a REQUEST-only WAR filter wraps the response and calls getOutputStream(), and then startAsync()/dispatch() runs. The container-loaded HttpServlet.service creates a new wrapper, reuses the same request token, and casts the other loader's StreamStates.
This is a plain wrapper method, not suppressed advice, so the exception reaches the servlet.
Repo guidance in instrumentation-state.instructions.md calls for a bootstrap owner when a handoff crosses class-loader boundaries.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prevent recycled Jetty output streams from carrying stale snippet state into later requests. A cached five-byte Content-Length could truncate a 228-byte JSP to 37 bytes.
Scope stream state to the request lifetime in both javax and jakarta instrumentation, while keeping stream and writer matching independent and preserving late content-type support.
Fixes #21096.