Skip to content

Commit d86a45c

Browse files
committed
instrument MailToURLConnection
1 parent 68e996e commit d86a45c

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
@@ -371,6 +371,10 @@ public interface EntitlementChecker {
371371

372372
void check$sun_net_www_URLConnection$getContentLength(Class<?> callerClass, java.net.URLConnection that);
373373

374+
void check$sun_net_www_protocol_mailto_MailToURLConnection$connect(Class<?> callerClass, java.net.URLConnection that);
375+
376+
void check$sun_net_www_protocol_mailto_MailToURLConnection$getOutputStream(Class<?> callerClass, java.net.URLConnection that);
377+
374378
// Network miscellanea
375379

376380
// 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;
@@ -63,4 +64,8 @@ public static Path createTempSymbolicLink() throws IOException {
6364
public static URLConnection createHttpURLConnection() throws IOException {
6465
return URI.create("http://127.0.0.1:12345/").toURL().openConnection();
6566
}
67+
68+
public static URLConnection createMailToURLConnection() throws URISyntaxException, IOException {
69+
return new URI("mailto", "[email protected]", null).toURL().openConnection();
70+
}
6671
}

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
@@ -79,6 +79,17 @@ private static void withJdkHttpConnection(CheckedConsumer<HttpURLConnection, Exc
7979
}
8080
}
8181

82+
private static void withJdkMailToConnection(CheckedConsumer<URLConnection, Exception> connectionConsumer) throws Exception {
83+
var conn = EntitledActions.createMailToURLConnection();
84+
// Be sure we got the connection implementation we want
85+
assert conn.getClass().getSimpleName().equals("MailToURLConnection");
86+
try {
87+
connectionConsumer.accept(conn);
88+
} catch (IOException e) {
89+
// It's OK, it means we passed entitlement checks, and we tried to perform some IO
90+
}
91+
}
92+
8293
@EntitlementTest(expectedAccess = PLUGINS)
8394
static void urlOpenConnection() throws Exception {
8495
URI.create("http://127.0.0.1:12345/").toURL().openConnection();
@@ -218,4 +229,14 @@ static void baseUrlConnectionGetContentWithClasses() throws Exception {
218229
static void sunHttpConnectionGetContentWithClasses() throws Exception {
219230
withJdkHttpConnection(conn -> conn.getContent(new Class<?>[] { String.class }));
220231
}
232+
233+
@EntitlementTest(expectedAccess = PLUGINS)
234+
static void sunMailToURLConnectionConnect() throws Exception {
235+
withJdkMailToConnection(URLConnection::connect);
236+
}
237+
238+
@EntitlementTest(expectedAccess = PLUGINS)
239+
static void sunMailToURLConnectionGetOutputStream() throws Exception {
240+
withJdkMailToConnection(URLConnection::connect);
241+
}
221242
}

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
@@ -835,6 +835,16 @@ private static boolean isNetworkUrlConnection(java.net.URLConnection urlConnecti
835835
}
836836
}
837837

838+
@Override
839+
public void check$sun_net_www_protocol_mailto_MailToURLConnection$connect(Class<?> callerClass, java.net.URLConnection that) {
840+
policyManager.checkOutboundNetworkAccess(callerClass);
841+
}
842+
843+
@Override
844+
public void check$sun_net_www_protocol_mailto_MailToURLConnection$getOutputStream(Class<?> callerClass, java.net.URLConnection that) {
845+
policyManager.checkOutboundNetworkAccess(callerClass);
846+
}
847+
838848
@Override
839849
public void check$jdk_internal_net_http_HttpClientImpl$send(
840850
Class<?> callerClass,

0 commit comments

Comments
 (0)