Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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> 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));
}

}

}
Loading