-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathAppSecRequestContext.java
More file actions
706 lines (584 loc) · 19.1 KB
/
AppSecRequestContext.java
File metadata and controls
706 lines (584 loc) · 19.1 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
package com.datadog.appsec.gateway;
import static datadog.trace.api.telemetry.LogCollector.SEND_TELEMETRY;
import static java.util.Collections.emptySet;
import com.datadog.appsec.event.data.Address;
import com.datadog.appsec.event.data.DataBundle;
import com.datadog.appsec.report.AppSecEvent;
import com.datadog.appsec.util.StandardizedLogging;
import com.datadog.ddwaf.WafContext;
import com.datadog.ddwaf.WafHandle;
import com.datadog.ddwaf.WafMetrics;
import datadog.trace.api.Config;
import datadog.trace.api.http.StoredBodySupplier;
import datadog.trace.api.internal.TraceSegment;
import datadog.trace.util.stacktrace.StackTraceEvent;
import java.io.Closeable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// TODO: different methods to be called by different parts perhaps splitting it would make sense
// or at least create separate interfaces
public class AppSecRequestContext implements DataBundle, Closeable {
private static final Logger log = LoggerFactory.getLogger(AppSecRequestContext.class);
// Values MUST be lowercase! Lookup with Ignore Case
// was removed due performance reason
// request headers that will always be set when appsec is enabled
public static final Set<String> DEFAULT_REQUEST_HEADERS_ALLOW_LIST =
new TreeSet<>(
Arrays.asList(
"content-type",
"user-agent",
"accept",
"x-amzn-trace-id",
"cloudfront-viewer-ja3-fingerprint",
"cf-ray",
"x-cloud-trace-context",
"x-appgw-trace-id",
"x-sigsci-requestid",
"x-sigsci-tags",
"akamai-user-risk"));
// request headers when there are security events
public static final Set<String> REQUEST_HEADERS_ALLOW_LIST =
new TreeSet<>(
Arrays.asList(
"x-forwarded-for",
"x-real-ip",
"true-client-ip",
"x-client-ip",
"x-forwarded",
"forwarded-for",
"x-cluster-client-ip",
"fastly-client-ip",
"cf-connecting-ip",
"cf-connecting-ipv6",
"forwarded",
"via",
"content-length",
"content-encoding",
"content-language",
"host",
"accept-encoding",
"accept-language"));
// response headers when there are security events
public static final Set<String> RESPONSE_HEADERS_ALLOW_LIST =
new TreeSet<>(
Arrays.asList("content-length", "content-type", "content-encoding", "content-language"));
static {
REQUEST_HEADERS_ALLOW_LIST.addAll(DEFAULT_REQUEST_HEADERS_ALLOW_LIST);
}
private final ConcurrentHashMap<Address<?>, Object> persistentData = new ConcurrentHashMap<>();
private volatile Queue<AppSecEvent> appSecEvents;
private volatile Queue<StackTraceEvent> stackTraceEvents;
// assume these will always be written and read by the same thread
private String scheme;
private String method;
private String savedRawURI;
private String route;
private final Map<String, List<String>> requestHeaders = new LinkedHashMap<>();
private final Map<String, List<String>> responseHeaders = new LinkedHashMap<>();
private volatile Map<String, List<String>> collectedCookies;
private boolean finishedRequestHeaders;
private boolean finishedResponseHeaders;
private String peerAddress;
private int peerPort;
private String inferredClientIp;
private volatile StoredBodySupplier storedRequestBodySupplier;
private String dbType;
private int responseStatus;
private boolean reqDataPublished;
private boolean rawReqBodyPublished;
private boolean convertedReqBodyPublished;
private boolean responseBodyPublished;
private boolean respDataPublished;
private boolean pathParamsPublished;
private volatile Map<String, String> derivatives;
private final AtomicBoolean rateLimited = new AtomicBoolean(false);
private volatile boolean throttled;
// should be guarded by this
private volatile WafContext wafContext;
private volatile boolean wafContextClosed;
// set after wafContext is set
private volatile WafMetrics wafMetrics;
private volatile WafMetrics raspMetrics;
private final AtomicInteger raspMetricsCounter = new AtomicInteger(0);
private volatile boolean wafBlocked;
private volatile boolean wafErrors;
private volatile boolean wafTruncated;
private volatile boolean wafRequestBlockFailure;
private volatile boolean wafRateLimited;
private volatile int wafTimeouts;
private volatile int raspTimeouts;
private volatile Object processedRequestBody;
private volatile boolean raspMatched;
// keep a reference to the last published usr.id
private volatile String userId;
// keep a reference to the last published usr.login
private volatile String userLogin;
// keep a reference to the last published usr.session_id
private volatile String sessionId;
// Used to detect missing request-end event at close.
private volatile boolean requestEndCalled;
private volatile boolean keepOpenForApiSecurityPostProcessing;
private volatile Long apiSecurityEndpointHash;
private static final AtomicIntegerFieldUpdater<AppSecRequestContext> WAF_TIMEOUTS_UPDATER =
AtomicIntegerFieldUpdater.newUpdater(AppSecRequestContext.class, "wafTimeouts");
private static final AtomicIntegerFieldUpdater<AppSecRequestContext> RASP_TIMEOUTS_UPDATER =
AtomicIntegerFieldUpdater.newUpdater(AppSecRequestContext.class, "raspTimeouts");
// to be called by the Event Dispatcher
public void addAll(DataBundle newData) {
for (Map.Entry<Address<?>, Object> entry : newData) {
Address<?> address = entry.getKey();
Object value = entry.getValue();
if (value == null) {
log.debug(SEND_TELEMETRY, "Address {} ignored, because contains null value.", address);
continue;
}
Object prev = persistentData.putIfAbsent(address, value);
if (prev == value || value.equals(prev)) {
continue;
} else if (prev != null) {
log.debug(SEND_TELEMETRY, "Attempt to replace context value for {}", address);
}
if (log.isDebugEnabled()) {
StandardizedLogging.addressPushed(log, address);
}
}
}
public WafMetrics getWafMetrics() {
return wafMetrics;
}
public WafMetrics getRaspMetrics() {
return raspMetrics;
}
public AtomicInteger getRaspMetricsCounter() {
return raspMetricsCounter;
}
public void setWafBlocked() {
this.wafBlocked = true;
}
public boolean isWafBlocked() {
return wafBlocked;
}
public void setWafErrors() {
this.wafErrors = true;
}
public boolean hasWafErrors() {
return wafErrors;
}
public void setWafTruncated() {
this.wafTruncated = true;
}
public boolean isWafTruncated() {
return wafTruncated;
}
public void setWafRequestBlockFailure() {
this.wafRequestBlockFailure = true;
}
public boolean isWafRequestBlockFailure() {
return wafRequestBlockFailure;
}
public void setWafRateLimited() {
this.wafRateLimited = true;
}
public boolean isWafRateLimited() {
return wafRateLimited;
}
public void increaseWafTimeouts() {
WAF_TIMEOUTS_UPDATER.incrementAndGet(this);
}
public void increaseRaspTimeouts() {
RASP_TIMEOUTS_UPDATER.incrementAndGet(this);
}
public int getWafTimeouts() {
return wafTimeouts;
}
public int getRaspTimeouts() {
return raspTimeouts;
}
public WafContext getOrCreateWafContext(WafHandle ctx, boolean createMetrics, boolean isRasp) {
if (createMetrics) {
if (wafMetrics == null) {
this.wafMetrics = ctx.createMetrics();
}
if (isRasp && raspMetrics == null) {
this.raspMetrics = ctx.createMetrics();
}
}
WafContext curWafContext;
synchronized (this) {
curWafContext = this.wafContext;
if (curWafContext != null) {
return curWafContext;
}
curWafContext = ctx.openContext();
this.wafContext = curWafContext;
}
return curWafContext;
}
public void closeWafContext() {
if (wafContext != null) {
synchronized (this) {
if (wafContext != null) {
try {
wafContextClosed = true;
wafContext.close();
} finally {
wafContext = null;
}
}
}
}
}
/* Implementation of DataBundle */
@Override
public boolean hasAddress(Address<?> addr) {
return persistentData.containsKey(addr);
}
@Override
public Collection<Address<?>> getAllAddresses() {
return persistentData.keySet();
}
@Override
public int size() {
return persistentData.size();
}
@Override
@SuppressWarnings("unchecked")
public <T> T get(Address<T> addr) {
return (T) persistentData.get(addr);
}
@Override
public Iterator<Map.Entry<Address<?>, Object>> iterator() {
return persistentData.entrySet().iterator();
}
/* Interface for use of GatewayBridge */
String getScheme() {
return scheme;
}
void setScheme(String scheme) {
this.scheme = scheme;
}
public String getMethod() {
return method;
}
void setMethod(String method) {
this.method = method;
}
String getSavedRawURI() {
return savedRawURI;
}
void setRawURI(String savedRawURI) {
if (this.savedRawURI != null && this.savedRawURI.compareToIgnoreCase(savedRawURI) != 0) {
throw new IllegalStateException(
"Forbidden attempt to set different raw URI for given request context");
}
this.savedRawURI = savedRawURI;
}
public String getRoute() {
return route;
}
public void setRoute(String route) {
this.route = route;
}
public void setKeepOpenForApiSecurityPostProcessing(final boolean flag) {
this.keepOpenForApiSecurityPostProcessing = flag;
}
public boolean isKeepOpenForApiSecurityPostProcessing() {
return this.keepOpenForApiSecurityPostProcessing;
}
public void setApiSecurityEndpointHash(long hash) {
this.apiSecurityEndpointHash = hash;
}
public Long getApiSecurityEndpointHash() {
return this.apiSecurityEndpointHash;
}
void addRequestHeader(String name, String value) {
if (finishedRequestHeaders) {
throw new IllegalStateException("Request headers were said to be finished before");
}
if (name == null || value == null) {
return;
}
List<String> strings =
requestHeaders.computeIfAbsent(name.toLowerCase(Locale.ROOT), h -> new ArrayList<>(1));
strings.add(value);
}
void finishRequestHeaders() {
this.finishedRequestHeaders = true;
}
boolean isFinishedRequestHeaders() {
return finishedRequestHeaders;
}
Map<String, List<String>> getRequestHeaders() {
return requestHeaders;
}
void addResponseHeader(String name, String value) {
if (finishedResponseHeaders) {
throw new IllegalStateException("Response headers were said to be finished before");
}
if (name == null || value == null) {
return;
}
List<String> strings =
responseHeaders.computeIfAbsent(name.toLowerCase(Locale.ROOT), h -> new ArrayList<>(1));
strings.add(value);
}
public void finishResponseHeaders() {
this.finishedResponseHeaders = true;
}
public boolean isFinishedResponseHeaders() {
return finishedResponseHeaders;
}
Map<String, List<String>> getResponseHeaders() {
return responseHeaders;
}
void addCookies(Map<String, List<String>> cookies) {
if (finishedRequestHeaders) {
throw new IllegalStateException("Request headers were said to be finished before");
}
if (collectedCookies == null) {
collectedCookies = cookies;
} else {
collectedCookies.putAll(cookies);
}
}
Map<String, ? extends Collection<String>> getCookies() {
return collectedCookies != null ? collectedCookies : Collections.emptyMap();
}
String getPeerAddress() {
return peerAddress;
}
void setPeerAddress(String peerAddress) {
this.peerAddress = peerAddress;
}
public int getPeerPort() {
return peerPort;
}
public void setPeerPort(int peerPort) {
this.peerPort = peerPort;
}
void setInferredClientIp(String ipAddress) {
this.inferredClientIp = ipAddress;
}
String getInferredClientIp() {
return inferredClientIp;
}
void setStoredRequestBodySupplier(StoredBodySupplier storedRequestBodySupplier) {
this.storedRequestBodySupplier = storedRequestBodySupplier;
}
public String getDbType() {
return dbType;
}
public void setDbType(String dbType) {
this.dbType = dbType;
}
public int getResponseStatus() {
return responseStatus;
}
public void setResponseStatus(int responseStatus) {
this.responseStatus = responseStatus;
}
public boolean isReqDataPublished() {
return reqDataPublished;
}
public void setReqDataPublished(boolean reqDataPublished) {
this.reqDataPublished = reqDataPublished;
}
public boolean isPathParamsPublished() {
return pathParamsPublished;
}
public void setPathParamsPublished(boolean pathParamsPublished) {
this.pathParamsPublished = pathParamsPublished;
}
public boolean isRawReqBodyPublished() {
return rawReqBodyPublished;
}
public void setRawReqBodyPublished(boolean rawReqBodyPublished) {
this.rawReqBodyPublished = rawReqBodyPublished;
}
public boolean isConvertedReqBodyPublished() {
return convertedReqBodyPublished;
}
public void setConvertedReqBodyPublished(boolean convertedReqBodyPublished) {
this.convertedReqBodyPublished = convertedReqBodyPublished;
}
public boolean isResponseBodyPublished() {
return responseBodyPublished;
}
public void setResponseBodyPublished(final boolean responseBodyPublished) {
this.responseBodyPublished = responseBodyPublished;
}
public boolean isRespDataPublished() {
return respDataPublished;
}
public void setRespDataPublished(boolean respDataPublished) {
this.respDataPublished = respDataPublished;
}
/**
* Updates the current used usr.id
*
* @return {@code false} if the user id has not been updated
*/
public boolean updateUserId(String userId) {
if (Objects.equals(this.userId, userId)) {
return false;
}
this.userId = userId;
return true;
}
/**
* Updates current used usr.login
*
* @return {@code false} if the user login has not been updated
*/
public boolean updateUserLogin(String userLogin) {
if (Objects.equals(this.userLogin, userLogin)) {
return false;
}
this.userLogin = userLogin;
return true;
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public String getSessionId() {
return sessionId;
}
/**
* Close the context and release all resources. This method is idempotent and can be called
* multiple times. For each root span, this method is always called from
* CoreTracer#onRootSpanPublished.
*/
@Override
public void close() {
if (!requestEndCalled) {
log.debug(SEND_TELEMETRY, "Request end event was not called before close");
}
// For API Security, we sometimes keep contexts open for late processing. In that case, this
// flag needs to be
// later reset by the API Security post-processor and close must be called again.
if (!keepOpenForApiSecurityPostProcessing) {
if (wafContext != null) {
log.debug(
SEND_TELEMETRY, "WAF object had not been closed (probably missed request-end event)");
closeWafContext();
}
collectedCookies = null;
requestHeaders.clear();
responseHeaders.clear();
persistentData.clear();
if (derivatives != null) {
derivatives.clear();
derivatives = null;
}
}
}
/** @return the portion of the body read so far, if any */
public CharSequence getStoredRequestBody() {
StoredBodySupplier storedRequestBodySupplier = this.storedRequestBodySupplier;
if (storedRequestBodySupplier == null) {
return null;
}
return storedRequestBodySupplier.get();
}
public void reportEvents(Collection<AppSecEvent> appSecEvents) {
for (AppSecEvent event : appSecEvents) {
StandardizedLogging.attackDetected(log, event);
}
if (this.appSecEvents == null) {
synchronized (this) {
if (this.appSecEvents == null) {
this.appSecEvents = new ConcurrentLinkedQueue<>();
}
}
}
this.appSecEvents.addAll(appSecEvents);
}
public void reportStackTrace(StackTraceEvent stackTraceEvent) {
if (this.stackTraceEvents == null) {
synchronized (this) {
if (this.stackTraceEvents == null) {
this.stackTraceEvents = new ConcurrentLinkedQueue<>();
}
}
}
if (stackTraceEvents.size() <= Config.get().getAppSecMaxStackTraces()) {
this.stackTraceEvents.add(stackTraceEvent);
}
}
Collection<AppSecEvent> transferCollectedEvents() {
if (this.appSecEvents == null) {
return Collections.emptyList();
}
Collection<AppSecEvent> events = new ArrayList<>();
AppSecEvent item;
while ((item = this.appSecEvents.poll()) != null) {
events.add(item);
}
return events;
}
List<StackTraceEvent> getStackTraces() {
if (this.stackTraceEvents == null) {
return null;
}
List<StackTraceEvent> stackTraces = new ArrayList<>();
StackTraceEvent item;
while ((item = this.stackTraceEvents.poll()) != null) {
stackTraces.add(item);
}
return stackTraces;
}
public void reportDerivatives(Map<String, String> data) {
log.debug("Reporting derivatives: {}", data);
if (data == null || data.isEmpty()) return;
if (derivatives == null) {
derivatives = data;
} else {
derivatives.putAll(data);
}
}
public boolean commitDerivatives(TraceSegment traceSegment) {
log.debug("Committing derivatives: {} for {}", derivatives, traceSegment);
if (traceSegment == null || derivatives == null) {
return false;
}
derivatives.forEach(traceSegment::setTagTop);
log.debug("Committed derivatives: {} for {}", derivatives, traceSegment);
derivatives = null;
return true;
}
// Mainly used for testing and logging
Set<String> getDerivativeKeys() {
return derivatives == null ? emptySet() : new HashSet<>(derivatives.keySet());
}
public boolean isThrottled(RateLimiter rateLimiter) {
if (rateLimiter != null && rateLimited.compareAndSet(false, true)) {
throttled = rateLimiter.isThrottled();
}
return throttled;
}
public boolean isWafContextClosed() {
return wafContextClosed;
}
/** Must be called during request end event processing. */
void setRequestEndCalled() {
requestEndCalled = true;
}
public void setProcessedRequestBody(Object processedRequestBody) {
this.processedRequestBody = processedRequestBody;
}
public Object getProcessedRequestBody() {
return processedRequestBody;
}
public boolean isRaspMatched() {
return raspMatched;
}
public void setRaspMatched(boolean raspMatched) {
this.raspMatched = raspMatched;
}
}