author | chegar |
Thu, 08 Mar 2018 17:42:16 +0000 | |
branch | http-client-branch |
changeset 56265 | ec34ae013fbe |
parent 56089 | 42208b2f224e |
child 56451 | 9585061fdb04 |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @summary Basic security checks for WebSocket URI from the Builder |
|
27 |
* @compile ../DummyWebSocketServer.java ../../ProxyServer.java |
|
28 |
* @run testng/othervm/java.security.policy=httpclient.policy WSURLPermissionTest |
|
29 |
*/ |
|
30 |
||
31 |
import java.io.IOException; |
|
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56089
diff
changeset
|
32 |
import java.net.InetAddress; |
48083 | 33 |
import java.net.InetSocketAddress; |
34 |
import java.net.Proxy; |
|
35 |
import java.net.ProxySelector; |
|
36 |
import java.net.SocketAddress; |
|
37 |
import java.net.URI; |
|
38 |
import java.net.URLPermission; |
|
39 |
import java.security.AccessControlContext; |
|
40 |
import java.security.AccessController; |
|
41 |
import java.security.Permission; |
|
42 |
import java.security.Permissions; |
|
43 |
import java.security.PrivilegedActionException; |
|
44 |
import java.security.PrivilegedExceptionAction; |
|
45 |
import java.security.ProtectionDomain; |
|
46 |
import java.util.List; |
|
47 |
import java.util.concurrent.ExecutionException; |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
55973
diff
changeset
|
48 |
import java.net.http.HttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
55973
diff
changeset
|
49 |
import java.net.http.WebSocket; |
48083 | 50 |
import org.testng.annotations.AfterTest; |
51 |
import org.testng.annotations.BeforeTest; |
|
52 |
import org.testng.annotations.DataProvider; |
|
53 |
import org.testng.annotations.Test; |
|
54 |
import static org.testng.Assert.*; |
|
55 |
||
56 |
public class WSURLPermissionTest { |
|
57 |
||
58 |
static AccessControlContext withPermissions(Permission... perms) { |
|
59 |
Permissions p = new Permissions(); |
|
60 |
for (Permission perm : perms) { |
|
61 |
p.add(perm); |
|
62 |
} |
|
63 |
ProtectionDomain pd = new ProtectionDomain(null, p); |
|
64 |
return new AccessControlContext(new ProtectionDomain[]{ pd }); |
|
65 |
} |
|
66 |
||
67 |
static AccessControlContext noPermissions() { |
|
68 |
return withPermissions(/*empty*/); |
|
69 |
} |
|
70 |
||
71 |
URI wsURI; |
|
72 |
DummyWebSocketServer webSocketServer; |
|
73 |
InetSocketAddress proxyAddress; |
|
74 |
||
75 |
@BeforeTest |
|
76 |
public void setup() throws Exception { |
|
77 |
ProxyServer proxyServer = new ProxyServer(0, true); |
|
56265
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56089
diff
changeset
|
78 |
proxyAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), |
ec34ae013fbe
http-client-branch: tests should bind to the loopback only
chegar
parents:
56089
diff
changeset
|
79 |
proxyServer.getPort()); |
48083 | 80 |
webSocketServer = new DummyWebSocketServer(); |
81 |
webSocketServer.open(); |
|
82 |
wsURI = webSocketServer.getURI(); |
|
83 |
||
84 |
System.out.println("Proxy Server: " + proxyAddress); |
|
85 |
System.out.println("DummyWebSocketServer: " + wsURI); |
|
86 |
} |
|
87 |
||
88 |
@AfterTest |
|
89 |
public void teardown() { |
|
90 |
webSocketServer.close(); |
|
91 |
} |
|
92 |
||
93 |
static class NoOpListener implements WebSocket.Listener {} |
|
94 |
static final WebSocket.Listener noOpListener = new NoOpListener(); |
|
95 |
||
96 |
@DataProvider(name = "passingScenarios") |
|
97 |
public Object[][] passingScenarios() { |
|
98 |
HttpClient noProxyClient = HttpClient.newHttpClient(); |
|
99 |
return new Object[][]{ |
|
100 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
101 |
noProxyClient.newWebSocketBuilder() |
|
102 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
103 |
return null; }, // no actions |
|
104 |
new URLPermission[] { new URLPermission(wsURI.toString()) }, |
|
105 |
"0" /* for log file identification */ }, |
|
106 |
||
107 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
108 |
noProxyClient.newWebSocketBuilder() |
|
109 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
110 |
return null; }, // scheme wildcard |
|
111 |
new URLPermission[] { new URLPermission("ws://*") }, |
|
112 |
"0.1" }, |
|
113 |
||
114 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
115 |
noProxyClient.newWebSocketBuilder() |
|
116 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
117 |
return null; }, // port wildcard |
|
118 |
new URLPermission[] { new URLPermission("ws://"+wsURI.getHost()+":*") }, |
|
119 |
"0.2" }, |
|
120 |
||
121 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
122 |
noProxyClient.newWebSocketBuilder() |
|
123 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
124 |
return null; }, // empty actions |
|
125 |
new URLPermission[] { new URLPermission(wsURI.toString(), "") }, |
|
126 |
"1" }, |
|
127 |
||
128 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
129 |
noProxyClient.newWebSocketBuilder() |
|
130 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
131 |
return null; }, // colon |
|
132 |
new URLPermission[] { new URLPermission(wsURI.toString(), ":") }, |
|
133 |
"2" }, |
|
134 |
||
135 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
136 |
noProxyClient.newWebSocketBuilder() |
|
137 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
138 |
return null; }, // wildcard |
|
139 |
new URLPermission[] { new URLPermission(wsURI.toString(), "*:*") }, |
|
140 |
"3" }, |
|
141 |
||
142 |
// WS permission checking is agnostic of method, any/none will do |
|
143 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
144 |
noProxyClient.newWebSocketBuilder() |
|
145 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
146 |
return null; }, // specific method |
|
147 |
new URLPermission[] { new URLPermission(wsURI.toString(), "GET") }, |
|
148 |
"3.1" }, |
|
149 |
||
150 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
151 |
noProxyClient.newWebSocketBuilder() |
|
152 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
153 |
return null; }, // specific method |
|
154 |
new URLPermission[] { new URLPermission(wsURI.toString(), "POST") }, |
|
155 |
"3.2" }, |
|
156 |
||
157 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
158 |
URI uriWithPath = wsURI.resolve("/path/x"); |
|
159 |
noProxyClient.newWebSocketBuilder() |
|
160 |
.buildAsync(uriWithPath, noOpListener).get().abort(); |
|
161 |
return null; }, // path |
|
162 |
new URLPermission[] { new URLPermission(wsURI.resolve("/path/x").toString()) }, |
|
163 |
"4" }, |
|
164 |
||
165 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
166 |
URI uriWithPath = wsURI.resolve("/path/x"); |
|
167 |
noProxyClient.newWebSocketBuilder() |
|
168 |
.buildAsync(uriWithPath, noOpListener).get().abort(); |
|
169 |
return null; }, // same dir wildcard |
|
170 |
new URLPermission[] { new URLPermission(wsURI.resolve("/path/*").toString()) }, |
|
171 |
"5" }, |
|
172 |
||
173 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
174 |
URI uriWithPath = wsURI.resolve("/path/x"); |
|
175 |
noProxyClient.newWebSocketBuilder() |
|
176 |
.buildAsync(uriWithPath, noOpListener).get().abort(); |
|
177 |
return null; }, // recursive |
|
178 |
new URLPermission[] { new URLPermission(wsURI.resolve("/path/-").toString()) }, |
|
179 |
"6" }, |
|
180 |
||
181 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
182 |
URI uriWithPath = wsURI.resolve("/path/x"); |
|
183 |
noProxyClient.newWebSocketBuilder() |
|
184 |
.buildAsync(uriWithPath, noOpListener).get().abort(); |
|
185 |
return null; }, // recursive top |
|
186 |
new URLPermission[] { new URLPermission(wsURI.resolve("/-").toString()) }, |
|
187 |
"7" }, |
|
188 |
||
189 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
190 |
noProxyClient.newWebSocketBuilder() |
|
191 |
.header("A-Header", "A-Value") // header |
|
192 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
193 |
return null; }, |
|
194 |
new URLPermission[] { new URLPermission(wsURI.toString(), ":A-Header") }, |
|
195 |
"8" }, |
|
196 |
||
197 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
198 |
noProxyClient.newWebSocketBuilder() |
|
199 |
.header("A-Header", "A-Value") // header |
|
200 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
201 |
return null; }, // wildcard |
|
202 |
new URLPermission[] { new URLPermission(wsURI.toString(), ":*") }, |
|
203 |
"9" }, |
|
204 |
||
205 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
206 |
noProxyClient.newWebSocketBuilder() |
|
207 |
.header("A-Header", "A-Value") // headers |
|
208 |
.header("B-Header", "B-Value") // headers |
|
209 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
210 |
return null; }, |
|
211 |
new URLPermission[] { new URLPermission(wsURI.toString(), ":A-Header,B-Header") }, |
|
212 |
"10" }, |
|
213 |
||
214 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
215 |
noProxyClient.newWebSocketBuilder() |
|
216 |
.header("A-Header", "A-Value") // headers |
|
217 |
.header("B-Header", "B-Value") // headers |
|
218 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
219 |
return null; }, // wildcard |
|
220 |
new URLPermission[] { new URLPermission(wsURI.toString(), ":*") }, |
|
221 |
"11" }, |
|
222 |
||
223 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
224 |
noProxyClient.newWebSocketBuilder() |
|
225 |
.header("A-Header", "A-Value") // headers |
|
226 |
.header("B-Header", "B-Value") // headers |
|
227 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
228 |
return null; }, // wildcards |
|
229 |
new URLPermission[] { new URLPermission(wsURI.toString(), "*:*") }, |
|
230 |
"12" }, |
|
231 |
||
232 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
233 |
noProxyClient.newWebSocketBuilder() |
|
234 |
.header("A-Header", "A-Value") // multi-value |
|
235 |
.header("A-Header", "B-Value") // headers |
|
236 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
237 |
return null; }, // wildcard |
|
238 |
new URLPermission[] { new URLPermission(wsURI.toString(), ":*") }, |
|
239 |
"13" }, |
|
240 |
||
241 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
242 |
noProxyClient.newWebSocketBuilder() |
|
243 |
.header("A-Header", "A-Value") // multi-value |
|
244 |
.header("A-Header", "B-Value") // headers |
|
245 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
246 |
return null; }, // single grant |
|
247 |
new URLPermission[] { new URLPermission(wsURI.toString(), ":A-Header") }, |
|
248 |
"14" }, |
|
249 |
||
250 |
// client with a DIRECT proxy |
|
251 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
252 |
ProxySelector ps = ProxySelector.of(null); |
|
253 |
HttpClient client = HttpClient.newBuilder().proxy(ps).build(); |
|
254 |
client.newWebSocketBuilder() |
|
255 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
256 |
return null; }, |
|
257 |
new URLPermission[] { new URLPermission(wsURI.toString()) }, |
|
258 |
"15" }, |
|
259 |
||
260 |
// client with a SOCKS proxy! ( expect implementation to ignore SOCKS ) |
|
261 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
262 |
ProxySelector ps = new ProxySelector() { |
|
263 |
@Override public List<Proxy> select(URI uri) { |
|
264 |
return List.of(new Proxy(Proxy.Type.SOCKS, proxyAddress)); } |
|
265 |
@Override |
|
266 |
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { } |
|
267 |
}; |
|
268 |
HttpClient client = HttpClient.newBuilder().proxy(ps).build(); |
|
269 |
client.newWebSocketBuilder() |
|
270 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
271 |
return null; }, |
|
272 |
new URLPermission[] { new URLPermission(wsURI.toString()) }, |
|
273 |
"16" }, |
|
274 |
||
275 |
// client with a HTTP/HTTPS proxy |
|
276 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
277 |
assert proxyAddress != null; |
|
278 |
ProxySelector ps = ProxySelector.of(proxyAddress); |
|
279 |
HttpClient client = HttpClient.newBuilder().proxy(ps).build(); |
|
280 |
client.newWebSocketBuilder() |
|
281 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
282 |
return null; }, |
|
283 |
new URLPermission[] { |
|
284 |
new URLPermission(wsURI.toString()), // CONNECT action string |
|
285 |
new URLPermission("socket://"+proxyAddress.getHostName() |
|
286 |
+":"+proxyAddress.getPort(), "CONNECT")}, |
|
287 |
"17" }, |
|
288 |
||
289 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
290 |
assert proxyAddress != null; |
|
291 |
ProxySelector ps = ProxySelector.of(proxyAddress); |
|
292 |
HttpClient client = HttpClient.newBuilder().proxy(ps).build(); |
|
293 |
client.newWebSocketBuilder() |
|
294 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
295 |
return null; }, |
|
296 |
new URLPermission[] { |
|
297 |
new URLPermission(wsURI.toString()), // no action string |
|
298 |
new URLPermission("socket://"+proxyAddress.getHostName() |
|
299 |
+":"+proxyAddress.getPort())}, |
|
300 |
"18" }, |
|
301 |
||
302 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
303 |
assert proxyAddress != null; |
|
304 |
ProxySelector ps = ProxySelector.of(proxyAddress); |
|
305 |
HttpClient client = HttpClient.newBuilder().proxy(ps).build(); |
|
306 |
client.newWebSocketBuilder() |
|
307 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
308 |
return null; }, |
|
309 |
new URLPermission[] { |
|
310 |
new URLPermission(wsURI.toString()), // wildcard headers |
|
311 |
new URLPermission("socket://"+proxyAddress.getHostName() |
|
312 |
+":"+proxyAddress.getPort(), "CONNECT:*")}, |
|
313 |
"19" }, |
|
314 |
||
315 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
316 |
assert proxyAddress != null; |
|
317 |
CountingProxySelector ps = CountingProxySelector.of(proxyAddress); |
|
318 |
HttpClient client = HttpClient.newBuilder().proxy(ps).build(); |
|
319 |
client.newWebSocketBuilder() |
|
320 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
321 |
assertEquals(ps.count(), 1); // ps.select only invoked once |
|
322 |
return null; }, |
|
323 |
new URLPermission[] { |
|
324 |
new URLPermission(wsURI.toString()), // empty headers |
|
325 |
new URLPermission("socket://"+proxyAddress.getHostName() |
|
326 |
+":"+proxyAddress.getPort(), "CONNECT:")}, |
|
327 |
"20" }, |
|
328 |
||
329 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
330 |
assert proxyAddress != null; |
|
331 |
ProxySelector ps = ProxySelector.of(proxyAddress); |
|
332 |
HttpClient client = HttpClient.newBuilder().proxy(ps).build(); |
|
333 |
client.newWebSocketBuilder() |
|
334 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
335 |
return null; }, |
|
336 |
new URLPermission[] { |
|
337 |
new URLPermission(wsURI.toString()), |
|
338 |
new URLPermission("socket://*")}, // wildcard socket URL |
|
339 |
"21" }, |
|
340 |
||
341 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
342 |
assert proxyAddress != null; |
|
343 |
ProxySelector ps = ProxySelector.of(proxyAddress); |
|
344 |
HttpClient client = HttpClient.newBuilder().proxy(ps).build(); |
|
345 |
client.newWebSocketBuilder() |
|
346 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
347 |
return null; }, |
|
348 |
new URLPermission[] { |
|
349 |
new URLPermission("ws://*"), // wildcard ws URL |
|
350 |
new URLPermission("socket://*")}, // wildcard socket URL |
|
351 |
"22" }, |
|
352 |
||
353 |
}; |
|
354 |
} |
|
355 |
||
356 |
@Test(dataProvider = "passingScenarios") |
|
357 |
public void testWithNoSecurityManager(PrivilegedExceptionAction<?> action, |
|
358 |
URLPermission[] unused, |
|
359 |
String dataProviderId) |
|
360 |
throws Exception |
|
361 |
{ |
|
362 |
// sanity ( no security manager ) |
|
363 |
System.setSecurityManager(null); |
|
364 |
try { |
|
365 |
AccessController.doPrivileged(action); |
|
366 |
} finally { |
|
367 |
System.setSecurityManager(new SecurityManager()); |
|
368 |
} |
|
369 |
} |
|
370 |
||
371 |
@Test(dataProvider = "passingScenarios") |
|
372 |
public void testWithAllPermissions(PrivilegedExceptionAction<?> action, |
|
373 |
URLPermission[] unused, |
|
374 |
String dataProviderId) |
|
375 |
throws Exception |
|
376 |
{ |
|
377 |
// Run with all permissions, i.e. no further restrictions than test's AllPermission |
|
378 |
assert System.getSecurityManager() != null; |
|
379 |
AccessController.doPrivileged(action); |
|
380 |
} |
|
381 |
||
382 |
@Test(dataProvider = "passingScenarios") |
|
383 |
public void testWithMinimalPermissions(PrivilegedExceptionAction<?> action, |
|
384 |
URLPermission[] perms, |
|
385 |
String dataProviderId) |
|
386 |
throws Exception |
|
387 |
{ |
|
388 |
// Run with minimal permissions, i.e. just what is required |
|
389 |
assert System.getSecurityManager() != null; |
|
390 |
AccessControlContext minimalACC = withPermissions(perms); |
|
391 |
AccessController.doPrivileged(action, minimalACC); |
|
392 |
} |
|
393 |
||
394 |
@Test(dataProvider = "passingScenarios") |
|
395 |
public void testWithNoPermissions(PrivilegedExceptionAction<?> action, |
|
396 |
URLPermission[] unused, |
|
397 |
String dataProviderId) |
|
398 |
throws Exception |
|
399 |
{ |
|
400 |
// Run with NO permissions, i.e. expect SecurityException |
|
401 |
assert System.getSecurityManager() != null; |
|
402 |
try { |
|
403 |
AccessController.doPrivileged(action, noPermissions()); |
|
404 |
fail("EXPECTED SecurityException"); |
|
405 |
} catch (PrivilegedActionException expected) { |
|
406 |
Throwable t = expected.getCause(); |
|
407 |
if (t instanceof ExecutionException) |
|
408 |
t = t.getCause(); |
|
409 |
||
410 |
if (t instanceof SecurityException) |
|
411 |
System.out.println("Caught expected SE:" + expected); |
|
412 |
else |
|
413 |
fail("Expected SecurityException, but got: " + t); |
|
414 |
} |
|
415 |
} |
|
416 |
||
417 |
// --- Negative tests --- |
|
418 |
||
419 |
@DataProvider(name = "failingScenarios") |
|
420 |
public Object[][] failingScenarios() { |
|
421 |
HttpClient noProxyClient = HttpClient.newHttpClient(); |
|
422 |
return new Object[][]{ |
|
423 |
{ (PrivilegedExceptionAction<?>) () -> { |
|
424 |
noProxyClient.newWebSocketBuilder() |
|
425 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
426 |
return null; |
|
427 |
}, |
|
428 |
new URLPermission[]{ /* no permissions */ }, |
|
429 |
"50" /* for log file identification */}, |
|
430 |
||
431 |
{ (PrivilegedExceptionAction<?>) () -> { |
|
432 |
noProxyClient.newWebSocketBuilder() |
|
433 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
434 |
return null; |
|
435 |
}, // wrong scheme |
|
436 |
new URLPermission[]{ new URLPermission("http://*") }, |
|
437 |
"51" }, |
|
438 |
||
439 |
{ (PrivilegedExceptionAction<?>) () -> { |
|
440 |
noProxyClient.newWebSocketBuilder() |
|
441 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
442 |
return null; |
|
443 |
}, // wrong scheme |
|
444 |
new URLPermission[]{ new URLPermission("socket://*") }, |
|
445 |
"52" }, |
|
446 |
||
447 |
{ (PrivilegedExceptionAction<?>) () -> { |
|
448 |
noProxyClient.newWebSocketBuilder() |
|
449 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
450 |
return null; |
|
451 |
}, // wrong host |
|
452 |
new URLPermission[]{ new URLPermission("ws://foo.com/") }, |
|
453 |
"53" }, |
|
454 |
||
455 |
{ (PrivilegedExceptionAction<?>) () -> { |
|
456 |
noProxyClient.newWebSocketBuilder() |
|
457 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
458 |
return null; |
|
459 |
}, // wrong port |
|
460 |
new URLPermission[]{ new URLPermission("ws://"+ wsURI.getHost()+":5") }, |
|
461 |
"54" }, |
|
462 |
||
463 |
{ (PrivilegedExceptionAction<?>) () -> { |
|
464 |
noProxyClient.newWebSocketBuilder() |
|
465 |
.header("A-Header", "A-Value") |
|
466 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
467 |
return null; |
|
468 |
}, // only perm to set B not A |
|
469 |
new URLPermission[] { new URLPermission(wsURI.toString(), "*:B-Header") }, |
|
470 |
"55" }, |
|
471 |
||
472 |
{ (PrivilegedExceptionAction<?>) () -> { |
|
473 |
noProxyClient.newWebSocketBuilder() |
|
474 |
.header("A-Header", "A-Value") |
|
475 |
.header("B-Header", "B-Value") |
|
476 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
477 |
return null; |
|
478 |
}, // only perm to set B not A |
|
479 |
new URLPermission[] { new URLPermission(wsURI.toString(), "*:B-Header") }, |
|
480 |
"56" }, |
|
481 |
||
482 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
483 |
URI uriWithPath = wsURI.resolve("/path/x"); |
|
484 |
noProxyClient.newWebSocketBuilder() |
|
485 |
.buildAsync(uriWithPath, noOpListener).get().abort(); |
|
486 |
return null; }, // wrong path |
|
487 |
new URLPermission[] { new URLPermission(wsURI.resolve("/aDiffPath/").toString()) }, |
|
488 |
"57" }, |
|
489 |
||
490 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
491 |
URI uriWithPath = wsURI.resolve("/path/x"); |
|
492 |
noProxyClient.newWebSocketBuilder() |
|
493 |
.buildAsync(uriWithPath, noOpListener).get().abort(); |
|
494 |
return null; }, // more specific path |
|
495 |
new URLPermission[] { new URLPermission(wsURI.resolve("/path/x/y").toString()) }, |
|
496 |
"58" }, |
|
497 |
||
498 |
// client with a HTTP/HTTPS proxy |
|
499 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
500 |
assert proxyAddress != null; |
|
501 |
ProxySelector ps = ProxySelector.of(proxyAddress); |
|
502 |
HttpClient client = HttpClient.newBuilder().proxy(ps).build(); |
|
503 |
client.newWebSocketBuilder() |
|
504 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
505 |
return null; }, // missing proxy perm |
|
506 |
new URLPermission[] { new URLPermission(wsURI.toString()) }, |
|
507 |
"100" }, |
|
508 |
||
509 |
// client with a HTTP/HTTPS proxy |
|
510 |
{ (PrivilegedExceptionAction<?>)() -> { |
|
511 |
assert proxyAddress != null; |
|
512 |
ProxySelector ps = ProxySelector.of(proxyAddress); |
|
513 |
HttpClient client = HttpClient.newBuilder().proxy(ps).build(); |
|
514 |
client.newWebSocketBuilder() |
|
515 |
.buildAsync(wsURI, noOpListener).get().abort(); |
|
516 |
return null; }, |
|
517 |
new URLPermission[] { |
|
518 |
new URLPermission(wsURI.toString()), // missing proxy CONNECT |
|
519 |
new URLPermission("socket://*", "GET") }, |
|
520 |
"101" }, |
|
521 |
}; |
|
522 |
} |
|
523 |
||
524 |
@Test(dataProvider = "failingScenarios") |
|
525 |
public void testWithoutEnoughPermissions(PrivilegedExceptionAction<?> action, |
|
526 |
URLPermission[] perms, |
|
527 |
String dataProviderId) |
|
528 |
throws Exception |
|
529 |
{ |
|
530 |
// Run without Enough permissions, i.e. expect SecurityException |
|
531 |
assert System.getSecurityManager() != null; |
|
532 |
AccessControlContext notEnoughPermsACC = withPermissions(perms); |
|
533 |
try { |
|
534 |
AccessController.doPrivileged(action, notEnoughPermsACC); |
|
535 |
fail("EXPECTED SecurityException"); |
|
536 |
} catch (PrivilegedActionException expected) { |
|
537 |
Throwable t = expected.getCause(); |
|
538 |
if (t instanceof ExecutionException) |
|
539 |
t = t.getCause(); |
|
540 |
||
541 |
if (t instanceof SecurityException) |
|
542 |
System.out.println("Caught expected SE:" + expected); |
|
543 |
else |
|
544 |
fail("Expected SecurityException, but got: " + t); |
|
545 |
} |
|
546 |
} |
|
547 |
||
548 |
/** |
|
549 |
* A Proxy Selector that wraps a ProxySelector.of(), and counts the number |
|
550 |
* of times its select method has been invoked. This can be used to ensure |
|
551 |
* that the Proxy Selector is invoked only once per WebSocket.Builder::buildAsync |
|
552 |
* invocation. |
|
553 |
*/ |
|
554 |
static class CountingProxySelector extends ProxySelector { |
|
555 |
private final ProxySelector proxySelector; |
|
556 |
private volatile int count; // 0 |
|
557 |
private CountingProxySelector(InetSocketAddress proxyAddress) { |
|
558 |
proxySelector = ProxySelector.of(proxyAddress); |
|
559 |
} |
|
560 |
||
561 |
public static CountingProxySelector of(InetSocketAddress proxyAddress) { |
|
562 |
return new CountingProxySelector(proxyAddress); |
|
563 |
} |
|
564 |
||
565 |
int count() { return count; } |
|
566 |
||
567 |
@Override |
|
568 |
public List<Proxy> select(URI uri) { |
|
569 |
System.out.println("PS: uri"); |
|
570 |
Throwable t = new Throwable(); |
|
571 |
t.printStackTrace(System.out); |
|
572 |
count++; |
|
573 |
return proxySelector.select(uri); |
|
574 |
} |
|
575 |
||
576 |
@Override |
|
577 |
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { |
|
578 |
proxySelector.connectFailed(uri, sa, ioe); |
|
579 |
} |
|
580 |
} |
|
581 |
} |