Skip to content

Commit e706341

Browse files
committed
wip
1 parent 53add4c commit e706341

File tree

4 files changed

+23
-113
lines changed

4 files changed

+23
-113
lines changed

dd-java-agent/appsec/src/main/java/com/datadog/appsec/ddwaf/WAFModule.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ private boolean initOrUpdateWafHandle(
251251
config.dirtyStatus.markAllDirty();
252252
}
253253
try {
254-
wafHandle = wafBuilder.buildWafHandleInstance(wafHandle);
254+
wafHandle = wafBuilder.buildWafHandleInstance(null);
255255
} catch (AbstractWafException e) {
256256
log.info("Could not initialize waf handle, no rules were added!", e);
257257
throw new AppSecModuleActivationException(
@@ -329,14 +329,6 @@ public void onDataAvailable(
329329
DataBundle newData,
330330
GatewayContext gwCtx) {
331331
Waf.ResultWithData resultWithData;
332-
if (reqCtx.isWafContextClosed()) {
333-
log.debug("Skipped; the WAF context is closed");
334-
if (gwCtx.isRasp) {
335-
WafMetricCollector.get().raspRuleSkipped(gwCtx.raspRuleType);
336-
}
337-
return;
338-
}
339-
340332
StandardizedLogging.executingWAF(log);
341333
long start = 0L;
342334
if (log.isDebugEnabled()) {
@@ -537,7 +529,6 @@ private StackTraceEvent createExploitStackTraceEvent(String stackId) {
537529
private Waf.ResultWithData doRunWaf(
538530
AppSecRequestContext reqCtx, DataBundle newData, GatewayContext gwCtx)
539531
throws AbstractWafException {
540-
WafContext wafContext = reqCtx.getOrCreateWafContext(wafHandle, gwCtx.isRasp);
541532
WafMetrics metrics;
542533
if (gwCtx.isRasp) {
543534
metrics = reqCtx.getRaspMetrics();
@@ -547,9 +538,11 @@ private Waf.ResultWithData doRunWaf(
547538
}
548539

549540
if (gwCtx.isTransient) {
550-
return runWafTransient(wafContext, metrics, newData);
541+
return runWafTransient(
542+
reqCtx.getOrCreateWafContext(wafHandle, gwCtx.isRasp), metrics, newData);
551543
} else {
552-
return runWafContext(wafContext, metrics, newData);
544+
return runWafContext(
545+
reqCtx.getOrCreateWafContext(wafHandle, gwCtx.isRasp), metrics, newData);
553546
}
554547
}
555548

dd-java-agent/appsec/src/main/java/com/datadog/appsec/gateway/AppSecRequestContext.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ public class AppSecRequestContext implements DataBundle, Closeable {
114114

115115
// should be guarded by this
116116
private volatile WafContext wafContext;
117-
private volatile boolean wafContextClosed;
118117
// set after wafContext is set
119118
private volatile WafMetrics wafMetrics;
120119
private volatile WafMetrics raspMetrics;
@@ -248,22 +247,24 @@ public WafContext getOrCreateWafContext(WafHandle wafHandle, boolean isRasp) {
248247
}
249248

250249
WafContext curWafContext;
251-
synchronized (this) {
252-
curWafContext = new WafContext(wafHandle);
253-
if (this.wafContext != null && !wafContextClosed) {
254-
this.wafContext.close();
250+
if (!isRasp || wafContext == null) {
251+
synchronized (this) {
252+
curWafContext = new WafContext(wafHandle);
253+
if (this.wafContext != null && this.wafContext.isOnline()) {
254+
this.wafContext.close();
255+
}
256+
this.wafContext = curWafContext;
257+
return curWafContext;
255258
}
256-
this.wafContext = curWafContext;
257-
return curWafContext;
258259
}
260+
return wafContext;
259261
}
260262

261263
public void closeWafContext() {
262-
if (wafContext != null) {
264+
if (wafContext != null && wafContext.isOnline()) {
263265
synchronized (this) {
264266
if (wafContext != null) {
265267
try {
266-
wafContextClosed = true;
267268
wafContext.close();
268269
} finally {
269270
wafContext = null;
@@ -554,7 +555,7 @@ public void close() {
554555
// flag needs to be
555556
// later reset by the API Security post-processor and close must be called again.
556557
if (!keepOpenForApiSecurityPostProcessing) {
557-
if (wafContext != null) {
558+
if (wafContext != null && wafContext.isOnline()) {
558559
log.debug(
559560
SEND_TELEMETRY, "WAF object had not been closed (probably missed request-end event)");
560561
closeWafContext();
@@ -667,7 +668,7 @@ public boolean isThrottled(RateLimiter rateLimiter) {
667668
}
668669

669670
public boolean isWafContextClosed() {
670-
return wafContextClosed;
671+
return wafContext == null || !wafContext.isOnline();
671672
}
672673

673674
/** Must be called during request end event processing. */

0 commit comments

Comments
 (0)