-
Notifications
You must be signed in to change notification settings - Fork 324
Add IAST taint tracking for DB values #8072
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 8 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
988b767
First view of the new TaintableDb
Mariovido 5c87acd
Add tainted db values with contextStore
Mariovido 1e15689
Add config file for maximum number of taints per ResultSet
Mariovido fe595d2
Improve tests for IastResultSetInstrumentation
Mariovido 41ae7eb
Improve instrumentation and tests
Mariovido a12b842
Remove exclusion
Mariovido bfac899
Add additional names to the instrumentation
Mariovido da1e53c
Add new test
Mariovido d1abb9d
Update env variable name
Mariovido 0e3795c
Add source name to tainted objects
Mariovido 9aa13fd
Merge branch with master
Mariovido f808e6a
Cover edge case of nested calls of getString
Mariovido 75b3a8b
Remove unnecesary naming
Mariovido 6ea517d
Replace changing source for CallDepthThreadLocalMap approach
Mariovido b080de3
Merge branch 'master' into mario.vidal/taint_db_values_iast
Mariovido f250faf
Fix build error
Mariovido e2ee151
Merge with master
Mariovido ed39adf
Rename ENV Variable and add more coverage to tests
Mariovido d9b5d81
Refactor excluded sources to be more generic
Mariovido 5899711
Fix Code Quality Violation
Mariovido cc3f5ef
Modify byte[] to BitSet
Mariovido ec09325
Merge with master
Mariovido 8c5291e
Fix tests
Mariovido 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
104 changes: 104 additions & 0 deletions
104
...n/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/IastResultSetInstrumentation.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,104 @@ | ||
| package datadog.trace.instrumentation.jdbc; | ||
|
|
||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface; | ||
| import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
| import static java.util.Collections.singletonMap; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| 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.Config; | ||
| import datadog.trace.api.gateway.RequestContext; | ||
| import datadog.trace.api.gateway.RequestContextSlot; | ||
| import datadog.trace.api.iast.IastContext; | ||
| import datadog.trace.api.iast.InstrumentationBridge; | ||
| import datadog.trace.api.iast.Source; | ||
| import datadog.trace.api.iast.SourceTypes; | ||
| import datadog.trace.api.iast.propagation.PropagationModule; | ||
| import datadog.trace.bootstrap.ContextStore; | ||
| import datadog.trace.bootstrap.InstrumentationContext; | ||
| import java.sql.ResultSet; | ||
| import java.util.Map; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
|
|
||
| @AutoService(InstrumenterModule.class) | ||
| public class IastResultSetInstrumentation extends InstrumenterModule.Iast | ||
| implements Instrumenter.ForTypeHierarchy { | ||
|
|
||
| public IastResultSetInstrumentation() { | ||
| super("jdbc", "jdbc-resultset", "iast-resultset"); | ||
| } | ||
|
|
||
| @Override | ||
| public String hierarchyMarkerType() { | ||
| return "java.sql.ResultSet"; | ||
| } | ||
|
|
||
| @Override | ||
| public ElementMatcher<TypeDescription> hierarchyMatcher() { | ||
| return implementsInterface(named("java.sql.ResultSet")); | ||
| } | ||
|
|
||
| @Override | ||
| public void methodAdvice(MethodTransformer transformer) { | ||
| transformer.applyAdvice( | ||
| isMethod().and(named("next").and(takesArguments(0))), | ||
| IastResultSetInstrumentation.class.getName() + "$NextAdvice"); | ||
| transformer.applyAdvice( | ||
| isMethod() | ||
| .and(named("getString").or(named("getNString"))) | ||
| .and(takesArguments(int.class).or(takesArguments(String.class))), | ||
| IastResultSetInstrumentation.class.getName() + "$GetParameterAdvice"); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> contextStore() { | ||
| return singletonMap("java.sql.ResultSet", Integer.class.getName()); | ||
| } | ||
|
|
||
| public static class NextAdvice { | ||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| @Source(SourceTypes.SQL_TABLE) | ||
| public static void onExit(@Advice.This final ResultSet resultSet) { | ||
| ContextStore<ResultSet, Integer> contextStore = | ||
| InstrumentationContext.get(ResultSet.class, Integer.class); | ||
| if (contextStore.get(resultSet) != null) { | ||
| contextStore.put(resultSet, contextStore.get(resultSet) + 1); | ||
| } else { | ||
| // first time | ||
| contextStore.put(resultSet, 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @RequiresRequestContext(RequestContextSlot.IAST) | ||
| public static class GetParameterAdvice { | ||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| @Source(SourceTypes.SQL_TABLE) | ||
| public static void onExit( | ||
| @Advice.Return final String value, | ||
| @Advice.This final ResultSet resultSet, | ||
| @ActiveRequestContext RequestContext reqCtx) { | ||
| ContextStore<ResultSet, Integer> contextStore = | ||
| InstrumentationContext.get(ResultSet.class, Integer.class); | ||
| if (contextStore.get(resultSet) > Config.get().getIastDbRowsToTaint()) { | ||
| return; | ||
| } | ||
| if (value == null) { | ||
| return; | ||
| } | ||
| final PropagationModule module = InstrumentationBridge.PROPAGATION; | ||
| if (module == null) { | ||
| return; | ||
| } | ||
| IastContext ctx = reqCtx.getData(RequestContextSlot.IAST); | ||
| module.taintString(ctx, value, SourceTypes.SQL_TABLE); | ||
| } | ||
| } | ||
| } |
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.