Skip to content

Commit 2963701

Browse files
committed
WIP
1 parent da78ebc commit 2963701

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

dd-java-agent/appsec/src/test/groovy/com/datadog/appsec/ddwaf/WAFModuleSpecification.groovy

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.datadog.appsec.ddwaf
22

3+
import com.datadog.ddwaf.WafErrorCode as LibWafErrorCode
4+
import datadog.trace.api.telemetry.WafMetricCollector.WafErrorCode as InternalWafErrorCode
5+
36
import com.datadog.appsec.AppSecModule
47
import com.datadog.appsec.config.AppSecConfig
58
import com.datadog.appsec.config.AppSecData
@@ -17,7 +20,6 @@ import com.datadog.appsec.event.data.MapDataBundle
1720
import com.datadog.appsec.gateway.AppSecRequestContext
1821
import com.datadog.appsec.gateway.GatewayContext
1922
import com.datadog.appsec.report.AppSecEvent
20-
import com.datadog.ddwaf.WafErrorCode
2123
import com.datadog.ddwaf.exception.AbstractWafException
2224
import com.datadog.ddwaf.exception.InternalWafException
2325
import com.datadog.ddwaf.exception.InvalidArgumentWafException
@@ -1790,7 +1792,7 @@ class WAFModuleSpecification extends DDSpecification {
17901792
0 * _
17911793

17921794
where:
1793-
wafErrorCode << WafErrorCode.values()
1795+
wafErrorCode << LibWafErrorCode.values()
17941796
}
17951797

17961798
void 'test wafErrorCode metric is increased when waf call throws #wafErrorCode '() {
@@ -1819,21 +1821,30 @@ class WAFModuleSpecification extends DDSpecification {
18191821
0 * _
18201822

18211823
where:
1822-
wafErrorCode << WafErrorCode.values()
1824+
wafErrorCode << LibWafErrorCode.values()
1825+
}
1826+
1827+
def 'internal-api WafErrorCode enum matches libddwaf-java by name and code'() {
1828+
given:
1829+
def libddwaf = com.datadog.ddwaf.WafErrorCode.values().collectEntries { [it.name(), it.code] }
1830+
def internal = datadog.trace.api.telemetry.WafMetricCollector.WafErrorCode.values().collectEntries { [it.name(), it.code] }
1831+
1832+
expect:
1833+
internal == libddwaf
18231834
}
18241835

18251836
/**
18261837
* Helper to return a concrete Waf exception for each WafErrorCode
18271838
*/
1828-
static AbstractWafException createWafException(WafErrorCode code) {
1839+
static AbstractWafException createWafException(LibWafErrorCode code) {
18291840
switch (code) {
1830-
case WafErrorCode.INVALID_ARGUMENT:
1841+
case LibWafErrorCode.INVALID_ARGUMENT:
18311842
return new InvalidArgumentWafException(code.code)
1832-
case WafErrorCode.INVALID_OBJECT:
1843+
case LibWafErrorCode.INVALID_OBJECT:
18331844
return new InvalidObjectWafException(code.code)
1834-
case WafErrorCode.INTERNAL_ERROR:
1845+
case LibWafErrorCode.INTERNAL_ERROR:
18351846
return new InternalWafException(code.code)
1836-
case WafErrorCode.BINDING_ERROR:
1847+
case LibWafErrorCode.BINDING_ERROR:
18371848
return new UnclassifiedWafException(code.code)
18381849
default:
18391850
throw new IllegalStateException("Unhandled WafErrorCode: $code")

internal-api/src/main/java/datadog/trace/api/telemetry/WafMetricCollector.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,19 @@ public final void increment() {
610610
}
611611
}
612612

613-
enum WafErrorCode {
613+
/**
614+
* Mirror of the {@code WafErrorCode} enum defined in the {@code libddwaf-java} module.
615+
*
616+
* <p>This enum is duplicated here to avoid adding a dependency on the native bindings module
617+
* (`libddwaf-java`) within the {@code internal-api} module.
618+
*
619+
* <p>IMPORTANT: If the {@code WafErrorCode} definition in {@code libddwaf-java} is updated, this
620+
* enum must be kept in sync manually to ensure correct behavior and compatibility.
621+
*
622+
* <p>Each enum value represents a specific WAF error condition, typically returned when running a
623+
* WAF rule evaluation.
624+
*/
625+
public enum WafErrorCode {
614626
INVALID_ARGUMENT(-1),
615627
INVALID_OBJECT(-2),
616628
INTERNAL_ERROR(-3),

0 commit comments

Comments
 (0)