Skip to content

Commit 9c751f9

Browse files
authored
Security manager test & fix (#3077)
1 parent e6dcd04 commit 9c751f9

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ communication - {pull}2996[#2996]
5252
* Fix exceptions filtering based on <<config-ignore-exceptions>> when those are <<config-unnest-exceptions, nested>> - {pull}3025[#3025]
5353
* Fix usage of `HttpUrlConnection.getResponseCode()` causing an error event due to exception capturing, even when it is internally handled - {pull}3024[#3024]
5454
* Fix source code jar to contain apm-agent sources - {pull}3063[#3063]
55+
* Fix security exception when security manager is used with `log_level=debug` - {pull}3077[#3077]
5556
5657
[[release-notes-1.x]]
5758
=== Java Agent version 1.x

apm-agent-core/src/main/java/co/elastic/apm/agent/util/PrivilegedActionUtils.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.FileInputStream;
2424
import java.io.FileNotFoundException;
2525
import java.io.IOException;
26+
import java.net.ProxySelector;
2627
import java.nio.file.Files;
2728
import java.nio.file.Path;
2829
import java.security.AccessController;
@@ -62,6 +63,35 @@ public Map<String, String> run() {
6263
});
6364
}
6465

66+
67+
@Nullable
68+
public static String getProperty(final String name) {
69+
if (System.getSecurityManager() == null) {
70+
return System.getProperty(name);
71+
}
72+
73+
return AccessController.doPrivileged(new PrivilegedAction<String>() {
74+
@Override
75+
public String run() {
76+
return System.getProperty(name);
77+
}
78+
});
79+
}
80+
81+
@Nullable
82+
public static ProxySelector getDefaultProxySelector() {
83+
if (System.getSecurityManager() == null) {
84+
return ProxySelector.getDefault();
85+
}
86+
87+
return AccessController.doPrivileged(new PrivilegedAction<ProxySelector>() {
88+
@Override
89+
public ProxySelector run() {
90+
return ProxySelector.getDefault();
91+
}
92+
});
93+
}
94+
6595
@Nullable
6696
public static ClassLoader getClassLoader(final Class<?> type) {
6797
if (System.getSecurityManager() == null) {

apm-agent-core/src/main/java/co/elastic/apm/agent/util/UrlConnectionUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ public static URLConnection openUrlConnectionThreadSafely(URL url) throws IOExce
4646
}
4747

4848
private static void debugPrintProxySettings(URL url) {
49-
ProxySelector proxySelector = ProxySelector.getDefault();
49+
ProxySelector proxySelector = PrivilegedActionUtils.getDefaultProxySelector();
5050
if (proxySelector == null || proxySelector.getClass().getName().equals("sun.net.spi.DefaultProxySelector")) {
5151
String proxyHostProperty = url.getProtocol() + ".proxyHost";
5252
String proxyPortProperty = url.getProtocol() + ".proxyPort";
53-
String proxyHost = System.getProperty(proxyHostProperty);
54-
String proxyPort = System.getProperty(proxyPortProperty);
55-
String nonProxyHosts = System.getProperty("http.nonProxyHosts"); // common to http & https
53+
String proxyHost = PrivilegedActionUtils.getProperty(proxyHostProperty);
54+
String proxyPort = PrivilegedActionUtils.getProperty(proxyPortProperty);
55+
String nonProxyHosts = PrivilegedActionUtils.getProperty("http.nonProxyHosts"); // common to http & https
5656
if (proxyHost == null || proxyHost.isEmpty()) {
5757
logger.debug("Opening {} without proxy", url);
5858
} else {

apm-agent-core/src/test/java/co/elastic/apm/agent/util/PrivilegedActionUtilsTest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void getEnv() {
6060
String envKey = envMap.keySet().stream().findFirst().get();
6161
String envValue = envMap.get(envKey);
6262

63-
testWithAndWithoutSecurityManager(()->{
63+
testWithAndWithoutSecurityManager(() -> {
6464
assertThat(PrivilegedActionUtils.getEnv(envKey)).isEqualTo(envValue);
6565
assertThat(PrivilegedActionUtils.getEnv()).containsAllEntriesOf(envMap);
6666
});
@@ -70,13 +70,13 @@ void getEnv() {
7070
@Test
7171
void getClassLoader() {
7272
ClassLoader cl = PrivilegedActionUtilsTest.class.getClassLoader();
73-
testWithAndWithoutSecurityManager(()-> assertThat(PrivilegedActionUtils.getClassLoader(PrivilegedActionUtilsTest.class)).isSameAs(cl));
73+
testWithAndWithoutSecurityManager(() -> assertThat(PrivilegedActionUtils.getClassLoader(PrivilegedActionUtilsTest.class)).isSameAs(cl));
7474
}
7575

7676
@Test
7777
void getProtectionDomain() {
7878
ProtectionDomain pd = PrivilegedActionUtilsTest.class.getProtectionDomain();
79-
testWithAndWithoutSecurityManager(()-> assertThat(PrivilegedActionUtils.getProtectionDomain(PrivilegedActionUtilsTest.class)).isSameAs(pd));
79+
testWithAndWithoutSecurityManager(() -> assertThat(PrivilegedActionUtils.getProtectionDomain(PrivilegedActionUtilsTest.class)).isSameAs(pd));
8080
}
8181

8282
@Test
@@ -161,7 +161,12 @@ void createDirectories(@TempDir Path tempDir) throws IOException {
161161
});
162162
}
163163

164-
private static void testPrivileged(Runnable task){
164+
@Test
165+
void getProxySelector() {
166+
testWithAndWithoutSecurityManager(PrivilegedActionUtils::getDefaultProxySelector);
167+
}
168+
169+
private static void testPrivileged(Runnable task) {
165170
AccessController.doPrivileged(new PrivilegedAction<Object>() {
166171
@Override
167172
public Object run() {
@@ -171,7 +176,7 @@ public Object run() {
171176
});
172177
}
173178

174-
void testWithAndWithoutSecurityManager(Runnable assertions){
179+
void testWithAndWithoutSecurityManager(Runnable assertions) {
175180
assertions.run();
176181
try {
177182
enableSecurityManager();
@@ -209,7 +214,7 @@ public void checkPermission(Permission perm, Object context) {
209214
private static void checkPrivileged() {
210215
StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
211216
for (StackTraceElement e : stackTrace) {
212-
if(e.getClassName().equals("java.security.AccessController") && e.getMethodName().equals("doPrivileged")){
217+
if (e.getClassName().equals("java.security.AccessController") && e.getMethodName().equals("doPrivileged")) {
213218
return;
214219
}
215220
}

0 commit comments

Comments
 (0)