diff options
author | Vitaly Takmazov | 2024-08-30 05:55:13 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2024-08-30 05:55:13 +0300 |
commit | 14519508e9e02760ddd1a7b2c91ad79968329198 (patch) | |
tree | d35a1affde123f4cbc5c18a7198adc1b4077dfe9 /src | |
parent | 54394658178201f4a8fd6c7000bc9edd503db7d6 (diff) |
Fix URL regex
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/juick/util/MessageUtils.java | 2 | ||||
-rw-r--r-- | src/test/java/com/juick/MessageTest.java | 12 |
2 files changed, 11 insertions, 3 deletions
diff --git a/src/main/java/com/juick/util/MessageUtils.java b/src/main/java/com/juick/util/MessageUtils.java index 42c97d84..316adb90 100644 --- a/src/main/java/com/juick/util/MessageUtils.java +++ b/src/main/java/com/juick/util/MessageUtils.java @@ -57,7 +57,7 @@ public class MessageUtils { private final static String urlWhiteSpacePrefix = "((?<=\\s)|(?<=\\A))"; - private final static String urlRegex = "((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\n\\\"]+)/?[^\\]\\s\\n\\\"\\>]*)"; + private final static String urlRegex = "((?:(?:ht|f)tps?://(?:www\\.)?([^\\s()<>/?#]+)([^\\s()<>]*)?))"; private final static String urlWithWhitespacesRegex = urlWhiteSpacePrefix + urlRegex; diff --git a/src/test/java/com/juick/MessageTest.java b/src/test/java/com/juick/MessageTest.java index 14d6175a..6ce01f4a 100644 --- a/src/test/java/com/juick/MessageTest.java +++ b/src/test/java/com/juick/MessageTest.java @@ -213,8 +213,8 @@ public class MessageTest { assertThat(MessageUtils.formatMessage(msg), is("<blockquote>quote</blockquote>message")); String brokenComment = "<!-- read next"; assertThat(MessageUtils.formatMessage(brokenComment), is("<!-- read next")); - String url = "[ya](http://ya.ru)"; - assertThat(MessageUtils.formatMessage(url), is("<a href=\"http://ya.ru\" rel=\"nofollow\">ya</a>")); + String url = "test [ya](https://juick.com/a/1)? hh"; + assertThat(MessageUtils.formatMessage(url), is("test <a href=\"https://juick.com/a/1\" rel=\"nofollow\">ya</a>? hh")); String complexMessage = "У футболистов нет мозгов. Что в России, что в Беларуси:\n" + "\n" + ">Отец футболиста Лухвича, объехавшего пробку по тротуару: «Сына задержали, Infiniti арестовали» https://auto.onliner.by/2019/01/23/probka-9\n" + @@ -238,4 +238,12 @@ public class MessageTest { testMessage.setTags(MessageUtils.parseTags("NSFW test")); assertThat(MessageUtils.isSensitive(testMessage), is(true)); } + @Test + public void stripHashesTest() { + var safeMessage = "And that is a [odd](https://juick.com/a/1)? aaapaa"; + var nonsafeMessage = "[Here is my link](https://juick.com?hash=12345)"; + var filteredMessage = "[Here is my link](https://juick.com)"; + assertThat(MessageUtils.stripNonSafeUrls(safeMessage), is(safeMessage)); + assertThat(MessageUtils.stripNonSafeUrls(nonsafeMessage), is(filteredMessage)); + } } |