Skip to content

Commit

Permalink
Add support to specify trusted addresses
Browse files Browse the repository at this point in the history
If a trusted domain entry contains "@", we regard it as address, not domain.
  • Loading branch information
HashidaTKS committed May 20, 2024
1 parent d913335 commit 56a501e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 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
35 changes: 29 additions & 6 deletions FlexConfirmMail.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,39 @@
<NoStandardLibraries>false</NoStandardLibraries>
<RootNamespace>FlexConfirmMail</RootNamespace>
<AssemblyName>FlexConfirmMail</AssemblyName>
<LoadBehavior>3</LoadBehavior>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<DefineConstants>VSTO40</DefineConstants>
<BootstrapperEnabled>true</BootstrapperEnabled>
<BootstrapperComponentsLocation>HomeSite</BootstrapperComponentsLocation>
<PublishUrl>公開\</PublishUrl>
<InstallUrl />
<TargetCulture>ja</TargetCulture>
<ApplicationVersion>1.0.0.0</ApplicationVersion>
<AutoIncrementApplicationRevision>true</AutoIncrementApplicationRevision>
<UpdateEnabled>true</UpdateEnabled>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>days</UpdateIntervalUnits>
<IsWebBootstrapper>False</IsWebBootstrapper>
<ProductName>FlexConfirmMail</ProductName>
<PublisherName />
<SupportUrl />
<FriendlyName>FlexConfirmMail</FriendlyName>
<OfficeApplicationDescription />
<LoadBehavior>3</LoadBehavior>
</PropertyGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.7.2">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.7.2 %28x86 および x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.VSTORuntime.4.0">
<Visible>False</Visible>
<ProductName>Microsoft Visual Studio 2010 Tools for Office Runtime %28x86 and x64%29</ProductName>
<ProductName>Microsoft Visual Studio 2010 Tools for Office Runtime %28x86 および x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
Expand Down Expand Up @@ -202,6 +225,7 @@
<DesignTime>True</DesignTime>
</Compile>
<None Include="FlexConfirmMail.iss" />
<None Include="FlexConfirmMail_TemporaryKey.pfx" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down Expand Up @@ -256,11 +280,10 @@
<SignManifests>true</SignManifests>
</PropertyGroup>
<PropertyGroup>
<ManifestKeyFile>
</ManifestKeyFile>
<ManifestKeyFile>FlexConfirmMail_TemporaryKey.pfx</ManifestKeyFile>
</PropertyGroup>
<PropertyGroup>
<ManifestCertificateThumbprint>73E7B9D1F72EDA033E7A9D6B17BC37A96CE8513A</ManifestCertificateThumbprint>
<ManifestCertificateThumbprint>64B05F83B44EB4280B93BFCE98903E9770346EA4</ManifestCertificateThumbprint>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>false</SignAssembly>
Expand Down

0 comments on commit 56a501e

Please sign in to comment.