Skip to content

Update metrics: appsec.waf.requests #8353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 27, 2025
2 changes: 1 addition & 1 deletion .circleci/config.continue.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ instrumentation_modules: &instrumentation_modules "dd-java-agent/instrumentation
debugger_modules: &debugger_modules "dd-java-agent/agent-debugger|dd-java-agent/agent-bootstrap|dd-java-agent/agent-builder|internal-api|communication|dd-trace-core"
profiling_modules: &profiling_modules "dd-java-agent/agent-profiling"

default_system_tests_commit: &default_system_tests_commit 315bc8a32cc888834726397f088336ba8038277f
default_system_tests_commit: &default_system_tests_commit d4974a9d88a10a70a8688afd08697affb5e82261

parameters:
nightly:
Expand Down
4 changes: 2 additions & 2 deletions dd-java-agent/appsec/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies {
implementation project(':internal-api')
implementation project(':communication')
implementation project(':telemetry')
implementation group: 'io.sqreen', name: 'libsqreen', version: '11.2.0'
implementation group: 'io.sqreen', name: 'libsqreen', version: '12.0.0'
implementation libs.moshi

testImplementation libs.bytebuddy
Expand Down Expand Up @@ -68,6 +68,7 @@ ext {
excludedClassesCoverage = [
'com.datadog.appsec.config.MergedAsmData.InvalidAsmDataException',
'com.datadog.appsec.powerwaf.LibSqreenInitialization',
'com.datadog.appsec.powerwaf.PowerWAFModule.PowerWAFDataCallback',
'com.datadog.appsec.report.*',
'com.datadog.appsec.config.AppSecConfigServiceImpl.SubscribeFleetServiceRunnable.1',
'com.datadog.appsec.util.StandardizedLogging',
Expand All @@ -87,7 +88,6 @@ ext {
'com.datadog.appsec.config.CurrentAppSecConfig',
// equals() / hashCode() are not well covered
'com.datadog.appsec.config.AppSecConfig.Helper',
'com.datadog.appsec.powerwaf.PowerWAFModule.PowerWAFDataCallback',
'com.datadog.appsec.powerwaf.PowerWAFModule.PowerWAFEventsCallback',
// assert never fails
'com.datadog.appsec.util.StandardizedLogging',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import datadog.trace.api.gateway.Flow;
import datadog.trace.api.telemetry.LogCollector;
import datadog.trace.api.telemetry.WafMetricCollector;
import datadog.trace.api.telemetry.WafTruncatedType;
import datadog.trace.api.time.SystemTimeSource;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
Expand Down Expand Up @@ -445,6 +446,7 @@ public void onDataAvailable(
if (!reqCtx.isAdditiveClosed()) {
log.error("Error calling WAF", e);
}
WafMetricCollector.get().wafRequestError();
return;
} catch (AbstractPowerwafException e) {
if (gwCtx.isRasp) {
Expand All @@ -460,6 +462,27 @@ public void onDataAvailable(
long elapsed = System.currentTimeMillis() - start;
StandardizedLogging.finishedExecutionWAF(log, elapsed);
}
if (!gwCtx.isRasp) {
PowerwafMetrics wafMetrics = reqCtx.getWafMetrics();
if (wafMetrics != null) {
final long stringTooLong = wafMetrics.getTruncatedStringTooLongCount();
final long listMapTooLarge = wafMetrics.getTruncatedListMapTooLargeCount();
final long objectTooDeep = wafMetrics.getTruncatedObjectTooDeepCount();

if (stringTooLong > 0) {
WafMetricCollector.get()
.wafInputTruncated(WafTruncatedType.STRING_TOO_LONG, stringTooLong);
}
if (listMapTooLarge > 0) {
WafMetricCollector.get()
.wafInputTruncated(WafTruncatedType.LIST_MAP_TOO_LARGE, listMapTooLarge);
}
if (objectTooDeep > 0) {
WafMetricCollector.get()
.wafInputTruncated(WafTruncatedType.OBJECT_TOO_DEEP, objectTooDeep);
}
}
}
}

StandardizedLogging.inAppWafReturn(log, resultWithData);
Expand Down Expand Up @@ -495,29 +518,35 @@ public void onDataAvailable(
}
} else {
log.info("Ignoring action with type {}", actionInfo.type);
WafMetricCollector.get().wafRequestBlockFailure();
}
}
Collection<AppSecEvent> events = buildEvents(resultWithData);

if (!events.isEmpty() && !reqCtx.isThrottled(rateLimiter)) {
AgentSpan activeSpan = AgentTracer.get().activeSpan();
if (activeSpan != null) {
log.debug("Setting force-keep tag on the current span");
// Keep event related span, because it could be ignored in case of
// reduced datadog sampling rate.
activeSpan.getLocalRootSpan().setTag(Tags.ASM_KEEP, true);
// If APM is disabled, inform downstream services that the current
// distributed trace contains at least one ASM event and must inherit
// the given force-keep priority
activeSpan
.getLocalRootSpan()
.setTag(Tags.PROPAGATED_TRACE_SOURCE, ProductTraceSource.ASM);
if (!events.isEmpty()) {
if (!reqCtx.isThrottled(rateLimiter)) {
AgentSpan activeSpan = AgentTracer.get().activeSpan();
if (activeSpan != null) {
log.debug("Setting force-keep tag on the current span");
// Keep event related span, because it could be ignored in case of
// reduced datadog sampling rate.
activeSpan.getLocalRootSpan().setTag(Tags.ASM_KEEP, true);
// If APM is disabled, inform downstream services that the current
// distributed trace contains at least one ASM event and must inherit
// the given force-keep priority
activeSpan
.getLocalRootSpan()
.setTag(Tags.PROPAGATED_TRACE_SOURCE, ProductTraceSource.ASM);
} else {
// If active span is not available the ASM_KEEP tag will be set in the GatewayBridge
// when the request ends
log.debug("There is no active span available");
}
reqCtx.reportEvents(events);
} else {
// If active span is not available the ASK_KEEP tag will be set in the GatewayBridge
// when the request ends
log.debug("There is no active span available");
log.debug("Rate limited WAF events");
WafMetricCollector.get().wafRequestRateLimited();
}
reqCtx.reportEvents(events);
}

if (flow.isBlocking()) {
Expand Down Expand Up @@ -551,6 +580,7 @@ private Flow.Action.RequestBlockingAction createBlockRequestAction(ActionInfo ac
return new Flow.Action.RequestBlockingAction(statusCode, blockingContentType);
} catch (RuntimeException cce) {
log.warn("Invalid blocking action data", cce);
WafMetricCollector.get().wafRequestBlockFailure();
return null;
}
}
Expand All @@ -576,6 +606,7 @@ private Flow.Action.RequestBlockingAction createRedirectRequestAction(ActionInfo
return Flow.Action.RequestBlockingAction.forRedirect(statusCode, location);
} catch (RuntimeException cce) {
log.warn("Invalid blocking action data", cce);
WafMetricCollector.get().wafRequestBlockFailure();
return null;
}
}
Expand Down
Loading
Loading