diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/DigestAuthenticationProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/DigestAuthenticationProvider.java index 6e2687521d2..0d8430ae65a 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/DigestAuthenticationProvider.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/DigestAuthenticationProvider.java @@ -18,7 +18,6 @@ package org.apache.zookeeper.server.auth; -import static java.nio.charset.StandardCharsets.UTF_8; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache.zookeeper.KeeperException; @@ -119,7 +118,9 @@ public static String generateDigest(String idPassword) throws NoSuchAlgorithmExc // @VisibleForTesting public static byte[] digest(String idPassword) throws NoSuchAlgorithmException { - return MessageDigest.getInstance(DIGEST_ALGORITHM).digest(idPassword.getBytes(UTF_8)); + // Platform default charset, as in branch-3.6: persisted digest ACL ids were hashed this way, so a fixed + // charset would stop non-ASCII credentials from matching them on hosts whose default charset is not UTF-8. + return MessageDigest.getInstance(DIGEST_ALGORITHM).digest(idPassword.getBytes()); } public KeeperException.Code handleAuthentication(ServerCnxn cnxn, byte[] authData) { diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/auth/DigestAuthenticationProviderCharsetTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/auth/DigestAuthenticationProviderCharsetTest.java new file mode 100644 index 00000000000..73d5ec09095 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/auth/DigestAuthenticationProviderCharsetTest.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.zookeeper.server.auth; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.security.MessageDigest; +import java.util.Base64; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.server.ServerCnxn; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +/** + * Digest ACL ids must be hashed with the platform default charset, as in branch-3.6, so that ids persisted by 3.6 + * keep matching after an upgrade on hosts whose default charset is not UTF-8. A JVM that defaults to UTF-8 cannot + * tell the two apart, so the check runs in a child JVM started with file.encoding=US-ASCII. + */ +public class DigestAuthenticationProviderCharsetTest { + + private static final String RESULT_PREFIX = "RESULT:"; + + @Test + public void testNonAsciiCredentialHashedWithPlatformCharset() throws Exception { + String java = System.getProperty("java.home") + "/bin/java"; + Process child = new ProcessBuilder(java, "-Dfile.encoding=US-ASCII", "-cp", System.getProperty("java.class.path"), + Child.class.getName()).redirectErrorStream(true).start(); + String result = null; + try (BufferedReader out = new BufferedReader(new InputStreamReader(child.getInputStream(), UTF_8))) { + String line; + while ((line = out.readLine()) != null) { + if (line.startsWith(RESULT_PREFIX)) { + result = line.substring(RESULT_PREFIX.length()); + } + } + } + assertTrue(child.waitFor(60, TimeUnit.SECONDS), "child JVM did not exit"); + assertEquals(0, child.exitValue()); + + String[] parts = result.split(",", 2); + // A UTF-8 client sends the two UTF-8 bytes of the u-umlaut; the US-ASCII server decodes them to two + // replacement characters, which US-ASCII encodes as '?'. + assertEquals("US-ASCII", parts[0]); + byte[] expected = MessageDigest.getInstance("SHA1").digest("??:pw".getBytes(UTF_8)); + assertEquals(Base64.getEncoder().encodeToString(expected), parts[1]); + } + + public static class Child { + + public static void main(String[] args) throws Exception { + ServerCnxn cnxn = mock(ServerCnxn.class); + KeeperException.Code code = new DigestAuthenticationProvider().handleAuthentication(cnxn, "\u00fc:pw".getBytes(UTF_8)); + if (code != KeeperException.Code.OK) { + throw new IllegalStateException("authentication failed: " + code); + } + ArgumentCaptor id = ArgumentCaptor.forClass(Id.class); + verify(cnxn).addAuthInfo(id.capture()); + String digestId = id.getValue().getId(); + System.out.println(RESULT_PREFIX + Charset.defaultCharset().name() + "," + digestId.substring(digestId.lastIndexOf(':') + 1)); + } + + } + +}