Skip to content

Commit 0a46acc

Browse files
committed
WIP add tests
1 parent 408c87d commit 0a46acc

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,9 @@ public void onDataAvailable(
448448
if (!reqCtx.isWafContextClosed()) {
449449
log.error("Error calling WAF", e);
450450
}
451+
// TODO this is wrong and will be fixed in another PR
451452
WafMetricCollector.get().wafRequestError();
452-
// TODO: replace -127 for e.code when UnclassifiedPowerwafException code is fixed
453-
incrementErrorCodeMetric(gwCtx, -127);
453+
incrementErrorCodeMetric(gwCtx, e.code);
454454
return;
455455
} catch (AbstractWafException e) {
456456
incrementErrorCodeMetric(gwCtx, e.code);

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import com.datadog.appsec.event.data.MapDataBundle
1717
import com.datadog.appsec.gateway.AppSecRequestContext
1818
import com.datadog.appsec.gateway.GatewayContext
1919
import com.datadog.appsec.report.AppSecEvent
20+
import com.datadog.ddwaf.WafErrorCode
21+
import com.datadog.ddwaf.exception.AbstractWafException
22+
import com.datadog.ddwaf.exception.InternalWafException
23+
import com.datadog.ddwaf.exception.InvalidArgumentWafException
24+
import com.datadog.ddwaf.exception.InvalidObjectWafException
25+
import com.datadog.ddwaf.exception.UnclassifiedWafException
2026
import datadog.trace.api.telemetry.RuleType
2127
import datadog.trace.util.stacktrace.StackTraceEvent
2228
import com.datadog.appsec.test.StubAppSecConfigService
@@ -1755,6 +1761,85 @@ class WAFModuleSpecification extends DDSpecification {
17551761
0 * _
17561762
}
17571763

1764+
void 'test raspErrorCode metric is increased when waf call throws #wafErrorCode '() {
1765+
setup:
1766+
ChangeableFlow flow = Mock()
1767+
GatewayContext gwCtxMock = new GatewayContext(false, RuleType.SQL_INJECTION)
1768+
WafContext wafContext = Mock()
1769+
1770+
when:
1771+
setupWithStubConfigService('rules_with_data_config.json')
1772+
dataListener = wafModule.dataSubscriptions.first()
1773+
1774+
def bundle = MapDataBundle.of(
1775+
KnownAddresses.USER_ID,
1776+
'legit-user'
1777+
)
1778+
dataListener.onDataAvailable(flow, ctx, bundle, gwCtxMock)
1779+
1780+
then:
1781+
(1..2) * ctx.isWafContextClosed() >> false // if UnclassifiedWafException it's called twice
1782+
1 * ctx.getOrCreateWafContext(_, true, true) >> wafContext
1783+
1 * wafMetricCollector.raspRuleEval(RuleType.SQL_INJECTION)
1784+
1 * wafContext.run(_, _, _) >> { throw createWafException(wafErrorCode) }
1785+
1 * wafMetricCollector.wafInit(Waf.LIB_VERSION, _, true)
1786+
1 * ctx.getRaspMetrics()
1787+
1 * ctx.getRaspMetricsCounter()
1788+
(0..1) * WafMetricCollector.get().wafRequestError() // TODO: remove this line when WAFModule is removed
1789+
1 * wafMetricCollector.raspErrorCode(RuleType.SQL_INJECTION, wafErrorCode.code)
1790+
0 * _
1791+
1792+
where:
1793+
wafErrorCode << WafErrorCode.values()
1794+
}
1795+
1796+
void 'test wafErrorCode metric is increased when waf call throws #wafErrorCode '() {
1797+
setup:
1798+
ChangeableFlow flow = Mock()
1799+
WafContext wafContext = Mock()
1800+
1801+
when:
1802+
setupWithStubConfigService('rules_with_data_config.json')
1803+
dataListener = wafModule.dataSubscriptions.first()
1804+
1805+
def bundle = MapDataBundle.of(
1806+
KnownAddresses.USER_ID,
1807+
'legit-user'
1808+
)
1809+
dataListener.onDataAvailable(flow, ctx, bundle, gwCtx)
1810+
1811+
then:
1812+
(1..2) * ctx.isWafContextClosed() >> false // if UnclassifiedWafException it's called twice
1813+
1 * ctx.getOrCreateWafContext(_, true, false) >> wafContext
1814+
1 * wafContext.run(_, _, _) >> { throw createWafException(wafErrorCode) }
1815+
1 * wafMetricCollector.wafInit(Waf.LIB_VERSION, _, true)
1816+
2 * ctx.getWafMetrics()
1817+
(0..1) * WafMetricCollector.get().wafRequestError() // TODO: remove this line when WAFModule is removed
1818+
1 * wafMetricCollector.wafErrorCode(wafErrorCode.code)
1819+
0 * _
1820+
1821+
where:
1822+
wafErrorCode << WafErrorCode.values()
1823+
}
1824+
1825+
/**
1826+
* Helper to return a concrete Waf exception for each WafErrorCode
1827+
*/
1828+
static AbstractWafException createWafException(WafErrorCode code) {
1829+
switch (code) {
1830+
case WafErrorCode.INVALID_ARGUMENT:
1831+
return new InvalidArgumentWafException(code.code)
1832+
case WafErrorCode.INVALID_OBJECT:
1833+
return new InvalidObjectWafException(code.code)
1834+
case WafErrorCode.INTERNAL_ERROR:
1835+
return new InternalWafException(code.code)
1836+
case WafErrorCode.BINDING_ERROR:
1837+
return new UnclassifiedWafException(code.code)
1838+
default:
1839+
throw new IllegalStateException("Unhandled WafErrorCode: $code")
1840+
}
1841+
}
1842+
17581843
private Map<String, Object> getDefaultConfig() {
17591844
def service = new StubAppSecConfigService()
17601845
service.init()

0 commit comments

Comments
 (0)