Skip to content

Instantly share code, notes, and snippets.

@LucGosso
Last active February 5, 2022 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucGosso/a07c2d5dc4cade33393eb97b182cc25a to your computer and use it in GitHub Desktop.
Save LucGosso/a07c2d5dc4cade33393eb97b182cc25a to your computer and use it in GitHub Desktop.
Mailto email obfuscation helper
using System.Text.RegularExpressions;
namespace Gosso.Mvc.Helpers
{
public class MailToHelper
{
private static readonly string replaceTemplate = @"#"" onclick=""window.location = 'mailto:' + ['{0}'].join(''); return false;";
public static string TransformMailToLinks(string text)
{
Regex regex = new Regex(@"mailto:([^""]+)");
var matches = regex.Matches(text);
if (matches.Count > 0)
{
foreach (Match match in matches)
{
var email = match.Groups[1]?.Value;
var emailAfter = string.Join("','", email.ToCharArray());
Regex regReplace = new Regex(match.Value.Replace("?",@"\?"));
text = regReplace.Replace(text, string.Format(replaceTemplate, emailAfter), 1);//only replace first occurancy
}
}
//now replace any email in plain text
regex = new Regex(@"([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)");
matches = regex.Matches(text);
if (matches.Count > 0)
{
foreach (Match match in matches)
{
var email = match.Groups[1]?.Value;
text = text.Replace(email, email.Replace("@", @" [at] "));
}
}
return text;
}
}
}
using Gosso.Mvc.Helpers;
using Xunit;
namespace XUnitTest
{
public class MailtoReplaceTests
{
[Fact]
public void MailtoTest()
{
var testStr = @"<p><a href=""mailto:luc@first.se""> test
whateva <a href=""mailto:luc@secondo.uk.com?subject=title of subject""> whateva <a href=""https://www.secondo.uk.com/"">test</a>
whateva <a href=""mailto:louis@tres.se""> sdf</p>";
var output = MailToHelper.TransformMailToLinks(testStr);
Assert.Contains("['l','o','u','i','s','@','t','r','e','s','.','s','e']", output);
}
[Fact]
public void MailtoTest2()
{
var testStr = @"test <a href=""mailto:luc@first.se""> whateva
whateva <a href=""mailto:luc@secondo.se?test=yes""> whateva
whateva <a href=""mailto:louis@tres.se""> whateva";
var output = MailToHelper.TransformMailToLinks(testStr);
Assert.StartsWith(@"test <a href=""#"" onclick=""window.location = 'mailto:' + ['l','u','c','@','f','i','r','s','t','.','s','e'].join(''); return false;"">", output);
}
[Fact]
public void MailtoTest3()
{
var testStr = @"test <a href=""mailto:luc@first.se"">luc@first.se</a> test
whateva <a href=""mailto:luc@secondo.se?test=yes""> whateva
whateva <a href=""mailto:louis@tres.se""> whateva";
var output = MailToHelper.TransformMailToLinks(testStr);
Assert.StartsWith(@"test <a href=""#"" onclick=""window.location = 'mailto:' + ['l','u','c','@','f','i','r','s','t','.','s','e'].join(''); return false;"">", output);
Assert.Contains(@"luc [at] first.se</a> test", output);
}
[Fact]
public void Mailto_second_and_third_formatchange()
{
var testStr = @"<p><a href=""mailto:luc@first.se""> whateva
whateva sdfdfs secondo <a href=""mailto:luc@secondo.se?test=yes""> test <a href=""https://www.secondo.uk.com/"">test</a>
tres <a href=""mailto:louis@tres.se""> whateva</p>";
var output = MailToHelper.TransformMailToLinks(testStr);
Assert.Contains(@"secondo <a href=""#"" onclick=""window.location = 'mailto:'", output);
Assert.Contains(@"tres <a href=""#"" onclick=""window.location = 'mailto:'", output);
}
[Fact]
public void Mailto_second_and_third_sameAddress()
{
var testStr = @"<p><a href=""mailto:luc@first.se""> whateva </a>
whateva whateva secondo <a href=""mailto:luc@first.se?test=yes""/> test <a href=""https://www.secondo.uk.com/"">test</a>
tres <a href=""mailto:louis@tres.se""/> sdf</a></p>";
var output = MailToHelper.TransformMailToLinks(testStr);
Assert.Contains(@"<a href=""#"" onclick=""window.location = 'mailto:' + ['l','u','c','@','f','i','r','s','t','.','s','e'].join", output);
Assert.Contains(@"'l','u','c','@','f','i','r','s','t','.','s','e','?','t','e','s','t','=','y','e','s'].join('')", output);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment