From cd36c629358f050ca5e85d982f41536fba418f3b Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Thu, 11 Feb 2021 00:45:55 -0500 Subject: [PATCH] escape URL regex --- .../Tools/StatusExtractor.cs | 2 +- .../Tools/StatusExtractorTests.cs | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/BirdsiteLive.Domain/Tools/StatusExtractor.cs b/src/BirdsiteLive.Domain/Tools/StatusExtractor.cs index 7df4c0e..5cbff68 100644 --- a/src/BirdsiteLive.Domain/Tools/StatusExtractor.cs +++ b/src/BirdsiteLive.Domain/Tools/StatusExtractor.cs @@ -64,7 +64,7 @@ namespace BirdsiteLive.Domain.Tools secondPart = truncatedUrl.Substring(30); } - messageContent = Regex.Replace(messageContent, m.ToString(), + messageContent = Regex.Replace(messageContent, Regex.Escape(m.ToString()), $@"{m.Groups[1]}{protocol}{firstPart}{secondPart}"); } diff --git a/src/Tests/BirdsiteLive.Domain.Tests/Tools/StatusExtractorTests.cs b/src/Tests/BirdsiteLive.Domain.Tests/Tools/StatusExtractorTests.cs index 06d8f9a..a279dd0 100644 --- a/src/Tests/BirdsiteLive.Domain.Tests/Tools/StatusExtractorTests.cs +++ b/src/Tests/BirdsiteLive.Domain.Tests/Tools/StatusExtractorTests.cs @@ -217,6 +217,25 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion } + [TestMethod] + public void Extract_UrlRegexChars_Test() + { + #region Stubs + var message = @"🐣 juniors & tech(http://tech.guru maker)"; + #endregion + + #region Mocks + var logger = new Mock>(); + #endregion + + var service = new StatusExtractor(_settings, logger.Object); + var result = service.Extract(message); + + #region Validations + Assert.AreEqual(@"🐣 juniors & tech(http://tech.guru maker)", result.content); + #endregion + } + [TestMethod] public void Extract_SingleHashTag_Test() { @@ -345,7 +364,7 @@ namespace BirdsiteLive.Domain.Tests.Tools Assert.IsTrue(result.content.Contains(@"@mynickname")); #endregion } - + [TestMethod] public void Extract_SingleMentionTag_RT_Test() {