Skip to content

Commit

Permalink
Added prefix validation to ipv4 and ipv6 filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimok committed Mar 24, 2021
1 parent b29324f commit ea8814f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
Expand Down
14 changes: 12 additions & 2 deletions src/test/java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,18 @@ public void itAddsErrorOnInvalidFunctionName() {

@Test
public void itFiltersValidIpv4Addresses() {
List<Object> validAddresses = Arrays.asList("255.182.100.1", " 128.0.0.1 ");
List<Object> validAddresses = Arrays.asList(
"255.182.100.1",
" 128.0.0.1 ",
"192.168.1.0/000024",
"0000192.0168.000000.1"
);
List<Object> 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",
Expand All @@ -233,7 +239,10 @@ public void itFiltersValidIpv4Addresses() {
public void itFiltersValidIpv6Addresses() {
List<Object> 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<Object> invalidAddresses = Arrays.asList(
"255.182.100.abc",
Expand All @@ -243,6 +252,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
Expand Down

0 comments on commit ea8814f

Please sign in to comment.