-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Entitlements] Add "always denied" network access checks #119867
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,14 +34,19 @@ | |
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.net.DatagramSocket; | ||
import java.net.DatagramSocketImpl; | ||
import java.net.DatagramSocketImplFactory; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.ProxySelector; | ||
import java.net.ResponseCache; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.net.URLConnection; | ||
import java.net.URLStreamHandler; | ||
import java.net.spi.InetAddressResolver; | ||
import java.net.spi.InetAddressResolverProvider; | ||
import java.net.spi.URLStreamHandlerProvider; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
@@ -50,13 +55,17 @@ | |
|
||
import javax.net.ssl.HttpsURLConnection; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLSession; | ||
import javax.net.ssl.SSLSocket; | ||
import javax.net.ssl.SSLSocketFactory; | ||
|
||
import static java.util.Map.entry; | ||
import static org.elasticsearch.entitlement.qa.common.RestEntitlementsCheckAction.CheckAction.alwaysDenied; | ||
import static org.elasticsearch.entitlement.qa.common.RestEntitlementsCheckAction.CheckAction.deniedToPlugins; | ||
import static org.elasticsearch.entitlement.qa.common.RestEntitlementsCheckAction.CheckAction.forPlugins; | ||
import static org.elasticsearch.rest.RestRequest.Method.GET; | ||
|
||
@SuppressWarnings("unused") | ||
public class RestEntitlementsCheckAction extends BaseRestHandler { | ||
private static final Logger logger = LogManager.getLogger(RestEntitlementsCheckAction.class); | ||
public static final Thread NO_OP_SHUTDOWN_HOOK = new Thread(() -> {}, "Shutdown hook for testing"); | ||
|
@@ -125,9 +134,87 @@ static CheckAction alwaysDenied(Runnable action) { | |
entry("socket_setSocketImplFactory", alwaysDenied(RestEntitlementsCheckAction::socket$$setSocketImplFactory)), | ||
entry("url_setURLStreamHandlerFactory", alwaysDenied(RestEntitlementsCheckAction::url$$setURLStreamHandlerFactory)), | ||
entry("urlConnection_setFileNameMap", alwaysDenied(RestEntitlementsCheckAction::urlConnection$$setFileNameMap)), | ||
entry("urlConnection_setContentHandlerFactory", alwaysDenied(RestEntitlementsCheckAction::urlConnection$$setContentHandlerFactory)) | ||
entry("urlConnection_setContentHandlerFactory", alwaysDenied(RestEntitlementsCheckAction::urlConnection$$setContentHandlerFactory)), | ||
|
||
entry("proxySelector_setDefault", alwaysDenied(RestEntitlementsCheckAction::setDefaultProxySelector)), | ||
entry("responseCache_setDefault", alwaysDenied(RestEntitlementsCheckAction::setDefaultResponseCache)), | ||
entry("createInetAddressResolverProvider", alwaysDenied(RestEntitlementsCheckAction::createInetAddressResolverProvider)), | ||
entry("createURLStreamHandlerProvider", alwaysDenied(RestEntitlementsCheckAction::createURLStreamHandlerProvider)), | ||
entry("createURLWithURLStreamHandler", alwaysDenied(RestEntitlementsCheckAction::createURLWithURLStreamHandler)), | ||
entry("createURLWithURLStreamHandler2", alwaysDenied(RestEntitlementsCheckAction::createURLWithURLStreamHandler2)), | ||
entry("sslSessionImpl_getSessionContext", alwaysDenied(RestEntitlementsCheckAction::sslSessionImplGetSessionContext)) | ||
); | ||
|
||
private static void createURLStreamHandlerProvider() { | ||
var x = new URLStreamHandlerProvider() { | ||
@Override | ||
public URLStreamHandler createURLStreamHandler(String protocol) { | ||
return null; | ||
} | ||
}; | ||
} | ||
|
||
private static void sslSessionImplGetSessionContext() { | ||
SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory(); | ||
try (SSLSocket socket = (SSLSocket) factory.createSocket()) { | ||
SSLSession session = socket.getSession(); | ||
|
||
session.getSessionContext(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
private static void createURLWithURLStreamHandler() { | ||
try { | ||
var x = new URL("http", "host", 1234, "file", new URLStreamHandler() { | ||
@Override | ||
protected URLConnection openConnection(URL u) { | ||
return null; | ||
} | ||
}); | ||
} catch (MalformedURLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
private static void createURLWithURLStreamHandler2() { | ||
try { | ||
var x = new URL(null, "spec", new URLStreamHandler() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we forbidding people from making There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are forbidding only making URls with a specific, custom stream handler. This has a special permission in SM, that's why these 2 ctors arrived on our radar. |
||
@Override | ||
protected URLConnection openConnection(URL u) { | ||
return null; | ||
} | ||
}); | ||
} catch (MalformedURLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static void createInetAddressResolverProvider() { | ||
var x = new InetAddressResolverProvider() { | ||
@Override | ||
public InetAddressResolver get(Configuration configuration) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return "TEST"; | ||
} | ||
}; | ||
} | ||
|
||
private static void setDefaultResponseCache() { | ||
ResponseCache.setDefault(null); | ||
} | ||
|
||
private static void setDefaultProxySelector() { | ||
ProxySelector.setDefault(null); | ||
} | ||
|
||
private static void setDefaultSSLContext() { | ||
try { | ||
SSLContext.setDefault(SSLContext.getDefault()); | ||
|
@@ -270,12 +357,7 @@ private static void setHttpsConnectionProperties() { | |
@SuppressForbidden(reason = "We're required to prevent calls to this forbidden API") | ||
private static void datagramSocket$$setDatagramSocketImplFactory() { | ||
try { | ||
DatagramSocket.setDatagramSocketImplFactory(new DatagramSocketImplFactory() { | ||
@Override | ||
public DatagramSocketImpl createDatagramSocketImpl() { | ||
throw new IllegalStateException(); | ||
} | ||
}); | ||
DatagramSocket.setDatagramSocketImplFactory(() -> { throw new IllegalStateException(); }); | ||
} catch (IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It occurs to me that if we changed
CheckAction
to take a version ofRunnable
that allows exceptions, we could remove all these re-throws.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ummm... good idea. I'll do that in my other open PR to avoid churn on this one.