diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java index 4412dc30f..b1c7768ea 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java @@ -54,13 +54,14 @@ ) public class IpAddrFilter implements Filter { private static final Pattern IP4_PATTERN = Pattern.compile( - "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])" + "((0*1?\\d\\d?|0*2[0-4]\\d|0*25[0-5])\\.){3}(0*1?\\d\\d?|0*2[0-4]\\d|0*25[0-5])(/0*([1-2]?\\d|3[0-2])?)?" ); private static final Pattern IP6_PATTERN = Pattern.compile( - "^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$" + "^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}(/0*(\\d{1,2}|1[0-1]\\d|12[0-8])?)?$" ); private static final Pattern IP6_COMPRESSED_PATTERN = Pattern.compile( - "^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$" + "^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)" + + "(/0*(\\d{1,2}|1[0-1]\\d|12[0-8])?)?$" ); private static final Splitter PREFIX_SPLITTER = Splitter.on('/'); diff --git a/src/test/java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java b/src/test/java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java index 6e1bd7a4a..65b1fb131 100644 --- a/src/test/java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java @@ -209,12 +209,17 @@ public void itAddsErrorOnInvalidFunctionName() { @Test public void itFiltersValidIpv4Addresses() { - List validAddresses = Arrays.asList("255.182.100.1", " 128.0.0.1 "); + List validAddresses = Arrays.asList( + "255.182.100.1", + " 128.0.0.1 ", + "192.168.1.0/000024" + ); List invalidAddresses = Arrays.asList( "255.182.100.abc", "125.512.100.1", "125.512.100.1.1", "125.512.100", + "192.168.1.1/33", 104, "1200:0000:AB00:1234:0000:2552:7777:1313", "1200::AB00:1234::2552:7777:1313", @@ -233,7 +238,10 @@ public void itFiltersValidIpv4Addresses() { public void itFiltersValidIpv6Addresses() { List validAddresses = Arrays.asList( "1200:0000:AB00:1234:0000:2552:7777:1313", - "2000::" + "2000::", + "2000::/99", + "1200:0000:AB00:1234:0000:2552:7777:1313/0000021", + "1200:0000:AB00:1234:0000:2552:7777:1313/128" ); List invalidAddresses = Arrays.asList( "255.182.100.abc", @@ -243,6 +251,7 @@ public void itFiltersValidIpv6Addresses() { 104, "1200:0000:AB00:1234:O000:2552:7777:1313", "1200::AB00:1234::2552:7777:1313:1232", + "1200:0000:AB00:1234:0000:2552:7777:1313/132", "321", null, true