-
Notifications
You must be signed in to change notification settings - Fork 293
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
Taint gson sources (deserialization) v1.4 and v1.5 #6084
Closed
jandro996
wants to merge
27
commits into
master
from
alejandro.gonzalez/Taint_gson_1.4_sources_deserialization
Closed
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
8ea7e5d
First gson deserialization propagation version, tested for 1.1
jandro996 73e7f23
Refactor, current instrumentation only works for v1.1
jandro996 dde6d7a
Gson instrumentation for 1.4 and 1.5
jandro996 8c62450
Gson instrumentation from 1.6
jandro996 52e583d
Fix gradle
jandro996 c5e84cb
Update smoke test
jandro996 f98d8b8
fix spotless
jandro996 daeaeaa
fix muzzle
jandro996 1c24265
Merge branch 'master' into alejandro.gonzalez/Taint_gson_sources_dese…
jandro996 50813e8
remove unnecessary exclusions
jandro996 165bce8
Merge branch 'master' into alejandro.gonzalez/Taint_gson_sources_dese…
jandro996 4205f7c
Merge branch 'master' into alejandro.gonzalez/Taint_gson_sources_dese…
jandro996 c65ffa2
First gson deserialization propagation version, tested for 1.1
jandro996 8144934
Refactor, current instrumentation only works for v1.1
jandro996 83a0d41
Gson instrumentation for 1.4 and 1.5
jandro996 8999064
Gson instrumentation from 1.6
jandro996 b508a18
Introduce PII redaction based on keywords
jpbempel 503d337
Throw exception on expression language in any case
jpbempel 7cfac2a
remove unnecessary exclusions
jandro996 af5be91
Add instrumentation to constructor with InputStream and ReInit methods
jandro996 64644a2
remove support for version v1.1
jandro996 a182e4b
use new version in smoke tests
jandro996 7ed9ebe
remove unnecessary exclusion from v1.1
jandro996 368750a
make gson deserialization test more readable
jandro996 4804b2d
add smoke tests with StringReader
jandro996 7087104
Fix tests
jandro996 2cf8009
Gson IAST source propagation for version 1.4 and 1.5
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 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 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,23 @@ | ||
muzzle { | ||
pass { | ||
group = 'com.google.code.gson' | ||
module = 'gson' | ||
versions = '[1.4, 1.5]' | ||
assertInverse = true | ||
} | ||
} | ||
|
||
apply from: "$rootDir/gradle/java.gradle" | ||
apply plugin: 'call-site-instrumentation' | ||
|
||
addTestSuiteForDir('latestDepTest', 'test') | ||
|
||
dependencies { | ||
compileOnly group: 'com.google.code.gson', name: 'gson', version: '1.4' | ||
|
||
testImplementation group: 'com.google.code.gson', name: 'gson', version: '1.4' | ||
|
||
testRuntimeOnly project(':dd-java-agent:instrumentation:iast-instrumenter') | ||
|
||
latestDepTestImplementation group: 'com.google.code.gson', name: 'gson', version: '1.5' | ||
} |
69 changes: 69 additions & 0 deletions
69
...1.4/src/main/java/datadog/trace/instrumentation/gson/JsonParserJavaccInstrumentation.java
This file contains 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,69 @@ | ||
package datadog.trace.instrumentation.gson; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.gson.JsonPrimitive; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.agent.tooling.muzzle.Reference; | ||
import datadog.trace.api.iast.InstrumentationBridge; | ||
import datadog.trace.api.iast.Propagation; | ||
import datadog.trace.api.iast.propagation.PropagationModule; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
@AutoService(Instrumenter.class) | ||
public class JsonParserJavaccInstrumentation extends Instrumenter.Iast | ||
implements Instrumenter.ForSingleType { | ||
|
||
public JsonParserJavaccInstrumentation() { | ||
super("gson"); | ||
} | ||
|
||
@Override | ||
public String instrumentedType() { | ||
return "com.google.gson.JsonParserJavacc"; | ||
} | ||
|
||
@Override | ||
public Reference[] additionalMuzzleReferences() { | ||
return new Reference[] {new Reference.Builder("com.google.gson.JsonParserJavacc").build()}; | ||
} | ||
|
||
@Override | ||
public void adviceTransformations(AdviceTransformation transformation) { | ||
transformation.applyAdvice( | ||
isConstructor().and(takesArguments(1)).and(takesArgument(0, named("java.io.Reader"))), | ||
getClass().getName() + "$ConstructAdvice"); | ||
transformation.applyAdvice( | ||
isMethod().and(takesArguments(0).and(named("JsonString"))), | ||
getClass().getName() + "$ParseAdvice"); | ||
} | ||
|
||
public static class ConstructAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
@Propagation | ||
public static void afterInit( | ||
@Advice.This Object self, @Advice.Argument(0) final java.io.Reader input) { | ||
final PropagationModule iastModule = InstrumentationBridge.PROPAGATION; | ||
if (iastModule != null && input != null) { | ||
iastModule.taintIfInputIsTainted(self, input); | ||
} | ||
} | ||
} | ||
|
||
public static class ParseAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
@Propagation | ||
public static void afterParse( | ||
@Advice.This Object self, @Advice.Return final JsonPrimitive result) { | ||
final PropagationModule iastModule = InstrumentationBridge.PROPAGATION; | ||
if (iastModule != null && result != null) { | ||
iastModule.taintIfInputIsTainted(result.getAsString(), self); | ||
} | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...test/groovy/datadog/trace/instrumentation/gson/JsonParserJavaccInstrumentationTest.groovy
This file contains 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,46 @@ | ||
package datadog.trace.instrumentation.gson | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.JsonParser | ||
import com.google.gson.JsonParserJavacc | ||
import datadog.trace.agent.test.AgentTestRunner | ||
import datadog.trace.api.iast.InstrumentationBridge | ||
import datadog.trace.api.iast.propagation.PropagationModule | ||
|
||
class JsonParserJavaccInstrumentationTest extends AgentTestRunner { | ||
|
||
@Override | ||
protected void configurePreAgent() { | ||
injectSysConfig("dd.iast.enabled", "true") | ||
} | ||
|
||
void 'test'() { | ||
given: | ||
final module = Mock(PropagationModule) | ||
InstrumentationBridge.registerIastModule(module) | ||
|
||
when: | ||
new JsonParser().parse(new StringReader(json)) | ||
|
||
then: | ||
1 * module.taintIfInputIsTainted(_ as JsonParserJavacc, _ as StringReader) | ||
callsAfterParse * module.taintIfInputIsTainted(_ as String, _ as JsonParserJavacc) | ||
0 * _ | ||
|
||
where: | ||
json | callsAfterParse | ||
'"Test"' | 1 | ||
'1' | 0 | ||
'{"name": "nameTest", "value" : "valueTest"}' | 4 | ||
'[{"name": "nameTest", "value" : "valueTest"}]' | 4 | ||
'[{"name": "nameTest", "value" : "valueTest"}, {"name": "nameTest2", "value" : "valueTest2"}]' | 8 | ||
} | ||
|
||
|
||
static final class TestBean { | ||
|
||
private String name | ||
|
||
private String value | ||
} | ||
} |
This file contains 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
25 changes: 25 additions & 0 deletions
25
...n/java-io/src/main/java/datadog/trace/instrumentation/java/lang/StringReaderCallSite.java
This file contains 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,25 @@ | ||
package datadog.trace.instrumentation.java.lang; | ||
|
||
import datadog.trace.agent.tooling.csi.CallSite; | ||
import datadog.trace.api.iast.IastCallSites; | ||
import datadog.trace.api.iast.InstrumentationBridge; | ||
import datadog.trace.api.iast.Propagation; | ||
import datadog.trace.api.iast.propagation.PropagationModule; | ||
import java.io.StringReader; | ||
import javax.annotation.Nonnull; | ||
|
||
@Propagation | ||
@CallSite(spi = IastCallSites.class) | ||
public class StringReaderCallSite { | ||
|
||
@CallSite.After("void java.io.StringReader.<init>(java.lang.String)") | ||
public static StringReader afterInit( | ||
@CallSite.AllArguments @Nonnull final Object[] params, | ||
@CallSite.Return @Nonnull final StringReader result) { | ||
final PropagationModule propagationModule = InstrumentationBridge.PROPAGATION; | ||
if (propagationModule != null) { | ||
propagationModule.taintIfInputIsTainted(result, params[0]); | ||
} | ||
return result; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...-io/src/test/groovy/datadog/trace/instrumentation/java/io/StringReaderCallSiteTest.groovy
This file contains 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,21 @@ | ||
package datadog.trace.instrumentation.java.io | ||
|
||
import datadog.trace.api.iast.InstrumentationBridge | ||
import datadog.trace.api.iast.propagation.PropagationModule | ||
import foo.bar.TestStringReaderSuite | ||
|
||
class StringReaderCallSiteTest extends BaseIoCallSiteTest{ | ||
|
||
void 'test StringReader.<init>'(){ | ||
given: | ||
PropagationModule iastModule = Mock(PropagationModule) | ||
InstrumentationBridge.registerIastModule(iastModule) | ||
final input = 'Test input' | ||
|
||
when: | ||
TestStringReaderSuite.init(input) | ||
|
||
then: | ||
1 * iastModule.taintIfInputIsTainted(_ as StringReader, input) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
dd-java-agent/instrumentation/java-io/src/test/java/foo/bar/TestStringReaderSuite.java
This file contains 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,10 @@ | ||
package foo.bar; | ||
|
||
import java.io.StringReader; | ||
|
||
public class TestStringReaderSuite { | ||
|
||
public static void init(String input) { | ||
new StringReader(input); | ||
} | ||
} |
This file contains 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 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 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 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 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.
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.
Needs to add more advices:
(InputStream, String)
ReInit(InputStream)
ReInit(StringReader)