mlocati / spf-lib

PHP library to parse, build and validate SPF (Sender Policy Framework) DNS records
MIT License
54 stars 6 forks source link

Error not showing for necessary checks of number for DNS lookups #16

Closed sdebarun closed 3 years ago

sdebarun commented 3 years ago

I am checking a domain here and it gives me an error related to required number of DNS look up to evaluate the spf. But If I use this piece of code from the library documentation, $record = (new \SPFLib\Decoder())->getRecordFromTXT($spfString); $issues = (new \SPFLib\SemanticValidator())->validate($record); foreach ($issues as $key => $issue) { echo $issue, "\n"; } I am not getting any thing similar. I would like to know why there is a difference in result between the library and https://dmarcian.com/ and if anything I can do to fix it. Thank You,

mlocati commented 3 years ago

This library checks direct the DNS lookups only. In your case, we have this SPF record

v=spf1 mx a include:spf.smartemailing.cz include:spf.mailkit.eu include:_spf.google.com ip4:90.181.224.125 ip4:178.238.37.227 ~all

that's a total of 5 queries (mx + a + include + include + include). It seems that dmarcian.com also check the SPF records of the "included" records too:

And that's a total of 11 lookups (the value reported by dmarcian).

sdebarun commented 3 years ago

So I have ran an recursion and it gives me the desired output like dmarcian. But I cannot return the final value as returning will stop the recusrsion. Please take a look in the code and it will be very much helpful if you can help me out. I made the recursion with lumen(micro framework of Laravel) by tinkering the core code (in SemanticValidator by adding return $count at line 85) and returning counts all the time of all the domains included in it. I have a bit success but did not achieve what I exactly wanted. I got the counts of the individual domains of the included domains too but failed to add them all and get a final sum to check if it is greater than 10 or not. Here is my lumen code of the controller.

public function validateSpf($domain){
            //$originalDomainSpfLookUpCount = [];
            $decoder = new \SPFLib\Decoder();
            $originalSpf = $decoder->getRecordFromDomain($domain);
            $record = (new \SPFLib\Decoder())->getRecordFromTXT($originalSpf);
            $issues = (new \SPFLib\SemanticValidator())->validate($record);
            $originalDomainSpfLookUpCount[] = isset($issues['totalCount']) ? $issues['totalCount'] : false ;
            // return $originalDomainSpfLookUpCount;
            //looking for how many included domains are there.

            $partsOfSpf = explode(' ', $originalSpf );
            $includeddomains = [];
            foreach($partsOfSpf as $key => $part){
                $includeddomains[] = str_replace('include:','',strstr($part,"include:"));
                $includeddomains = array_filter($includeddomains);
                // str_replace('inlcude:','');
            }
            //return $includeddomains;

            //running recursion
            if(count($includeddomains) > 0){
                foreach($includeddomains as $index => $include){
                    $this->validateSpf($include);
                }
            }

            echo "<pre>";
            print_r($originalDomainSpfLookUpCount[0]);
            echo "</pre>";
        }

        public function validatedOutput(Request $request){
            return $this->validateSpf($request->domain);
        }
sdebarun commented 3 years ago

Hi, I have done a fresh installation of the package. But Yet I am not getting the count as dmarcian.com. it still returns count 5 for kojenecke-obleceni.eu. Do I need to do something else? I am running this code

   $decoder = new \SPFLib\Decoder();
    $spf = $decoder->getRecordFromDomain('kojenecke-obleceni.eu');
    $record = (new \SPFLib\Decoder())->getRecordFromTXT($spf);
    $issues = (new \SPFLib\SemanticValidator())->validate($record);
    foreach ($issues as $issue) {
        echo (string) $issue, "\n";
    }
mlocati commented 3 years ago

You should use OnlineSemanticValidator instead of SemanticValidator - see the relevant README.md section.

In any case, I haven't published a new SPFLib version including it yet: keep an eye at the releases page. You can be notified for new releases via email by choosing the Watch → Releases only option you can find top right: watch-releases

mlocati commented 3 years ago

I've just published version 3.1.0 with this new OnlineSemanticValidator class.