Skip to content

Commit

Permalink
Add support to specify trusted addresses (#25)
Browse files Browse the repository at this point in the history
* Add support to specify trusted addresses

If a trusted domain entry contains "@", we regard it as address, not domain.

* Modify resources
  • Loading branch information
HashidaTKS authored May 28, 2024
1 parent d913335 commit 598e717
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 264 deletions.
9 changes: 7 additions & 2 deletions Config/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class Config
public bool SafeNewDomainsEnabled = true;
public HashSet<ConfigOption> Modified;

public string TrustedDomainsPattern = "";
public string TrustedDomainsPattern = "";
public string TrustedAddressesPattern = "";
public string UnsafeDomainsPattern = "";
public string UnsafeFilesPattern = "";

Expand Down Expand Up @@ -93,7 +94,11 @@ public void Merge(Config other)

public void RebuildPatterns()
{
TrustedDomainsPattern = $"^({string.Join("|", TrustedDomains.Select(ConvertWildCardToRegex))})$";
var trustedAddressList = TrustedDomains.Where(_ => _.Contains("@"));
var trustedDomainList = TrustedDomains.Where(_ => !_.Contains("@"));

TrustedDomainsPattern = $"^({string.Join("|", trustedDomainList.Select(ConvertWildCardToRegex))})$";
TrustedAddressesPattern = $"^({string.Join("|", trustedAddressList.Select(ConvertWildCardToRegex))})$";
UnsafeDomainsPattern = $"^({string.Join("|", UnsafeDomains.Select(ConvertWildCardToRegex))})$";
UnsafeFilesPattern = $"({string.Join("|", UnsafeFiles.Select(ConvertWildCardToRegex))})";
}
Expand Down
19 changes: 14 additions & 5 deletions Dialog/MainDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private List<string> ToLower(List<string> list)
return ret;
}

private bool IsTrustedDomain(string domain, HashSet<string> trusted)
private bool IsTrustedDomain(string domain)
{
// Note: DOMAIN_EXCHANGE basically means "LegacyDN recipients whose
// SMTP address we don't know". We assume they are internal users,
Expand All @@ -152,17 +152,27 @@ private bool IsTrustedDomain(string domain, HashSet<string> trusted)
}
catch (RegexMatchTimeoutException) { }

return false;
}

private bool IsTrustedAddress(string address)
{
try
{
return Regex.IsMatch(address, _config.TrustedAddressesPattern, RegexOptions.IgnoreCase);
}
catch (RegexMatchTimeoutException) { }

return false;
}

private void RenderTrustedList(List<RecipientInfo> recipients)
{
HashSet<string> seen = new HashSet<string>();
HashSet<string> trusted = GetHashSet(ToLower(_config.TrustedDomains));

foreach (RecipientInfo info in recipients)
{
if (IsTrustedDomain(info.Domain, trusted))
if (IsTrustedDomain(info.Domain) || IsTrustedAddress(info.Address))
{
if (!seen.Contains(info.Domain))
{
Expand All @@ -177,11 +187,10 @@ private void RenderTrustedList(List<RecipientInfo> recipients)
private void RenderExternalList(List<RecipientInfo> list)
{
HashSet<string> seen = new HashSet<string>();
HashSet<string> trusted = GetHashSet(ToLower(_config.TrustedDomains));

foreach (RecipientInfo info in list)
{
if (!IsTrustedDomain(info.Domain, trusted))
if (!(IsTrustedDomain(info.Domain) || IsTrustedAddress(info.Address)))
{
if (!seen.Contains(info.Domain))
{
Expand Down
136 changes: 71 additions & 65 deletions Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 598e717

Please sign in to comment.