Skip to content

Commit a2f0ccd

Browse files
Add propagation to URI#toURL method
1 parent 4905729 commit a2f0ccd

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

dd-java-agent/instrumentation/java-net/src/main/java/datadog/trace/instrumentation/java/net/URICallSite.java

+16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import datadog.trace.api.iast.propagation.CodecModule;
1010
import datadog.trace.api.iast.propagation.PropagationModule;
1111
import java.net.URI;
12+
import java.net.URL;
1213
import javax.annotation.Nonnull;
1314
import javax.annotation.Nullable;
1415

@@ -105,4 +106,19 @@ public static URI afterNormalize(
105106
}
106107
return result;
107108
}
109+
110+
@Propagation
111+
@CallSite.After("java.net.URL java.net.URI.toURL()")
112+
public static URL afterToURL(@CallSite.This final URI uri, @CallSite.Return final URL result) {
113+
final PropagationModule module = InstrumentationBridge.PROPAGATION;
114+
if (module != null && result != null) {
115+
try {
116+
boolean keepRanges = uri.toString().equals(result.toString());
117+
module.taintObjectIfTainted(result, uri, keepRanges, NOT_MARKED);
118+
} catch (final Throwable e) {
119+
module.onUnexpectedException("After toURL threw", e);
120+
}
121+
}
122+
return result;
123+
}
108124
}

dd-java-agent/instrumentation/java-net/src/test/groovy/datadog/trace/instrumentation/java/net/URICallSIteTest.groovy

+1
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,6 @@ class URICallSIteTest extends AgentTestRunner {
6969
'toASCIIString' | 'String' | [new URI('http://test.com/index?name=value#fragment')] | true
7070
'toASCIIString' | 'String' | [new URI('http://test.com/漢/index?name=value#fragment')] | false
7171
'toString' | 'String' | [new URI('http://test.com/index?name=value#fragment')] | true
72+
'toURL' | 'Object' | [new URI('http://test.com/index?name=value#fragment')] | true
7273
}
7374
}

dd-java-agent/instrumentation/java-net/src/test/java/foo/bar/TestURICallSiteSuite.java

+9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package foo.bar;
22

3+
import java.net.MalformedURLException;
34
import java.net.URI;
45
import java.net.URISyntaxException;
6+
import java.net.URL;
57
import org.slf4j.Logger;
68
import org.slf4j.LoggerFactory;
79

@@ -105,4 +107,11 @@ public static String toASCIIString(final URI uri) {
105107
LOGGER.debug("After toAsciiString {}", result);
106108
return result;
107109
}
110+
111+
public static URL toURL(final URI uri) throws MalformedURLException {
112+
LOGGER.debug("Before toURL {}", uri);
113+
final URL result = uri.toURL();
114+
LOGGER.debug("After toURL {}", result);
115+
return result;
116+
}
108117
}

dd-smoke-tests/iast-util/src/main/java/datadog/smoketest/springboot/controller/SsrfController.java

+16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.squareup.okhttp.OkHttpClient;
44
import com.squareup.okhttp.Request;
55
import java.net.HttpURLConnection;
6+
import java.net.URI;
67
import java.net.URL;
78
import org.apache.commons.httpclient.HttpClient;
89
import org.apache.commons.httpclient.HttpMethod;
@@ -39,6 +40,21 @@ public String ssrf(
3940
return "ok";
4041
}
4142

43+
@PostMapping("/uri")
44+
public String uri(
45+
@RequestParam(value = "url", required = false) final String url,
46+
@RequestParam(value = "host", required = false) final String host) {
47+
try {
48+
final URI uri =
49+
url != null ? new URI(url) : new URI("https", null, host, 443, "/test", null, null);
50+
final URL target = uri.toURL();
51+
final HttpURLConnection conn = (HttpURLConnection) target.openConnection();
52+
conn.disconnect();
53+
} catch (final Exception e) {
54+
}
55+
return "ok";
56+
}
57+
4258
@PostMapping("/apache-httpclient4")
4359
public String apacheHttpClient4(
4460
@RequestParam(value = "url", required = false) final String url,

dd-smoke-tests/iast-util/src/testFixtures/groovy/datadog/smoketest/AbstractIastSpringBootTest.groovy

+6-4
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ abstract class AbstractIastSpringBootTest extends AbstractIastServerSmokeTest {
688688

689689
void 'ssrf is present'() {
690690
setup:
691-
final url = "http://localhost:${httpPort}/ssrf"
691+
final url = "http://localhost:${httpPort}/ssrf${path}"
692692
final body = new FormBody.Builder().add(parameter, value).build()
693693
final request = new Request.Builder().url(url).post(body).build()
694694

@@ -715,9 +715,11 @@ abstract class AbstractIastSpringBootTest extends AbstractIastServerSmokeTest {
715715
}
716716

717717
where:
718-
parameter | value
719-
'url' | 'https://dd.datad0g.com/'
720-
'host' | 'dd.datad0g.com'
718+
path | parameter | value
719+
'' | 'url' | 'https://dd.datad0g.com/'
720+
'' | 'host' | 'dd.datad0g.com'
721+
'/uri' | 'url' | 'https://dd.datad0g.com/'
722+
'/uri' | 'host' | 'dd.datad0g.com'
721723
}
722724

723725
void 'ssrf is present (#path) (#parameter)'() {

0 commit comments

Comments
 (0)