Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
20 changes: 10 additions & 10 deletions LICENSE-binary
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,16 @@ jackson-module-jaxb-annotations-2.12.6
jackson-module-scala_2.13-2.12.6
jakarta.validation-api-2.0.2
javassist-3.27.0-GA
jetty-client-9.4.44.v20210927
jetty-continuation-9.4.44.v20210927
jetty-http-9.4.44.v20210927
jetty-io-9.4.44.v20210927
jetty-security-9.4.44.v20210927
jetty-server-9.4.44.v20210927
jetty-servlet-9.4.44.v20210927
jetty-servlets-9.4.44.v20210927
jetty-util-9.4.44.v20210927
jetty-util-ajax-9.4.44.v20210927
jetty-client-9.4.48.v20220622
jetty-continuation-9.4.48.v20220622
jetty-http-9.4.48.v20220622
jetty-io-9.4.48.v20220622
jetty-security-9.4.48.v20220622
jetty-server-9.4.48.v20220622
jetty-servlet-9.4.48.v20220622
jetty-servlets-9.4.48.v20220622
jetty-util-9.4.48.v20220622
jetty-util-ajax-9.4.48.v20220622
jersey-common-2.34
jersey-server-2.34
log4j-1.2.17
Expand Down
6 changes: 6 additions & 0 deletions checkstyle/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
files="(Fetcher|Sender|SenderTest|ConsumerCoordinator|KafkaConsumer|KafkaProducer|Utils|TransactionManager|TransactionManagerTest|KafkaAdminClient|NetworkClient|Admin|KafkaRaftClient|KafkaRaftClientTest|RaftClientTestContext).java"/>
<suppress checks="ClassFanOutComplexity"
files="(SaslServerAuthenticator|SaslAuthenticatorTest).java"/>
<suppress checks="NPath"
files="SaslServerAuthenticator.java"/>
<suppress checks="ClassFanOutComplexity"
files="Errors.java"/>
<suppress checks="ClassFanOutComplexity"
Expand Down Expand Up @@ -150,6 +152,10 @@
<suppress checks="JavaNCSS"
files="DistributedHerderTest.java"/>

<!-- Raft -->
<suppress checks="NPathComplexity"
files="RecordsIterator.java"/>

<!-- Streams -->
<suppress checks="ClassFanOutComplexity"
files="(KafkaStreams|KStreamImpl|KTableImpl|StreamsPartitionAssignor).java"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class BrokerSecurityConfigs {
public static final String SASL_SERVER_CALLBACK_HANDLER_CLASS = "sasl.server.callback.handler.class";
public static final String SSL_PRINCIPAL_MAPPING_RULES_CONFIG = "ssl.principal.mapping.rules";
public static final String CONNECTIONS_MAX_REAUTH_MS = "connections.max.reauth.ms";
public static final int DEFAULT_SASL_SERVER_MAX_RECEIVE_SIZE = 524288;
public static final String SASL_SERVER_MAX_RECEIVE_SIZE_CONFIG = "sasl.server.max.receive.size";

public static final String PRINCIPAL_BUILDER_CLASS_DOC = "The fully qualified name of a class that implements the " +
"KafkaPrincipalBuilder interface, which is used to build the KafkaPrincipal object used during " +
Expand Down Expand Up @@ -89,4 +91,8 @@ public class BrokerSecurityConfigs {
+ "The broker will disconnect any such connection that is not re-authenticated within the session lifetime and that is then subsequently "
+ "used for any purpose other than re-authentication. Configuration names can optionally be prefixed with listener prefix and SASL "
+ "mechanism name in lower-case. For example, listener.name.sasl_ssl.oauthbearer.connections.max.reauth.ms=3600000";

public static final String SASL_SERVER_MAX_RECEIVE_SIZE_DOC = "The maximum receive size allowed before and during initial SASL authentication." +
" Default receive size is 512KB. GSSAPI limits requests to 64K, but we allow upto 512KB by default for custom SASL mechanisms. In practice," +
" PLAIN, SCRAM and OAUTH mechanisms can use much smaller limits.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,15 @@ public double readDouble() {
}

@Override
public void readArray(byte[] arr) {
public byte[] readArray(int size) {
int remaining = buf.remaining();
if (size > remaining) {
throw new RuntimeException("Error reading byte array of " + size + " byte(s): only " + remaining +
" byte(s) available");
}
byte[] arr = new byte[size];
buf.get(arr);
return arr;
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,23 @@ public interface Readable {
int readInt();
long readLong();
double readDouble();
void readArray(byte[] arr);
byte[] readArray(int length);
int readUnsignedVarint();
ByteBuffer readByteBuffer(int length);
int readVarint();
long readVarlong();
int remaining();

default String readString(int length) {
byte[] arr = new byte[length];
readArray(arr);
byte[] arr = readArray(length);
return new String(arr, StandardCharsets.UTF_8);
}

default List<RawTaggedField> readUnknownTaggedField(List<RawTaggedField> unknowns, int tag, int size) {
if (unknowns == null) {
unknowns = new ArrayList<>();
}
byte[] data = new byte[size];
readArray(data);
byte[] data = readArray(size);
unknowns.add(new RawTaggedField(tag, data));
return unknowns;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ private static DefaultRecord readFrom(ByteBuffer buffer,
int numHeaders = ByteUtils.readVarint(buffer);
if (numHeaders < 0)
throw new InvalidRecordException("Found invalid number of record headers " + numHeaders);
if (numHeaders > buffer.remaining())
throw new InvalidRecordException("Found invalid number of record headers. " + numHeaders + " is larger than the remaining size of the buffer");

final Header[] headers;
if (numHeaders == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.kafka.common.errors.UnsupportedVersionException;
import org.apache.kafka.common.message.SaslAuthenticateResponseData;
import org.apache.kafka.common.message.SaslHandshakeResponseData;
import org.apache.kafka.common.network.InvalidReceiveException;
import org.apache.kafka.common.network.Authenticator;
import org.apache.kafka.common.network.ByteBufferSend;
import org.apache.kafka.common.network.ChannelBuilders;
Expand Down Expand Up @@ -88,8 +89,6 @@
import java.util.function.Supplier;

public class SaslServerAuthenticator implements Authenticator {
// GSSAPI limits requests to 64K, but we allow a bit extra for custom SASL mechanisms
static final int MAX_RECEIVE_SIZE = 524288;
private static final Logger LOG = LoggerFactory.getLogger(SaslServerAuthenticator.class);

/**
Expand Down Expand Up @@ -140,6 +139,7 @@ private enum SaslState {
private String saslMechanism;

// buffers used in `authenticate`
private Integer saslAuthRequestMaxReceiveSize;
private NetworkReceive netInBuffer;
private Send netOutBuffer;
private Send authenticationFailureSend = null;
Expand Down Expand Up @@ -189,6 +189,10 @@ public SaslServerAuthenticator(Map<String, ?> configs,
// Note that the old principal builder does not support SASL, so we do not need to pass the
// authenticator or the transport layer
this.principalBuilder = ChannelBuilders.createPrincipalBuilder(configs, kerberosNameParser, null);

saslAuthRequestMaxReceiveSize = (Integer) configs.get(BrokerSecurityConfigs.SASL_SERVER_MAX_RECEIVE_SIZE_CONFIG);
if (saslAuthRequestMaxReceiveSize == null)
saslAuthRequestMaxReceiveSize = BrokerSecurityConfigs.DEFAULT_SASL_SERVER_MAX_RECEIVE_SIZE;
}

private void createSaslServer(String mechanism) throws IOException {
Expand Down Expand Up @@ -252,9 +256,13 @@ public void authenticate() throws IOException {
}

// allocate on heap (as opposed to any socket server memory pool)
if (netInBuffer == null) netInBuffer = new NetworkReceive(MAX_RECEIVE_SIZE, connectionId);
if (netInBuffer == null) netInBuffer = new NetworkReceive(saslAuthRequestMaxReceiveSize, connectionId);

netInBuffer.readFrom(transportLayer);
try {
netInBuffer.readFrom(transportLayer);
} catch (InvalidReceiveException e) {
throw new SaslAuthenticationException("Failing SASL authentication due to invalid receive size", e);
}
if (!netInBuffer.complete())
return;
netInBuffer.payload().rewind();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.kafka.common.message;

import org.apache.kafka.common.protocol.ByteBufferAccessor;
import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class SimpleArraysMessageTest {
@Test
public void testArrayBoundsChecking() {
// SimpleArraysMessageData takes 2 arrays
final ByteBuffer buf = ByteBuffer.wrap(new byte[] {
(byte) 0x7f, // Set size of first array to 126 which is larger than the size of this buffer
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
});
final SimpleArraysMessageData out = new SimpleArraysMessageData();
ByteBufferAccessor accessor = new ByteBufferAccessor(buf);
assertEquals("Tried to allocate a collection of size 126, but there are only 7 bytes remaining.",
assertThrows(RuntimeException.class, () -> out.read(accessor, (short) 2)).getMessage());
}

@Test
public void testArrayBoundsCheckingOtherArray() {
// SimpleArraysMessageData takes 2 arrays
final ByteBuffer buf = ByteBuffer.wrap(new byte[] {
(byte) 0x01, // Set size of first array to 0
(byte) 0x7e, // Set size of second array to 125 which is larger than the size of this buffer
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
});
final SimpleArraysMessageData out = new SimpleArraysMessageData();
ByteBufferAccessor accessor = new ByteBufferAccessor(buf);
assertEquals("Tried to allocate a collection of size 125, but there are only 6 bytes remaining.",
assertThrows(RuntimeException.class, () -> out.read(accessor, (short) 2)).getMessage());
}
}
Loading