Skip to content

Commit 6493e94

Browse files
committed
ZOOKEEPER-5058. Remove special characters from ensemble name before logging
1 parent 981a2fd commit 6493e94

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/EnsembleAuthenticationProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ public KeeperException.Code handleAuthentication(ServerCnxn cnxn, byte[] authDat
9292
long currentTime = System.currentTimeMillis();
9393
if (lastFailureLogged + MIN_LOGGING_INTERVAL_MS < currentTime) {
9494
String id = cnxn.getRemoteSocketAddress().getAddress().getHostAddress();
95-
LOG.warn("Unexpected ensemble name: ensemble name: {} client ip: {}", receivedEnsembleName, id);
95+
String logEnsembleName = receivedEnsembleName.replaceAll("[\\x00-\\x1F]", "");
96+
LOG.warn("Unexpected ensemble name: ensemble name: {} client ip: {}", logEnsembleName, id);
9697
lastFailureLogged = currentTime;
9798
}
9899
/*
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.zookeeper.server.auth;
19+
20+
import ch.qos.logback.classic.Level;
21+
import org.apache.zookeeper.KeeperException;
22+
import org.apache.zookeeper.server.ServerCnxn;
23+
import org.apache.zookeeper.test.LoggerTestTool;
24+
import org.junit.jupiter.api.AfterAll;
25+
import org.junit.jupiter.api.Assertions;
26+
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.Test;
28+
29+
import java.io.IOException;
30+
import java.net.InetSocketAddress;
31+
import java.nio.charset.StandardCharsets;
32+
33+
import static org.mockito.Mockito.doReturn;
34+
import static org.mockito.Mockito.mock;
35+
36+
public class EnsembleAuthenticationProviderTest {
37+
private static LoggerTestTool loggerTestTool;
38+
39+
@BeforeAll
40+
public static void setupBeforeClass() {
41+
loggerTestTool = new LoggerTestTool(EnsembleAuthenticationProvider.class, Level.INFO);
42+
}
43+
44+
@AfterAll
45+
public static void tearDownAfterClass() throws Exception {
46+
loggerTestTool.close();
47+
}
48+
49+
@Test
50+
public void testLogForgeryWithSpecialCharacters() throws IOException {
51+
ServerCnxn mockServerCnxn = mock(ServerCnxn.class);
52+
InetSocketAddress mockAddress = new InetSocketAddress("127.0.0.1", 1234);
53+
doReturn(mockAddress).when(mockServerCnxn).getRemoteSocketAddress();
54+
55+
EnsembleAuthenticationProvider provider = new EnsembleAuthenticationProvider();
56+
provider.setEnsembleNames("test-ensemble");
57+
58+
byte[] authData = "andor-ensemble\nTHIS SHOULD\t NOT\r BE HERE".getBytes(StandardCharsets.UTF_8);
59+
KeeperException.Code err = provider.handleAuthentication(mockServerCnxn, authData);
60+
String logLine = loggerTestTool.readLogLine("andor-ensemble");
61+
Assertions.assertTrue(logLine.contains("THIS SHOULD NOT BE HERE"), "Log line doesn't contain the entire ensemble name. Forged?");
62+
63+
Assertions.assertEquals(KeeperException.Code.BADARGUMENTS, err);
64+
}
65+
}

0 commit comments

Comments
 (0)