Skip to content

Commit 236b458

Browse files
authored
[Entitlements] MailToURLConnection instrumentation (elastic#123829) (elastic#124232)
1 parent 0f57473 commit 236b458

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

libs/entitlement/bridge/src/main/java/org/elasticsearch/entitlement/bridge/EntitlementChecker.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,10 @@ public interface EntitlementChecker {
515515
javax.net.ssl.HttpsURLConnection that
516516
);
517517

518+
void check$sun_net_www_protocol_mailto_MailToURLConnection$connect(Class<?> callerClass, java.net.URLConnection that);
519+
520+
void check$sun_net_www_protocol_mailto_MailToURLConnection$getOutputStream(Class<?> callerClass, java.net.URLConnection that);
521+
518522
// Network miscellanea
519523

520524
// HttpClient#send and sendAsync are abstract, so we instrument their internal implementations

libs/entitlement/qa/entitled-plugin/src/main/java/org/elasticsearch/entitlement/qa/entitled/EntitledActions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.io.IOException;
1515
import java.net.URI;
16+
import java.net.URISyntaxException;
1617
import java.net.URLConnection;
1718
import java.nio.file.Files;
1819
import java.nio.file.Path;
@@ -76,4 +77,8 @@ public static URLConnection createFileURLConnection() throws IOException {
7677
var fileUrl = createTempFileForWrite().toUri().toURL();
7778
return fileUrl.openConnection();
7879
}
80+
81+
public static URLConnection createMailToURLConnection() throws URISyntaxException, IOException {
82+
return new URI("mailto", "[email protected]", null).toURL().openConnection();
83+
}
7984
}

libs/entitlement/qa/entitlement-test-plugin/src/main/java/org/elasticsearch/entitlement/qa/test/URLConnectionNetworkActions.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ private static void withJdkFtpConnection(CheckedConsumer<URLConnection, Exceptio
103103
}
104104
}
105105

106+
private static void withJdkMailToConnection(CheckedConsumer<URLConnection, Exception> connectionConsumer) throws Exception {
107+
var conn = EntitledActions.createMailToURLConnection();
108+
// Be sure we got the connection implementation we want
109+
assert conn.getClass().getSimpleName().equals("MailToURLConnection");
110+
try {
111+
connectionConsumer.accept(conn);
112+
} catch (IOException e) {
113+
// It's OK, it means we passed entitlement checks, and we tried to perform some IO
114+
}
115+
}
116+
106117
@EntitlementTest(expectedAccess = PLUGINS)
107118
static void urlOpenConnection() throws Exception {
108119
URI.create("http://127.0.0.1:12345/").toURL().openConnection();
@@ -429,4 +440,14 @@ static void sunHttpsURLConnectionImplGetContent() throws Exception {
429440
static void sunHttpsURLConnectionImplGetContentWithClasses() throws Exception {
430441
withJdkHttpsConnection(httpsURLConnection -> httpsURLConnection.getContent(new Class<?>[] { String.class }));
431442
}
443+
444+
@EntitlementTest(expectedAccess = PLUGINS)
445+
static void sunMailToURLConnectionConnect() throws Exception {
446+
withJdkMailToConnection(URLConnection::connect);
447+
}
448+
449+
@EntitlementTest(expectedAccess = PLUGINS)
450+
static void sunMailToURLConnectionGetOutputStream() throws Exception {
451+
withJdkMailToConnection(URLConnection::getOutputStream);
452+
}
432453
}

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/api/ElasticsearchEntitlementChecker.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,16 @@ private static boolean isFileUrlConnection(java.net.URLConnection urlConnection)
11771177
policyManager.checkOutboundNetworkAccess(callerClass);
11781178
}
11791179

1180+
@Override
1181+
public void check$sun_net_www_protocol_mailto_MailToURLConnection$connect(Class<?> callerClass, java.net.URLConnection that) {
1182+
policyManager.checkOutboundNetworkAccess(callerClass);
1183+
}
1184+
1185+
@Override
1186+
public void check$sun_net_www_protocol_mailto_MailToURLConnection$getOutputStream(Class<?> callerClass, java.net.URLConnection that) {
1187+
policyManager.checkOutboundNetworkAccess(callerClass);
1188+
}
1189+
11801190
@Override
11811191
public void check$jdk_internal_net_http_HttpClientImpl$send(
11821192
Class<?> callerClass,

0 commit comments

Comments
 (0)