Skip to content

Commit c5a1955

Browse files
ppkarwaszvy
andauthored
fix: Add resource: protocol to allowed URL schemes by default (#3795)
* fix: Add `resource:` protocol to allowed URL schemes by default This update includes `resource:` in the list of allowed URL schemes for retrieving configuration files. See [`log4j2.configurationAllowedProtocols`](https://logging.apache.org/log4j/2.x/manual/systemproperties.html#log4j2.configurationAllowedProtocols) Currently, the `resource:` protocol is used exclusively by a `URLStreamHandler` that retrieves files from the embedded resources in a GraalVM native image. This makes it a secure and appropriate source for trusted configuration files. This change cannot be easily and reliably tested through a unit test. An integration test will be provided in apache/logging-log4j-samples#345 Closes #3790 * fix: Add `resource` protocol only in native images This change introduces an internal `SystemUtils.isGraalVm()` method to detect the presence of GraalVM and enable the `resource` protocol. * Reword changelog entry --------- Co-authored-by: Volkan Yazıcı <[email protected]>
1 parent cab8ae8 commit c5a1955

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

log4j-core/src/main/java/org/apache/logging/log4j/core/net/UrlConnectionFactory.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.logging.log4j.core.net.ssl.SslConfiguration;
3434
import org.apache.logging.log4j.core.net.ssl.SslConfigurationFactory;
3535
import org.apache.logging.log4j.core.util.AuthorizationProvider;
36+
import org.apache.logging.log4j.core.util.internal.SystemUtils;
3637
import org.apache.logging.log4j.util.PropertiesUtil;
3738
import org.apache.logging.log4j.util.Strings;
3839

@@ -51,7 +52,24 @@ public class UrlConnectionFactory {
5152
private static final String HTTP = "http";
5253
private static final String HTTPS = "https";
5354
private static final String JAR = "jar";
54-
private static final String DEFAULT_ALLOWED_PROTOCOLS = "https, file, jar";
55+
/**
56+
* Default list of protocols that are allowed to be used for configuration files and other trusted resources.
57+
* <p>
58+
* By default, we trust the following protocols:
59+
* <dl>
60+
* <dt>file</dt>
61+
* <dd>Local files</dd>
62+
* <dt>https</dt>
63+
* <dd>Resources retrieved through TLS to guarantee their integrity</dd>
64+
* <dt>jar</dt>
65+
* <dd>Resources retrieved from JAR files</dd>
66+
* <dt>resource</dt>
67+
* <dd>Resources embedded in a GraalVM native image</dd>
68+
* </dl>
69+
*/
70+
private static final String DEFAULT_ALLOWED_PROTOCOLS =
71+
SystemUtils.isGraalVm() ? "file, https, jar, resource" : "file, https, jar";
72+
5573
private static final String NO_PROTOCOLS = "_none";
5674
public static final String ALLOWED_PROTOCOLS = "log4j2.Configuration.allowedProtocols";
5775

log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/SystemUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,21 @@ public static boolean isOsAndroid() {
3636
return getJavaVendor().contains("Android");
3737
}
3838

39+
/**
40+
* Checks if the current runtime is GraalVM.
41+
* <p>
42+
* See <a href="https://www.graalvm.org/sdk/javadoc/org/graalvm/nativeimage/ImageInfo.html#PROPERTY_IMAGE_CODE_KEY">ImageInfo.PROPERTY_IMAGE_CODE_KEY</a>.
43+
* </p>
44+
* @return true if the current runtime is GraalVM, false otherwise.
45+
*/
46+
public static boolean isGraalVm() {
47+
try {
48+
return System.getProperty("org.graalvm.nativeimage.imagecode") != null;
49+
} catch (final SecurityException e) {
50+
LOGGER.debug("Unable to determine if the current runtime is GraalVM.", e);
51+
return false;
52+
}
53+
}
54+
3955
private SystemUtils() {}
4056
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns="https://logging.apache.org/xml/ns"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="
5+
https://logging.apache.org/xml/ns
6+
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
7+
type="fixed">
8+
<issue id="3790" link="https://github.com/apache/logging-log4j2/issues/3790"/>
9+
<description format="asciidoc">
10+
Allow `resource:` protocol for configuration files by default, if the current runtime is GraalVM.
11+
</description>
12+
</entry>

src/site/antora/modules/ROOT/partials/manual/systemproperties/properties-transport-security.adoc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,17 @@
2121
2222
[cols="1h,5"]
2323
|===
24-
| Env. variable | `LOG4J_CONFIGURATION_ALLOWED_PROTOCOLS`
25-
| Type | Comma-separated list of https://docs.oracle.com/javase/{java-target-version}/docs/api/java/net/URL.html[`URL`] protocols
26-
| Default value | `file, https, jar`
24+
| Env. variable
25+
| `LOG4J_CONFIGURATION_ALLOWED_PROTOCOLS`
26+
27+
| Type
28+
| Comma-separated list of https://docs.oracle.com/javase/{java-target-version}/docs/api/java/net/URL.html[`URL`] protocols
29+
30+
| Default value
31+
|
32+
`file, https, jar` (JVM)
33+
34+
`file, https, jar, resource` (GraalVM)
2735
|===
2836
2937
A comma separated list of https://docs.oracle.com/javase/{java-target-version}/docs/api/java/net/URL.html[`URL`] protocols that may be used to load any kind of configuration source.

0 commit comments

Comments
 (0)