Skip to content

Commit 005d600

Browse files
Improve IAST metric unwrapping logic
1 parent b856443 commit 005d600

3 files changed

Lines changed: 36 additions & 32 deletions

File tree

internal-api/src/main/java/datadog/trace/api/iast/telemetry/IastMetric.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import datadog.trace.api.iast.SourceTypes;
44
import datadog.trace.api.iast.VulnerabilityTypes;
55
import java.util.Locale;
6-
import java.util.function.Function;
76
import javax.annotation.Nonnull;
87
import javax.annotation.Nullable;
98

@@ -132,34 +131,40 @@ public String getSpanTag(final byte tagValue) {
132131
return spanTags[tagValue];
133132
}
134133

135-
public static final class Tag {
134+
public abstract static class Tag {
136135

137136
public static final Tag VULNERABILITY_TYPE =
138-
new Tag("vulnerability_type", VulnerabilityTypes.STRINGS, VulnerabilityTypes::unwrap);
137+
new Tag("vulnerability_type", VulnerabilityTypes.STRINGS) {
138+
@Nullable
139+
@Override
140+
public byte[] unwrap(byte tagValue) {
141+
return VulnerabilityTypes.unwrap(tagValue);
142+
}
143+
};
139144

140145
public static final Tag SOURCE_TYPE =
141-
new Tag("source_type", SourceTypes.STRINGS, SourceTypes::unwrap);
146+
new Tag("source_type", SourceTypes.STRINGS) {
142147

143-
private final String name;
148+
@Nullable
149+
@Override
150+
public byte[] unwrap(byte tagValue) {
151+
return SourceTypes.unwrap(tagValue);
152+
}
153+
};
144154

145-
private final String[] values;
155+
protected final String name;
146156

147-
private final String[] telemetryTags;
157+
protected final String[] values;
148158

149-
@Nullable private final Function<Byte, byte[]> unwrap;
159+
protected final String[] telemetryTags;
150160

151161
private Tag(final String name, final String[] values) {
152-
this(name, values, null);
153-
}
154-
155-
private Tag(final String name, final String[] values, final Function<Byte, byte[]> unwrap) {
156162
this.name = name;
157163
this.values = values;
158164
telemetryTags = new String[values.length];
159165
for (int i = 0; i < values.length; i++) {
160166
telemetryTags[i] = name + ":" + values[i];
161167
}
162-
this.unwrap = unwrap;
163168
}
164169

165170
public String getName() {
@@ -171,9 +176,7 @@ public int count() {
171176
}
172177

173178
@Nullable
174-
public byte[] unwrap(final byte tagValue) {
175-
return unwrap == null ? null : unwrap.apply(tagValue);
176-
}
179+
public abstract byte[] unwrap(final byte tagValue);
177180

178181
public String getTelemetryTag(final byte tagValue) {
179182
return telemetryTags[tagValue];

internal-api/src/main/java/datadog/trace/api/iast/telemetry/IastMetricCollector.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,20 @@ public static void add(
103103
}
104104

105105
public void addMetric(final IastMetric metric, final byte tagValue, final int value) {
106-
final Tag tag = metric.getTag();
107-
if (tag != null) {
108-
final byte[] unwrapped = tag.unwrap(tagValue);
106+
final int index = metric.getIndex(tagValue);
107+
if (index >= 0) {
108+
counters.getAndAdd(index, value);
109+
} else if (tagValue < 0) {
110+
final Tag tag = metric.getTag();
111+
final byte[] unwrapped = tag == null ? null : tag.unwrap(tagValue);
109112
if (unwrapped != null) {
110-
// e.g.: VulnerabilityTypes.RESPONSE_HEADER
111-
for (final byte unwrappedValue : unwrapped) {
112-
increment(metric.getIndex(unwrappedValue), value);
113+
for (byte unwrappedValue : unwrapped) {
114+
final int unwrappedIndex = metric.getIndex(unwrappedValue);
115+
if (unwrappedIndex >= 0) {
116+
counters.getAndAdd(unwrappedIndex, value);
117+
}
113118
}
114-
} else {
115-
increment(metric.getIndex(tagValue), value);
116119
}
117-
} else {
118-
increment(metric.getIndex(tagValue), value);
119-
}
120-
}
121-
122-
private void increment(final int index, final int value) {
123-
if (index >= 0) {
124-
counters.getAndAdd(index, value);
125120
}
126121
}
127122

internal-api/src/test/groovy/datadog/trace/api/iast/telemetry/IastMetricCollectorTest.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,15 @@ class IastMetricCollectorTest extends DDSpecification {
226226
IastMetric.INSTRUMENTED_SINK | VulnerabilityTypes.SPRING_RESPONSE // wrapped spring response
227227
IastMetric.EXECUTED_SINK | VulnerabilityTypes.SPRING_RESPONSE
228228

229+
IastMetric.INSTRUMENTED_SINK | VulnerabilityTypes.APPLICATION // wrapped application vuls
230+
IastMetric.EXECUTED_SINK | VulnerabilityTypes.APPLICATION
231+
229232
IastMetric.INSTRUMENTED_SOURCE | SourceTypes.REQUEST_HEADER_NAME
230233
IastMetric.EXECUTED_SOURCE | SourceTypes.REQUEST_HEADER_NAME
231234

235+
IastMetric.INSTRUMENTED_SOURCE | SourceTypes.KAFKA_MESSAGE // wrapped kafka sources
236+
IastMetric.EXECUTED_SOURCE | SourceTypes.KAFKA_MESSAGE
237+
232238
IastMetric.EXECUTED_TAINTED | null
233239
}
234240
}

0 commit comments

Comments
 (0)