From f931ea2a2c4f4b120035427f14ac2a9792da8414 Mon Sep 17 00:00:00 2001 From: Brandon Fowler Date: Sat, 21 Jun 2025 17:44:15 -0400 Subject: [PATCH 1/3] Move TXT record finding to its own function --- src/Decoder.php | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Decoder.php b/src/Decoder.php index 95504be..bdd6630 100644 --- a/src/Decoder.php +++ b/src/Decoder.php @@ -40,18 +40,16 @@ public function __construct(?Resolver $dnsResolver = null, ?MacroStringDecoder $ } /** - * Extract the SPF record associated to a domain. + * Get the raw SPF TXT record associated to a domain. * * @throws \SPFLib\Exception\DNSResolutionException in case of DNS resolution errors * @throws \SPFLib\Exception\MultipleSPFRecordsException if the domain has more that 1 SPF record - * @throws \SPFLib\Exception\InvalidTermException if the SPF record contains invalid terms - * @throws \SPFLib\Exception\InvalidMacroStringException if the SPF record contains a term with an invalid macro-string * - * @return \SPFLib\Record|null return NULL if no SPF record has been found + * @return string|null return NULL if no SPF TXT record has been found * * @see https://tools.ietf.org/html/rfc7208#section-4.5 */ - public function getRecordFromDomain(string $domain): ?Record + public function getTXTRecordFromDomain(string $domain): ?string { $rawSpfRecords = []; $txtRecords = $this->getDNSResolver()->getTXTRecords($domain); @@ -64,12 +62,33 @@ public function getRecordFromDomain(string $domain): ?Record case 0: return null; case 1: - return $this->getRecordFromTXT($rawSpfRecords[0]); + return $rawSpfRecords[0]; default: throw new Exception\MultipleSPFRecordsException($domain, $rawSpfRecords); } } + /** + * Extract the SPF record associated to a domain. + * + * @throws \SPFLib\Exception\DNSResolutionException in case of DNS resolution errors + * @throws \SPFLib\Exception\MultipleSPFRecordsException if the domain has more that 1 SPF record + * @throws \SPFLib\Exception\InvalidTermException if the SPF record contains invalid terms + * @throws \SPFLib\Exception\InvalidMacroStringException if the SPF record contains a term with an invalid macro-string + * + * @return \SPFLib\Record|null return NULL if no SPF record has been found + * + * @see https://tools.ietf.org/html/rfc7208#section-4.5 + */ + public function getRecordFromDomain(string $domain): ?Record + { + $txtRecord = $this->getTXTRecordFromDomain($domain); + if ($txtRecord === null) { + return null; + } + return $this->getRecordFromTXT($txtRecord); + } + /** * Parse a TXT record and extract the SPF data. * From 3c373e1bbf68bb712aff18e0dc6c5f085b8e92dd Mon Sep 17 00:00:00 2001 From: Brandon Fowler Date: Sat, 21 Jun 2025 18:08:13 -0400 Subject: [PATCH 2/3] Add newline to follow coding style --- src/Decoder.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Decoder.php b/src/Decoder.php index bdd6630..7030fc6 100644 --- a/src/Decoder.php +++ b/src/Decoder.php @@ -86,6 +86,7 @@ public function getRecordFromDomain(string $domain): ?Record if ($txtRecord === null) { return null; } + return $this->getRecordFromTXT($txtRecord); } From 4da1fd5ff1b032b3628d0aa67076a415e66b2e96 Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Mon, 23 Jun 2025 14:12:55 +0200 Subject: [PATCH 3/3] Let getTXTRecordFromDomain() return an empty string in case there's no SFP record --- src/Decoder.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Decoder.php b/src/Decoder.php index 7030fc6..c4d10bd 100644 --- a/src/Decoder.php +++ b/src/Decoder.php @@ -45,11 +45,11 @@ public function __construct(?Resolver $dnsResolver = null, ?MacroStringDecoder $ * @throws \SPFLib\Exception\DNSResolutionException in case of DNS resolution errors * @throws \SPFLib\Exception\MultipleSPFRecordsException if the domain has more that 1 SPF record * - * @return string|null return NULL if no SPF TXT record has been found + * @return string returns an empty string if no SPF TXT record has been found * * @see https://tools.ietf.org/html/rfc7208#section-4.5 */ - public function getTXTRecordFromDomain(string $domain): ?string + public function getTXTRecordFromDomain(string $domain): string { $rawSpfRecords = []; $txtRecords = $this->getDNSResolver()->getTXTRecords($domain); @@ -60,7 +60,7 @@ public function getTXTRecordFromDomain(string $domain): ?string } switch (count($rawSpfRecords)) { case 0: - return null; + return ''; case 1: return $rawSpfRecords[0]; default: @@ -83,7 +83,7 @@ public function getTXTRecordFromDomain(string $domain): ?string public function getRecordFromDomain(string $domain): ?Record { $txtRecord = $this->getTXTRecordFromDomain($domain); - if ($txtRecord === null) { + if ($txtRecord === '') { return null; }