Skip to content

Commit ac29f8f

Browse files
committed
Switch Basic Authentication encoding to UTF-8
1 parent ce3a6de commit ac29f8f

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
@@ -17,13 +17,16 @@
1717
package org.apache.logging.log4j.core.util;
1818

1919
import java.net.URLConnection;
20+
import java.nio.charset.Charset;
21+
import java.util.Base64;
2022

2123
import org.apache.logging.log4j.Logger;
2224
import org.apache.logging.log4j.status.StatusLogger;
23-
import org.apache.logging.log4j.util.Base64Util;
2425
import org.apache.logging.log4j.util.LoaderUtil;
2526
import org.apache.logging.log4j.util.PropertiesUtil;
2627

28+
import static java.nio.charset.StandardCharsets.UTF_8;
29+
2730
/**
2831
* Provides the Basic Authorization header to a request.
2932
*/
@@ -35,6 +38,11 @@ public class BasicAuthorizationProvider implements AuthorizationProvider {
3538
public static final String CONFIG_USER_NAME = "log4j2.configurationUserName";
3639
public static final String CONFIG_PASSWORD = "log4j2.configurationPassword";
3740
public static final String PASSWORD_DECRYPTOR = "log4j2.passwordDecryptor";
41+
/*
42+
* Properties used to specify the encoding in HTTP Basic Authentication
43+
*/
44+
private static final String BASIC_AUTH_ENCODING = "log4j2.configurationAuthorizationEncoding";
45+
private static final String SPRING_BASIC_AUTH_ENCODING = "logging.auth.encoding";
3846

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

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

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/1961[1961])
5051
* Fix MDC pattern converter causing issues for `%notEmpty` (https://github.com/apache/logging-log4j2/issues/1922[1922])
5152
* Fix `NotSerializableException` when `Logger` is serialized with a `ReusableMessageFactory` (https://github.com/apache/logging-log4j2/issues/1884[1884])
5253
* Update `co.elastic.clients:elasticsearch-java` to version `8.11.0` (https://github.com/apache/logging-log4j2/pull/1953[1953])

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)