-
Notifications
You must be signed in to change notification settings - Fork 330
Add server.request.body.filenames AppSec address for commons-fileupload #10949
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 7 commits into
master
from
alejandro.gonzalez/APPSEC-61873-1
Mar 25, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5918bab
Add server.request.body.filenames IG event and GatewayBridge wiring
jandro996 a817780
Add server.request.body.filenames support for commons-fileupload
jandro996 9b87129
Add smoke test for malicious file upload filename blocking
jandro996 2a10f65
Fix smoke test multipart: exclude MultipartAutoConfiguration
jandro996 51adb1e
Fix import ordering and named() matcher consistency
jandro996 7a65e8a
Merge branch 'master' into alejandro.gonzalez/APPSEC-61873-1
jandro996 f186acc
spotless
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
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
93 changes: 93 additions & 0 deletions
93
.../java/datadog/trace/instrumentation/commons/fileupload/CommonsFileUploadAppSecModule.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,93 @@ | ||
| package datadog.trace.instrumentation.commons.fileupload; | ||
|
|
||
| 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.takesArgument; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import datadog.appsec.api.blocking.BlockingException; | ||
| import datadog.trace.advice.ActiveRequestContext; | ||
| import datadog.trace.advice.RequiresRequestContext; | ||
| import datadog.trace.agent.tooling.Instrumenter; | ||
| import datadog.trace.agent.tooling.InstrumenterModule; | ||
| import datadog.trace.api.gateway.BlockResponseFunction; | ||
| 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.AgentTracer; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.BiFunction; | ||
| import net.bytebuddy.asm.Advice; | ||
| import org.apache.commons.fileupload.FileItem; | ||
|
|
||
| @AutoService(InstrumenterModule.class) | ||
| public class CommonsFileUploadAppSecModule extends InstrumenterModule.AppSec | ||
| implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice { | ||
|
|
||
| public CommonsFileUploadAppSecModule() { | ||
| super("commons-fileupload"); | ||
| } | ||
|
|
||
| @Override | ||
| public String instrumentedType() { | ||
| return "org.apache.commons.fileupload.servlet.ServletFileUpload"; | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| named("parseRequest") | ||
| .and(isPublic()) | ||
| .and(takesArgument(0, named("javax.servlet.http.HttpServletRequest"))), | ||
| getClass().getName() + "$ParseRequestAdvice"); | ||
| } | ||
|
|
||
| @RequiresRequestContext(RequestContextSlot.APPSEC) | ||
| public static class ParseRequestAdvice { | ||
| @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
| static void after( | ||
| @Advice.Return final List<FileItem> fileItems, | ||
| @ActiveRequestContext RequestContext reqCtx, | ||
| @Advice.Thrown(readOnly = false) Throwable t) { | ||
| if (t != null || fileItems == null || fileItems.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| CallbackProvider cbp = AgentTracer.get().getCallbackProvider(RequestContextSlot.APPSEC); | ||
| BiFunction<RequestContext, List<String>, Flow<Void>> callback = | ||
| cbp.getCallback(EVENTS.requestFilesFilenames()); | ||
| if (callback == null) { | ||
| return; | ||
| } | ||
|
|
||
| List<String> filenames = new ArrayList<>(); | ||
| for (FileItem fileItem : fileItems) { | ||
| if (fileItem.isFormField()) { | ||
| continue; | ||
| } | ||
| String name = fileItem.getName(); | ||
| if (name != null && !name.isEmpty()) { | ||
| filenames.add(name); | ||
| } | ||
| } | ||
| if (filenames.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| Flow<Void> flow = callback.apply(reqCtx, filenames); | ||
| Flow.Action action = flow.getAction(); | ||
| if (action instanceof Flow.Action.RequestBlockingAction) { | ||
| Flow.Action.RequestBlockingAction rba = (Flow.Action.RequestBlockingAction) action; | ||
| BlockResponseFunction brf = reqCtx.getBlockResponseFunction(); | ||
| if (brf != null) { | ||
| brf.tryCommitBlockingResponse(reqCtx.getTraceSegment(), rba); | ||
| t = new BlockingException("Blocked request (multipart file upload)"); | ||
| reqCtx.getTraceSegment().effectivelyBlocked(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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
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
1 change: 1 addition & 0 deletions
1
dd-smoke-tests/appsec/springboot/src/main/resources/application.properties
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| logging.level.root=WARN | ||
| spring.servlet.multipart.enabled=false |
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
Oops, something went wrong.
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.