From b8722e09a14569396e2aa8a0168e76363c29fb32 Mon Sep 17 00:00:00 2001 From: Jiri Formacek Date: Sun, 29 Dec 2024 13:18:35 +0100 Subject: [PATCH] feat: macro expansion support in include method in Test-SpfHost --- Commands/Public/Test-SpfIHost.ps1 | 21 +++++++++++++++++++++ Module/SpfAnalyzer/SpfAnalyzer.psd1 | 2 +- Module/SpfAnalyzer/SpfAnalyzer.psm1 | 21 +++++++++++++++++++++ README.md | 2 +- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Commands/Public/Test-SpfIHost.ps1 b/Commands/Public/Test-SpfIHost.ps1 index 1e6568a..66b4a7e 100644 --- a/Commands/Public/Test-SpfIHost.ps1 +++ b/Commands/Public/Test-SpfIHost.ps1 @@ -73,6 +73,27 @@ More about SPF, see http://www.openspf.org/ and https://tools.ietf.org/html/rfc7 #silently ignore not found expanded macro } } + $record.Entries ` + | Where-Object { $_.Prefix -eq 'include' } ` + | Where-Object { $_.Value -match '%{.' } ` + | ForEach-Object { + $macro = Expand-SpfMacro -Macro $_.Value -Domain $spfRecords[0].Source -IpAddress $ip -SenderAddress $SenderAddress + if($macro -match '%{.' ) { + throw "Unsupported macro $macro after expansion of $( $_.Value )" + } + try { + $rawRecord = [Dns]::GetRecord($macro, [DnsClient.QueryType]::TXT) + if($null -ne $rawRecord) + { + $additionalRecord = [SpfRecord]::Parse($_.Source, $rawRecord) + $additionalRecord ` + | Test-SpfHost -IpAddress $IpAddress -SenderAddress $SenderAddress + } + } + catch { + #silently ignore not found expanded macro + } + } } } } \ No newline at end of file diff --git a/Module/SpfAnalyzer/SpfAnalyzer.psd1 b/Module/SpfAnalyzer/SpfAnalyzer.psd1 index 1843414..de84456 100644 --- a/Module/SpfAnalyzer/SpfAnalyzer.psd1 +++ b/Module/SpfAnalyzer/SpfAnalyzer.psd1 @@ -12,7 +12,7 @@ RootModule = '.\SpfAnalyzer.psm1' # Version number of this module. -ModuleVersion = '1.0.1' +ModuleVersion = '1.0.2' # Supported PSEditions CompatiblePSEditions = @('Core') diff --git a/Module/SpfAnalyzer/SpfAnalyzer.psm1 b/Module/SpfAnalyzer/SpfAnalyzer.psm1 index 0a10b69..8f198c8 100644 --- a/Module/SpfAnalyzer/SpfAnalyzer.psm1 +++ b/Module/SpfAnalyzer/SpfAnalyzer.psm1 @@ -637,6 +637,27 @@ More about SPF, see http://www.openspf.org/ and https://tools.ietf.org/html/rfc7 #silently ignore not found expanded macro } } + $record.Entries ` + | Where-Object { $_.Prefix -eq 'include' } ` + | Where-Object { $_.Value -match '%{.' } ` + | ForEach-Object { + $macro = Expand-SpfMacro -Macro $_.Value -Domain $spfRecords[0].Source -IpAddress $ip -SenderAddress $SenderAddress + if($macro -match '%{.' ) { + throw "Unsupported macro $macro after expansion of $( $_.Value )" + } + try { + $rawRecord = [Dns]::GetRecord($macro, [DnsClient.QueryType]::TXT) + if($null -ne $rawRecord) + { + $additionalRecord = [SpfRecord]::Parse($_.Source, $rawRecord) + $additionalRecord ` + | Test-SpfHost -IpAddress $IpAddress -SenderAddress $SenderAddress + } + } + catch { + #silently ignore not found expanded macro + } + } } } } diff --git a/README.md b/README.md index c02faa7..d17c536 100644 --- a/README.md +++ b/README.md @@ -50,4 +50,4 @@ Module provides additional helper commands that return list of IP addresses and IPv4 and IPv6 addresses are supported. Only Powershell Core edition is supported. Macro expansion in `exists` mechanism does not yet cover complete specification in RFC 7208 - looking for collaborators to enhance parsing. -Macros in `include` method aren't currently expanded and such record is not tried to be parsed +Macros in `include` method aren't currently expanded and such record is not tried to be parsed. Hoever, such record is processed by `Test-SpfHost` command