Skip to content

Commit b083d21

Browse files
Ensure blocking exceptions are propagated for SSRF (#9790)
1 parent ac15781 commit b083d21

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/decorator/HttpClientDecorator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,13 @@ public AgentSpan onRequest(final AgentSpan span, final REQUEST request) {
104104
} else if (shouldSetResourceName()) {
105105
span.setResourceName(DEFAULT_RESOURCE_NAME);
106106
}
107+
} catch (final BlockingException e) {
108+
throw e;
107109
} catch (final Exception e) {
108110
log.debug("Error tagging url", e);
111+
} finally {
112+
ssrfIastCheck(request);
109113
}
110-
ssrfIastCheck(request);
111114
}
112115
return span;
113116
}

dd-java-agent/instrumentation/okhttp/okhttp-2.2/src/main/java/datadog/trace/instrumentation/okhttp2/AppSecInterceptor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public Response intercept(final Chain chain) throws IOException {
5656
final Request request = onRequest(span, sampled, chain.request());
5757
final Response response = chain.proceed(request);
5858
return onResponse(span, sampled, response);
59+
} catch (final BlockingException e) {
60+
throw e;
5961
} catch (final Exception e) {
6062
LOGGER.debug("Failed to intercept request", e);
6163
return chain.proceed(chain.request());

dd-java-agent/instrumentation/okhttp/okhttp-3.0/src/main/java/datadog/trace/instrumentation/okhttp3/AppSecInterceptor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public Response intercept(final Chain chain) throws IOException {
5656
final Request request = onRequest(span, sampled, chain.request());
5757
final Response response = chain.proceed(request);
5858
return onResponse(span, sampled, response);
59+
} catch (final BlockingException e) {
60+
throw e;
5961
} catch (final Exception e) {
6062
LOGGER.debug("Failed to intercept request", e);
6163
return chain.proceed(chain.request());

dd-smoke-tests/appsec/springboot/src/main/java/datadog/smoketest/appsec/springboot/controller/WebController.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.squareup.okhttp.OkHttpClient;
99
import com.squareup.okhttp.Request;
1010
import com.squareup.okhttp.Response;
11+
import datadog.appsec.api.blocking.BlockingException;
1112
import datadog.smoketest.appsec.springboot.service.AsyncService;
1213
import java.io.ByteArrayOutputStream;
1314
import java.io.File;
@@ -93,7 +94,9 @@ public String sqliHeader(@RequestHeader("x-custom-header") String id) throws Exc
9394
public String ssrfQuery(@RequestParam("domain") final String domain) {
9495
try {
9596
new URL("http://" + domain).openStream().close();
96-
} catch (Throwable e) {
97+
} catch (final BlockingException e) {
98+
throw e;
99+
} catch (final Throwable e) {
97100
// ignore errors opening connection
98101
}
99102
return "EXECUTED";
@@ -105,7 +108,9 @@ public String apacheHttpClient4(@RequestParam("domain") final String domain) {
105108
try {
106109
final HttpGet request = new HttpGet("http://" + domain);
107110
client.execute(request);
108-
} catch (Exception e) {
111+
} catch (final BlockingException e) {
112+
throw e;
113+
} catch (final Exception e) {
109114
// ignore errors opening connection
110115
}
111116
client.getConnectionManager().shutdown();
@@ -118,6 +123,8 @@ public String commonsHttpClient2(@RequestParam("domain") final String domain) {
118123
final HttpMethod method = new GetMethod("http://" + domain);
119124
try {
120125
client.executeMethod(method);
126+
} catch (final BlockingException e) {
127+
throw e;
121128
} catch (final Exception e) {
122129
// ignore errors opening connection
123130
}
@@ -131,6 +138,8 @@ public String okHttp2(@RequestParam(value = "domain") final String domain) {
131138
final Request request = new Request.Builder().url("http://" + domain).build();
132139
try {
133140
client.newCall(request).execute();
141+
} catch (final BlockingException e) {
142+
throw e;
134143
} catch (final Exception e) {
135144
// ignore errors opening connection
136145
}
@@ -145,6 +154,8 @@ public String okHttp3(@RequestParam("domain") final String domain) {
145154
final okhttp3.Request request = new okhttp3.Request.Builder().url("http://" + domain).build();
146155
try {
147156
client.newCall(request).execute();
157+
} catch (final BlockingException e) {
158+
throw e;
148159
} catch (final Exception e) {
149160
// ignore errors opening connection
150161
}

dd-smoke-tests/appsec/springboot/src/test/groovy/datadog/smoketest/appsec/SpringBootSmokeTest.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.smoketest.appsec
22

3+
import datadog.appsec.api.blocking.BlockingException
34
import datadog.trace.agent.test.utils.OkHttpUtils
45
import datadog.trace.agent.test.utils.ThreadUtils
56
import groovy.json.JsonSlurper
@@ -650,6 +651,7 @@ class SpringBootSmokeTest extends AbstractAppSecServerSmokeTest {
650651
def rootSpans = this.rootSpans.toList()
651652
rootSpans.size() == 1
652653
def rootSpan = rootSpans[0]
654+
assert rootSpan.meta.get('error.message').contains(BlockingException.name) // ensure the block was propagated
653655
assert rootSpan.meta.get('appsec.blocked') == 'true', 'appsec.blocked is not set'
654656
assert rootSpan.meta.get('_dd.appsec.json') != null, '_dd.appsec.json is not set'
655657
def trigger = null

0 commit comments

Comments
 (0)