Skip to content
Merged
Show file tree
Hide file tree
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 May 4, 2026
0d5fd68
fix(appsec): add muzzleDirective and requestFilesContent to GlassFish…
jandro996 May 4, 2026
9668f39
refactor(appsec): align GlassFish multipart advice with canonical Com…
jandro996 May 4, 2026
0878a25
fix(appsec): bypass Java module-system IllegalAccessException in Glas…
jandro996 May 4, 2026
2fc4494
fix(appsec): protect each GlassFish multipart part iteration with per…
jandro996 May 4, 2026
c08eb04
revert: remove unnecessary setAccessible(true) from ParameterCollector
jandro996 May 4, 2026
c5091b1
style: replace FQN datadog.trace.api.Config with regular import
jandro996 May 4, 2026
bf02571
style: skip filenames collection when filenamesCb is null
jandro996 May 4, 2026
6e7f129
feat(appsec): add blocking fallback for GlassFish/Payara multipart in…
jandro996 May 6, 2026
b0fe146
style(appsec): address PR review feedback on GlassFish multipart inst…
jandro996 May 6, 2026
13388cf
fix: restrict GlassFish muzzle range to [4.0, 6.1.0) to exclude jakar…
jandro996 May 6, 2026
2f1e1be
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-payara
jandro996 May 7, 2026
e3426cd
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-payara
jandro996 May 7, 2026
aaac59e
fix(appsec): make GlassFishBlockingHelper fields public and avoid Blo…
jandro996 May 7, 2026
a45b404
refactor(appsec): extract tryBlock() helper and add unit tests for Gl…
jandro996 May 7, 2026
f6f2feb
refactor: move GlassFish multipart processing into GlassFishBlockingH…
jandro996 May 8, 2026
8f27c35
style(appsec): address manuel review comments on GlassFish instrument…
jandro996 May 15, 2026
7acbeb2
Type @Advice.FieldValue to connector.Request to eliminate all reflection
jandro996 May 18, 2026
ccb8d5d
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-payara
jandro996 May 18, 2026
2524323
Revert @Advice.FieldValue type to Object to fix muzzle AssertPass
jandro996 May 18, 2026
da2602a
Eliminate reflection in GlassFishMultipartInstrumentation
jandro996 May 19, 2026
6887660
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-payara
jandro996 May 19, 2026
cfe2721
ci: retrigger CI
jandro996 May 19, 2026
4fd42a2
Type @Advice.FieldValue as org.apache.catalina.Request interface
jandro996 May 20, 2026
2683f7e
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-payara
jandro996 May 20, 2026
b02aeb0
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-payara
jandro996 May 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,34 @@ muzzle {
extraDependency 'org.apache.tomcat:tomcat-catalina:7.0.4'
assertInverse = true
}
pass {
name = 'glassfish'
group = 'org.glassfish.main.extras'
module = 'glassfish-embedded-all'
versions = '[4.0, 6.1.0)' // GlassFish 6.1.0+ uses jakarta.* namespace; our advice uses javax.servlet.http.Part
assertInverse = true
}
}

apply from: "$rootDir/gradle/java.gradle"

dependencies {
compileOnly group: 'org.apache.tomcat', name: 'tomcat-catalina', version: '7.0.4'
compileOnly group: 'org.apache.tomcat', name: 'tomcat-coyote', version: '7.0.4'
// Servlet 3.1 API needed to reference Part.getSubmittedFileName() in GlassFishMultipartInstrumentation.
// tomcat-catalina:7.0.4 provides only Servlet 3.0 (no getSubmittedFileName); GlassFish 4+ is Servlet 3.1.
compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
// org.apache.catalina.Request and org.apache.catalina.Response interfaces are GlassFish-specific —
// Tomcat 7.x removed them from its published API. Required to cast connector.Request to the
// catalina Request interface in GlassFishBlockingHelper without reflection.
compileOnly group: 'org.glassfish.main.extras', name: 'glassfish-embedded-all', version: '4.0'
implementation project(':dd-java-agent:instrumentation:tomcat:tomcat-common')

testImplementation group: 'org.apache.tomcat', name: 'tomcat-catalina', version: '7.0.4'
testImplementation group: 'org.apache.tomcat', name: 'tomcat-coyote', version: '7.0.4'
testImplementation group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
testImplementation libs.bundles.mockito
testCompileOnly group: 'org.glassfish.main.extras', name: 'glassfish-embedded-all', version: '4.0'
}

// testing happens in tomcat-5.5 module
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
}
Comment thread
jandro996 marked this conversation as resolved.
}

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;
}
}
}
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()),
Comment thread
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();
}
}
}
}
Loading