GeezerVison is showing github.com — Fix IP allow-list bypass from ruleType-blind dedup in IpSubnetFilter · Raghuvardhan-Reddy78/netty@d82c6c3
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/handler/ipfilter test/java/io/netty/handler/ipfilter
Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
| 20 | 20 | import io.netty.channel.ChannelFutureListener; | 21 | 21 | import io.netty.channel.ChannelHandlerContext; | 22 | 22 | import io.netty.channel.ChannelInboundHandlerAdapter; | | 23 | +import io.netty.util.internal.PlatformDependent; | 23 | 24 |
| 24 | 25 | import java.net.SocketAddress; | 25 | 26 |
| @@ -61,11 +62,22 @@ private boolean handleNewChannel(ChannelHandlerContext ctx) throws Exception { | 61 | 62 | return false; | 62 | 63 | } | 63 | 64 |
| 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. | 65 | 77 | // Also, this will prevent the subsequent events from being handled by this handler. | 66 | 78 | ctx.pipeline().remove(this); | 67 | 79 |
| 68 | | - if (accept(ctx, remoteAddress)) { | | 80 | + if (accepted) { | 69 | 81 | channelAccepted(ctx, remoteAddress); | 70 | 82 | } else { | 71 | 83 | ChannelFuture rejectedFuture = channelRejected(ctx, remoteAddress); | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 27 | 27 | import java.util.ArrayList; | 28 | 28 | import java.util.Arrays; | 29 | 29 | import java.util.Collections; | | 30 | +import java.util.Comparator; | 30 | 31 | import java.util.Iterator; | 31 | 32 | import java.util.List; | 32 | 33 |
| @@ -199,7 +200,17 @@ protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddr | 199 | 200 | */ | 200 | 201 | @SuppressWarnings("ZeroLengthArrayAllocation") | 201 | 202 | 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 | + }); | 203 | 214 | Iterator<IpSubnetFilterRule> iterator = rules.iterator(); | 204 | 215 | List<IpSubnetFilterRule> toKeep = new ArrayList<IpSubnetFilterRule>(); | 205 | 216 |
| @@ -215,11 +226,25 @@ private static IpSubnetFilterRule[] sortAndFilter(List<IpSubnetFilterRule> rules | 215 | 226 |
| 216 | 227 | // If parentRule matches childRule, then there's no need to keep the child rule. | 217 | 228 | // 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; | 222 | 243 | } | | 244 | + | | 245 | + toKeep.add(childRule); | | 246 | + // Then we'll keep the child rule around as the parent for the next round. | | 247 | + parentRule = childRule; | 223 | 248 | } | 224 | 249 |
| 225 | 250 | return toKeep.toArray(new IpSubnetFilterRule[0]); | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -154,10 +154,31 @@ int compareTo(InetSocketAddress inetSocketAddress) { | 154 | 154 | } | 155 | 155 |
| 156 | 156 | /** | 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. | 158 | 174 | */ | 159 | 175 | 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() + ')'; | 161 | 182 | } | 162 | 183 |
| 163 | 184 | static final class Ip4SubnetFilterRule implements IpFilterRule { | |
Collapse file Copy file name to clipboard +60Lines changed: 60 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 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 number | Diff line number | Diff line change |
|---|
| 23 | 23 | import io.netty.channel.embedded.EmbeddedChannel; | 24 | 24 | import io.netty.util.internal.SocketUtils; | 25 | 25 | import org.junit.jupiter.api.Test; | | 26 | +import org.junit.jupiter.api.function.Executable; | 26 | 27 |
| 27 | 28 | import java.net.Inet4Address; | 28 | 29 | import java.net.Inet6Address; | | 34 | 35 |
| 35 | 36 | import static org.junit.jupiter.api.Assertions.assertEquals; | 36 | 37 | import static org.junit.jupiter.api.Assertions.assertFalse; | | 38 | +import static org.junit.jupiter.api.Assertions.assertThrows; | 37 | 39 | import static org.junit.jupiter.api.Assertions.assertTrue; | 38 | 40 | import static org.junit.jupiter.api.Assertions.fail; | 39 | 41 |
| @@ -237,6 +239,35 @@ public void testIpv6MatchesNoFalsePositiveForAllOnesNetworkBits() { | 237 | 239 | assertFalse(rule.matches(newSockAddress("FFFF:FFFF::1"))); | 238 | 240 | } | 239 | 241 |
| | 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 | + | 240 | 271 | private static IpSubnetFilterRule buildRejectIP(String ipAddress, int mask) { | 241 | 272 | return new IpSubnetFilterRule(ipAddress, mask, IpFilterRuleType.REJECT); | 242 | 273 | } | |
|