Skip to content

drakkar-lig/abnf-dmarc-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dmarcparser

dmarcparser parses a TXT resource record and determines whether it contains a valid DMARC record including syntax and semantics validation. The syntax parser is built on top of the apg-py library for parsing ABNF and the ABNF grammar defined in RFCs 7489, 6376, 3986. The semantics validation follows the specification from the DMARC RFC7489 such as tag default values, ignoring unknown tags, the sp inheritance, or p-downgrade in the case of a valid rua tag present in the record. In case of a valid DMARC record, the result object contains the parsed (understood) values following the specification.

Install :

pip install git+https://github.com/drakkar-lig/abnf-dmarc-parser

Usage

Parsing a valid DMARC record:

from dmarcparser import DmarcParser

dmarc_record = "v=DMARC1; p=reject; rua=mailto:[email protected]; ruf=mailto:[email protected]; fo=0; adkim=r; aspf=r"
parser = DmarcParser()
result = parser.parse(dmarc_record, follow_downgrade=True)

assert result.v.effective_value == "DMARC1"
assert result.p.effective_value == "reject"
assert result.sp.effective_value == "reject"
assert result.rua.valid[0]['email'] == "[email protected]"

Parsing an invalid DMARC record raises an exception containing an error code an explanation:

from dmarcparser import DmarcParser, DmarcException

dmarc_record = "v=DMARC1; p=invalidstring;"
parser = DmarcParser()
try:
    result = parser.parse(dmarc_record, follow_downgrade=True)
except DmarcException as e:
    print(f"error in DMARC record: {e.value}")

About

DMARC record Python parser.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages