Skip to content

Commit 819b738

Browse files
committed
Merge remote-tracking branch ppkarwasz/basic-auth into 2.x (#1970)
2 parents bbf94c4 + ac29f8f commit 819b738

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
*/
1717
package org.apache.logging.log4j.core.util;
1818

19+
import static java.nio.charset.StandardCharsets.UTF_8;
20+
1921
import java.net.URLConnection;
22+
import java.nio.charset.Charset;
23+
import java.util.Base64;
2024
import org.apache.logging.log4j.Logger;
2125
import org.apache.logging.log4j.status.StatusLogger;
22-
import org.apache.logging.log4j.util.Base64Util;
2326
import org.apache.logging.log4j.util.LoaderUtil;
2427
import org.apache.logging.log4j.util.PropertiesUtil;
2528

@@ -34,6 +37,11 @@ public class BasicAuthorizationProvider implements AuthorizationProvider {
3437
public static final String CONFIG_USER_NAME = "log4j2.configurationUserName";
3538
public static final String CONFIG_PASSWORD = "log4j2.configurationPassword";
3639
public static final String PASSWORD_DECRYPTOR = "log4j2.passwordDecryptor";
40+
/*
41+
* Properties used to specify the encoding in HTTP Basic Authentication
42+
*/
43+
private static final String BASIC_AUTH_ENCODING = "log4j2.configurationAuthorizationEncoding";
44+
private static final String SPRING_BASIC_AUTH_ENCODING = "logging.auth.encoding";
3745

3846
private static final Logger LOGGER = StatusLogger.getLogger();
3947

@@ -46,6 +54,11 @@ public BasicAuthorizationProvider(final PropertiesUtil props) {
4654
props.getStringProperty(PREFIXES, AUTH_PASSWORD, () -> props.getStringProperty(CONFIG_PASSWORD));
4755
final String decryptor = props.getStringProperty(
4856
PREFIXES, AUTH_PASSWORD_DECRYPTOR, () -> props.getStringProperty(PASSWORD_DECRYPTOR));
57+
// Password encoding
58+
Charset passwordCharset = props.getCharsetProperty(BASIC_AUTH_ENCODING);
59+
if (passwordCharset == null) {
60+
props.getCharsetProperty(SPRING_BASIC_AUTH_ENCODING, UTF_8);
61+
}
4962
if (decryptor != null) {
5063
try {
5164
final Object obj = LoaderUtil.newInstanceOf(decryptor);
@@ -57,7 +70,13 @@ public BasicAuthorizationProvider(final PropertiesUtil props) {
5770
}
5871
}
5972
if (userName != null && password != null) {
60-
authString = "Basic " + Base64Util.encode(userName + ":" + password);
73+
/*
74+
* https://datatracker.ietf.org/doc/html/rfc7617#appendix-B
75+
*
76+
* If the user didn't specify a charset to use, we fallback to UTF-8
77+
*/
78+
authString = "Basic "
79+
+ Base64.getEncoder().encodeToString((userName + ":" + password).getBytes(passwordCharset));
6180
}
6281
}
6382

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://logging.apache.org/log4j/changelog"
4+
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.2.xsd"
5+
type="changed">
6+
<issue id="1970" link="https://github.com/apache/logging-log4j2/issues/1970"/>
7+
<description format="asciidoc">
8+
Change default encoding of HTTP Basic Authentication to UTF-8 and add `log4j2.configurationAuthorizationEncoding` property to overwrite it.
9+
</description>
10+
</entry>

src/site/_release-notes/_2.x.x.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The module name of four bridges (`log4j-slf4j-impl`, `log4j-slf4j2-impl`, `log4j
4747
=== Changed
4848
4949
* Change the order of evaluation of `FormattedMessage` formatters. Messages are evaluated using `java.util.Format` only if they don't comply to the `java.text.MessageFormat` or `ParameterizedMessage` format. (https://github.com/apache/logging-log4j2/issues/1223[1223])
50+
* Change default encoding of HTTP Basic Authentication to UTF-8 and add `log4j2.configurationAuthorizationEncoding` property to overwrite it. (https://github.com/apache/logging-log4j2/issues/1970[1970])
5051
* Update `com.fasterxml.jackson:jackson-bom` to version `2.16.0` (https://github.com/apache/logging-log4j2/pull/1974[1974])
5152
* Update `com.github.luben:zstd-jni` to version `1.5.5-10` (https://github.com/apache/logging-log4j2/pull/1940[1940])
5253
* Update `com.google.guava:guava` to version `32.1.3-jre` (https://github.com/apache/logging-log4j2/pull/1875[1875])

src/site/markdown/log4j-spring-cloud-config-client.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ the alternatives may be used in any configuration location.
6666
|----------|---------|---------|---------|
6767
| log4j2.configurationUserName | log4j2.config.username | logging.auth.username | User name for basic authentication |
6868
| log4j2.configurationPassword | log4j2.config.password | logging.auth.password | Password for basic authentication |
69-
| log4j2.authorizationProvider | log4j2.config.authorizationProvider | logging.auth.authorizationProvider | Class used to create HTTP Authorization header |
69+
| log4j2.configurationAuthorizationEncoding | | logging.auth.encoding | Encoding for basic authentication (defaults to UTF-8) |
70+
| log4j2.configurationAuthorizationProvider | log4j2.config.authorizationProvider | logging.auth.authorizationProvider | Class used to create HTTP Authorization header |
7071

7172
```
7273
log4j2.configurationUserName=guest

src/site/xdoc/manual/configuration.xml.vm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,6 +2127,14 @@ public class AwesomeTest {
21272127
"https, file, jar". To completely prevent accessing the configuration via a URL specify a value of "_none".
21282128
</td>
21292129
</tr>
2130+
<tr>
2131+
<td><a name="log4j2.configurationAuthorizationEncoding"/>log4j2.configurationAuthorizationEncoding</td>
2132+
<td>LOG4J_CONFIGURATION_AUTHORIZATION_ENCODING</td>
2133+
<td>UTF-8</td>
2134+
<td>
2135+
The encoding used in Basic Authentication (cf. <a href="https://datatracker.ietf.org/doc/html/rfc7617">RFC 7617</a>).
2136+
</td>
2137+
</tr>
21302138
<tr>
21312139
<td><a name="configurationAuthorizationProvider"/>log4j2.Configuration.authorizationProvider
21322140
<br />

0 commit comments

Comments
 (0)