Skip to content

Commit 07df282

Browse files
authored
Merge branch 'master' into shatzi/fix-code-origin-tags
2 parents 3f73dee + 043adb2 commit 07df282

File tree

14 files changed

+296
-40
lines changed

14 files changed

+296
-40
lines changed

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/decorator/TestDecoratorImpl.java

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class TestDecoratorImpl implements TestDecorator {
1515
private final String component;
1616
private final String sessionName;
1717
private final Map<String, String> ciTags;
18+
private final int cpuCount;
1819

1920
public TestDecoratorImpl(
2021
String component, String sessionName, String testCommand, Map<String, String> ciTags) {
@@ -27,6 +28,7 @@ public TestDecoratorImpl(
2728
this.sessionName =
2829
Strings.isNotBlank(ciJobName) ? ciJobName + "-" + testCommand : testCommand;
2930
}
31+
cpuCount = Runtime.getRuntime().availableProcessors();
3032
}
3133

3234
protected String testType() {
@@ -46,6 +48,7 @@ public CharSequence component() {
4648
public AgentSpan afterStart(final AgentSpan span) {
4749
span.setSamplingPriority(PrioritySampling.SAMPLER_KEEP);
4850
span.setTag(DDTags.ORIGIN_KEY, CIAPP_TEST_ORIGIN);
51+
span.setTag(DDTags.HOST_VCPU_COUNT, cpuCount);
4952
span.setTag(Tags.TEST_TYPE, testType());
5053
span.setTag(Tags.COMPONENT, component());
5154
span.setTag(Tags.TEST_SESSION_NAME, sessionName);

dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/decorator/TestDecoratorImplTest.groovy

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class TestDecoratorImplTest extends Specification {
2323
1 * span.setTag(Tags.TEST_TYPE, decorator.testType())
2424
1 * span.setSamplingPriority(PrioritySampling.SAMPLER_KEEP)
2525
1 * span.setTag(DDTags.ORIGIN_KEY, decorator.origin())
26+
1 * span.setTag(DDTags.HOST_VCPU_COUNT, Runtime.runtime.availableProcessors())
2627
1 * span.setTag("ci-tag-1", "value")
2728
1 * span.setTag("ci-tag-2", "another value")
2829

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/agent/DebuggerAgent.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
/** Debugger agent implementation */
4949
public class DebuggerAgent {
5050
private static final Logger LOGGER = LoggerFactory.getLogger(DebuggerAgent.class);
51-
public static final Duration EXCEPTION_CAPTURE_INTERVAL = Duration.ofHours(1);
5251
private static ConfigurationPoller configurationPoller;
5352
private static DebuggerSink sink;
5453
private static String agentVersion;
@@ -95,7 +94,7 @@ public static synchronized void run(
9594
new DefaultExceptionDebugger(
9695
configurationUpdater,
9796
classNameFiltering,
98-
EXCEPTION_CAPTURE_INTERVAL,
97+
Duration.ofSeconds(config.getDebuggerExceptionCaptureInterval()),
9998
config.getDebuggerMaxExceptionPerSecond());
10099
DebuggerContext.initExceptionDebugger(defaultExceptionDebugger);
101100
}

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/LogProbe.java

+20-10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.List;
4242
import java.util.Objects;
4343
import java.util.regex.Pattern;
44+
import java.util.function.Consumer;
4445
import org.slf4j.Logger;
4546
import org.slf4j.LoggerFactory;
4647

@@ -575,24 +576,33 @@ protected boolean fillSnapshot(
575576
shouldCommit = true;
576577
}
577578
if (entryStatus.shouldReportError()) {
578-
if (entryContext.getCapturedThrowable() != null) {
579-
// report also uncaught exception
580-
snapshot.setEntry(entryContext);
581-
}
582-
snapshot.addEvaluationErrors(entryStatus.getErrors());
579+
populateErrors(entryContext, snapshot, entryStatus, snapshot::setEntry);
583580
shouldCommit = true;
584581
}
585582
if (exitStatus.shouldReportError()) {
586-
if (exitContext.getCapturedThrowable() != null) {
587-
// report also uncaught exception
588-
snapshot.setExit(exitContext);
589-
}
590-
snapshot.addEvaluationErrors(exitStatus.getErrors());
583+
populateErrors(exitContext, snapshot, exitStatus, snapshot::setExit);
591584
shouldCommit = true;
592585
}
593586
return shouldCommit;
594587
}
595588

589+
private static void populateErrors(
590+
CapturedContext context,
591+
Snapshot snapshot,
592+
LogStatus status,
593+
Consumer<CapturedContext> contextSetter) {
594+
if (context.getCapturedThrowable() != null) {
595+
// report also uncaught exception
596+
contextSetter.accept(context);
597+
}
598+
snapshot.addEvaluationErrors(status.getErrors());
599+
if (status.getMessage() != null) {
600+
snapshot.setMessage(status.getMessage());
601+
} else if (!status.getErrors().isEmpty()) {
602+
snapshot.setMessage(status.getErrors().get(0).getMessage());
603+
}
604+
}
605+
596606
private LogStatus convertStatus(CapturedContext.Status status) {
597607
if (status == CapturedContext.Status.EMPTY_STATUS) {
598608
return LogStatus.EMPTY_LOG_STATUS;

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/CapturedSnapshotTest.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1147,11 +1147,12 @@ public void nullCondition() throws IOException, URISyntaxException {
11471147
TestSnapshotListener listener = installProbes(CLASS_NAME, logProbes);
11481148
Class<?> testClass = compileAndLoadClass(CLASS_NAME);
11491149
int result = Reflect.onClass(testClass).call("main", "1").get();
1150-
assertEquals(1, listener.snapshots.size());
1151-
List<EvaluationError> evaluationErrors = listener.snapshots.get(0).getEvaluationErrors();
1152-
Assertions.assertEquals(1, evaluationErrors.size());
1153-
Assertions.assertEquals("nullTyped.fld.fld", evaluationErrors.get(0).getExpr());
1154-
Assertions.assertEquals("Cannot dereference field: fld", evaluationErrors.get(0).getMessage());
1150+
Snapshot snapshot = assertOneSnapshot(listener);
1151+
List<EvaluationError> evaluationErrors = snapshot.getEvaluationErrors();
1152+
assertEquals(1, evaluationErrors.size());
1153+
assertEquals("nullTyped.fld.fld", evaluationErrors.get(0).getExpr());
1154+
assertEquals("Cannot dereference field: fld", evaluationErrors.get(0).getMessage());
1155+
assertEquals("Cannot dereference field: fld", snapshot.getMessage());
11551156
}
11561157

11571158
@Test

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/probe/LogProbeTest.java

+27-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.datadog.debugger.sink.Snapshot;
99
import datadog.trace.bootstrap.debugger.CapturedContext;
10+
import datadog.trace.bootstrap.debugger.EvaluationError;
1011
import datadog.trace.bootstrap.debugger.MethodLocation;
1112
import datadog.trace.bootstrap.debugger.ProbeId;
1213
import java.util.stream.Stream;
@@ -134,12 +135,35 @@ private static Stream<Arguments> statusValues() {
134135
public void fillSnapshot_shouldSend_exit() {
135136
LogProbe logProbe = createLog(null).evaluateAt(MethodLocation.EXIT).build();
136137
CapturedContext entryContext = new CapturedContext();
137-
entryContext.evaluate(PROBE_ID.getEncodedId(), logProbe, "", 0, MethodLocation.ENTRY);
138-
entryContext.getStatus(PROBE_ID.getEncodedId());
138+
prepareContext(entryContext, logProbe, MethodLocation.ENTRY);
139+
CapturedContext exitContext = new CapturedContext();
140+
prepareContext(exitContext, logProbe, MethodLocation.EXIT);
141+
Snapshot snapshot = new Snapshot(Thread.currentThread(), logProbe, 10);
142+
assertTrue(logProbe.fillSnapshot(entryContext, exitContext, null, snapshot));
143+
}
144+
145+
@Test
146+
public void fillSnapshot_shouldSend_evalErrors() {
147+
LogProbe logProbe = createLog(null).evaluateAt(MethodLocation.EXIT).build();
148+
CapturedContext entryContext = new CapturedContext();
149+
LogProbe.LogStatus logStatus = prepareContext(entryContext, logProbe, MethodLocation.ENTRY);
150+
logStatus.addError(new EvaluationError("expr", "msg1"));
151+
logStatus.setLogTemplateErrors(true);
152+
entryContext.addThrowable(new RuntimeException("errorEntry"));
139153
CapturedContext exitContext = new CapturedContext();
140-
exitContext.evaluate(PROBE_ID.getEncodedId(), logProbe, "", 0, MethodLocation.EXIT);
154+
logStatus = prepareContext(exitContext, logProbe, MethodLocation.EXIT);
155+
logStatus.addError(new EvaluationError("expr", "msg2"));
156+
logStatus.setLogTemplateErrors(true);
157+
exitContext.addThrowable(new RuntimeException("errorExit"));
141158
Snapshot snapshot = new Snapshot(Thread.currentThread(), logProbe, 10);
142159
assertTrue(logProbe.fillSnapshot(entryContext, exitContext, null, snapshot));
160+
assertEquals(2, snapshot.getEvaluationErrors().size());
161+
assertEquals("msg1", snapshot.getEvaluationErrors().get(0).getMessage());
162+
assertEquals("msg2", snapshot.getEvaluationErrors().get(1).getMessage());
163+
assertEquals(
164+
"errorEntry", snapshot.getCaptures().getEntry().getCapturedThrowable().getMessage());
165+
assertEquals(
166+
"errorExit", snapshot.getCaptures().getReturn().getCapturedThrowable().getMessage());
143167
}
144168

145169
private LogProbe.Builder createLog(String template) {

dd-java-agent/appsec/src/main/java/com/datadog/appsec/powerwaf/PowerWAFModule.java

-16
Original file line numberDiff line numberDiff line change
@@ -389,22 +389,6 @@ private static Collection<Address<?>> getUsedAddresses(PowerwafContext ctx) {
389389
addressList.add(address);
390390
}
391391
}
392-
393-
// TODO: get addresses dynamically when will it be implemented in waf
394-
addressList.add(KnownAddresses.WAF_CONTEXT_PROCESSOR);
395-
addressList.add(KnownAddresses.HEADERS_NO_COOKIES);
396-
addressList.add(KnownAddresses.REQUEST_QUERY);
397-
addressList.add(KnownAddresses.REQUEST_PATH_PARAMS);
398-
addressList.add(KnownAddresses.REQUEST_COOKIES);
399-
addressList.add(KnownAddresses.REQUEST_BODY_RAW);
400-
addressList.add(KnownAddresses.RESPONSE_HEADERS_NO_COOKIES);
401-
addressList.add(KnownAddresses.RESPONSE_BODY_OBJECT);
402-
addressList.add(KnownAddresses.GRAPHQL_SERVER_ALL_RESOLVERS);
403-
addressList.add(KnownAddresses.DB_TYPE);
404-
addressList.add(KnownAddresses.DB_SQL_QUERY);
405-
addressList.add(KnownAddresses.IO_NET_URL);
406-
addressList.add(KnownAddresses.IO_FS_FILE);
407-
408392
return addressList;
409393
}
410394

dd-java-agent/appsec/src/test/groovy/com/datadog/appsec/powerwaf/PowerWAFModuleSpecification.groovy

+16
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,22 @@ class PowerWAFModuleSpecification extends DDSpecification {
15391539
})
15401540
}
15411541

1542+
void 'retrieve used addresses'() {
1543+
when:
1544+
setupWithStubConfigService('small_config.json')
1545+
def ctx0 = pwafModule.ctxAndAddresses.get().ctx
1546+
def addresses = pwafModule.getUsedAddresses(ctx0)
1547+
1548+
then:
1549+
addresses.size() == 6
1550+
addresses.contains(KnownAddresses.REQUEST_INFERRED_CLIENT_IP)
1551+
addresses.contains(KnownAddresses.REQUEST_QUERY)
1552+
addresses.contains(KnownAddresses.REQUEST_PATH_PARAMS)
1553+
addresses.contains(KnownAddresses.HEADERS_NO_COOKIES)
1554+
addresses.contains(KnownAddresses.REQUEST_URI_RAW)
1555+
addresses.contains(KnownAddresses.REQUEST_BODY_OBJECT)
1556+
}
1557+
15421558
private Map<String, Object> getDefaultConfig() {
15431559
def service = new StubAppSecConfigService()
15441560
service.init()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
{
2+
"version": "2.1",
3+
"metadata": {
4+
"rules_version": "0.42.0"
5+
},
6+
"actions": [
7+
{
8+
"id": "block",
9+
"type": "block_request",
10+
"parameters": {
11+
"status_code": 418,
12+
"type": "html"
13+
}
14+
}
15+
],
16+
"rules": [
17+
{
18+
"id": "ip_match_rule",
19+
"name": "rule1",
20+
"tags": {
21+
"type": "flow1",
22+
"category": "category1"
23+
},
24+
"conditions": [
25+
{
26+
"operator": "ip_match",
27+
"parameters": {
28+
"inputs": [
29+
{
30+
"address": "http.client_ip"
31+
}
32+
],
33+
"data": "ip_data"
34+
}
35+
}
36+
],
37+
"on_match": ["block"]
38+
},
39+
{
40+
"id": "crs-913-110",
41+
"name": "Found request header associated with Acunetix security scanner",
42+
"tags": {
43+
"type": "security_scanner",
44+
"crs_id": "913110",
45+
"category": "attack_attempt"
46+
},
47+
"conditions": [
48+
{
49+
"parameters": {
50+
"inputs": [
51+
{
52+
"address": "server.request.headers.no_cookies"
53+
}
54+
],
55+
"list": [
56+
"acunetix-product",
57+
"(acunetix web vulnerability scanner",
58+
"acunetix-scanning-agreement",
59+
"acunetix-user-agreement"
60+
]
61+
},
62+
"operator": "phrase_match"
63+
}
64+
],
65+
"transformers": [
66+
"lowercase"
67+
]
68+
},
69+
{
70+
"id": "crs-913-120",
71+
"name": "Found request filename/argument associated with security scanner",
72+
"tags": {
73+
"type": "security_scanner",
74+
"crs_id": "913120",
75+
"category": "attack_attempt"
76+
},
77+
"conditions": [
78+
{
79+
"parameters": {
80+
"inputs": [
81+
{
82+
"address": "server.request.query"
83+
},
84+
{
85+
"address": "server.request.body"
86+
},
87+
{
88+
"address": "server.request.path_params"
89+
}
90+
],
91+
"list": [
92+
"/.adsensepostnottherenonobook",
93+
"/<invalid>hello.html",
94+
"/actsensepostnottherenonotive",
95+
"/acunetix-wvs-test-for-some-inexistent-file",
96+
"/antidisestablishmentarianism",
97+
"/appscan_fingerprint/mac_address",
98+
"/arachni-",
99+
"/cybercop",
100+
"/nessus_is_probing_you_",
101+
"/nessustest",
102+
"/netsparker-",
103+
"/rfiinc.txt",
104+
"/thereisnowaythat-you-canbethere",
105+
"/w3af/remotefileinclude.html",
106+
"appscan_fingerprint",
107+
"w00tw00t.at.isc.sans.dfind",
108+
"w00tw00t.at.blackhats.romanian.anti-sec"
109+
]
110+
},
111+
"operator": "phrase_match"
112+
}
113+
],
114+
"transformers": [
115+
"lowercase"
116+
]
117+
},
118+
{
119+
"id": "crs-920-260",
120+
"name": "Unicode Full/Half Width Abuse Attack Attempt",
121+
"tags": {
122+
"type": "http_protocol_violation",
123+
"crs_id": "920260",
124+
"category": "attack_attempt"
125+
},
126+
"conditions": [
127+
{
128+
"parameters": {
129+
"inputs": [
130+
{
131+
"address": "server.request.uri.raw"
132+
}
133+
],
134+
"regex": "\\%u[fF]{2}[0-9a-fA-F]{2}",
135+
"options": {
136+
"case_sensitive": true,
137+
"min_length": 6
138+
}
139+
},
140+
"operator": "match_regex"
141+
}
142+
],
143+
"transformers": []
144+
},
145+
{
146+
"id": "crs-921-110",
147+
"name": "HTTP Request Smuggling Attack",
148+
"tags": {
149+
"type": "http_protocol_violation",
150+
"crs_id": "921110",
151+
"category": "attack_attempt"
152+
},
153+
"conditions": [
154+
{
155+
"parameters": {
156+
"inputs": [
157+
{
158+
"address": "server.request.query"
159+
},
160+
{
161+
"address": "server.request.body"
162+
},
163+
{
164+
"address": "server.request.path_params"
165+
}
166+
],
167+
"regex": "(?:get|post|head|options|connect|put|delete|trace|track|patch|propfind|propatch|mkcol|copy|move|lock|unlock)\\s+[^\\s]+\\s+http/\\d",
168+
"options": {
169+
"case_sensitive": true,
170+
"min_length": 12
171+
}
172+
},
173+
"operator": "match_regex"
174+
}
175+
],
176+
"transformers": [
177+
"lowercase"
178+
]
179+
}
180+
]
181+
}

0 commit comments

Comments
 (0)