Skip to content

Commit e9eb782

Browse files
Refactor propagation module to include two different APIs for strings and objects (#6820)
1 parent 6b60035 commit e9eb782

25 files changed

Lines changed: 734 additions & 306 deletions

File tree

dd-java-agent/agent-iast/src/main/java/com/datadog/iast/propagation/PropagationModuleImpl.java

Lines changed: 472 additions & 171 deletions
Large diffs are not rendered by default.

dd-java-agent/agent-iast/src/test/groovy/com/datadog/iast/propagation/BaseCodecModuleTest.groovy

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ abstract class BaseCodecModuleTest extends IastModuleImplTestBase {
2525
}
2626
}
2727

28-
void '#method null or empty'() {
28+
void '#method null'() {
2929
when:
3030
module.&"$method".call(args.toArray())
3131

@@ -35,15 +35,10 @@ abstract class BaseCodecModuleTest extends IastModuleImplTestBase {
3535
where:
3636
method | args
3737
'onUrlDecode' | ['test', 'utf-8', null]
38-
'onUrlDecode' | ['test', 'utf-8', '']
3938
'onStringGetBytes' | ['test', 'utf-8', null]
40-
'onStringGetBytes' | ['test', 'utf-8', [] as byte[]]
4139
'onStringFromBytes' | ['test'.bytes, 0, 2, 'utf-8', null]
42-
'onStringFromBytes' | ['test'.bytes, 0, 2, 'utf-8', '']
4340
'onBase64Encode' | ['test'.bytes, null]
44-
'onBase64Encode' | ['test'.bytes, [] as byte[]]
4541
'onBase64Decode' | ['test'.bytes, null]
46-
'onBase64Decode' | ['test'.bytes, [] as byte[]]
4742
}
4843

4944
void '#method no context'() {

dd-java-agent/agent-iast/src/testFixtures/groovy/com/datadog/iast/test/IastRequestContextPreparationTrait.groovy

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,14 @@ trait IastRequestContextPreparationTrait {
6969
return tainted
7070
}
7171

72-
private final static Logger LOGGER = LoggerFactory.getLogger("map tainted objects")
73-
static {
74-
((ch.qos.logback.classic.Logger) LOGGER).level = ch.qos.logback.classic.Level.DEBUG
72+
private final static Logger LOGGER = withLogger("map tainted objects")
73+
74+
private static Logger withLogger(final String name) {
75+
final logger = LoggerFactory.getLogger(name)
76+
if (logger instanceof ch.qos.logback.classic.Logger) {
77+
((ch.qos.logback.classic.Logger) logger).level = ch.qos.logback.classic.Level.DEBUG
78+
}
79+
return logger
7580
}
7681

7782
private static void logTaint(Object o) {

dd-java-agent/instrumentation/akka-http-10.0/src/main/java/datadog/trace/instrumentation/akkahttp/iast/PathMatcherInstrumentation.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,23 @@ static void onExit(
5656
return;
5757
}
5858

59+
scala.Tuple1 tuple = (scala.Tuple1) extractions;
60+
Object value = tuple._1();
61+
5962
PropagationModule module = InstrumentationBridge.PROPAGATION;
60-
if (module == null) {
63+
if (module == null || !(value instanceof String)) {
6164
return;
6265
}
63-
64-
scala.Tuple1 tuple = (scala.Tuple1) extractions;
65-
Object value = tuple._1();
66+
final String stringValue = (String) value;
6667

6768
final IastContext ctx = reqCtx.getData(RequestContextSlot.IAST);
6869

6970
// in the test, 4 instances of PathMatcher$Match are created, all with the same value
70-
if (module.isTainted(ctx, value)) {
71+
if (module.isTainted(ctx, stringValue)) {
7172
return;
7273
}
7374

74-
if (value instanceof String) {
75-
module.taint(ctx, value, SourceTypes.REQUEST_PATH_PARAMETER);
76-
}
75+
module.taint(ctx, stringValue, SourceTypes.REQUEST_PATH_PARAMETER);
7776
}
7877
}
7978
}

dd-java-agent/instrumentation/akka-http-10.0/src/main/java/datadog/trace/instrumentation/akkahttp/iast/helpers/TaintSingleParameterFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public Tuple1<Object> apply(Tuple1<Object> v1) {
5050
while (iterator.hasNext()) {
5151
Object o = iterator.next();
5252
if (o instanceof String) {
53-
mod.taint(ctx, o, SourceTypes.REQUEST_PARAMETER_VALUE, paramName);
53+
mod.taint(ctx, (String) o, SourceTypes.REQUEST_PARAMETER_VALUE, paramName);
5454
}
5555
}
5656
} else if (value instanceof String) {
57-
mod.taint(ctx, value, SourceTypes.REQUEST_PARAMETER_VALUE, paramName);
57+
mod.taint(ctx, (String) value, SourceTypes.REQUEST_PARAMETER_VALUE, paramName);
5858
}
5959

6060
return v1;

dd-java-agent/instrumentation/akka-http-10.2-iast/src/main/java/datadog/trace/instrumentation/akkahttp102/iast/helpers/TaintParametersFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public Tuple1<T> apply(Tuple1<T> v1) {
4141
while (iterator.hasNext()) {
4242
Object o = iterator.next();
4343
if (o instanceof String) {
44-
mod.taint(ctx, o, SourceTypes.REQUEST_PARAMETER_VALUE, paramName);
44+
mod.taint(ctx, (String) o, SourceTypes.REQUEST_PARAMETER_VALUE, paramName);
4545
}
4646
}
4747
} else if (value instanceof String) {
48-
mod.taint(ctx, value, SourceTypes.REQUEST_PARAMETER_VALUE, paramName);
48+
mod.taint(ctx, (String) value, SourceTypes.REQUEST_PARAMETER_VALUE, paramName);
4949
}
5050

5151
return v1;

dd-java-agent/instrumentation/java-io/src/main/java/datadog/trace/instrumentation/java/lang/StringReaderCallSite.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ public static StringReader afterInit(
1818
@CallSite.Return @Nonnull final StringReader result) {
1919
final PropagationModule propagationModule = InstrumentationBridge.PROPAGATION;
2020
if (propagationModule != null) {
21-
propagationModule.taintIfTainted(result, params[0]);
21+
try {
22+
propagationModule.taintIfTainted(result, params[0]);
23+
} catch (Throwable e) {
24+
propagationModule.onUnexpectedException("afterInit threw", e);
25+
}
2226
}
2327
return result;
2428
}

dd-java-agent/instrumentation/java-lang/src/main/java/datadog/trace/instrumentation/java/lang/StringCallSite.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static String aroundJoin(
123123
final StringModule module = InstrumentationBridge.STRING;
124124
if (module != null) {
125125
try {
126-
module.onStringJoin(result, delimiter, copy.toArray(new CharSequence[copy.size()]));
126+
module.onStringJoin(result, delimiter, copy.toArray(new CharSequence[0]));
127127
} catch (final Throwable e) {
128128
module.onUnexpectedException("afterSubSequence threw", e);
129129
}
@@ -412,7 +412,7 @@ public static String[] afterSplit(
412412
public static String[] afterSplitWithLimit(
413413
@CallSite.This @Nonnull final String self,
414414
@CallSite.Argument(0) @Nonnull final String regex,
415-
@CallSite.Argument(1) @Nonnull final int pos,
415+
@CallSite.Argument(1) final int pos,
416416
@CallSite.Return @Nonnull final String[] result) {
417417
final StringModule module = InstrumentationBridge.STRING;
418418
if (module != null) {

dd-java-agent/instrumentation/jersey/src/main/java/datadog/trace/instrumentation/jersey/AbstractParamValueExtractorInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static void onExit(
5454
final PropagationModule module = InstrumentationBridge.PROPAGATION;
5555
if (module != null) {
5656
IastContext ctx = reqCtx.getData(RequestContextSlot.IAST);
57-
module.taint(ctx, result, ThreadLocalSourceType.get(), parameterName);
57+
module.taint(ctx, (String) result, ThreadLocalSourceType.get(), parameterName);
5858
}
5959
}
6060
}

dd-java-agent/instrumentation/jersey/src/main/java/datadog/trace/instrumentation/jersey/AbstractStringReaderAdvice.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static void onExit(
2121
final PropagationModule module = InstrumentationBridge.PROPAGATION;
2222
if (module != null) {
2323
IastContext ctx = reqCtx.getData(RequestContextSlot.IAST);
24-
module.taint(ctx, result, SourceTypes.REQUEST_PARAMETER_VALUE);
24+
module.taint(ctx, (String) result, SourceTypes.REQUEST_PARAMETER_VALUE);
2525
}
2626
}
2727
}

0 commit comments

Comments
 (0)