|
34 | 34 | import java.io.IOException; |
35 | 35 | import java.io.UncheckedIOException; |
36 | 36 | import java.net.DatagramSocket; |
37 | | -import java.net.DatagramSocketImpl; |
38 | | -import java.net.DatagramSocketImplFactory; |
39 | 37 | import java.net.HttpURLConnection; |
| 38 | +import java.net.MalformedURLException; |
| 39 | +import java.net.ProxySelector; |
| 40 | +import java.net.ResponseCache; |
40 | 41 | import java.net.ServerSocket; |
41 | 42 | import java.net.Socket; |
42 | 43 | import java.net.URL; |
43 | 44 | import java.net.URLClassLoader; |
44 | 45 | import java.net.URLConnection; |
| 46 | +import java.net.URLStreamHandler; |
| 47 | +import java.net.spi.InetAddressResolver; |
| 48 | +import java.net.spi.InetAddressResolverProvider; |
| 49 | +import java.net.spi.URLStreamHandlerProvider; |
45 | 50 | import java.security.NoSuchAlgorithmException; |
46 | 51 | import java.util.List; |
47 | 52 | import java.util.Map; |
|
50 | 55 |
|
51 | 56 | import javax.net.ssl.HttpsURLConnection; |
52 | 57 | import javax.net.ssl.SSLContext; |
| 58 | +import javax.net.ssl.SSLSession; |
| 59 | +import javax.net.ssl.SSLSocket; |
| 60 | +import javax.net.ssl.SSLSocketFactory; |
53 | 61 |
|
54 | 62 | import static java.util.Map.entry; |
55 | 63 | import static org.elasticsearch.entitlement.qa.common.RestEntitlementsCheckAction.CheckAction.alwaysDenied; |
56 | 64 | import static org.elasticsearch.entitlement.qa.common.RestEntitlementsCheckAction.CheckAction.deniedToPlugins; |
57 | 65 | import static org.elasticsearch.entitlement.qa.common.RestEntitlementsCheckAction.CheckAction.forPlugins; |
58 | 66 | import static org.elasticsearch.rest.RestRequest.Method.GET; |
59 | 67 |
|
| 68 | +@SuppressWarnings("unused") |
60 | 69 | public class RestEntitlementsCheckAction extends BaseRestHandler { |
61 | 70 | private static final Logger logger = LogManager.getLogger(RestEntitlementsCheckAction.class); |
62 | 71 | public static final Thread NO_OP_SHUTDOWN_HOOK = new Thread(() -> {}, "Shutdown hook for testing"); |
@@ -125,9 +134,87 @@ static CheckAction alwaysDenied(Runnable action) { |
125 | 134 | entry("socket_setSocketImplFactory", alwaysDenied(RestEntitlementsCheckAction::socket$$setSocketImplFactory)), |
126 | 135 | entry("url_setURLStreamHandlerFactory", alwaysDenied(RestEntitlementsCheckAction::url$$setURLStreamHandlerFactory)), |
127 | 136 | entry("urlConnection_setFileNameMap", alwaysDenied(RestEntitlementsCheckAction::urlConnection$$setFileNameMap)), |
128 | | - entry("urlConnection_setContentHandlerFactory", alwaysDenied(RestEntitlementsCheckAction::urlConnection$$setContentHandlerFactory)) |
| 137 | + entry("urlConnection_setContentHandlerFactory", alwaysDenied(RestEntitlementsCheckAction::urlConnection$$setContentHandlerFactory)), |
| 138 | + |
| 139 | + entry("proxySelector_setDefault", alwaysDenied(RestEntitlementsCheckAction::setDefaultProxySelector)), |
| 140 | + entry("responseCache_setDefault", alwaysDenied(RestEntitlementsCheckAction::setDefaultResponseCache)), |
| 141 | + entry("createInetAddressResolverProvider", alwaysDenied(RestEntitlementsCheckAction::createInetAddressResolverProvider)), |
| 142 | + entry("createURLStreamHandlerProvider", alwaysDenied(RestEntitlementsCheckAction::createURLStreamHandlerProvider)), |
| 143 | + entry("createURLWithURLStreamHandler", alwaysDenied(RestEntitlementsCheckAction::createURLWithURLStreamHandler)), |
| 144 | + entry("createURLWithURLStreamHandler2", alwaysDenied(RestEntitlementsCheckAction::createURLWithURLStreamHandler2)), |
| 145 | + entry("sslSessionImpl_getSessionContext", alwaysDenied(RestEntitlementsCheckAction::sslSessionImplGetSessionContext)) |
129 | 146 | ); |
130 | 147 |
|
| 148 | + private static void createURLStreamHandlerProvider() { |
| 149 | + var x = new URLStreamHandlerProvider() { |
| 150 | + @Override |
| 151 | + public URLStreamHandler createURLStreamHandler(String protocol) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + }; |
| 155 | + } |
| 156 | + |
| 157 | + private static void sslSessionImplGetSessionContext() { |
| 158 | + SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory(); |
| 159 | + try (SSLSocket socket = (SSLSocket) factory.createSocket()) { |
| 160 | + SSLSession session = socket.getSession(); |
| 161 | + |
| 162 | + session.getSessionContext(); |
| 163 | + } catch (IOException e) { |
| 164 | + throw new RuntimeException(e); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @SuppressWarnings("deprecation") |
| 169 | + private static void createURLWithURLStreamHandler() { |
| 170 | + try { |
| 171 | + var x = new URL("http", "host", 1234, "file", new URLStreamHandler() { |
| 172 | + @Override |
| 173 | + protected URLConnection openConnection(URL u) { |
| 174 | + return null; |
| 175 | + } |
| 176 | + }); |
| 177 | + } catch (MalformedURLException e) { |
| 178 | + throw new RuntimeException(e); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + @SuppressWarnings("deprecation") |
| 183 | + private static void createURLWithURLStreamHandler2() { |
| 184 | + try { |
| 185 | + var x = new URL(null, "spec", new URLStreamHandler() { |
| 186 | + @Override |
| 187 | + protected URLConnection openConnection(URL u) { |
| 188 | + return null; |
| 189 | + } |
| 190 | + }); |
| 191 | + } catch (MalformedURLException e) { |
| 192 | + throw new RuntimeException(e); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private static void createInetAddressResolverProvider() { |
| 197 | + var x = new InetAddressResolverProvider() { |
| 198 | + @Override |
| 199 | + public InetAddressResolver get(Configuration configuration) { |
| 200 | + return null; |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public String name() { |
| 205 | + return "TEST"; |
| 206 | + } |
| 207 | + }; |
| 208 | + } |
| 209 | + |
| 210 | + private static void setDefaultResponseCache() { |
| 211 | + ResponseCache.setDefault(null); |
| 212 | + } |
| 213 | + |
| 214 | + private static void setDefaultProxySelector() { |
| 215 | + ProxySelector.setDefault(null); |
| 216 | + } |
| 217 | + |
131 | 218 | private static void setDefaultSSLContext() { |
132 | 219 | try { |
133 | 220 | SSLContext.setDefault(SSLContext.getDefault()); |
@@ -270,12 +357,7 @@ private static void setHttpsConnectionProperties() { |
270 | 357 | @SuppressForbidden(reason = "We're required to prevent calls to this forbidden API") |
271 | 358 | private static void datagramSocket$$setDatagramSocketImplFactory() { |
272 | 359 | try { |
273 | | - DatagramSocket.setDatagramSocketImplFactory(new DatagramSocketImplFactory() { |
274 | | - @Override |
275 | | - public DatagramSocketImpl createDatagramSocketImpl() { |
276 | | - throw new IllegalStateException(); |
277 | | - } |
278 | | - }); |
| 360 | + DatagramSocket.setDatagramSocketImplFactory(() -> { throw new IllegalStateException(); }); |
279 | 361 | } catch (IOException e) { |
280 | 362 | throw new IllegalStateException(e); |
281 | 363 | } |
|
0 commit comments