-
Notifications
You must be signed in to change notification settings - Fork 344
Add server.request.body.files_content AppSec address for Vert.x 3/4/5 #11724
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0df87c1
feat(appsec): fire server.request.body.files_content for Vert.x 3/4/5
jandro996 54d524b
fix(appsec): use Vert.x FileUpload.charSet() when decoding file content
jandro996 def3020
fix(appsec): extract FileUploadHelper to fix muzzle validation for Ve…
jandro996 91bec07
fix: make FileUploadHelper public and fix max files limit test for Ve…
jandro996 79b1ef0
style: replace em-dashes with colons in test comments
jandro996 f29a9a9
fix: skip ordering-dependent max files limit test for Vert.x HashSet
jandro996 4e4b7d6
fix: skip files_content test in VertxRxCircuitBreakerHttpServerForked…
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
51 changes: 51 additions & 0 deletions
51
...eb-3.4/src/main/java/datadog/trace/instrumentation/vertx_3_4/server/FileUploadHelper.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,51 @@ | ||
| package datadog.trace.instrumentation.vertx_3_4.server; | ||
|
|
||
| import datadog.appsec.api.blocking.BlockingException; | ||
| 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 io.vertx.ext.web.FileUpload; | ||
| import java.io.FileInputStream; | ||
| import java.util.List; | ||
| import java.util.function.BiFunction; | ||
|
|
||
| public class FileUploadHelper { | ||
|
|
||
| public static BlockingException commitBlockingResponse( | ||
| BiFunction<RequestContext, List<String>, Flow<Void>> cb, | ||
| RequestContext reqCtx, | ||
| List<String> data, | ||
| String reason) { | ||
| Flow<Void> flow = cb.apply(reqCtx, data); | ||
| Flow.Action action = flow.getAction(); | ||
| if (action instanceof Flow.Action.RequestBlockingAction) { | ||
| BlockResponseFunction brf = reqCtx.getBlockResponseFunction(); | ||
| if (brf != null) { | ||
| brf.tryCommitBlockingResponse( | ||
| reqCtx.getTraceSegment(), (Flow.Action.RequestBlockingAction) action); | ||
| return new BlockingException(reason); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public static String readUploadContent(FileUpload upload, int maxBytes) { | ||
| try { | ||
| String path = upload.uploadedFileName(); | ||
| if (path == null || path.isEmpty()) { | ||
| return ""; | ||
| } | ||
| String charSet = upload.charSet(); | ||
| String contentType = | ||
| charSet != null && !charSet.isEmpty() | ||
| ? upload.contentType() + "; charset=" + charSet | ||
| : upload.contentType(); | ||
| try (FileInputStream fis = new FileInputStream(path)) { | ||
| return MultipartContentDecoder.readInputStream(fis, maxBytes, contentType); | ||
| } | ||
| } catch (Exception ignored) { | ||
| return ""; | ||
| } | ||
| } | ||
| } |
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
51 changes: 51 additions & 0 deletions
51
...eb-4.0/src/main/java/datadog/trace/instrumentation/vertx_4_0/server/FileUploadHelper.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,51 @@ | ||
| package datadog.trace.instrumentation.vertx_4_0.server; | ||
|
|
||
| import datadog.appsec.api.blocking.BlockingException; | ||
| 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 io.vertx.ext.web.FileUpload; | ||
| import java.io.FileInputStream; | ||
| import java.util.List; | ||
| import java.util.function.BiFunction; | ||
|
|
||
| public class FileUploadHelper { | ||
|
|
||
| public static BlockingException commitBlockingResponse( | ||
| BiFunction<RequestContext, List<String>, Flow<Void>> cb, | ||
| RequestContext reqCtx, | ||
| List<String> data, | ||
| String reason) { | ||
| Flow<Void> flow = cb.apply(reqCtx, data); | ||
| Flow.Action action = flow.getAction(); | ||
| if (action instanceof Flow.Action.RequestBlockingAction) { | ||
| BlockResponseFunction brf = reqCtx.getBlockResponseFunction(); | ||
| if (brf != null) { | ||
| brf.tryCommitBlockingResponse( | ||
| reqCtx.getTraceSegment(), (Flow.Action.RequestBlockingAction) action); | ||
| return new BlockingException(reason); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public static String readUploadContent(FileUpload upload, int maxBytes) { | ||
| try { | ||
| String path = upload.uploadedFileName(); | ||
| if (path == null || path.isEmpty()) { | ||
| return ""; | ||
| } | ||
| String charSet = upload.charSet(); | ||
| String contentType = | ||
| charSet != null && !charSet.isEmpty() | ||
| ? upload.contentType() + "; charset=" + charSet | ||
| : upload.contentType(); | ||
| try (FileInputStream fis = new FileInputStream(path)) { | ||
| return MultipartContentDecoder.readInputStream(fis, maxBytes, contentType); | ||
| } | ||
| } catch (Exception ignored) { | ||
| return ""; | ||
| } | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readUploadConent()performs blocking file I/O (FileInputStream.read) on the vertx event loop thread. Since each loop serves many concurrent connections, blocking stops all requests on the thread for time of file reading. Under the high load or large files, this can cause significant latency increase