From 26fec6cff1604189a1bce1d6d68c03e120f88540 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sat, 19 Dec 2020 13:20:11 -0600 Subject: [PATCH 1/5] Fix invalid IPv4 linking; optimize thx @href ! --- lib/linkify/parser.ex | 20 +++----------------- test/linkify_test.exs | 8 ++++++++ 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/lib/linkify/parser.ex b/lib/linkify/parser.ex index 680ddac..325b69c 100644 --- a/lib/linkify/parser.ex +++ b/lib/linkify/parser.ex @@ -258,23 +258,9 @@ defmodule Linkify.Parser do end def ip?(buffer) do - v4 = String.split(buffer, ".") - - v6 = - buffer - |> String.trim_leading("[") - |> String.trim_trailing("]") - |> String.split(":", trim: true) - - cond do - length(v4) == 4 -> - !Enum.any?(v4, fn x -> safe_to_integer(x, 10) not in 0..255 end) - - length(v6) in 1..8 -> - !Enum.any?(v4, fn x -> safe_to_integer(x, 16) not in 0..0xFFFF end) - - false -> - false + case :inet.parse_strict_address(to_charlist(buffer)) do + {:error, _} -> false + {:ok, _} -> true end end diff --git a/test/linkify_test.exs b/test/linkify_test.exs index 504fc8e..0550362 100644 --- a/test/linkify_test.exs +++ b/test/linkify_test.exs @@ -760,5 +760,13 @@ defmodule LinkifyTest do assert Linkify.link(text) == expected end + + test "IPv4 and IPv6 are linked" do + text = "1.1.1.1" + + expected = "1.1.1.1" + + assert Linkify.link(text) == expected + end end end From 713b986fa542897d4f099a91d3cb4bd4f34997ce Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Mon, 21 Dec 2020 11:32:05 -0600 Subject: [PATCH 2/5] Only support IPv4 for the moment --- test/linkify_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/linkify_test.exs b/test/linkify_test.exs index 0550362..83b5b57 100644 --- a/test/linkify_test.exs +++ b/test/linkify_test.exs @@ -761,7 +761,7 @@ defmodule LinkifyTest do assert Linkify.link(text) == expected end - test "IPv4 and IPv6 are linked" do + test "IPv4 is linked" do text = "1.1.1.1" expected = "1.1.1.1" From 8091bdbc7b7a8cc8782f67d6ce7898c972bb8c62 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Mon, 21 Dec 2020 14:02:04 -0600 Subject: [PATCH 3/5] Document fix, prepare for release --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63d612c..095ed6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog -## [Unreleased] +## 0.4.1 - 2020-12-21 + +### Fixed + +- Incorrect detection of IPv4 addresses causing random numbers (e.g., $123.45) to get linked ## 0.4.0 - 2020-11-24 From e0fd2e7620984f52d81595bfb01550d43907f55d Mon Sep 17 00:00:00 2001 From: Lain Soykaf Date: Tue, 22 Dec 2020 14:17:13 +0100 Subject: [PATCH 4/5] LinkifyTest: Add test for short IPv4 bug. --- test/linkify_test.exs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/linkify_test.exs b/test/linkify_test.exs index 83b5b57..64489c1 100644 --- a/test/linkify_test.exs +++ b/test/linkify_test.exs @@ -768,5 +768,13 @@ defmodule LinkifyTest do assert Linkify.link(text) == expected end + + test "shortened IPv4 are not linked" do + text = "109.99" + + expected = "109.99" + + assert Linkify.link(text) == expected + end end end From 603cf8c720c5563e76247dc87696756982fbb96c Mon Sep 17 00:00:00 2001 From: Lain Soykaf Date: Tue, 22 Dec 2020 14:18:02 +0100 Subject: [PATCH 5/5] Mix: Update version --- mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index 6bc7f6d..4edd91b 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Linkify.Mixfile do use Mix.Project - @version "0.4.0" + @version "0.4.1" def project do [