-
Notifications
You must be signed in to change notification settings - Fork 331
feat(appsec): expose uploaded file content as new WAF address server.request.body.files_content for commons-fileupload #11137
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
Open
jandro996
wants to merge
9
commits into
master
Choose a base branch
from
alejandro.gonzalez/APPSEC-61875-file-upload-content
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
37e7d09
feat(appsec): expose uploaded file content as WAF address server.requ…
jandro996 0817b8f
refactor(appsec): fetch both callbacks upfront before iterating file …
jandro996 dfe31ce
test(appsec): add unit tests for readContent truncation in CommonsFil…
jandro996 304846b
fix(appsec): close file item stream after reading content to avoid fd…
jandro996 22d70a0
fix(test): register requestFilesContent in InstrumentationGatewayTest…
jandro996 2076c7b
fix(appsec): extract readContent to helper class to fix muzzle failure
jandro996 f76f389
fix(appsec): cap number of file contents sent to WAF at 25
jandro996 ab77c73
Merge branch 'master' into alejandro.gonzalez/APPSEC-61875-file-uploa…
jandro996 92ed45d
Merge branch 'master' into alejandro.gonzalez/APPSEC-61875-file-uploa…
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
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
29 changes: 29 additions & 0 deletions
29
...src/main/java/datadog/trace/instrumentation/commons/fileupload/FileItemContentReader.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,29 @@ | ||
| package datadog.trace.instrumentation.commons.fileupload; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import org.apache.commons.fileupload.FileItem; | ||
|
|
||
| /** Helper class injected into the application classloader by the AppSec instrumentation. */ | ||
| public final class FileItemContentReader { | ||
| public static final int MAX_CONTENT_BYTES = 4096; | ||
| public static final int MAX_FILES_TO_INSPECT = 25; | ||
|
|
||
| public static String readContent(FileItem fileItem) { | ||
| try (InputStream is = fileItem.getInputStream()) { | ||
| byte[] buf = new byte[MAX_CONTENT_BYTES]; | ||
| int total = 0; | ||
| int n; | ||
| while (total < MAX_CONTENT_BYTES | ||
| && (n = is.read(buf, total, MAX_CONTENT_BYTES - total)) != -1) { | ||
| total += n; | ||
| } | ||
| return new String(buf, 0, total, StandardCharsets.ISO_8859_1); | ||
| } catch (IOException ignored) { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| private FileItemContentReader() {} | ||
| } |
64 changes: 64 additions & 0 deletions
64
...mentation/commons-fileupload-1.5/src/test/groovy/CommonsFileUploadAppSecModuleTest.groovy
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,64 @@ | ||
| import datadog.trace.instrumentation.commons.fileupload.FileItemContentReader | ||
| import org.apache.commons.fileupload.FileItem | ||
| import spock.lang.Specification | ||
|
|
||
| class CommonsFileUploadAppSecModuleTest extends Specification { | ||
|
|
||
| def "readContent returns full content when smaller than limit"() { | ||
| given: | ||
| def content = 'Hello, World!' | ||
| def item = fileItem(content) | ||
|
|
||
| expect: | ||
| FileItemContentReader.readContent(item) == content | ||
| } | ||
|
|
||
| def "readContent truncates content to MAX_CONTENT_BYTES"() { | ||
| given: | ||
| def largeContent = 'X' * (FileItemContentReader.MAX_CONTENT_BYTES + 500) | ||
| def item = fileItem(largeContent) | ||
|
|
||
| when: | ||
| def result = FileItemContentReader.readContent(item) | ||
|
|
||
| then: | ||
| result.length() == FileItemContentReader.MAX_CONTENT_BYTES | ||
| result == 'X' * FileItemContentReader.MAX_CONTENT_BYTES | ||
| } | ||
|
|
||
| def "readContent returns empty string when getInputStream throws"() { | ||
| given: | ||
| FileItem item = Stub(FileItem) | ||
| item.getInputStream() >> { throw new IOException('simulated error') } | ||
|
|
||
| expect: | ||
| FileItemContentReader.readContent(item) == '' | ||
| } | ||
|
|
||
| def "readContent returns empty string for empty content"() { | ||
| given: | ||
| def item = fileItem('') | ||
|
|
||
| expect: | ||
| FileItemContentReader.readContent(item) == '' | ||
| } | ||
|
|
||
| def "readContent reads exactly MAX_CONTENT_BYTES when content equals the limit"() { | ||
| given: | ||
| def content = 'A' * FileItemContentReader.MAX_CONTENT_BYTES | ||
| def item = fileItem(content) | ||
|
|
||
| when: | ||
| def result = FileItemContentReader.readContent(item) | ||
|
|
||
| then: | ||
| result.length() == FileItemContentReader.MAX_CONTENT_BYTES | ||
| result == content | ||
| } | ||
|
|
||
| private FileItem fileItem(String content) { | ||
| FileItem item = Stub(FileItem) | ||
| item.getInputStream() >> new ByteArrayInputStream(content.getBytes('ISO-8859-1')) | ||
| return item | ||
| } | ||
| } |
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.