12func (e parseErr) Error() string {
16// ParseRecord parses a DMARC TXT record.
18// Fields and values are are case-insensitive in DMARC are returned in lower case
19// for easy comparison.
21// DefaultRecord provides default values for tags not present in s.
22func ParseRecord(s string) (record *Record, isdmarc bool, rerr error) {
23 return parseRecord(s, true)
26// ParseRecordNoRequired is like ParseRecord, but don't check for required fields
27// for regular DMARC records. Useful for checking the _report._dmarc record.
28func ParseRecordNoRequired(s string) (record *Record, isdmarc bool, rerr error) {
29 return parseRecord(s, false)
32func parseRecord(s string, checkRequired bool) (record *Record, isdmarc bool, rerr error) {
38 if err, ok := x.(parseErr); ok {
53 r.Version = p.xtakecase("DMARC1")
57 seen := map[string]bool{}
64 w := strings.ToLower(W)
66 // RFC does not say anything about duplicate tags. They can only confuse, so we
68 p.xerrorf("duplicate tag %q", W)
78 // We just parse until the next semicolon or end.
88 p.xerrorf("p= (policy) must be first tag")
90 r.Policy = DMARCPolicy(p.xtakelist("none", "quarantine", "reject"))
92 r.SubdomainPolicy = DMARCPolicy(p.xkeyword())
93 // note: we check if the value is valid before returning.
95 r.AggregateReportAddresses = append(r.AggregateReportAddresses, p.xuri())
99 r.AggregateReportAddresses = append(r.AggregateReportAddresses, p.xuri())
103 r.FailureReportAddresses = append(r.FailureReportAddresses, p.xuri())
107 r.FailureReportAddresses = append(r.FailureReportAddresses, p.xuri())
111 r.ADKIM = Align(p.xtakelist("r", "s"))
113 r.ASPF = Align(p.xtakelist("r", "s"))
115 r.AggregateReportingInterval = p.xnumber()
117 r.FailureReportingOptions = []string{p.xtakelist("0", "1", "d", "s")}
121 r.FailureReportingOptions = append(r.FailureReportingOptions, p.xtakelist("0", "1", "d", "s"))
125 r.ReportingFormat = []string{p.xkeyword()}
129 r.ReportingFormat = append(r.ReportingFormat, p.xkeyword())
133 r.Percentage = p.xnumber()
134 if r.Percentage > 100 {
135 p.xerrorf("bad percentage %d", r.Percentage)
139 if !p.take(";") && !p.empty() {
140 p.xerrorf("expected ;")
145 // able to parse a record without a "p" or with invalid "sp" tag.
146 sp := r.SubdomainPolicy
147 if checkRequired && (!seen["p"] || sp != PolicyEmpty && sp != PolicyNone && sp != PolicyQuarantine && sp != PolicyReject) {
148 if len(r.AggregateReportAddresses) > 0 {
149 r.Policy = PolicyNone
150 r.SubdomainPolicy = PolicyEmpty
152 p.xerrorf("invalid (subdomain)policy and no valid aggregate reporting address")
165// toLower lower cases bytes that are A-Z. strings.ToLower does too much. and
166// would replace invalid bytes with unicode replacement characters, which would
167// break our requirement that offsets into the original and upper case strings
168// point to the same character.
169func toLower(s string) string {
171 for i, c := range r {
172 if c >= 'A' && c <= 'Z' {
179func newParser(s string) *parser {
186func (p *parser) xerrorf(format string, args ...any) {
187 msg := fmt.Sprintf(format, args...)
189 msg += fmt.Sprintf(" (remain %q)", p.s[p.o:])
194func (p *parser) empty() bool {
195 return p.o >= len(p.s)
198func (p *parser) peek(b byte) bool {
199 return p.o < len(p.s) && p.s[p.o] == b
202// case insensitive prefix
203func (p *parser) prefix(s string) bool {
204 return strings.HasPrefix(p.lower[p.o:], s)
207func (p *parser) take(s string) bool {
215func (p *parser) xtaken(n int) string {
216 r := p.lower[p.o : p.o+n]
221func (p *parser) xtake(s string) string {
223 p.xerrorf("expected %q", s)
225 return p.xtaken(len(s))
228func (p *parser) xtakecase(s string) string {
229 if !strings.HasPrefix(p.s[p.o:], s) {
230 p.xerrorf("expected %q", s)
232 r := p.s[p.o : p.o+len(s)]
238func (p *parser) wsp() {
239 for !p.empty() && (p.s[p.o] == ' ' || p.s[p.o] == '\t') {
244// take one of the strings in l.
245func (p *parser) xtakelist(l ...string) string {
246 for _, s := range l {
248 return p.xtaken(len(s))
251 p.xerrorf("expected on one %v", l)
255func (p *parser) xtakefn1case(fn func(byte, int) bool) string {
256 for i, b := range []byte(p.lower[p.o:]) {
259 p.xerrorf("expected at least one char")
265 p.xerrorf("expected at least 1 char")
272// used for the tag keys.
273func (p *parser) xword() string {
274 return p.xtakefn1case(func(c byte, i int) bool {
275 return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9'
279func (p *parser) xdigits() string {
280 return p.xtakefn1case(func(b byte, i int) bool {
287func (p *parser) xuri() URI {
288 // Ideally, we would simply parse an URI here. But a URI can contain a semicolon so
289 // could consume the rest of the DMARC record. Instead, we'll assume no one uses
290 // semicolons in URIs in DMARC records and first collect
291 // space/comma/semicolon/end-separated characters, then parse.
293 v := p.xtakefn1case(func(b byte, i int) bool {
294 return b != ',' && b != ' ' && b != '\t' && b != ';'
296 t := strings.SplitN(v, "!", 2)
297 u, err := url.Parse(t[0])
299 p.xerrorf("parsing uri %q: %s", t[0], err)
302 p.xerrorf("missing scheme in uri")
312 case 'k', 'K', 'm', 'M', 'g', 'G', 't', 'T':
313 uri.Unit = strings.ToLower(o[len(o)-1:])
317 uri.MaxSize, err = strconv.ParseUint(o, 10, 64)
319 p.xerrorf("parsing max size for uri: %s", err)
325func (p *parser) xnumber() int {
326 digits := p.xdigits()
327 v, err := strconv.Atoi(digits)
329 p.xerrorf("parsing %q: %s", digits, err)
334func (p *parser) xkeyword() string {
338 return p.xtakefn1case(func(b byte, i int) bool {
339 return isalphadigit(b) || (b == '-' && i < n-1 && isalphadigit(p.s[p.o+i+1]))
343func isdigit(b byte) bool {
344 return b >= '0' && b <= '9'
347func isalpha(b byte) bool {
348 return b >= 'a' && b <= 'z' || b >= 'A' && b <= 'Z'
351func isalphadigit(b byte) bool {
352 return isdigit(b) || isalpha(b)