Skip to content

Commit 484d9a8

Browse files
devhritwikclaude
andcommitted
co-pilot comments
- ProxyCustomizer: fail closed when peer address cannot be evaluated (non-InetSocketAddress or null InetAddress) instead of trusting PROXY data - CidrRange: use Guava InetAddresses.forString() instead of InetAddress.getByName() to reject hostnames and avoid DNS resolution during config parsing - CidrRange: trim address and prefix components before parsing Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9e60795 commit 484d9a8

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

core/src/main/java/io/confluent/rest/customizer/CidrRange.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616

1717
package io.confluent.rest.customizer;
1818

19+
import com.google.common.net.InetAddresses;
20+
1921
import org.apache.kafka.common.config.ConfigException;
2022

2123
import java.net.InetAddress;
22-
import java.net.UnknownHostException;
2324

2425
/**
2526
* Represents a CIDR range (e.g., "10.240.0.0/16") and provides a method to check
@@ -48,18 +49,22 @@ public static CidrRange parse(String cidr) {
4849
throw new ConfigException("Invalid CIDR notation: " + cidr);
4950
}
5051

52+
String addressStr = parts[0].trim();
53+
String prefixStr = parts[1].trim();
54+
5155
InetAddress network;
5256
try {
53-
network = InetAddress.getByName(parts[0]);
54-
} catch (UnknownHostException e) {
55-
throw new ConfigException("Invalid network address in CIDR: " + parts[0]);
57+
// Use InetAddresses.forString() to reject hostnames and avoid DNS resolution.
58+
network = InetAddresses.forString(addressStr);
59+
} catch (IllegalArgumentException e) {
60+
throw new ConfigException("Invalid network address in CIDR: " + addressStr);
5661
}
5762

5863
int prefixLength;
5964
try {
60-
prefixLength = Integer.parseInt(parts[1]);
65+
prefixLength = Integer.parseInt(prefixStr);
6166
} catch (NumberFormatException e) {
62-
throw new ConfigException("Invalid prefix length in CIDR: " + parts[1]);
67+
throw new ConfigException("Invalid prefix length in CIDR: " + prefixStr);
6368
}
6469

6570
byte[] networkBytes = network.getAddress();

core/src/main/java/io/confluent/rest/customizer/ProxyCustomizer.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,32 @@ public Request customize(Request request, HttpFields.Mutable mutable) {
8888
if (endPoint instanceof ProxyConnectionFactory.ProxyEndPoint proxyEndPoint) {
8989
EndPoint underlyingEndpoint = proxyEndPoint.unwrap();
9090

91-
// Check peer IP against accepted range before using PROXY data
91+
// Check peer IP against accepted range before using PROXY data.
92+
// Fail closed: if the peer address cannot be evaluated, ignore PROXY data.
9293
if (acceptedIpRange != null) {
9394
SocketAddress rawRemote = underlyingEndpoint.getRemoteSocketAddress();
94-
if (rawRemote instanceof InetSocketAddress inetRemote) {
95-
InetAddress peerAddress = inetRemote.getAddress();
96-
if (!acceptedIpRange.contains(peerAddress)) {
97-
log.debug(
98-
"Peer IP {} is not in accepted range, ignoring PROXY protocol data",
99-
peerAddress.getHostAddress());
100-
// Override the connection metadata to use raw TCP addresses,
101-
// undoing the ProxyEndPoint's effect on getRemoteAddr()
102-
return new RawPeerRequest(request, underlyingEndpoint);
103-
}
95+
if (!(rawRemote instanceof InetSocketAddress inetRemote)) {
96+
log.debug(
97+
"Raw remote address {} is not an InetSocketAddress;"
98+
+ " ignoring PROXY protocol data",
99+
rawRemote);
100+
return new RawPeerRequest(request, underlyingEndpoint);
101+
}
102+
103+
InetAddress peerAddress = inetRemote.getAddress();
104+
if (peerAddress == null) {
105+
log.debug(
106+
"Raw remote InetSocketAddress {} has null InetAddress;"
107+
+ " ignoring PROXY protocol data",
108+
inetRemote);
109+
return new RawPeerRequest(request, underlyingEndpoint);
110+
}
111+
112+
if (!acceptedIpRange.contains(peerAddress)) {
113+
log.debug(
114+
"Peer IP {} is not in accepted range, ignoring PROXY protocol data",
115+
peerAddress.getHostAddress());
116+
return new RawPeerRequest(request, underlyingEndpoint);
104117
}
105118
}
106119

0 commit comments

Comments
 (0)