Skip to content

Commit a75bde1

Browse files
committed
Optimize parse logic for invalid data
1 parent a3f185b commit a75bde1

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

src/Nager.EmailAuthentication.UnitTest/DkimPublicKeyRecordParserTests/BasicTest.cs

+22
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,27 @@ public void TryParse_InvalidDkimPublicKeyRecord3_ReturnsTrueAndPopulatesDataFrag
3838
Assert.IsNotNull(dkimPublicKeyRecordDataFragment);
3939
Assert.IsNotNull(parsingResults, "ParsingResults is null");
4040
}
41+
42+
[TestMethod]
43+
public void TryParse_WrongDkimPublicKeyRecord1_ReturnsTrueAndPopulatesDataFragment()
44+
{
45+
var dkimPublicKeyRecordRaw = "v=DMARC1;p=reject;";
46+
47+
var isSuccessful = DkimPublicKeyRecordDataFragmentParser.TryParse(dkimPublicKeyRecordRaw, out var dkimPublicKeyRecordDataFragment, out var parsingResults);
48+
49+
Assert.IsTrue(isSuccessful);
50+
Assert.IsNotNull(dkimPublicKeyRecordDataFragment);
51+
Assert.IsNotNull(parsingResults, "ParsingResults is null");
52+
}
53+
54+
[TestMethod]
55+
public void TryParse_WrongDkimPublicKeyRecord2_ReturnsTrueAndPopulatesDataFragment()
56+
{
57+
var dkimPublicKeyRecordRaw = "v=DMARC1;p=reject;";
58+
59+
var isSuccessful = DkimPublicKeyRecordParser.TryParse(dkimPublicKeyRecordRaw, out var dkimPublicKeyRecord);
60+
Assert.IsFalse(isSuccessful);
61+
Assert.IsNull(dkimPublicKeyRecord, "DkimPublicKeyRecord is not null");
62+
}
4163
}
4264
}

src/Nager.EmailAuthentication/DkimPublicKeyRecordParser.cs

+27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ namespace Nager.EmailAuthentication
88
/// </summary>
99
public static class DkimPublicKeyRecordParser
1010
{
11+
private static bool ValidateRaw(string? dkimPublicKeyRecordRaw)
12+
{
13+
if (string.IsNullOrWhiteSpace(dkimPublicKeyRecordRaw))
14+
{
15+
return false;
16+
}
17+
18+
if (!dkimPublicKeyRecordRaw.StartsWith("v=DKIM1", StringComparison.OrdinalIgnoreCase))
19+
{
20+
return false;
21+
}
22+
23+
return true;
24+
}
25+
1126
/// <summary>
1227
/// TryParse
1328
/// </summary>
@@ -18,6 +33,12 @@ public static bool TryParse(
1833
string? dkimPublicKeyRecordRaw,
1934
[NotNullWhen(true)] out DkimPublicKeyRecord? dkimPublicKeyRecord)
2035
{
36+
if (!ValidateRaw(dkimPublicKeyRecordRaw))
37+
{
38+
dkimPublicKeyRecord = null;
39+
return false;
40+
}
41+
2142
if (!DkimPublicKeyRecordDataFragmentParser.TryParse(dkimPublicKeyRecordRaw, out var dataFragment, out _))
2243
{
2344
dkimPublicKeyRecord = null;
@@ -45,6 +66,12 @@ public static bool TryParse(
4566
return false;
4667
}
4768

69+
if (!ValidateRaw(dkimPublicKeyRecordRaw))
70+
{
71+
dkimPublicKeyRecord = null;
72+
return false;
73+
}
74+
4875
return TryParse(dataFragment, out dkimPublicKeyRecord);
4976
}
5077

src/Nager.EmailAuthentication/DmarcRecordParser.cs

+27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ namespace Nager.EmailAuthentication
88
/// </summary>
99
public static class DmarcRecordParser
1010
{
11+
private static bool ValidateRaw(string? dmarcRaw)
12+
{
13+
if (string.IsNullOrWhiteSpace(dmarcRaw))
14+
{
15+
return false;
16+
}
17+
18+
if (!dmarcRaw.StartsWith("v=DMARC1", StringComparison.OrdinalIgnoreCase))
19+
{
20+
return false;
21+
}
22+
23+
return true;
24+
}
25+
1126
/// <summary>
1227
/// Attempts to parse a raw DMARC record string into a <see cref="DmarcRecord"/> object.
1328
/// </summary>
@@ -24,6 +39,12 @@ public static bool TryParse(
2439
string? dmarcRaw,
2540
[NotNullWhen(true)] out DmarcRecord? dmarcRecord)
2641
{
42+
if (!ValidateRaw(dmarcRaw))
43+
{
44+
dmarcRecord = null;
45+
return false;
46+
}
47+
2748
if (!DmarcRecordDataFragmentParser.TryParse(dmarcRaw, out var dataFragment, out _))
2849
{
2950
dmarcRecord = null;
@@ -61,6 +82,12 @@ public static bool TryParse(
6182
return false;
6283
}
6384

85+
if (!ValidateRaw(dmarcRaw))
86+
{
87+
dmarcRecord = null;
88+
return false;
89+
}
90+
6491
return TryParse(dataFragment, out dmarcRecord);
6592
}
6693

src/Nager.EmailAuthentication/Nager.EmailAuthentication.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
2222

23-
<Version>1.5.8</Version>
23+
<Version>1.5.9</Version>
2424
</PropertyGroup>
2525

2626
<ItemGroup>

0 commit comments

Comments
 (0)