GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix IP allow-list bypass from ruleType-blind dedup in IpSubnetFilter · Raghuvardhan-Reddy78/netty@d82c6c3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/ipfilter
      • AbstractRemoteAddressFilter.java
      • IpSubnetFilter.java
      • IpSubnetFilterRule.java
    • test/java/io/netty/handler/ipfilter
      • AbstractRemoteAddressFilterTest.java
      • IpSubnetFilterTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler/src/main/java/io/netty/handler/ipfilter/AbstractRemoteAddressFilter.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ipfilter/AbstractRemoteAddressFilter.java
+14-2Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.netty.channel.ChannelFutureListener;
2121
import io.netty.channel.ChannelHandlerContext;
2222
import io.netty.channel.ChannelInboundHandlerAdapter;
23+
import io.netty.util.internal.PlatformDependent;
2324

2425
import java.net.SocketAddress;
2526

@@ -61,11 +62,22 @@ private boolean handleNewChannel(ChannelHandlerContext ctx) throws Exception {
6162
return false;
6263
}
6364

64-
// No need to keep this handler in the pipeline anymore because the decision is going to be made now.
65+
// Evaluate the decision before removing this handler from the pipeline, so that if accept(...) throws,
66+
// the channel is closed instead of silently continuing through the rest of the pipeline unfiltered.
67+
boolean accepted;
68+
try {
69+
accepted = accept(ctx, remoteAddress);
70+
} catch (Throwable cause) {
71+
ctx.close();
72+
PlatformDependent.throwException(cause);
73+
return true;
74+
}
75+
76+
// No need to keep this handler in the pipeline anymore because the decision has been made.
6577
// Also, this will prevent the subsequent events from being handled by this handler.
6678
ctx.pipeline().remove(this);
6779

68-
if (accept(ctx, remoteAddress)) {
80+
if (accepted) {
6981
channelAccepted(ctx, remoteAddress);
7082
} else {
7183
ChannelFuture rejectedFuture = channelRejected(ctx, remoteAddress);
Collapse file

‎handler/src/main/java/io/netty/handler/ipfilter/IpSubnetFilter.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ipfilter/IpSubnetFilter.java
+30-5Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.ArrayList;
2828
import java.util.Arrays;
2929
import java.util.Collections;
30+
import java.util.Comparator;
3031
import java.util.Iterator;
3132
import java.util.List;
3233

@@ -199,7 +200,17 @@ protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddr
199200
*/
200201
@SuppressWarnings("ZeroLengthArrayAllocation")
201202
private static IpSubnetFilterRule[] sortAndFilter(List<IpSubnetFilterRule> rules) {
202-
Collections.sort(rules);
203+
// Equal network addresses: broader subnet first, so it becomes the parent of the narrower one.
204+
Collections.sort(rules, new Comparator<IpSubnetFilterRule>() {
205+
@Override
206+
public int compare(IpSubnetFilterRule a, IpSubnetFilterRule b) {
207+
int cmp = a.compareTo(b);
208+
if (cmp != 0) {
209+
return cmp;
210+
}
211+
return a.contains(b) ? (b.contains(a) ? 0 : -1) : 1;
212+
}
213+
});
203214
Iterator<IpSubnetFilterRule> iterator = rules.iterator();
204215
List<IpSubnetFilterRule> toKeep = new ArrayList<IpSubnetFilterRule>();
205216

@@ -215,11 +226,25 @@ private static IpSubnetFilterRule[] sortAndFilter(List<IpSubnetFilterRule> rules
215226

216227
// If parentRule matches childRule, then there's no need to keep the child rule.
217228
// Otherwise, the rules are distinct and we need both.
218-
if (!parentRule.matches(new InetSocketAddress(childRule.getIpAddress(), 1))) {
219-
toKeep.add(childRule);
220-
// Then we'll keep the child rule around as the parent for the next round.
221-
parentRule = childRule;
229+
if (parentRule.contains(childRule)) {
230+
// The child rule is a subset of the parent rule. If they don't share the same rule type
231+
// we can't safely drop the child rule, as doing so would silently change the outcome for
232+
// addresses covered by the child but not otherwise expressible with the parent's ruleType.
233+
// This binary-search based design can't represent longest-prefix-match semantics, so reject
234+
// the unsupported configuration instead of failing open.
235+
if (childRule.ruleType() != parentRule.ruleType()) {
236+
throw new IllegalArgumentException(
237+
"IpSubnetFilter does not support overlapping rules with different ruleTypes: "
238+
+ parentRule + " (" + parentRule.ruleType() + ") overlaps "
239+
+ childRule + " (" + childRule.ruleType() + "). "
240+
+ "Use RuleBasedIpFilter for mixed ACCEPT/REJECT carve-outs.");
241+
}
242+
continue;
222243
}
244+
245+
toKeep.add(childRule);
246+
// Then we'll keep the child rule around as the parent for the next round.
247+
parentRule = childRule;
223248
}
224249

225250
return toKeep.toArray(new IpSubnetFilterRule[0]);
Collapse file

‎handler/src/main/java/io/netty/handler/ipfilter/IpSubnetFilterRule.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ipfilter/IpSubnetFilterRule.java
+23-2Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,31 @@ int compareTo(InetSocketAddress inetSocketAddress) {
154154
}
155155

156156
/**
157-
* Equivalent to {@link Integer#compare(int, int)}
157+
* {@code true} if every address matched by {@code other} is also matched by this rule.
158+
*/
159+
boolean contains(IpSubnetFilterRule other) {
160+
if (filterRule instanceof Ip4SubnetFilterRule) {
161+
Ip4SubnetFilterRule parent = (Ip4SubnetFilterRule) filterRule;
162+
Ip4SubnetFilterRule child = (Ip4SubnetFilterRule) other.filterRule;
163+
return (parent.subnetMask & child.subnetMask) == parent.subnetMask &&
164+
(child.networkAddress & parent.subnetMask) == parent.networkAddress;
165+
}
166+
Ip6SubnetFilterRule parent = (Ip6SubnetFilterRule) filterRule;
167+
Ip6SubnetFilterRule child = (Ip6SubnetFilterRule) other.filterRule;
168+
return parent.subnetMask.and(child.subnetMask).equals(parent.subnetMask) &&
169+
child.networkAddress.and(parent.subnetMask).equals(parent.networkAddress);
170+
}
171+
172+
/**
173+
* IPv4 addresses are compared as unsigned values, so 128.0.0.0 and above sort after 127.255.255.255.
158174
*/
159175
private static int compareInt(int x, int y) {
160-
return (x < y) ? -1 : ((x == y) ? 0 : 1);
176+
return Integer.compare(x ^ Integer.MIN_VALUE, y ^ Integer.MIN_VALUE);
177+
}
178+
179+
@Override
180+
public String toString() {
181+
return "IpSubnetFilterRule(" + ipAddress + ", " + ruleType() + ')';
161182
}
162183

163184
static final class Ip4SubnetFilterRule implements IpFilterRule {
Collapse file

‎handler/src/test/java/io/netty/handler/ipfilter/AbstractRemoteAddressFilterTest.java‎

Copy file name to clipboard
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.handler.ipfilter;
17+
18+
import io.netty.channel.ChannelHandlerContext;
19+
import io.netty.channel.embedded.EmbeddedChannel;
20+
import io.netty.util.internal.SocketUtils;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.function.Executable;
23+
24+
import java.net.InetSocketAddress;
25+
import java.net.SocketAddress;
26+
27+
import static org.junit.jupiter.api.Assertions.assertFalse;
28+
import static org.junit.jupiter.api.Assertions.assertThrows;
29+
30+
public class AbstractRemoteAddressFilterTest {
31+
32+
@Test
33+
public void channelIsClosedWhenAcceptThrows() {
34+
final RuntimeException boom = new RuntimeException("boom");
35+
AbstractRemoteAddressFilter<InetSocketAddress> filter = new AbstractRemoteAddressFilter<InetSocketAddress>() {
36+
@Override
37+
protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddress) {
38+
throw boom;
39+
}
40+
};
41+
42+
final EmbeddedChannel channel = new EmbeddedChannel(filter) {
43+
@Override
44+
protected SocketAddress remoteAddress0() {
45+
return isActive() ? SocketUtils.socketAddress("10.0.0.1", 1234) : null;
46+
}
47+
};
48+
49+
// The channel must be closed rather than left open and unfiltered when accept() throws.
50+
assertFalse(channel.isActive());
51+
assertFalse(channel.isOpen());
52+
53+
assertThrows(RuntimeException.class, new Executable() {
54+
@Override
55+
public void execute() {
56+
channel.checkException();
57+
}
58+
});
59+
}
60+
}
Collapse file

‎handler/src/test/java/io/netty/handler/ipfilter/IpSubnetFilterTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/ipfilter/IpSubnetFilterTest.java
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.netty.channel.embedded.EmbeddedChannel;
2424
import io.netty.util.internal.SocketUtils;
2525
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.function.Executable;
2627

2728
import java.net.Inet4Address;
2829
import java.net.Inet6Address;
@@ -34,6 +35,7 @@
3435

3536
import static org.junit.jupiter.api.Assertions.assertEquals;
3637
import static org.junit.jupiter.api.Assertions.assertFalse;
38+
import static org.junit.jupiter.api.Assertions.assertThrows;
3739
import static org.junit.jupiter.api.Assertions.assertTrue;
3840
import static org.junit.jupiter.api.Assertions.fail;
3941

@@ -237,6 +239,35 @@ public void testIpv6MatchesNoFalsePositiveForAllOnesNetworkBits() {
237239
assertFalse(rule.matches(newSockAddress("FFFF:FFFF::1")));
238240
}
239241

242+
@Test
243+
public void testOverlappingRulesWithDifferentRuleTypeThrows() {
244+
// ACCEPT 10.0.0.0/8 followed by REJECT 10.5.0.0/16: the REJECT rule is fully contained within
245+
// the broader ACCEPT supernet. Silently dropping it during de-duplication would turn the REJECT
246+
// into an implicit ACCEPT, so construction must fail loudly instead.
247+
List<IpSubnetFilterRule> rules = new ArrayList<IpSubnetFilterRule>();
248+
rules.add(new IpSubnetFilterRule("10.0.0.0", 8, IpFilterRuleType.ACCEPT));
249+
rules.add(new IpSubnetFilterRule("10.5.0.0", 16, IpFilterRuleType.REJECT));
250+
251+
assertThrows(IllegalArgumentException.class, new Executable() {
252+
@Override
253+
public void execute() {
254+
new IpSubnetFilter(rules);
255+
}
256+
});
257+
}
258+
259+
@Test
260+
public void testOverlappingRulesWithSameRuleTypeDoesNotThrow() {
261+
// Both rules are ACCEPT, so coalescing the more specific one is semantically safe.
262+
List<IpSubnetFilterRule> rules = new ArrayList<IpSubnetFilterRule>();
263+
rules.add(new IpSubnetFilterRule("10.0.0.0", 8, IpFilterRuleType.ACCEPT));
264+
rules.add(new IpSubnetFilterRule("10.5.0.0", 16, IpFilterRuleType.ACCEPT));
265+
266+
IpSubnetFilter filter = new IpSubnetFilter(false, rules);
267+
assertTrue(filter.accept(null, newSockAddress("10.5.1.1")));
268+
assertTrue(filter.accept(null, newSockAddress("10.1.1.1")));
269+
}
270+
240271
private static IpSubnetFilterRule buildRejectIP(String ipAddress, int mask) {
241272
return new IpSubnetFilterRule(ipAddress, mask, IpFilterRuleType.REJECT);
242273
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.