Skip to content

Commit db08e43

Browse files
committed
Fix GetFilenamesAdvice double-fire in jetty-appsec-8.1.3
In Jetty 8.x/9.0, _multiPartInputStream is null only on the first getParts() call. Add OnMethodEnter guard to skip the WAF callback on subsequent calls which return the cached multipart result.
1 parent c5268dd commit db08e43

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

dd-java-agent/instrumentation/jetty/jetty-appsec/jetty-appsec-8.1.3/src/main/java/datadog/trace/instrumentation/jetty8/RequestGetPartsInstrumentation.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import net.bytebuddy.description.method.MethodList;
3535
import net.bytebuddy.description.type.TypeDescription;
3636
import net.bytebuddy.implementation.Implementation;
37+
import net.bytebuddy.implementation.bytecode.assign.Assigner;
3738
import net.bytebuddy.jar.asm.ClassReader;
3839
import net.bytebuddy.jar.asm.ClassVisitor;
3940
import net.bytebuddy.jar.asm.ClassWriter;
@@ -205,12 +206,22 @@ static void muzzle(Request req) throws ServletException, IOException {
205206

206207
@RequiresRequestContext(RequestContextSlot.APPSEC)
207208
public static class GetFilenamesAdvice {
209+
@Advice.OnMethodEnter(suppress = Throwable.class)
210+
static boolean before(
211+
@Advice.FieldValue(value = "_multiPartInputStream", typing = Assigner.Typing.DYNAMIC)
212+
final Object multiPartInputStream) {
213+
// _multiPartInputStream is null only on the first getParts() call; subsequent calls
214+
// return the cached multipart result without re-parsing, but we must not re-fire the WAF.
215+
return multiPartInputStream == null;
216+
}
217+
208218
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
209219
static void after(
220+
@Advice.Enter boolean proceed,
210221
@Advice.Return Collection parts,
211222
@ActiveRequestContext RequestContext reqCtx,
212223
@Advice.Thrown(readOnly = false) Throwable t) {
213-
if (t != null || parts == null || parts.isEmpty()) {
224+
if (!proceed || t != null || parts == null || parts.isEmpty()) {
214225
return;
215226
}
216227
// Resolve getSubmittedFileName once (Servlet 3.1+; null on Servlet 3.0)

0 commit comments

Comments
 (0)