Skip to content

Prevent servlet snippet injection from corrupting recycled responses - #21104

Draft
trask with Copilot wants to merge 18 commits into
mainfrom
copilot/task-fix-snippet-injection-issue
Draft

trask with Copilot wants to merge 18 commits into
mainfrom
copilot/task-fix-snippet-injection-issue

Conversation

Copilot AI commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

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.

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

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.

Copilot AI 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
Copilot AI requested a review from trask October 31, 2026 18:48
@trask
trask requested a balanced review from Copilot October 31, 2026 18:55

Copilot AI 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.

🔵 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.
File Description
smoke-tests/​src/​test/​java/​io/​opentelemetry/​smoketest/​appserver/​JettySmokeTest.java Tests complete JSP output after a non-HTML response on the same connection.
instrumentation/​servlet/​servlet-common/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​servlet/​common/​snippet/​SnippetInjectingPrintWriter.java Accepts shared injection state and marks writer ownership.
instrumentation/​servlet/​servlet-common/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​servlet/​common/​snippet/​OutputStreamSnippetInjectionHelper.java Skips stream injection when the writer owns injection.
instrumentation/​servlet/​servlet-common/​bootstrap/​src/​main/​java/​io/​opentelemetry/​javaagent/​bootstrap/​servlet/​InjectionState.java Tracks request lifetime, output ownership, resets, and Content-Length adjustments.
instrumentation/​servlet/​servlet-5.0/​javaagent/​src/​unitTests/​java/​io/​opentelemetry/​javaagent/​instrumentation/​servlet/​v5_0/​snippet/​InjectionStateLifetimeTest.java Covers jakarta response reuse, redispatch, errors, and resets.
instrumentation/​servlet/​servlet-5.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​servlet/​v5_0/​snippet/​ServletOutputStreamInjectionState.java Shares body state and clears expired jakarta stream ownership.
instrumentation/​servlet/​servlet-5.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​servlet/​v5_0/​snippet/​Servlet5SnippetInjectingResponseWrapper.java Manages request-bound state and body resets for jakarta responses.
instrumentation/​servlet/​servlet-5.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​servlet/​v5_0/​service/​JakartaServletServiceAdvice.java Passes the request into the jakarta response wrapper.
instrumentation/​servlet/​servlet-3.0/​javaagent/​src/​unitTests/​java/​io/​opentelemetry/​javaagent/​instrumentation/​servlet/​v3_0/​snippet/​InjectionStateLifetimeTest.java Covers javax response reuse, redispatch, errors, and resets.
instrumentation/​servlet/​servlet-3.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​servlet/​v3_0/​snippet/​ServletOutputStreamInjectionState.java Shares body state and clears expired javax stream ownership.
instrumentation/​servlet/​servlet-3.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​servlet/​v3_0/​snippet/​Servlet3SnippetInjectingResponseWrapper.java Manages request-bound state and body resets for javax responses.
instrumentation/​servlet/​servlet-3.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​servlet/​v3_0/​Servlet3Advice.java Passes the request into the javax response wrapper.

Review effort: Balanced

Copilot AI 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.

🟡 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.

1 open issue

Review effort: Balanced

Copilot AI 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.

🔵 Needs a closer look

The ERROR-dispatch behavior lacks installed-agent regression coverage for Servlet 3 and Servlet 5, leaving wrapper-guard and reset-advice interactions, replacement-body completeness, single-snippet injection, and Content-Length consistency unverified.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Add installed-agent regression for ERROR-dispatch response replacement

instrumentation/​servlet/​servlet-3.0/​javaagent/​src/​main/​java/​io/​opentelemetry/​javaagent/​instrumentation/​servlet/​v3_0/​snippet/​Servlet3SnippetInjectingResponseWrapper.java:74

[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.

1 resolved since last review

Review effort: Balanced

Copilot AI requested a review from trask October 31, 2026 21:57
@trask
trask requested a balanced review from Copilot October 31, 2026 22:03
Copilot AI requested a review from trask November 27, 2026 23:01
@trask
trask requested a balanced review from Copilot November 27, 2026 23:06

Copilot AI 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.

🟡 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.

1 open issue

Review effort: Balanced

Copilot AI 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.

🟢 Approval recommended

No active review issues require resolution for the servlet snippet-injection lifecycle change.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@otelbot otelbot Bot 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.

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.

streamInjectionState = state;
}
if (request != null) {
StreamStates states = (StreamStates) request.getAttribute(STREAM_STATE_ATTRIBUTE);

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.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 50ad06b6 with a shared bootstrap holder. Installed-agent regression coverage verifies the cross-loader redispatch in both servlet namespaces.

Copilot AI 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.

🟢 Approval recommended

No active review issues remain for the request-scoped servlet snippet-injection change.

No open issues 🎉

Review effort: Balanced

Copilot AI and others added 18 commits November 28, 2026 10:09
…etime

Co-authored-by: trask <218610+trask@users.noreply.github.com>
Co-authored-by: trask <218610+trask@users.noreply.github.com>
Co-authored-by: trask <218610+trask@users.noreply.github.com>
Co-authored-by: trask <218610+trask@users.noreply.github.com>
Co-authored-by: trask <218610+trask@users.noreply.github.com>
Co-authored-by: trask <218610+trask@users.noreply.github.com>
Co-authored-by: trask <218610+trask@users.noreply.github.com>
Co-authored-by: trask <218610+trask@users.noreply.github.com>
Co-authored-by: trask <218610+trask@users.noreply.github.com>
Co-authored-by: trask <218610+trask@users.noreply.github.com>
Co-authored-by: trask <218610+trask@users.noreply.github.com>
Co-authored-by: trask <218610+trask@users.noreply.github.com>
Co-authored-by: trask <218610+trask@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

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.

Advice failure with snippet injection in jetty smoke tests

4 participants