1717import datadog .trace .api .gateway .RequestContext ;
1818import datadog .trace .api .gateway .RequestContextSlot ;
1919import datadog .trace .bootstrap .instrumentation .api .AgentTracer ;
20+ import java .io .IOException ;
21+ import java .io .InputStream ;
22+ import java .nio .charset .StandardCharsets ;
2023import java .util .ArrayList ;
2124import java .util .List ;
2225import java .util .function .BiFunction ;
@@ -47,6 +50,9 @@ public void methodAdvice(MethodTransformer transformer) {
4750
4851 @ RequiresRequestContext (RequestContextSlot .APPSEC )
4952 public static class ParseRequestAdvice {
53+
54+ static final int MAX_FILE_CONTENT_BYTES = 4096 ;
55+
5056 @ Advice .OnMethodExit (suppress = Throwable .class , onThrowable = Throwable .class )
5157 static void after (
5258 @ Advice .Return final List <FileItem > fileItems ,
@@ -57,11 +63,6 @@ static void after(
5763 }
5864
5965 CallbackProvider cbp = AgentTracer .get ().getCallbackProvider (RequestContextSlot .APPSEC );
60- BiFunction <RequestContext , List <String >, Flow <Void >> callback =
61- cbp .getCallback (EVENTS .requestFilesFilenames ());
62- if (callback == null ) {
63- return ;
64- }
6566
6667 List <String > filenames = new ArrayList <>();
6768 for (FileItem fileItem : fileItems ) {
@@ -77,14 +78,65 @@ static void after(
7778 return ;
7879 }
7980
80- Flow <Void > flow = callback .apply (reqCtx , filenames );
81- Flow .Action action = flow .getAction ();
82- if (action instanceof Flow .Action .RequestBlockingAction ) {
83- Flow .Action .RequestBlockingAction rba = (Flow .Action .RequestBlockingAction ) action ;
81+ // Fire filenames event
82+ BiFunction <RequestContext , List <String >, Flow <Void >> filenamesCallback =
83+ cbp .getCallback (EVENTS .requestFilesFilenames ());
84+ if (filenamesCallback != null ) {
85+ Flow <Void > flow = filenamesCallback .apply (reqCtx , filenames );
86+ Flow .Action action = flow .getAction ();
87+ if (action instanceof Flow .Action .RequestBlockingAction ) {
88+ Flow .Action .RequestBlockingAction rba = (Flow .Action .RequestBlockingAction ) action ;
89+ BlockResponseFunction brf = reqCtx .getBlockResponseFunction ();
90+ if (brf != null ) {
91+ brf .tryCommitBlockingResponse (reqCtx .getTraceSegment (), rba );
92+ t = new BlockingException ("Blocked request (multipart file upload)" );
93+ reqCtx .getTraceSegment ().effectivelyBlocked ();
94+ return ;
95+ }
96+ }
97+ }
98+
99+ // Fire content event only if not blocked
100+ BiFunction <RequestContext , List <String >, Flow <Void >> contentCallback =
101+ cbp .getCallback (EVENTS .requestFilesContent ());
102+ if (contentCallback == null ) {
103+ return ;
104+ }
105+ List <String > filesContent = new ArrayList <>();
106+ for (FileItem fileItem : fileItems ) {
107+ if (fileItem .isFormField ()) {
108+ continue ;
109+ }
110+ String name = fileItem .getName ();
111+ if (name == null || name .isEmpty ()) {
112+ continue ;
113+ }
114+ String content = "" ;
115+ try {
116+ InputStream is = fileItem .getInputStream ();
117+ byte [] buf = new byte [MAX_FILE_CONTENT_BYTES ];
118+ int total = 0 ;
119+ int n ;
120+ while (total < MAX_FILE_CONTENT_BYTES
121+ && (n = is .read (buf , total , MAX_FILE_CONTENT_BYTES - total )) != -1 ) {
122+ total += n ;
123+ }
124+ content = new String (buf , 0 , total , StandardCharsets .ISO_8859_1 );
125+ } catch (IOException ignored ) {
126+ }
127+ filesContent .add (content );
128+ }
129+ if (filesContent .isEmpty ()) {
130+ return ;
131+ }
132+ Flow <Void > contentFlow = contentCallback .apply (reqCtx , filesContent );
133+ Flow .Action contentAction = contentFlow .getAction ();
134+ if (contentAction instanceof Flow .Action .RequestBlockingAction ) {
135+ Flow .Action .RequestBlockingAction rba = (Flow .Action .RequestBlockingAction ) contentAction ;
84136 BlockResponseFunction brf = reqCtx .getBlockResponseFunction ();
85137 if (brf != null ) {
86138 brf .tryCommitBlockingResponse (reqCtx .getTraceSegment (), rba );
87- t = new BlockingException ("Blocked request (multipart file upload)" );
139+ t = new BlockingException ("Blocked request (multipart file upload content )" );
88140 reqCtx .getTraceSegment ().effectivelyBlocked ();
89141 }
90142 }
0 commit comments