-
Notifications
You must be signed in to change notification settings - Fork 336
Add server.request.body.filenames and files_content for GlassFish/Payara #11267
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
gh-worker-dd-mergequeue-cf854d
merged 26 commits into
master
from
alejandro.gonzalez/APPSEC-61873-payara
May 21, 2026
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
ad1dd81
feat(appsec/tomcat): add GlassFish/Payara multipart filename instrume…
jandro996 0d5fd68
fix(appsec): add muzzleDirective and requestFilesContent to GlassFish…
jandro996 9668f39
refactor(appsec): align GlassFish multipart advice with canonical Com…
jandro996 0878a25
fix(appsec): bypass Java module-system IllegalAccessException in Glas…
jandro996 2fc4494
fix(appsec): protect each GlassFish multipart part iteration with per…
jandro996 c08eb04
revert: remove unnecessary setAccessible(true) from ParameterCollector
jandro996 c5091b1
style: replace FQN datadog.trace.api.Config with regular import
jandro996 bf02571
style: skip filenames collection when filenamesCb is null
jandro996 6e7f129
feat(appsec): add blocking fallback for GlassFish/Payara multipart in…
jandro996 b0fe146
style(appsec): address PR review feedback on GlassFish multipart inst…
jandro996 13388cf
fix: restrict GlassFish muzzle range to [4.0, 6.1.0) to exclude jakar…
jandro996 2f1e1be
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-payara
jandro996 e3426cd
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-payara
jandro996 aaac59e
fix(appsec): make GlassFishBlockingHelper fields public and avoid Blo…
jandro996 a45b404
refactor(appsec): extract tryBlock() helper and add unit tests for Gl…
jandro996 f6f2feb
refactor: move GlassFish multipart processing into GlassFishBlockingH…
jandro996 8f27c35
style(appsec): address manuel review comments on GlassFish instrument…
jandro996 7acbeb2
Type @Advice.FieldValue to connector.Request to eliminate all reflection
jandro996 ccb8d5d
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-payara
jandro996 2524323
Revert @Advice.FieldValue type to Object to fix muzzle AssertPass
jandro996 da2602a
Eliminate reflection in GlassFishMultipartInstrumentation
jandro996 6887660
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-payara
jandro996 cfe2721
ci: retrigger CI
jandro996 4fd42a2
Type @Advice.FieldValue as org.apache.catalina.Request interface
jandro996 2683f7e
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-payara
jandro996 b02aeb0
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-payara
jandro996 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
184 changes: 184 additions & 0 deletions
184
...psec-7.0/src/main/java/datadog/trace/instrumentation/tomcat7/GlassFishBlockingHelper.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,184 @@ | ||
| package datadog.trace.instrumentation.tomcat7; | ||
|
|
||
| import datadog.appsec.api.blocking.BlockingContentType; | ||
| import datadog.trace.api.Config; | ||
| import datadog.trace.api.gateway.BlockResponseFunction; | ||
| import datadog.trace.api.gateway.Flow; | ||
| import datadog.trace.api.gateway.RequestContext; | ||
| import datadog.trace.api.http.MultipartContentDecoder; | ||
| import datadog.trace.bootstrap.blocking.BlockingActionHelper; | ||
| import java.io.InputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.BiFunction; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import javax.servlet.http.Part; | ||
|
|
||
| public final class GlassFishBlockingHelper { | ||
|
|
||
| public static final int MAX_FILE_CONTENT_COUNT = Config.get().getAppSecMaxFileContentCount(); | ||
| public static final int MAX_FILE_CONTENT_BYTES = Config.get().getAppSecMaxFileContentBytes(); | ||
|
|
||
| /** | ||
| * Attempts to commit a blocking response via the registered {@link BlockResponseFunction} or via | ||
| * the Servlet API fallback, then marks the trace segment as effectively blocked. | ||
| * | ||
| * <p>Returns {@code true} if the response was committed (regardless of whether {@link | ||
| * datadog.trace.api.internal.TraceSegment#effectivelyBlocked()} succeeded). Returns {@code false} | ||
| * if no response could be committed. | ||
| */ | ||
| public static boolean tryBlock( | ||
| RequestContext reqCtx, | ||
| HttpServletRequest fallbackReq, | ||
| HttpServletResponse fallbackResp, | ||
| Flow.Action.RequestBlockingAction rba) { | ||
| try { | ||
| BlockResponseFunction brf = reqCtx.getBlockResponseFunction(); | ||
| if (brf != null) { | ||
| brf.tryCommitBlockingResponse(reqCtx.getTraceSegment(), rba); | ||
| } else if (!commitBlocking(fallbackReq, fallbackResp, rba)) { | ||
| return false; | ||
| } | ||
| } catch (Exception ignored) { | ||
| // commit failed — response not sent, cannot block this request | ||
| return false; | ||
| } | ||
| // Response was committed — mark as blocked on a best-effort basis. | ||
| // effectivelyBlocked() can throw if the span is already finished; that must not suppress the | ||
| // true return value since the response has already been sent to the client. | ||
| try { | ||
| reqCtx.getTraceSegment().effectivelyBlocked(); | ||
| } catch (Exception ignored) { | ||
| // span already finished — response was sent, blocking succeeded | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Collects filenames and file contents from the given multipart parts, fires the AppSec IG | ||
| * callbacks, and commits a blocking response if the WAF requests one. | ||
| * | ||
| * <p>Returns {@code true} if a blocking response was committed (the caller should replace the | ||
| * parts collection with an empty list to prevent further processing). | ||
| */ | ||
| public static boolean processPartsAndBlock( | ||
| Collection<?> parts, | ||
| RequestContext reqCtx, | ||
| org.apache.catalina.Request catRequest, | ||
| BiFunction<RequestContext, List<String>, Flow<Void>> filenamesCb, | ||
| BiFunction<RequestContext, List<String>, Flow<Void>> contentCb) { | ||
| // org.apache.catalina.Request.getRequest() returns the underlying ServletRequest. | ||
| // TomcatServerInstrumentation is muzzled out in Payara (CoyoteAdapter.postParseRequest arg | ||
| // types differ from standard Tomcat), so BlockResponseFunction is never registered there — | ||
| // this Servlet API fallback is required to commit the blocking response in Payara. | ||
| javax.servlet.ServletRequest sr = catRequest != null ? catRequest.getRequest() : null; | ||
| HttpServletRequest fallbackReq = | ||
| sr instanceof HttpServletRequest ? (HttpServletRequest) sr : null; | ||
| HttpServletResponse fallbackResp = null; | ||
| if (catRequest != null) { | ||
| org.apache.catalina.Response cr = catRequest.getResponse(); | ||
| if (cr != null) { | ||
| javax.servlet.ServletResponse svr = cr.getResponse(); | ||
| if (svr instanceof HttpServletResponse) { | ||
| fallbackResp = (HttpServletResponse) svr; | ||
| } | ||
| } | ||
| } | ||
| List<String> filenames = null; | ||
| List<String> contents = null; | ||
| for (Object partObj : parts) { | ||
| try { | ||
| if (!(partObj instanceof Part)) { | ||
| continue; | ||
| } | ||
| Part part = (Part) partObj; | ||
| String filename = part.getSubmittedFileName(); | ||
| if (filename == null) { | ||
| continue; | ||
| } | ||
| if (filenamesCb != null && !filename.isEmpty()) { | ||
| if (filenames == null) { | ||
| filenames = new ArrayList<>(); | ||
| } | ||
| filenames.add(filename); | ||
| } | ||
| if (contentCb != null) { | ||
| if (contents == null) { | ||
| contents = new ArrayList<>(); | ||
| } | ||
| if (contents.size() < MAX_FILE_CONTENT_COUNT) { | ||
| try (InputStream is = part.getInputStream()) { | ||
| contents.add( | ||
| MultipartContentDecoder.readInputStream( | ||
| is, MAX_FILE_CONTENT_BYTES, part.getContentType())); | ||
| } catch (Exception ignored) { | ||
| // stream read failed — report empty content rather than skipping the part entirely | ||
| contents.add(""); | ||
| } | ||
| } | ||
| } | ||
| } catch (Exception ignored) { | ||
| // malformed or inaccessible part — skip and continue with remaining parts | ||
| } | ||
| } | ||
|
|
||
| if (filenames != null && !filenames.isEmpty()) { | ||
| Flow<Void> flow = filenamesCb.apply(reqCtx, filenames); | ||
| Flow.Action action = flow.getAction(); | ||
| if (action instanceof Flow.Action.RequestBlockingAction) { | ||
| if (tryBlock( | ||
| reqCtx, fallbackReq, fallbackResp, (Flow.Action.RequestBlockingAction) action)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (contents != null && !contents.isEmpty()) { | ||
| Flow<Void> contentFlow = contentCb.apply(reqCtx, contents); | ||
| Flow.Action contentAction = contentFlow.getAction(); | ||
| if (contentAction instanceof Flow.Action.RequestBlockingAction) { | ||
| return tryBlock( | ||
| reqCtx, fallbackReq, fallbackResp, (Flow.Action.RequestBlockingAction) contentAction); | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public static boolean commitBlocking( | ||
| HttpServletRequest request, | ||
| HttpServletResponse response, | ||
| Flow.Action.RequestBlockingAction rba) { | ||
| if (response == null) { | ||
| return false; | ||
| } | ||
| try { | ||
| if (response.isCommitted()) { | ||
| return false; | ||
| } | ||
| response.reset(); | ||
| response.setStatus(BlockingActionHelper.getHttpCode(rba.getStatusCode())); | ||
| for (Map.Entry<String, String> e : rba.getExtraHeaders().entrySet()) { | ||
| response.setHeader(e.getKey(), e.getValue()); | ||
| } | ||
| if (rba.getBlockingContentType() != BlockingContentType.NONE) { | ||
| String accept = request != null ? request.getHeader("Accept") : null; | ||
| BlockingActionHelper.TemplateType type = | ||
| BlockingActionHelper.determineTemplateType(rba.getBlockingContentType(), accept); | ||
| byte[] body = BlockingActionHelper.getTemplate(type, rba.getSecurityResponseId()); | ||
| if (body != null) { | ||
| response.setHeader("Content-Type", BlockingActionHelper.getContentType(type)); | ||
| response.setHeader("Content-Length", Integer.toString(body.length)); | ||
| response.getOutputStream().write(body); | ||
| } | ||
| } | ||
| response.flushBuffer(); | ||
| return true; | ||
| } catch (Exception e) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
99 changes: 99 additions & 0 deletions
99
...rc/main/java/datadog/trace/instrumentation/tomcat7/GlassFishMultipartInstrumentation.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,99 @@ | ||
| package datadog.trace.instrumentation.tomcat7; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static datadog.trace.api.gateway.Events.EVENTS; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.api.gateway.CallbackProvider; | ||
| import datadog.trace.api.gateway.Flow; | ||
| import datadog.trace.api.gateway.RequestContext; | ||
| import datadog.trace.api.gateway.RequestContextSlot; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentTracer; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.function.BiFunction; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| /** | ||
| * GlassFish/Payara does not have {@code Request.parseParts()} - instead {@code Request.getParts()} | ||
| * delegates to {@code org.apache.catalina.fileupload.Multipart.getParts()}. This instrumentation | ||
| * hooks that GlassFish-specific class to report uploaded file names and contents to the AppSec WAF | ||
| * via the {@code requestFilesFilenames} and {@code requestFilesContent} IG events. | ||
| * | ||
| * <p>Because {@code org.apache.catalina.fileupload.Multipart} does not exist in standard Tomcat, | ||
| * this instrumentation is automatically skipped by ByteBuddy on non-GlassFish containers. | ||
| */ | ||
| @AutoService(InstrumenterModule.class) | ||
| public class GlassFishMultipartInstrumentation extends InstrumenterModule.AppSec | ||
| implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
|
|
||
| public GlassFishMultipartInstrumentation() { | ||
| super("tomcat"); | ||
| } | ||
|
|
||
| @Override | ||
| public String muzzleDirective() { | ||
| return "glassfish"; | ||
| } | ||
|
|
||
| @Override | ||
| public String instrumentedType() { | ||
| return "org.apache.catalina.fileupload.Multipart"; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] helperClassNames() { | ||
| return new String[] { | ||
| "datadog.trace.instrumentation.tomcat7.GlassFishBlockingHelper", | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| named("getParts").and(takesArguments(0)).and(isPublic()), | ||
|
jandro996 marked this conversation as resolved.
|
||
| getClass().getName() + "$GetPartsAdvice"); | ||
| } | ||
|
|
||
| public static class GetPartsAdvice { | ||
|
|
||
| @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
| static void after( | ||
| @Advice.Return(readOnly = false) Collection<?> parts, | ||
| @Advice.Thrown Throwable t, | ||
| @Advice.FieldValue("request") org.apache.catalina.Request catRequest) { | ||
| if (t != null || parts == null || parts.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| AgentSpan agentSpan = AgentTracer.activeSpan(); | ||
| if (agentSpan == null) { | ||
| return; | ||
| } | ||
| RequestContext reqCtx = agentSpan.getRequestContext(); | ||
| if (reqCtx == null || reqCtx.getData(RequestContextSlot.APPSEC) == null) { | ||
| return; | ||
| } | ||
|
|
||
| CallbackProvider cbp = AgentTracer.get().getCallbackProvider(RequestContextSlot.APPSEC); | ||
| BiFunction<RequestContext, List<String>, Flow<Void>> filenamesCb = | ||
| cbp.getCallback(EVENTS.requestFilesFilenames()); | ||
| BiFunction<RequestContext, List<String>, Flow<Void>> contentCb = | ||
| cbp.getCallback(EVENTS.requestFilesContent()); | ||
| if (filenamesCb == null && contentCb == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (GlassFishBlockingHelper.processPartsAndBlock( | ||
| parts, reqCtx, catRequest, filenamesCb, contentCb)) { | ||
| parts = Collections.emptyList(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.