From d4d97243f694dc9cb1ab1f48647684f5800867a0 Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Wed, 19 Sep 2018 09:56:02 -0400 Subject: [PATCH 1/4] Adds filter to test if string is valid IP Address. --- .../jinjava/lib/filter/FilterLibrary.java | 1 + .../jinjava/lib/filter/IpAddrFilter.java | 50 +++++++++++++++++ .../jinjava/lib/filter/IpAddrFilterTest.java | 54 +++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java create mode 100644 src/test/java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/FilterLibrary.java b/src/main/java/com/hubspot/jinjava/lib/filter/FilterLibrary.java index c28d60d00..1d86e7948 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/FilterLibrary.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/FilterLibrary.java @@ -74,6 +74,7 @@ protected void registerDefaults() { ReverseFilter.class, RoundFilter.class, SumFilter.class, + IpAddrFilter.class, EscapeFilter.class, EAliasedEscapeFilter.class, diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java new file mode 100644 index 000000000..91fbce8de --- /dev/null +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java @@ -0,0 +1,50 @@ +package com.hubspot.jinjava.lib.filter; + +import java.util.regex.Pattern; + +import com.hubspot.jinjava.doc.annotations.JinjavaDoc; +import com.hubspot.jinjava.doc.annotations.JinjavaParam; +import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; +import com.hubspot.jinjava.interpret.JinjavaInterpreter; + +@JinjavaDoc( + value = "Evaluates to true if the value is a valid IPv4 or IPv6 address", + params = { + @JinjavaParam(value = "value", type = "string", desc = "String to check IP Address"), + }, + snippets = { + @JinjavaSnippet( + desc = "This example is an alternative to using the is divisibleby expression test", + code = "{% set ip = '1.0.0.1' %}\n" + + "{% if ip|ipaddr %}\n" + + " The string is a valid IP address\n" + + "{% endif %}") + }) +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])"); + private static final Pattern IP6_PATTERN = Pattern.compile("^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"); + 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})*)?)$"); + + @Override + public Object filter(Object object, JinjavaInterpreter interpreter, String... arg) { + + if (object == null) { + return false; + } + + if (object instanceof String) { + String address = (String) object; + return IP4_PATTERN.matcher(address).matches() + || IP6_PATTERN.matcher(address).matches() + || IP6_COMPRESSED_PATTERN.matcher(address).matches(); + } + return false; + } + + @Override + public String getName() { + return "ipaddr"; + } + +} diff --git a/src/test/java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java b/src/test/java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java new file mode 100644 index 000000000..97e914b8e --- /dev/null +++ b/src/test/java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java @@ -0,0 +1,54 @@ +package com.hubspot.jinjava.lib.filter; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import org.junit.Before; +import org.junit.Test; + +import com.hubspot.jinjava.Jinjava; +import com.hubspot.jinjava.interpret.JinjavaInterpreter; + +public class IpAddrFilterTest { + + private IpAddrFilter ipAddrFilter; + private JinjavaInterpreter interpreter; + + @Before + public void setup() { + ipAddrFilter = new IpAddrFilter(); + interpreter = new Jinjava().newInterpreter(); + } + + @Test + public void itAcceptsValidIpV4Address() { + assertThat(ipAddrFilter.filter("255.182.100.1", interpreter)).isEqualTo(true); + assertThat(ipAddrFilter.filter("125.0.100.1", interpreter)).isEqualTo(true); + assertThat(ipAddrFilter.filter("128.0.0.1", interpreter)).isEqualTo(true); + } + + @Test + public void itRejectsInvalidIpV4Address() { + assertThat(ipAddrFilter.filter("255.182.100.abc", interpreter)).isEqualTo(false); + assertThat(ipAddrFilter.filter("125.512.100.1", interpreter)).isEqualTo(false); + assertThat(ipAddrFilter.filter("125.512.100.1.1", interpreter)).isEqualTo(false); + assertThat(ipAddrFilter.filter("125.512.100", interpreter)).isEqualTo(false); + assertThat(ipAddrFilter.filter(104, interpreter)).isEqualTo(false); + } + + @Test + public void itAcceptsValidIpV6Address() { + assertThat(ipAddrFilter.filter("1200:0000:AB00:1234:0000:2552:7777:1313", interpreter)).isEqualTo(true); + assertThat(ipAddrFilter.filter("21DA:D3:0:2F3B:2AA:FF:FE28:9C5A", interpreter)).isEqualTo(true); + assertThat(ipAddrFilter.filter("2000::", interpreter)).isEqualTo(true); + } + + @Test + public void itRejectsInvalidIpV6Address() { + assertThat(ipAddrFilter.filter("1200::AB00:1234::2552:7777:1313", interpreter)).isEqualTo(false); + assertThat(ipAddrFilter.filter("1200:0000:AB00:1234:O000:2552:7777:1313", interpreter)).isEqualTo(false); + assertThat(ipAddrFilter.filter("1200::AB00:1234::2552:7777:1313:1232", interpreter)).isEqualTo(false); + assertThat(ipAddrFilter.filter("321", interpreter)).isEqualTo(false); + assertThat(ipAddrFilter.filter(104, interpreter)).isEqualTo(false); + } + +} From 8530ce17b2508d2118669f34b8333f06f958fb84 Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Wed, 19 Sep 2018 09:59:46 -0400 Subject: [PATCH 2/4] doc change. --- src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 91fbce8de..84e766bed 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java @@ -14,7 +14,7 @@ }, snippets = { @JinjavaSnippet( - desc = "This example is an alternative to using the is divisibleby expression test", + desc = "This example shows how to test if a string is a valid ip address", code = "{% set ip = '1.0.0.1' %}\n" + "{% if ip|ipaddr %}\n" + " The string is a valid IP address\n" + From e9a0934e986a75fbe4710dc67bc624139b2c7460 Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Wed, 19 Sep 2018 10:10:03 -0400 Subject: [PATCH 3/4] trim. --- src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java | 2 +- .../java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) 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 84e766bed..cf4cb92cd 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/IpAddrFilter.java @@ -34,7 +34,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar } if (object instanceof String) { - String address = (String) object; + String address = ((String) object).trim(); return IP4_PATTERN.matcher(address).matches() || IP6_PATTERN.matcher(address).matches() || IP6_COMPRESSED_PATTERN.matcher(address).matches(); 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 97e914b8e..bd8eca03b 100644 --- a/src/test/java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java @@ -24,6 +24,7 @@ public void itAcceptsValidIpV4Address() { assertThat(ipAddrFilter.filter("255.182.100.1", interpreter)).isEqualTo(true); assertThat(ipAddrFilter.filter("125.0.100.1", interpreter)).isEqualTo(true); assertThat(ipAddrFilter.filter("128.0.0.1", interpreter)).isEqualTo(true); + assertThat(ipAddrFilter.filter(" 128.0.0.1 ", interpreter)).isEqualTo(true); } @Test From 8bc8fa47c1eb4ddd1e111a10ee49e7c3053e54c8 Mon Sep 17 00:00:00 2001 From: Matt Coley Date: Wed, 19 Sep 2018 10:10:58 -0400 Subject: [PATCH 4/4] remove duplicate test. --- .../java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java | 1 - 1 file changed, 1 deletion(-) 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 bd8eca03b..a219d0852 100644 --- a/src/test/java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java +++ b/src/test/java/com/hubspot/jinjava/lib/filter/IpAddrFilterTest.java @@ -49,7 +49,6 @@ public void itRejectsInvalidIpV6Address() { assertThat(ipAddrFilter.filter("1200:0000:AB00:1234:O000:2552:7777:1313", interpreter)).isEqualTo(false); assertThat(ipAddrFilter.filter("1200::AB00:1234::2552:7777:1313:1232", interpreter)).isEqualTo(false); assertThat(ipAddrFilter.filter("321", interpreter)).isEqualTo(false); - assertThat(ipAddrFilter.filter(104, interpreter)).isEqualTo(false); } }