-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IAST support for commons fileupload (#6089)
- Loading branch information
1 parent
74313a3
commit df6decb
Showing
7 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
Validating CODEOWNERS rules …
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
Submodule integrations-core
updated
2595 files
13 changes: 13 additions & 0 deletions
13
dd-java-agent/instrumentation/commons-fileupload/build.gradle
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,13 @@ | ||
|
||
apply from: "$rootDir/gradle/java.gradle" | ||
addTestSuiteForDir('latestDepTest', 'test') | ||
|
||
dependencies { | ||
compileOnly group: 'org.apache.commons', name: 'commons-fileupload2', version: '2.0.0-M1' | ||
testImplementation group: 'org.apache.commons', name: 'commons-fileupload2', version: '2.0.0-M1' | ||
testImplementation group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '7.0.0' | ||
|
||
|
||
testRuntimeOnly project(':dd-java-agent:instrumentation:iast-instrumenter') | ||
latestDepTestImplementation group: 'org.apache.commons', name: 'commons-fileupload2', version: '+' | ||
} |
68 changes: 68 additions & 0 deletions
68
.../java/datadog/trace/instrumentation/commons/fileupload/CommonsFileuploadInstrumenter.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,68 @@ | ||
package datadog.trace.instrumentation.commons.fileupload; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
import static net.bytebuddy.matcher.ElementMatchers.returns; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
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 java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
@AutoService(Instrumenter.class) | ||
public class CommonsFileuploadInstrumenter extends Instrumenter.Iast | ||
implements Instrumenter.ForConfiguredTypes { | ||
|
||
public CommonsFileuploadInstrumenter() { | ||
super("commons-fileupload"); | ||
} | ||
|
||
@Override | ||
public void adviceTransformations(AdviceTransformation transformation) { | ||
transformation.applyAdvice( | ||
isMethod() | ||
.and(named("parse")) | ||
.and(isPublic()) | ||
.and(returns(Map.class)) | ||
.and(takesArguments(char[].class, int.class, int.class, char.class)), | ||
getClass().getName() + "$ParseAdvice"); | ||
} | ||
|
||
@Override | ||
public Collection<String> configuredMatchingTypes() { | ||
return Arrays.asList( | ||
new String[] { | ||
"org.apache.commons.fileupload.ParameterParser", | ||
"org.apache.tomcat.util.http.fileupload.ParameterParser" | ||
}); | ||
} | ||
|
||
public static class ParseAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class) | ||
@Source(SourceTypes.REQUEST_MULTIPART_PARAMETER) | ||
public static Map<String, String> onExit(@Advice.Return final Map<String, String> map) { | ||
if (!map.isEmpty()) { | ||
final PropagationModule module = InstrumentationBridge.PROPAGATION; | ||
if (module != null) { | ||
final IastContext ctx = IastContext.Provider.get(); | ||
for (final Map.Entry<String, String> entry : map.entrySet()) { | ||
if (entry.getValue() != null) { | ||
module.taint( | ||
ctx, entry.getValue(), SourceTypes.REQUEST_MULTIPART_PARAMETER, entry.getKey()); | ||
} | ||
} | ||
} | ||
} | ||
return map; | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...nt/instrumentation/commons-fileupload/src/test/groovy/MultipartInstrumentationTest.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,40 @@ | ||
import datadog.trace.agent.test.AgentTestRunner | ||
import datadog.trace.api.iast.InstrumentationBridge | ||
import datadog.trace.api.iast.SourceTypes | ||
import datadog.trace.api.iast.propagation.PropagationModule | ||
|
||
|
||
class MultipartInstrumentationTest extends AgentTestRunner { | ||
@Override | ||
protected void configurePreAgent() { | ||
injectSysConfig('dd.iast.enabled', 'true') | ||
} | ||
|
||
@Override | ||
void cleanup() { | ||
InstrumentationBridge.clearIastModules() | ||
} | ||
|
||
void 'test commons fileupload2 ParameterParser.parse'() { | ||
given: | ||
final module = Mock(PropagationModule) | ||
InstrumentationBridge.registerIastModule(module) | ||
final content = "Content-Disposition: form-data; name=\"file\"; filename=\"=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\"\r\n" | ||
final parser = clazz.newInstance() | ||
|
||
when: | ||
parser.parse(content, new char[]{ | ||
',', ';' | ||
}) | ||
|
||
then: | ||
1 * module.taint(null, 'file', SourceTypes.REQUEST_MULTIPART_PARAMETER, 'name') | ||
1 * module.taint(null, _, SourceTypes.REQUEST_MULTIPART_PARAMETER, 'filename') | ||
0 * _ | ||
|
||
where: | ||
clazz | _ | ||
org.apache.commons.fileupload.ParameterParser | _ | ||
org.apache.tomcat.util.http.fileupload.ParameterParser | _ | ||
} | ||
} |
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