-
Notifications
You must be signed in to change notification settings - Fork 332
feat(appsec): extend RASP callsite coverage to File-argument constructors of FileOutputStream and FileInputStream #11113
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
8
commits into
master
Choose a base branch
from
alejandro.gonzalez/APPSEC-61874_2
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
8 commits
Select commit
Hold shift + click to select a range
b63c7e2
feat(rasp): instrument FileOutputStream/FileInputStream(File) constru…
jandro996 c6458e3
Merge branch 'master' into alejandro.gonzalez/APPSEC-61874_2
jandro996 b1e991e
feat(appsec): extend RASP file I/O coverage to FileReader, FileWriter…
jandro996 6a35cf4
Apply spotless formatting
jandro996 55523c6
Merge branch 'master' into alejandro.gonzalez/APPSEC-61874_2
jandro996 2623f47
Add Java 11 test suite for RASP coverage of Java 11 file I/O APIs
jandro996 468e83f
Remove unused StandardCopyOption import from FilesCallSiteTest
jandro996 a66eb33
fix(appsec): gate FileChannel write callback on write-capable open op…
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
35 changes: 35 additions & 0 deletions
35
...a11Test/groovy/datadog/trace/instrumentation/java/io/FileReaderCharsetCallSiteTest.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,35 @@ | ||
| package datadog.trace.instrumentation.java.io | ||
|
|
||
| import datadog.trace.instrumentation.java.lang.FileIORaspHelper | ||
| import foo.bar.TestFileReaderCharsetSuite | ||
|
|
||
| import java.nio.charset.Charset | ||
|
|
||
| class FileReaderCharsetCallSiteTest extends BaseIoRaspCallSiteTest { | ||
|
|
||
| void 'test RASP new FileReader with String path and Charset'() { | ||
| setup: | ||
| final helper = Mock(FileIORaspHelper) | ||
| FileIORaspHelper.INSTANCE = helper | ||
| final file = newFile('test_rasp_fr_str_cs.txt') | ||
|
|
||
| when: | ||
| TestFileReaderCharsetSuite.newFileReader(file.path, Charset.defaultCharset()).close() | ||
|
|
||
| then: | ||
| 1 * helper.beforeFileLoaded(file.path) | ||
| } | ||
|
|
||
| void 'test RASP new FileReader with File and Charset'() { | ||
| setup: | ||
| final helper = Mock(FileIORaspHelper) | ||
| FileIORaspHelper.INSTANCE = helper | ||
| final file = newFile('test_rasp_fr_file_cs.txt') | ||
|
|
||
| when: | ||
| TestFileReaderCharsetSuite.newFileReader(file, Charset.defaultCharset()).close() | ||
|
|
||
| then: | ||
| 1 * helper.beforeFileLoaded(file.path) | ||
| } | ||
| } |
61 changes: 61 additions & 0 deletions
61
...a11Test/groovy/datadog/trace/instrumentation/java/io/FileWriterCharsetCallSiteTest.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,61 @@ | ||
| package datadog.trace.instrumentation.java.io | ||
|
|
||
| import datadog.trace.instrumentation.java.lang.FileIORaspHelper | ||
| import foo.bar.TestFileWriterCharsetSuite | ||
|
|
||
| import java.nio.charset.Charset | ||
|
|
||
| class FileWriterCharsetCallSiteTest extends BaseIoRaspCallSiteTest { | ||
|
|
||
| void 'test RASP new FileWriter with String path and Charset'() { | ||
| setup: | ||
| final helper = Mock(FileIORaspHelper) | ||
| FileIORaspHelper.INSTANCE = helper | ||
| final file = newFile('test_rasp_fw_str_cs.txt') | ||
|
|
||
| when: | ||
| TestFileWriterCharsetSuite.newFileWriter(file.path, Charset.defaultCharset()).close() | ||
|
|
||
| then: | ||
| 1 * helper.beforeFileWritten(file.path) | ||
| } | ||
|
|
||
| void 'test RASP new FileWriter with String path, Charset, and append flag'() { | ||
| setup: | ||
| final helper = Mock(FileIORaspHelper) | ||
| FileIORaspHelper.INSTANCE = helper | ||
| final file = newFile('test_rasp_fw_str_cs_append.txt') | ||
|
|
||
| when: | ||
| TestFileWriterCharsetSuite.newFileWriter(file.path, Charset.defaultCharset(), false).close() | ||
|
|
||
| then: | ||
| 1 * helper.beforeFileWritten(file.path) | ||
| } | ||
|
|
||
| void 'test RASP new FileWriter with File and Charset'() { | ||
| setup: | ||
| final helper = Mock(FileIORaspHelper) | ||
| FileIORaspHelper.INSTANCE = helper | ||
| final file = newFile('test_rasp_fw_file_cs.txt') | ||
|
|
||
| when: | ||
| TestFileWriterCharsetSuite.newFileWriter(file, Charset.defaultCharset()).close() | ||
|
|
||
| then: | ||
| 1 * helper.beforeFileWritten(file.path) | ||
| } | ||
|
|
||
| void 'test RASP new FileWriter with File, Charset, and append flag'() { | ||
| setup: | ||
| final helper = Mock(FileIORaspHelper) | ||
| FileIORaspHelper.INSTANCE = helper | ||
| final file = newFile('test_rasp_fw_file_cs_append.txt') | ||
|
|
||
| when: | ||
| TestFileWriterCharsetSuite.newFileWriter(file, Charset.defaultCharset(), false).close() | ||
|
|
||
| then: | ||
| 1 * helper.beforeFileWritten(file.path) | ||
| } | ||
| } |
61 changes: 61 additions & 0 deletions
61
...rc/java11Test/groovy/datadog/trace/instrumentation/java/io/FilesJava11CallSiteTest.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,61 @@ | ||
| package datadog.trace.instrumentation.java.io | ||
|
|
||
| import datadog.trace.instrumentation.java.lang.FileIORaspHelper | ||
| import foo.bar.TestFilesJava11Suite | ||
|
|
||
| import java.nio.charset.StandardCharsets | ||
|
|
||
| class FilesJava11CallSiteTest extends BaseIoRaspCallSiteTest { | ||
|
|
||
| void 'test RASP Files.writeString without charset'() { | ||
| setup: | ||
| final helper = Mock(FileIORaspHelper) | ||
| FileIORaspHelper.INSTANCE = helper | ||
| final path = temporaryFolder.resolve('test_rasp_writestring.txt') | ||
|
|
||
| when: | ||
| TestFilesJava11Suite.writeString(path, 'hello') | ||
|
|
||
| then: | ||
| 1 * helper.beforeFileWritten(path.toString()) | ||
| } | ||
|
|
||
| void 'test RASP Files.writeString with charset'() { | ||
| setup: | ||
| final helper = Mock(FileIORaspHelper) | ||
| FileIORaspHelper.INSTANCE = helper | ||
| final path = temporaryFolder.resolve('test_rasp_writestring_cs.txt') | ||
|
|
||
| when: | ||
| TestFilesJava11Suite.writeString(path, 'hello', StandardCharsets.UTF_8) | ||
|
|
||
| then: | ||
| 1 * helper.beforeFileWritten(path.toString()) | ||
| } | ||
|
|
||
| void 'test RASP Files.readString without charset'() { | ||
| setup: | ||
| final helper = Mock(FileIORaspHelper) | ||
| FileIORaspHelper.INSTANCE = helper | ||
| final path = newFile('test_rasp_readstring.txt').toPath() | ||
|
|
||
| when: | ||
| TestFilesJava11Suite.readString(path) | ||
|
|
||
| then: | ||
| 1 * helper.beforeFileLoaded(path.toString()) | ||
| } | ||
|
|
||
| void 'test RASP Files.readString with charset'() { | ||
| setup: | ||
| final helper = Mock(FileIORaspHelper) | ||
| FileIORaspHelper.INSTANCE = helper | ||
| final path = newFile('test_rasp_readstring_cs.txt').toPath() | ||
|
|
||
| when: | ||
| TestFilesJava11Suite.readString(path, StandardCharsets.UTF_8) | ||
|
|
||
| then: | ||
| 1 * helper.beforeFileLoaded(path.toString()) | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
...1.8/src/java11Test/groovy/datadog/trace/instrumentation/java/io/PathOfCallSiteTest.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,37 @@ | ||
| package datadog.trace.instrumentation.java.io | ||
|
|
||
| import datadog.trace.instrumentation.java.lang.FileIORaspHelper | ||
| import foo.bar.TestPathOfSuite | ||
|
|
||
| class PathOfCallSiteTest extends BaseIoRaspCallSiteTest { | ||
|
|
||
| void 'test RASP Path.of from strings'(final String first, final String... more) { | ||
| setup: | ||
| final helper = Mock(FileIORaspHelper) | ||
| FileIORaspHelper.INSTANCE = helper | ||
|
|
||
| when: | ||
| TestPathOfSuite.of(first, more) | ||
|
|
||
| then: | ||
| 1 * helper.beforeFileLoaded(first, more) | ||
|
|
||
| where: | ||
| first | more | ||
| 'test.txt' | [] as String[] | ||
| '/tmp' | ['log', 'test.txt'] as String[] | ||
| } | ||
|
|
||
| void 'test RASP Path.of from URI'() { | ||
| setup: | ||
| final helper = Mock(FileIORaspHelper) | ||
| FileIORaspHelper.INSTANCE = helper | ||
| final uri = new URI('file:/test.txt') | ||
|
|
||
| when: | ||
| TestPathOfSuite.of(uri) | ||
|
|
||
| then: | ||
| 1 * helper.beforeFileLoaded(uri) | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
...trumentation/java/java-io-1.8/src/java11Test/java/foo/bar/TestFileReaderCharsetSuite.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,19 @@ | ||
| package foo.bar; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileReader; | ||
| import java.io.IOException; | ||
| import java.nio.charset.Charset; | ||
|
|
||
| public class TestFileReaderCharsetSuite { | ||
|
|
||
| public static FileReader newFileReader(final String path, final Charset charset) | ||
| throws IOException { | ||
| return new FileReader(path, charset); | ||
| } | ||
|
|
||
| public static FileReader newFileReader(final File file, final Charset charset) | ||
| throws IOException { | ||
| return new FileReader(file, charset); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
...trumentation/java/java-io-1.8/src/java11Test/java/foo/bar/TestFileWriterCharsetSuite.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 foo.bar; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.nio.charset.Charset; | ||
|
|
||
| public class TestFileWriterCharsetSuite { | ||
|
|
||
| public static FileWriter newFileWriter(final String path, final Charset charset) | ||
| throws IOException { | ||
| return new FileWriter(path, charset); | ||
| } | ||
|
|
||
| public static FileWriter newFileWriter( | ||
| final String path, final Charset charset, final boolean append) throws IOException { | ||
| return new FileWriter(path, charset, append); | ||
| } | ||
|
|
||
| public static FileWriter newFileWriter(final File file, final Charset charset) | ||
| throws IOException { | ||
| return new FileWriter(file, charset); | ||
| } | ||
|
|
||
| public static FileWriter newFileWriter( | ||
| final File file, final Charset charset, final boolean append) throws IOException { | ||
| return new FileWriter(file, charset, append); | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
...nt/instrumentation/java/java-io-1.8/src/java11Test/java/foo/bar/TestFilesJava11Suite.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,32 @@ | ||
| package foo.bar; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.OpenOption; | ||
| import java.nio.file.Path; | ||
|
|
||
| public class TestFilesJava11Suite { | ||
|
|
||
| public static Path writeString( | ||
| final Path path, final CharSequence content, final OpenOption... options) throws IOException { | ||
| return Files.writeString(path, content, options); | ||
| } | ||
|
|
||
| public static Path writeString( | ||
| final Path path, | ||
| final CharSequence content, | ||
| final Charset charset, | ||
| final OpenOption... options) | ||
| throws IOException { | ||
| return Files.writeString(path, content, charset, options); | ||
| } | ||
|
|
||
| public static String readString(final Path path) throws IOException { | ||
| return Files.readString(path); | ||
| } | ||
|
|
||
| public static String readString(final Path path, final Charset charset) throws IOException { | ||
| return Files.readString(path, charset); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
...a-agent/instrumentation/java/java-io-1.8/src/java11Test/java/foo/bar/TestPathOfSuite.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,15 @@ | ||
| package foo.bar; | ||
|
|
||
| import java.net.URI; | ||
| import java.nio.file.Path; | ||
|
|
||
| public class TestPathOfSuite { | ||
|
|
||
| public static Path of(final String first, final String... more) { | ||
| return Path.of(first, more); | ||
| } | ||
|
|
||
| public static Path of(final URI uri) { | ||
| return Path.of(uri); | ||
| } | ||
| } |
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.
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.
@manuel-alvarez-alvarez I'm going to move this to a separated PR but I want your thoughts about this before do it