mlocati / spf-lib

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

OnlineSemanticValidator issue #22

Closed advokatb closed 2 years ago

advokatb commented 2 years ago
$validator = new \SPFLib\OnlineSemanticValidator();
// Check an online domain
$issues = $validator->validateDomain('google.com');
foreach ($issues as $issue) {
    echo (string) $issue, "\n";
}

but this code returns an empty array. Can you please take a look if everything works as expected? I'm trying to get SPF hierarchy tree with sub-elements, but no luck with the code.

mlocati commented 2 years ago

Is the SPF record of google.com semantically wrong?

advokatb commented 2 years ago

Is the SPF record of google.com semantically wrong?

Seems it's OK https://easydmarc.com/tools/spf-lookup/google.com?domain=google.com I want to create such tree http://prntscr.com/22g2227 but even var_dump() shows nothing:

$validator = new \SPFLib\OnlineSemanticValidator();
// Check an online domain
$issues = $validator->validateDomain('google.com');
foreach ($issues as $issue) {
    echo (string) $issue, "\n";
    echo '<pre>';
    var_dump ($issues);
    echo '</pre>';

}

Maybe I'm doing something wrong?

mlocati commented 2 years ago

I want to create such tree http://prntscr.com/22g2227

I can't see any relevant images there (hint: you can paste images directly here while writing a message)

var_dump() shows nothing:

The $issues variable is an empty because the SPF if correct (there's no issue).

If you want to inspect an SPF record, read here

advokatb commented 2 years ago

If you want to inspect an SPF record, read here

I want to achieve such result изображение

Can you please provide the code or point me in the correct direction about how to achieve this hierarchy tree with sub-elements?

mlocati commented 2 years ago

As I wrote before, you can use the SPFLib\Decoder class.

Here's a quick and dirty example:

use SPFLib\Decoder;
use SPFLib\Term\Modifier\RedirectModifier;
use SPFLib\Term\Mechanism\IncludeMechanism;

function checkDomain(Decoder $decoder, string $domain, int $depth = 0): void
{
    $indent = str_repeat('  ', $depth);
    echo $indent, "# Domain: {$domain}\n";
    $record = $decoder->getRecordFromDomain($domain);
    echo $indent, "- record: {$record}\n";
    echo $indent, "- modifiers:\n";
    foreach ($record->getModifiers() as $modifier) {
        echo $indent, '  ', (string) $modifier, "\n";
    }
    echo $indent, "- terms:\n";
    foreach ($record->getTerms() as $term) {
        echo $indent, '  ', (string) $term, "\n";
    }
    foreach ($record->getModifiers() as $modifier) {
        if ($modifier instanceof RedirectModifier) {
            checkDomain($decoder, (string) $modifier->getDomainSpec(), $depth + 1);
        }
    }
    foreach ($record->getTerms() as $term) {
        if ($term instanceof IncludeMechanism) {
            checkDomain($decoder, (string) $term->getDomainSpec(), $depth + 1);
        }
    }
}

checkDomain(new Decoder(), 'google.com');

its output is

# Domain: google.com
- record: v=spf1 include:_spf.google.com ~all
- modifiers:
- terms:
  include:_spf.google.com
  ~all
  # Domain: _spf.google.com
  - record: v=spf1 include:_netblocks.google.com include:_netblocks2.google.com include:_netblocks3.google.com ~all
  - modifiers:
  - terms:
    include:_netblocks.google.com
    include:_netblocks2.google.com
    include:_netblocks3.google.com
    ~all
    # Domain: _netblocks.google.com
    - record: v=spf1 ip4:35.190.247.0/24 ip4:64.233.160.0/19 ip4:66.102.0.0/20 ip4:66.249.80.0/20 ip4:72.14.192.0/18 ip4:74.125.0.0/16 ip4:108.177.8.0/21 ip4:173.194.0.0/16 ip4:209.85.128.0/17 ip4:216.58.192.0/19 ip4:216.239.32.0/19 ~all
    - modifiers:
    - terms:
      ip4:35.190.247.0/24
      ip4:64.233.160.0/19
      ip4:66.102.0.0/20
      ip4:66.249.80.0/20
      ip4:72.14.192.0/18
      ip4:74.125.0.0/16
      ip4:108.177.8.0/21
      ip4:173.194.0.0/16
      ip4:209.85.128.0/17
      ip4:216.58.192.0/19
      ip4:216.239.32.0/19
      ~all
    # Domain: _netblocks2.google.com
    - record: v=spf1 ip6:2001:4860:4000::/36 ip6:2404:6800:4000::/36 ip6:2607:f8b0:4000::/36 ip6:2800:3f0:4000::/36 ip6:2a00:1450:4000::/36 ip6:2c0f:fb50:4000::/36 ~all
    - modifiers:
    - terms:
      ip6:2001:4860:4000::/36
      ip6:2404:6800:4000::/36
      ip6:2607:f8b0:4000::/36
      ip6:2800:3f0:4000::/36
      ip6:2a00:1450:4000::/36
      ip6:2c0f:fb50:4000::/36
      ~all
    # Domain: _netblocks3.google.com
    - record: v=spf1 ip4:172.217.0.0/19 ip4:172.217.32.0/20 ip4:172.217.128.0/19 ip4:172.217.160.0/20 ip4:172.217.192.0/19 ip4:172.253.56.0/21 ip4:172.253.112.0/20 ip4:108.177.96.0/19 ip4:35.191.0.0/16 ip4:130.211.0.0/22 ~all
    - modifiers:
    - terms:
      ip4:172.217.0.0/19
      ip4:172.217.32.0/20
      ip4:172.217.128.0/19
      ip4:172.217.160.0/20
      ip4:172.217.192.0/19
      ip4:172.253.56.0/21
      ip4:172.253.112.0/20
      ip4:108.177.96.0/19
      ip4:35.191.0.0/16
      ip4:130.211.0.0/22
      ~all