-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathReporter.java
More file actions
196 lines (172 loc) · 6.91 KB
/
Reporter.java
File metadata and controls
196 lines (172 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.datadog.iast;
import static com.datadog.iast.IastTag.Enabled.ANALYZED;
import static datadog.trace.api.telemetry.LogCollector.SEND_TELEMETRY;
import static datadog.trace.util.stacktrace.StackTraceEvent.DEFAULT_LANGUAGE;
import com.datadog.iast.model.Vulnerability;
import com.datadog.iast.model.VulnerabilityBatch;
import com.datadog.iast.taint.TaintedObjects;
import datadog.trace.api.Config;
import datadog.trace.api.ProductTraceSource;
import datadog.trace.api.gateway.RequestContext;
import datadog.trace.api.gateway.RequestContextSlot;
import datadog.trace.api.internal.TraceSegment;
import datadog.trace.bootstrap.instrumentation.api.*;
import datadog.trace.util.AgentTaskScheduler;
import datadog.trace.util.stacktrace.StackTraceEvent;
import datadog.trace.util.stacktrace.StackTraceFrame;
import datadog.trace.util.stacktrace.StackUtils;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Reports IAST vulnerabilities. */
public class Reporter {
private static final Logger LOG = LoggerFactory.getLogger(Reporter.class);
private static final String IAST_TAG = "iast";
private static final String VULNERABILITY_SPAN_NAME = "vulnerability";
public static final String METASTRUCT_VULNERABILITY = "vulnerability";
private final Predicate<Vulnerability> duplicated;
Reporter() {
this(Config.get(), null);
}
public Reporter(final Config config, @Nullable final AgentTaskScheduler taskScheduler) {
this(
config.isIastDeduplicationEnabled()
? new HashBasedDeduplication(taskScheduler)
: v -> false);
}
Reporter(final Predicate<Vulnerability> duplicate) {
this.duplicated = duplicate;
}
public void report(@Nullable final AgentSpan span, @Nonnull final Vulnerability vulnerability) {
if (duplicated.test(vulnerability)) {
return;
}
if (span == null) {
final AgentSpan newSpan = startNewSpan();
try (final AgentScope autoClosed = tracer().activateManualSpan(newSpan)) {
vulnerability.updateSpan(newSpan);
reportVulnerability(newSpan, vulnerability);
} finally {
newSpan.finish();
}
} else {
reportVulnerability(span, vulnerability);
}
}
private void reportVulnerability(
@Nonnull final AgentSpan span, @Nonnull final Vulnerability vulnerability) {
final VulnerabilityBatch batch = getOrCreateVulnerabilityBatch(span);
if (batch != null) {
batch.add(vulnerability);
if (Config.get().isIastStackTraceEnabled()
&& batch.getVulnerabilities() != null
&& vulnerability.getLocation().getStackId() == null) {
String stackId =
addVulnerabilityStackTrace(span, String.valueOf(batch.getVulnerabilities().size()));
if (stackId != null) {
vulnerability.getLocation().setStackId(stackId);
}
}
}
}
@Nullable
private static String addVulnerabilityStackTrace(@Nonnull AgentSpan span, String index) {
final RequestContext reqCtx = span.getRequestContext();
if (reqCtx == null) {
return null;
}
List<StackTraceFrame> frames = StackUtils.generateUserCodeStackTrace();
StackTraceEvent stackTraceEvent = new StackTraceEvent(frames, DEFAULT_LANGUAGE, index, null);
StackUtils.addStacktraceEventsToMetaStruct(
reqCtx, METASTRUCT_VULNERABILITY, Collections.singletonList(stackTraceEvent));
return stackTraceEvent.getId();
}
@Nullable
private VulnerabilityBatch getOrCreateVulnerabilityBatch(final AgentSpan span) {
final RequestContext reqCtx = span.getRequestContext();
if (reqCtx == null) {
return null;
}
final TraceSegment segment = reqCtx.getTraceSegment();
final Object current = segment.getDataTop(IAST_TAG);
if (current instanceof VulnerabilityBatch) {
return (VulnerabilityBatch) current;
}
if (current == null) {
final IastRequestContext iastCtx = reqCtx.getData(RequestContextSlot.IAST);
final VulnerabilityBatch batch;
if (iastCtx != null) {
batch = iastCtx.getVulnerabilityBatch();
} else {
// in case the request is not handled by IAST create a new one and set the analyzed flag
batch = new VulnerabilityBatch();
ANALYZED.setTagTop(segment);
}
segment.setDataTop(IAST_TAG, batch);
// Once we have added a vulnerability, try to override sampling and keep the trace.
// TODO: We need to check if we can have an API with more fine-grained semantics on why traces
// are kept.
segment.setTagTop(Tags.ASM_KEEP, true);
segment.setTagTop(Tags.PROPAGATED_TRACE_SOURCE, ProductTraceSource.ASM);
return batch;
}
// TODO: can we do better here? (maybe create a new span)
LOG.debug(SEND_TELEMETRY, "Cannot attach vulnerability to span, current={}", current);
return null;
}
private AgentSpan startNewSpan() {
final AgentSpanContext tagContext =
new TagContext()
.withRequestContextDataIast(new IastRequestContext(TaintedObjects.NoOp.INSTANCE, true));
final AgentSpan span =
tracer()
.startSpan("iast", VULNERABILITY_SPAN_NAME, tagContext)
.setSpanType(InternalSpanTypes.VULNERABILITY);
ANALYZED.setTag(span);
return span;
}
protected AgentTracer.TracerAPI tracer() {
return AgentTracer.get();
}
/**
* This class maintains a set of vulnerability hashes that have already been reported, we don't
* care about thread safety too much as an occasional duplicated report is not a big deal.
*/
protected static class HashBasedDeduplication implements Predicate<Vulnerability> {
private static final int DEFAULT_MAX_SIZE = 1000;
private final int maxSize;
private final Set<Long> hashes;
public HashBasedDeduplication(@Nullable final AgentTaskScheduler taskScheduler) {
this(DEFAULT_MAX_SIZE, taskScheduler);
}
HashBasedDeduplication(final int size, @Nullable final AgentTaskScheduler taskScheduler) {
maxSize = size;
hashes = ConcurrentHashMap.newKeySet(size);
if (taskScheduler != null) {
// Reset deduplication cache every hour. This helps the backend when calculating exposure
// windows, by sending
// the same vulnerabilities from time to time.
taskScheduler.scheduleAtFixedRate(hashes::clear, 1, 1, TimeUnit.HOURS);
}
}
@Override
public boolean test(final Vulnerability vulnerability) {
if (!vulnerability.getType().isDeduplicable()) {
return false;
}
final boolean newVulnerability = hashes.add(vulnerability.getHash());
if (newVulnerability && hashes.size() > maxSize) {
hashes.clear();
hashes.add(vulnerability.getHash());
}
return !newVulnerability;
}
}
}