-
Notifications
You must be signed in to change notification settings - Fork 1k
JavaScript Snippet Injection Support Servlet 5 #8569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fd316fb
support servlet 5
siyuniu-ms 250e249
Update JakartaServletServiceAdvice.java
siyuniu-ms e033a46
Update OutputStreamSnippetInjectionHelper.java
siyuniu-ms 7e77d6b
Update JakartaServletServiceAdvice.java
siyuniu-ms 033d665
Sharing
trask 7476083
remove redundancy files
siyuniu-ms 5243b27
add unit test
siyuniu-ms f47b60b
Update OutputStreamSnippetInjectionHelper.java
siyuniu-ms ba2d1d4
Update RequestDispatcherServlet.java
siyuniu-ms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
34 changes: 34 additions & 0 deletions
34
...elemetry/javaagent/instrumentation/servlet/v5_0/Servlet5OutputStreamWriteBytesAdvice.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.servlet.v5_0; | ||
|
|
||
| import static io.opentelemetry.javaagent.instrumentation.servlet.v5_0.Servlet5Singletons.getSnippetInjectionHelper; | ||
|
|
||
| import io.opentelemetry.javaagent.instrumentation.servlet.snippet.InjectionState; | ||
| import io.opentelemetry.javaagent.instrumentation.servlet.v5_0.snippet.ServletOutputStreamInjectionState; | ||
| import jakarta.servlet.ServletOutputStream; | ||
| import java.io.IOException; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| public class Servlet5OutputStreamWriteBytesAdvice { | ||
|
|
||
| @Advice.OnMethodEnter(skipOn = Advice.OnDefaultValue.class, suppress = Throwable.class) | ||
| public static boolean methodEnter( | ||
| @Advice.This ServletOutputStream servletOutputStream, @Advice.Argument(0) byte[] write) | ||
| throws IOException { | ||
| InjectionState state = ServletOutputStreamInjectionState.getInjectionState(servletOutputStream); | ||
| if (state == null) { | ||
| return true; | ||
| } | ||
| // if handleWrite returns true, then it means the original bytes + the snippet were written | ||
| // to the servletOutputStream, and so we no longer need to execute the original method | ||
| // call (see skipOn above) | ||
| // if it returns false, then it means nothing was written to the servletOutputStream and the | ||
| // original method call should be executed | ||
| return !getSnippetInjectionHelper() | ||
| .handleWrite(state, servletOutputStream, write, 0, write.length); | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
...javaagent/instrumentation/servlet/v5_0/Servlet5OutputStreamWriteBytesAndOffsetAdvice.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.servlet.v5_0; | ||
|
|
||
| import static io.opentelemetry.javaagent.instrumentation.servlet.v5_0.Servlet5Singletons.getSnippetInjectionHelper; | ||
|
|
||
| import io.opentelemetry.javaagent.instrumentation.servlet.snippet.InjectionState; | ||
| import io.opentelemetry.javaagent.instrumentation.servlet.v5_0.snippet.ServletOutputStreamInjectionState; | ||
| import jakarta.servlet.ServletOutputStream; | ||
| import java.io.IOException; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| public class Servlet5OutputStreamWriteBytesAndOffsetAdvice { | ||
| @Advice.OnMethodEnter(skipOn = Advice.OnDefaultValue.class, suppress = Throwable.class) | ||
| public static boolean methodEnter( | ||
| @Advice.This ServletOutputStream servletOutputStream, | ||
| @Advice.Argument(value = 0) byte[] write, | ||
| @Advice.Argument(value = 1) int off, | ||
| @Advice.Argument(value = 2) int len) | ||
| throws IOException { | ||
| InjectionState state = ServletOutputStreamInjectionState.getInjectionState(servletOutputStream); | ||
| if (state == null) { | ||
| return true; | ||
| } | ||
| // if handleWrite returns true, then it means the original bytes + the snippet were written | ||
| // to the servletOutputStream, and so we no longer need to execute the original method | ||
| // call (see skipOn above) | ||
| // if it returns false, then it means nothing was written to the servletOutputStream and the | ||
| // original method call should be executed | ||
| return !getSnippetInjectionHelper().handleWrite(state, servletOutputStream, write, off, len); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
...ntelemetry/javaagent/instrumentation/servlet/v5_0/Servlet5OutputStreamWriteIntAdvice.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.servlet.v5_0; | ||
|
|
||
| import static io.opentelemetry.javaagent.instrumentation.servlet.v5_0.Servlet5Singletons.getSnippetInjectionHelper; | ||
|
|
||
| import io.opentelemetry.javaagent.instrumentation.servlet.snippet.InjectionState; | ||
| import io.opentelemetry.javaagent.instrumentation.servlet.v5_0.snippet.ServletOutputStreamInjectionState; | ||
| import jakarta.servlet.ServletOutputStream; | ||
| import java.io.IOException; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| public class Servlet5OutputStreamWriteIntAdvice { | ||
|
|
||
| @Advice.OnMethodEnter(skipOn = Advice.OnDefaultValue.class, suppress = Throwable.class) | ||
| public static boolean methodEnter( | ||
| @Advice.This ServletOutputStream servletOutputStream, @Advice.Argument(0) int write) | ||
| throws IOException { | ||
| InjectionState state = ServletOutputStreamInjectionState.getInjectionState(servletOutputStream); | ||
| if (state == null) { | ||
| return true; | ||
| } | ||
| // if handleWrite returns true, then it means the original bytes + the snippet were written | ||
| // to the servletOutputStream, and so we no longer need to execute the original method | ||
| // call (see skipOn above) | ||
| // if it returns false, then it means nothing was written to the servletOutputStream and the | ||
| // original method call should be executed | ||
| return !getSnippetInjectionHelper().handleWrite(state, servletOutputStream, write); | ||
| } | ||
| } |
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
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
154 changes: 154 additions & 0 deletions
154
...vaagent/instrumentation/servlet/v5_0/snippet/Servlet5SnippetInjectingResponseWrapper.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.servlet.v5_0.snippet; | ||
|
|
||
| import static io.opentelemetry.javaagent.instrumentation.servlet.v5_0.snippet.ServletOutputStreamInjectionState.initializeInjectionStateIfNeeded; | ||
| import static java.util.logging.Level.FINE; | ||
|
|
||
| import io.opentelemetry.javaagent.instrumentation.servlet.snippet.SnippetInjectingPrintWriter; | ||
| import io.opentelemetry.javaagent.instrumentation.servlet.snippet.SnippetInjectingResponseWrapper; | ||
| import jakarta.servlet.ServletOutputStream; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import jakarta.servlet.http.HttpServletResponseWrapper; | ||
| import java.io.IOException; | ||
| import java.io.PrintWriter; | ||
| import java.util.logging.Logger; | ||
|
|
||
| public class Servlet5SnippetInjectingResponseWrapper extends HttpServletResponseWrapper | ||
| implements SnippetInjectingResponseWrapper { | ||
|
|
||
| private static final Logger logger = Logger.getLogger(HttpServletResponseWrapper.class.getName()); | ||
|
|
||
| public static final String FAKE_SNIPPET_HEADER = "FAKE_SNIPPET_HEADER"; | ||
|
|
||
| private static final int UNSET = -1; | ||
| private final String snippet; | ||
| private final int snippetLength; | ||
|
|
||
| private long contentLength = UNSET; | ||
|
|
||
| private SnippetInjectingPrintWriter snippetInjectingPrintWriter = null; | ||
|
|
||
| public Servlet5SnippetInjectingResponseWrapper(HttpServletResponse response, String snippet) { | ||
| super(response); | ||
| this.snippet = snippet; | ||
| snippetLength = snippet.length(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean containsHeader(String name) { | ||
| // this function is overridden in order to make sure the response is wrapped | ||
| // but not wrapped twice | ||
| // we don't use req.setAttribute | ||
| // because async requests pass down their attributes, but don't pass down our wrapped response | ||
| // and so we would see the presence of the attribute and think the response was already wrapped | ||
| // when it really is not | ||
| // see also https://docs.oracle.com/javaee/7/api/javax/servlet/AsyncContext.html | ||
| if (name.equals(FAKE_SNIPPET_HEADER)) { | ||
| return true; | ||
| } | ||
| return super.containsHeader(name); | ||
| } | ||
|
|
||
| @Override | ||
| public void setHeader(String name, String value) { | ||
| // checking content-type is just an optimization to avoid unnecessary parsing | ||
| if ("Content-Length".equalsIgnoreCase(name) && isContentTypeTextHtml()) { | ||
| try { | ||
| contentLength = Long.parseLong(value); | ||
| } catch (NumberFormatException ex) { | ||
| logger.log(FINE, "NumberFormatException", ex); | ||
| } | ||
| } | ||
| super.setHeader(name, value); | ||
| } | ||
|
|
||
| @Override | ||
| public void addHeader(String name, String value) { | ||
| // checking content-type is just an optimization to avoid unnecessary parsing | ||
| if ("Content-Length".equalsIgnoreCase(name) && isContentTypeTextHtml()) { | ||
| try { | ||
| contentLength = Long.parseLong(value); | ||
| } catch (NumberFormatException ex) { | ||
| logger.log(FINE, "NumberFormatException", ex); | ||
| } | ||
| } | ||
| super.addHeader(name, value); | ||
| } | ||
|
|
||
| @Override | ||
| public void setIntHeader(String name, int value) { | ||
| // checking content-type is just an optimization to avoid unnecessary parsing | ||
| if ("Content-Length".equalsIgnoreCase(name) && isContentTypeTextHtml()) { | ||
| contentLength = value; | ||
| } | ||
| super.setIntHeader(name, value); | ||
| } | ||
|
|
||
| @Override | ||
| public void addIntHeader(String name, int value) { | ||
| // checking content-type is just an optimization to avoid unnecessary parsing | ||
| if ("Content-Length".equalsIgnoreCase(name) && isContentTypeTextHtml()) { | ||
| contentLength = value; | ||
| } | ||
| super.addIntHeader(name, value); | ||
| } | ||
|
|
||
| @Override | ||
| public void setContentLength(int len) { | ||
| contentLength = len; | ||
| super.setContentLength(len); | ||
| } | ||
|
|
||
| @Override | ||
| public void setContentLengthLong(long length) { | ||
| contentLength = length; | ||
| super.setContentLengthLong(length); | ||
| } | ||
|
|
||
| @Override | ||
| public ServletOutputStream getOutputStream() throws IOException { | ||
| ServletOutputStream output = super.getOutputStream(); | ||
| initializeInjectionStateIfNeeded(output, this); | ||
| return output; | ||
| } | ||
|
|
||
| @Override | ||
| public PrintWriter getWriter() throws IOException { | ||
| if (!isContentTypeTextHtml()) { | ||
| return super.getWriter(); | ||
| } | ||
| if (snippetInjectingPrintWriter == null) { | ||
| snippetInjectingPrintWriter = | ||
| new SnippetInjectingPrintWriter(super.getWriter(), snippet, this); | ||
| } | ||
| return snippetInjectingPrintWriter; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isContentTypeTextHtml() { | ||
| String contentType = super.getContentType(); | ||
| if (contentType == null) { | ||
| contentType = super.getHeader("content-type"); | ||
| } | ||
| return contentType != null && contentType.startsWith("text/html"); | ||
| } | ||
|
|
||
| @Override | ||
| public void updateContentLengthIfPreviouslySet() { | ||
| if (contentLength != UNSET) { | ||
| setContentLength((int) contentLength + snippetLength); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isNotSafeToInject() { | ||
| // if content-length was set and response was already committed (headers sent to the client), | ||
| // then it is not safe to inject because the content-length header cannot be updated to account | ||
| // for the snippet length | ||
| return contentLength != UNSET && isCommitted(); | ||
| } | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
...try/javaagent/instrumentation/servlet/v5_0/snippet/ServletOutputStreamInjectionState.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.servlet.v5_0.snippet; | ||
|
|
||
| import io.opentelemetry.instrumentation.api.util.VirtualField; | ||
| import io.opentelemetry.javaagent.instrumentation.servlet.snippet.InjectionState; | ||
| import jakarta.servlet.ServletOutputStream; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| public class ServletOutputStreamInjectionState { | ||
| private static final VirtualField<ServletOutputStream, InjectionState> virtualField = | ||
| VirtualField.find(ServletOutputStream.class, InjectionState.class); | ||
|
|
||
| public static void initializeInjectionStateIfNeeded( | ||
| ServletOutputStream servletOutputStream, Servlet5SnippetInjectingResponseWrapper wrapper) { | ||
| InjectionState state = virtualField.get(servletOutputStream); | ||
| if (!wrapper.isContentTypeTextHtml()) { | ||
| virtualField.set(servletOutputStream, null); | ||
| return; | ||
| } | ||
| if (state == null || state.getWrapper() != wrapper) { | ||
| state = new InjectionState(wrapper); | ||
| virtualField.set(servletOutputStream, state); | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| public static InjectionState getInjectionState(ServletOutputStream servletOutputStream) { | ||
| return virtualField.get(servletOutputStream); | ||
| } | ||
|
|
||
| private ServletOutputStreamInjectionState() {} | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.