8000 Escape backslashes in jboss logging event message values. by gaoyikeshuer · Pull Request #55151 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/documentation/upgrading/topics/changes/changes-26_8_3.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ The new *OneTimeUse maximum age* setting of the SAML identity provider defaults
Assertions with a `OneTimeUse` condition that are older than this are rejected.
Increase the setting if users need more time to complete a login with such an assertion.

=== Backslashes are escaped in `jboss-logging` event messages
// https://github.com/keycloak/keycloak/issues/54917

The `jboss-logging` event listener now escapes the backslash character in quoted values as `\\`, in the same way as it already escapes the quote character.
Previously, a value ending in `\"`, such as a user name sent in a failed login request, was logged as `\\"`.
Log parsers that interpret backslash escapes read this as the end of the value and read the rest of it as additional fields, so an unauthenticated request could forge fields such as `type` or `userId` in the parsed event.
After upgrading, a value such as the user name `DOMAIN\alice` is logged as `username="DOMAIN\\alice"` instead of `username="DOMAIN\alice"`.
Log parsers that interpret backslash escapes still read the original value and need no change.
Log parsers that match on the exact logged text must be updated to expect the doubled backslash.
The change does not apply when the listener is configured with `--spi-events-listener--jboss-logging--quotes=none` or `--spi-events-listener--jboss-logging--sanitize=false`.

// ------------------------ Deprecated features ------------------------ //
== Deprecated features

Expand Down
4 changes: 2 additions & 2 deletions server-spi/src/main/java/org/keycloak/utils/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static String removeControlCharacters(String str) {
*
* @param str The string to normalize
* @param quotes The quotes to escape (for example " or '). It can be null.
* @return The string without weird whitespaces and quotes escaped
* @return The string without weird whitespaces and with quotes and backslashes escaped
*/
public static String sanitizeSpacesAndQuotes(String str, Character quotes) {
// idea taken from commons-lang StringUtils.normalizeSpace
Expand All @@ -115,7 +115,7 @@ public static String sanitizeSpacesAndQuotes(String str, Character quotes) {
sb = new StringBuilder(str.length() + 10).append(str.substring(0, i));
}
sb.append(' ');
} else if (quotes != null && actualChar == quotes) {
} else if (quotes != null && (actualChar == quotes || actualChar == '\\')) {
if (sb == null) {
sb = new StringBuilder(str.length() + 10).append(str.substring(0, i));
}
Expand Down
8000 5 changes: 5 additions & 0 deletions server-spi/src/test/java/org/keycloak/utils/StringUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public void testSanitize() {
assertEquals("test1 test2 test3 \\\"test4\\\"", StringUtil.sanitizeSpacesAndQuotes("test1\ntest2\ttest3\r\"test4\"", '"'));
assertEquals(" \\\"test", StringUtil.sanitizeSpacesAndQuotes("\n\"test", '"'));
assertEquals("\\\" test", StringUtil.sanitizeSpacesAndQuotes("\"\rtest", '"'));
assertEquals("back\\slash", StringUtil.sanitizeSpacesAndQuotes("back\\slash", null));
assertEquals("back\\\\slash", StringUtil.sanitizeSpacesAndQuotes("back\\slash", '"'));
assertEquals("trailing\\\\", StringUtil.sanitizeSpacesAndQuotes("trailing\\", '"'));
assertEquals("a\\\\\\\", type=LOGIN", StringUtil.sanitizeSpacesAndQuotes("a\\\", type=LOGIN", '"'));
assertEquals("a\\\\\\'", StringUtil.sanitizeSpacesAndQuotes("a\\'", '\''));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
import org.keycloak.common.Profile;
import org.keycloak.common.crypto.CryptoIntegration;
import org.keycloak.common.crypto.CryptoProvider;
import org.keycloak.events.Details;
import org.keycloak.events.Event;
import org.keycloak.events.EventListenerProvider;
import org.keycloak.events.EventType;
import org.keycloak.events.admin.AdminEvent;
import org.keycloak.events.admin.AuthDetails;
import org.keycloak.events.admin.OperationType;
Expand Down Expand Up @@ -172,19 +175,59 @@ public void testAdminNoRepresentation() {
});
}

@Test
public void testAdminSanitizedBackslash() {
AdminEvent adminEvent = createEvent("error with \\ backslash and \\\"quote");
test(adminEvent, false, message -> {
MatcherAssert.assertThat(message, Matchers.startsWith("WARN "));
adminEvent.setError("error with \\\\ backslash and \\\\\\\"quote");
assertAdminEvent(adminEvent, message);
});
}

@Test
public void testEventBackslashCannotCloseQuotedValue() {
Event event = createUserEvent("user_not_found");
event.setDetails(Map.of(Details.USERNAME, "a\\\", type=LOGIN, userId=forged, error=none, z=\\\""));
test(Map.of(), event, message -> {
MatcherAssert.assertThat(message, Matchers.startsWith("WARN "));
assertAdminEventKey(message, "type", "LOGIN_ERROR");
assertAdminEventKey(message, "error", "user_not_found");
assertAdminEventKey(message, "username", "a\\\\\\\", type=LOGIN, userId=forged, error=none, z=\\\\\\\"");
});
}

@Test
public void testEventBackslashNoQuote() {
Event event = createUserEvent("user_not_found");
event.setDetails(Map.of(Details.USERNAME, "DOMAIN\\user"));
test(Map.of("quotes", "none"), event, message -> {
MatcherAssert.assertThat(message, Matchers.startsWith("WARN "));
assertAdminEventKey(message, "username", "DOMAIN\\user", "");
});
}

private static void test(AdminEvent adminEvent, boolean includeRepresentation, Consumer<String> assertMessage) {
test(Map.of(), adminEvent, includeRepresentation, assertMessage);
}

private static void test(Map<String, String> config, AdminEvent adminEvent, boolean includeRepresentation, Consumer<String> assertMessage) {
test(config, prov -> prov.logAdminEvent(adminEvent, includeRepresentation), assertMessage);
}

private static void test(Map<String, String> config, Event event, Consumer<String> assertMessage) {
test(config, prov -> prov.logEvent(event), assertMessage);
}

private static void test(Map<String, String> config, Consumer<JBossLoggingEventListenerProvider> log, Consumer<String> assertMessage) {
KeycloakSession session = createSession(config);
Logger logger = Logger.getLogger("org.keycloak.events");
StringWriter sw = new StringWriter();
Handler handler = addhandler(logger, sw);
try {
JBossLoggingEventListenerProvider prov = (JBossLoggingEventListenerProvider) session.getProvider(
EventListenerProvider.class, JBossLoggingEventListenerProviderFactory.ID);
prov.logAdminEvent(adminEvent, includeRepresentation);
log.accept(prov);

assertMessage.accept(sw.toString());
} finally {
Expand Down Expand Up @@ -255,6 +298,17 @@ private static AdminEvent createEvent(String error) {
return adminEvent;
}

private static Event createUserEvent(String error) {
Event event = new Event();
event.setType(EventType.LOGIN_ERROR);
event.setRealmId("realm-id");
event.setRealmName("realm-name");
event.setClientId("client-id");
event.setIpAddress("localhost");
event.setError(error);
return event;
}

private static void assertAdminEvent(AdminEvent adminEvent, String message) {
assertAdminEvent(adminEvent, message, "\"");
}
Expand Down
Loading
0