// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! One `v=spf1` record, parsed and checked but not evaluated. //! //! [RFC 7208 §4.6](https://datatracker.ietf.org/doc/html/rfc7208#section-4.6) //! asks for the whole record to be checked for syntax *before* any of it //! is evaluated, which is why this is a type and not an iterator the //! evaluator drives directly: a record whose last term is malformed must //! be a `permerror` even when an earlier mechanism would have matched. //! Without that rule a sender could hide a typo behind an `all` and never //! learn about it. //! //! Nothing here allocates. Every name, domain-spec and modifier value is //! a slice of the record text the caller still owns. const std = @import("std"); const z46 = @import("z46"); const macro = @import("macro.zig"); const Record = @This(); /// Everything after `v=spf1`, which `directives` walks again. terms: []const u8, /// The `redirect=` modifier's domain-spec, if the record has one. redirect: ?[]const u8, /// The `exp=` modifier's domain-spec, if the record has one. exp: ?[]const u8, /// Whether an `all` mechanism appears anywhere. /// /// §6.1 is easy to miss and changes the answer: `redirect` is ignored /// when the record has an `all`, because `all` always matches and the /// redirect could never be reached. A record with both is not an error, /// it is a record whose redirect is dead. has_all: bool, pub const ParseError = error{ /// The text does not begin with `v=spf1`, so it is not an SPF record /// at all. /// /// This is deliberately not one of the syntax errors: §4.5 filters /// TXT records by this prefix while *selecting* the record, and a /// domain whose TXT records are all something else has no SPF policy /// — which is `none`, not `permerror`. Everything else in this set /// means a record that claimed to be SPF and then was not, which is /// `permerror`. NotSpf, /// A term that is neither a known mechanism nor `name=value`. InvalidTerm, /// A mechanism given an argument it does not take, or denied one it /// requires. InvalidArgument, /// `redirect=` or `exp=` appeared twice. §6. DuplicateModifier, /// A CIDR prefix that is absent, not a number, out of range, or /// written with a leading zero, which the ABNF of §5.6 forbids. InvalidCidr, /// An `ip4:` or `ip6:` network that is not an address. InvalidAddress, /// A domain-spec that does not end in a name. §7.3. InvalidDomainSpec, } || macro.Error; /// The four qualifiers of §4.6.2, which are also the four results a /// matching mechanism can produce. pub const Qualifier = enum { /// `+`, and the default when none is written. pass, /// `-` fail, /// `~` softfail, /// `?` neutral, fn parse(byte: u8) ?Qualifier { return switch (byte) { '+' => .pass, '-' => .fail, '~' => .softfail, '?' => .neutral, else => null, }; } }; /// An `a` or `mx` mechanism: an optional domain and the prefix lengths /// the comparison uses, which differ for IPv4 and IPv6 and are written /// together as `/24//64`. pub const Dual = struct { /// Null means "the domain being evaluated", which is the common case. domain: ?[]const u8 = null, ip4_prefix: u8 = 32, ip6_prefix: u8 = 128, }; /// The eight mechanisms of §5. The domains are unexpanded macro-strings: /// expanding one needs a sender and a client address, which a parser has /// no business knowing. pub const Mechanism = union(enum) { all, include: []const u8, a: Dual, mx: Dual, ptr: ?[]const u8, /// The network as written, with any host bits cleared: a mechanism /// only ever asks whether the client is inside it, so `192.0.2.1/24` /// and `192.0.2.0/24` are the same question. ip4: z46.Ipv4Network, ip6: z46.Ipv6Network, exists: []const u8, /// Whether evaluating this mechanism costs DNS queries, which §4.6.4 /// caps at ten for the whole evaluation. `ip4`, `ip6` and `all` are /// free, and that is why a large sender publishes addresses rather /// than names. pub fn queries(mechanism: Mechanism) bool { return switch (mechanism) { .all, .ip4, .ip6 => false, .include, .a, .mx, .ptr, .exists => true, }; } }; pub const Directive = struct { qualifier: Qualifier = .pass, mechanism: Mechanism, }; pub const Modifier = struct { pub const Kind = enum { redirect, exp, unknown }; kind: Kind, /// The modifier's name as written, which is the only thing that /// distinguishes one unknown modifier from another. name: []const u8, /// The macro-string after the `=`, unexpanded. value: []const u8, }; pub const Term = union(enum) { directive: Directive, modifier: Modifier, }; /// Whether `text` claims to be an SPF record at all: `v=spf1` followed by /// a space or the end of the record. /// /// §4.5 selects a domain's record out of its TXT records with this test /// *before* any of them is parsed, and the distinction it draws is the /// one everything downstream depends on. A TXT record that fails this is /// not an SPF record and is simply not ours — a domain publishing only a /// DKIM key has no SPF policy, which is `none`. A record that passes it /// and then does not parse is a broken policy, which is `permerror`. pub fn isSpf(text: []const u8) bool { const version = "v=spf1"; if (text.len < version.len) return false; if (!std.ascii.eqlIgnoreCase(text[0..version.len], version)) return false; return text.len == version.len or text[version.len] == ' '; } /// Parses and fully validates a record. pub fn parse(text: []const u8) ParseError!Record { if (!isSpf(text)) return error.NotSpf; var record: Record = .{ .terms = text["v=spf1".len..], .redirect = null, .exp = null, .has_all = false, }; var terms = record.iterate(); while (try terms.next()) |term| switch (term) { .directive => |directive| { if (directive.mechanism == .all) record.has_all = true; }, .modifier => |modifier| switch (modifier.kind) { .redirect => { if (record.redirect != null) return error.DuplicateModifier; record.redirect = modifier.value; }, .exp => { if (record.exp != null) return error.DuplicateModifier; record.exp = modifier.value; }, .unknown => {}, }, }; return record; } /// Walks the terms in the order they were written, which is the order /// §4.6.2 evaluates them in. Already validated by `parse`, so the errors /// are unreachable in practice — they are still returned rather than /// asserted, because the alternative is a panic on a record somebody /// else published. pub fn iterate(record: Record) Iterator { return .{ .rest = record.terms }; } pub const Iterator = struct { rest: []const u8, pub fn next(iterator: *Iterator) ParseError!?Term { // `terms = *( 1*SP ( directive / modifier ) )`, so a run of // spaces separates terms and a trailing run is allowed. while (iterator.rest.len > 0 and iterator.rest[0] == ' ') { iterator.rest = iterator.rest[1..]; } if (iterator.rest.len == 0) return null; const end = std.mem.indexOfScalar(u8, iterator.rest, ' ') orelse iterator.rest.len; const text = iterator.rest[0..end]; iterator.rest = iterator.rest[end..]; return try parseTerm(text); } }; fn parseTerm(text: []const u8) ParseError!Term { // A modifier is `name "=" macro-string` and a name holds none of the // characters that begin or separate a mechanism's argument, so // whichever of `=` and the end of the name comes first decides which // of the two this is. `a:foo=bar` is a mechanism, because the run of // name characters stops at the colon. var index: usize = 0; while (index < text.len and isNameByte(text[index])) index += 1; if (index > 0 and std.ascii.isAlphabetic(text[0]) and index < text.len and text[index] == '=') { return .{ .modifier = try parseModifier(text[0..index], text[index + 1 ..]) }; } return .{ .directive = try parseDirective(text) }; } fn isNameByte(byte: u8) bool { return std.ascii.isAlphanumeric(byte) or byte == '-' or byte == '_' or byte == '.'; } fn parseModifier(name: []const u8, value: []const u8) ParseError!Modifier { if (std.ascii.eqlIgnoreCase(name, "redirect")) { try validateDomainSpec(value); return .{ .kind = .redirect, .name = name, .value = value }; } if (std.ascii.eqlIgnoreCase(name, "exp")) { try validateDomainSpec(value); return .{ .kind = .exp, .name = name, .value = value }; } // §6: an unrecognized modifier is ignored, but it still has to be a // macro-string — the record as a whole either matches the grammar or // is a permerror, and "ignored" is about evaluation, not syntax. try macro.validate(value, .{}); return .{ .kind = .unknown, .name = name, .value = value }; } fn parseDirective(text: []const u8) ParseError!Directive { var rest = text; var qualifier: Qualifier = .pass; if (rest.len > 0) { if (Qualifier.parse(rest[0])) |parsed| { qualifier = parsed; rest = rest[1..]; } } // The mechanism name runs to the first `:`, `/` or the end. var index: usize = 0; while (index < rest.len and rest[index] != ':' and rest[index] != '/') index += 1; const name = rest[0..index]; const tail = rest[index..]; const mechanism: Mechanism = if (std.ascii.eqlIgnoreCase(name, "all")) blk: { if (tail.len > 0) return error.InvalidArgument; break :blk .all; } else if (std.ascii.eqlIgnoreCase(name, "include")) blk: { break :blk .{ .include = try requiredDomainSpec(tail) }; } else if (std.ascii.eqlIgnoreCase(name, "exists")) blk: { break :blk .{ .exists = try requiredDomainSpec(tail) }; } else if (std.ascii.eqlIgnoreCase(name, "a")) blk: { break :blk .{ .a = try parseDual(tail) }; } else if (std.ascii.eqlIgnoreCase(name, "mx")) blk: { break :blk .{ .mx = try parseDual(tail) }; } else if (std.ascii.eqlIgnoreCase(name, "ptr")) blk: { // `ptr` takes a domain but never a CIDR. if (tail.len == 0) break :blk .{ .ptr = null }; if (tail[0] != ':') return error.InvalidArgument; break :blk .{ .ptr = try requiredDomainSpec(tail) }; } else if (std.ascii.eqlIgnoreCase(name, "ip4")) blk: { break :blk .{ .ip4 = try parseNet4(tail) }; } else if (std.ascii.eqlIgnoreCase(name, "ip6")) blk: { break :blk .{ .ip6 = try parseNet6(tail) }; } else return error.InvalidTerm; return .{ .qualifier = qualifier, .mechanism = mechanism }; } /// The `":" domain-spec` an `include:` or `exists:` must have. fn requiredDomainSpec(tail: []const u8) ParseError![]const u8 { if (tail.len < 2 or tail[0] != ':') return error.InvalidArgument; const spec = tail[1..]; try validateDomainSpec(spec); return spec; } /// `[ ":" domain-spec ] [ dual-cidr-length ]`, shared by `a` and `mx`. fn parseDual(tail: []const u8) ParseError!Dual { var dual: Dual = .{}; var rest = tail; // The CIDR comes off the end first, because a domain-spec may // legitimately contain a `/` — it is a macro-string, and `/` is one // of the delimiters a macro may be split on. Only a trailing run of // digits after a slash can be a prefix length, so reading from the // right is what disambiguates the two. if (splitPrefix(rest)) |split| { if (split.double) { dual.ip6_prefix = try parsePrefix(split.digits, 128, 3); rest = split.head; // An IPv4 prefix may precede the IPv6 one: `/24//64`. if (splitPrefix(rest)) |first| { if (first.double) return error.InvalidCidr; dual.ip4_prefix = try parsePrefix(first.digits, 32, 2); rest = first.head; } } else { dual.ip4_prefix = try parsePrefix(split.digits, 32, 2); rest = split.head; } } if (rest.len > 0) { if (rest[0] != ':') return error.InvalidArgument; dual.domain = try requiredDomainSpec(rest); } return dual; } const Split = struct { head: []const u8, digits: []const u8, /// Whether the slash was doubled, which makes this an IPv6 prefix. double: bool, }; /// Takes a trailing `/digits` or `//digits` off a mechanism's argument, /// or answers null when there is none. fn splitPrefix(text: []const u8) ?Split { const slash = std.mem.lastIndexOfScalar(u8, text, '/') orelse return null; const digits = text[slash + 1 ..]; if (digits.len == 0) return null; for (digits) |byte| { if (!std.ascii.isDigit(byte)) return null; } if (slash > 0 and text[slash - 1] == '/') { return .{ .head = text[0 .. slash - 1], .digits = digits, .double = true }; } return .{ .head = text[0..slash], .digits = digits, .double = false }; } /// §5.6's ABNF writes a prefix length as `"0" / %x31-39 0*NDIGIT`, so a /// leading zero is a syntax error rather than something to be lenient /// about: `/08` and `/032` are both malformed records. fn parsePrefix(digits: []const u8, max: u8, max_digits: usize) ParseError!u8 { if (digits.len == 0 or digits.len > max_digits) return error.InvalidCidr; if (digits.len > 1 and digits[0] == '0') return error.InvalidCidr; const value = std.fmt.parseInt(u8, digits, 10) catch return error.InvalidCidr; if (value > max) return error.InvalidCidr; return value; } fn parseNet4(tail: []const u8) ParseError!z46.Ipv4Network { if (tail.len < 2 or tail[0] != ':') return error.InvalidArgument; var rest = tail[1..]; var prefix: u8 = 32; // The prefix is split off here rather than handed to z46's network // parser, because §5.6 writes one more narrowly than CIDR does in // general: two digits at most, no leading zero, and none of the // dotted-netmask spellings a configuration file may use. if (splitPrefix(rest)) |split| { if (split.double) return error.InvalidCidr; prefix = try parsePrefix(split.digits, 32, 2); rest = split.head; } // z46's parser is exactly as strict as §5.6's `qnum`: four decimal // octets, no leading zeros, none of the `inet_aton` shorthands. // Being strict rather than generous is the point — `ip4:1.2.3.04` is // a malformed record, not an address, and two implementations that // disagree about which is a security problem rather than a nicety. const address = z46.Ipv4Address.parse(rest) catch return error.InvalidAddress; // Host bits set are allowed: a record may say `ip4:192.0.2.1/24`, // and the only question ever asked of it is `contains`. return z46.Ipv4Network.initTruncate(address, prefix) catch return error.InvalidCidr; } fn parseNet6(tail: []const u8) ParseError!z46.Ipv6Network { if (tail.len < 2 or tail[0] != ':') return error.InvalidArgument; var rest = tail[1..]; var prefix: u8 = 128; if (splitPrefix(rest)) |split| { // A `//` here would be an empty address followed by a prefix. if (split.double) return error.InvalidCidr; prefix = try parsePrefix(split.digits, 128, 3); rest = split.head; } // A scope id (`%eth0`) names an interface on one machine and means // nothing in a record the world reads, so it is refused rather than // resolved — which `parse` does and `parseZoned` would not. const address = z46.Ipv6Address.parse(rest) catch return error.InvalidAddress; return z46.Ipv6Network.initTruncate(address, prefix) catch return error.InvalidCidr; } /// §7.3's `domain-spec`: a macro-string that ends in either a macro or a /// real top label. /// /// The top label is the part that catches typos. `a:foo.1` is malformed /// because a label of digits alone cannot be a top-level domain, and an /// implementation that accepted it would look up a name that can never /// exist and report `softfail` where the domain meant `fail`. fn validateDomainSpec(spec: []const u8) ParseError!void { if (spec.len == 0) return error.InvalidDomainSpec; try macro.validate(spec, .{}); // `domain-end = ( "." toplabel [ "." ] ) / macro-expand`, so a spec // that ends with an expansion needs no further checking. if (spec[spec.len - 1] == '}') return; var rest = spec; // A single trailing dot is the root and is allowed. if (rest[rest.len - 1] == '.') rest = rest[0 .. rest.len - 1]; const dot = std.mem.lastIndexOfScalar(u8, rest, '.') orelse return error.InvalidDomainSpec; try validateTopLabel(rest[dot + 1 ..]); } /// `toplabel = ( *alphanum ALPHA *alphanum ) / ( 1*alphanum "-" *( alphanum / "-" ) alphanum )` fn validateTopLabel(label: []const u8) ParseError!void { if (label.len == 0) return error.InvalidDomainSpec; var has_alpha = false; var has_hyphen = false; for (label) |byte| { if (std.ascii.isAlphabetic(byte)) { has_alpha = true; } else if (byte == '-') { has_hyphen = true; } else if (!std.ascii.isDigit(byte)) { return error.InvalidDomainSpec; } } if (has_hyphen) { // The second alternative: hyphens are allowed inside, never at // either end, and the label need not contain a letter at all. if (label[0] == '-' or label[label.len - 1] == '-') return error.InvalidDomainSpec; return; } // The first: alphanumerics with at least one letter among them. if (!has_alpha) return error.InvalidDomainSpec; } const testing = std.testing; fn collect(text: []const u8, buffer: []Term) ![]Term { const record = try parse(text); var iterator = record.iterate(); var count: usize = 0; while (try iterator.next()) |term| : (count += 1) buffer[count] = term; return buffer[0..count]; } test "a record is its version and then its terms" { var buffer: [8]Term = undefined; const terms = try collect("v=spf1 a mx ip4:192.0.2.0/24 -all", &buffer); try testing.expectEqual(@as(usize, 4), terms.len); try testing.expectEqual(Qualifier.pass, terms[0].directive.qualifier); try testing.expect(terms[0].directive.mechanism == .a); try testing.expect(terms[1].directive.mechanism == .mx); try testing.expect(terms[2].directive.mechanism.ip4.eql( try z46.Ipv4Network.parse("192.0.2.0/24"), )); try testing.expectEqual(Qualifier.fail, terms[3].directive.qualifier); try testing.expect(terms[3].directive.mechanism == .all); } test "the version is matched case-insensitively and must stand alone" { _ = try parse("V=SpF1 -all"); _ = try parse("v=spf1"); // Trailing spaces are allowed by the ABNF and mean no more terms. _ = try parse("v=spf1 "); // §4.5 selects records by this prefix, so anything else is not an // SPF record and the domain has no policy rather than a broken one. try testing.expectError(error.NotSpf, parse("v=spf10 -all")); try testing.expectError(error.NotSpf, parse("v=spf2.0/pra")); try testing.expectError(error.NotSpf, parse("spf1 -all")); try testing.expectError(error.NotSpf, parse("")); try testing.expectError(error.NotSpf, parse("v=spf")); } test "every qualifier" { var buffer: [8]Term = undefined; const terms = try collect("v=spf1 +all -all ~all ?all", &buffer); try testing.expectEqual(Qualifier.pass, terms[0].directive.qualifier); try testing.expectEqual(Qualifier.fail, terms[1].directive.qualifier); try testing.expectEqual(Qualifier.softfail, terms[2].directive.qualifier); try testing.expectEqual(Qualifier.neutral, terms[3].directive.qualifier); } test "a dual CIDR is two prefixes written together" { var buffer: [8]Term = undefined; const terms = try collect("v=spf1 a/24 mx//64 a:x.example.com/24//64 mx", &buffer); try testing.expectEqual(@as(u8, 24), terms[0].directive.mechanism.a.ip4_prefix); try testing.expectEqual(@as(u8, 128), terms[0].directive.mechanism.a.ip6_prefix); try testing.expectEqual(@as(u8, 32), terms[1].directive.mechanism.mx.ip4_prefix); try testing.expectEqual(@as(u8, 64), terms[1].directive.mechanism.mx.ip6_prefix); const both = terms[2].directive.mechanism.a; try testing.expectEqualStrings("x.example.com", both.domain.?); try testing.expectEqual(@as(u8, 24), both.ip4_prefix); try testing.expectEqual(@as(u8, 64), both.ip6_prefix); // The defaults are the whole address, which is what makes a bare // `mx` mean "the exchangers themselves" rather than their networks. try testing.expectEqual(@as(?[]const u8, null), terms[3].directive.mechanism.mx.domain); try testing.expectEqual(@as(u8, 32), terms[3].directive.mechanism.mx.ip4_prefix); try testing.expectEqual(@as(u8, 128), terms[3].directive.mechanism.mx.ip6_prefix); } test "a prefix out of range or written with a leading zero is malformed" { try testing.expectError(error.InvalidCidr, parse("v=spf1 a/33")); try testing.expectError(error.InvalidCidr, parse("v=spf1 a//129")); try testing.expectError(error.InvalidCidr, parse("v=spf1 ip4:192.0.2.0/33")); // §5.6 writes the length as `"0" / %x31-39 0*1DIGIT`, so a leading // zero is not a lenient spelling of the same number. try testing.expectError(error.InvalidCidr, parse("v=spf1 a/08")); try testing.expectError(error.InvalidCidr, parse("v=spf1 a/032")); // But zero itself is a legitimate prefix: `ip4:0.0.0.0/0` is how a // record says "anywhere", and people do publish it. _ = try parse("v=spf1 ip4:0.0.0.0/0 a/0"); // And a network written with its host bits set is legal here, where // it would not be in a configuration file: the mechanism only ever // asks whether the client is inside it. var buffer: [4]Term = undefined; const terms = try collect("v=spf1 ip4:192.0.2.130/24", &buffer); try testing.expect(terms[0].directive.mechanism.ip4.contains( try z46.Ipv4Address.parse("192.0.2.1"), )); } test "an ip4 network is a strict dotted quad" { // The strictness is the point: each of these is a typo that would // otherwise be silently read as some other address. try testing.expectError(error.InvalidAddress, parse("v=spf1 ip4:1.2.3.04")); try testing.expectError(error.InvalidAddress, parse("v=spf1 ip4:1.2.3")); try testing.expectError(error.InvalidAddress, parse("v=spf1 ip4:1.2.3.4.5")); try testing.expectError(error.InvalidAddress, parse("v=spf1 ip4:1.2.3.256")); try testing.expectError(error.InvalidAddress, parse("v=spf1 ip4:example.com")); try testing.expectError(error.InvalidArgument, parse("v=spf1 ip4")); } test "an ip6 network is an RFC 4291 address with no scope" { var buffer: [8]Term = undefined; const terms = try collect("v=spf1 ip6:2001:db8::/32 ip6:::1", &buffer); try testing.expect(terms[0].directive.mechanism.ip6.eql( try z46.Ipv6Network.parse("2001:db8::/32"), )); try testing.expect(terms[1].directive.mechanism.ip6.contains(.loopback)); // An interface scope is a local notion and means nothing in a // record somebody else reads. try testing.expectError(error.InvalidAddress, parse("v=spf1 ip6:fe80::1%eth0")); try testing.expectError(error.InvalidAddress, parse("v=spf1 ip6:1.2.3.4")); } test "mechanisms that must or must not take an argument" { try testing.expectError(error.InvalidArgument, parse("v=spf1 all:example.com")); try testing.expectError(error.InvalidArgument, parse("v=spf1 all/24")); try testing.expectError(error.InvalidArgument, parse("v=spf1 include")); try testing.expectError(error.InvalidArgument, parse("v=spf1 include:")); try testing.expectError(error.InvalidArgument, parse("v=spf1 exists")); // `ptr` takes a domain but never a prefix: there is no network to // apply one to, only a name to compare. try testing.expectError(error.InvalidArgument, parse("v=spf1 ptr/24")); var buffer: [4]Term = undefined; const terms = try collect("v=spf1 ptr ptr:example.com", &buffer); try testing.expectEqual(@as(?[]const u8, null), terms[0].directive.mechanism.ptr); try testing.expectEqualStrings("example.com", terms[1].directive.mechanism.ptr.?); } test "an unknown mechanism is a syntax error, not something to skip" { try testing.expectError(error.InvalidTerm, parse("v=spf1 zz")); try testing.expectError(error.InvalidTerm, parse("v=spf1 a1:example.com")); // A term that is neither a mechanism nor `name=value` at all. try testing.expectError(error.InvalidTerm, parse("v=spf1 -")); } test "modifiers are found, and the two known ones may not repeat" { const record = try parse("v=spf1 redirect=_spf.example.com exp=why.example.com"); try testing.expectEqualStrings("_spf.example.com", record.redirect.?); try testing.expectEqualStrings("why.example.com", record.exp.?); try testing.expect(!record.has_all); try testing.expectError(error.DuplicateModifier, parse("v=spf1 redirect=a.example.com redirect=b.example.com")); try testing.expectError(error.DuplicateModifier, parse("v=spf1 exp=a.example.com exp=b.example.com")); } test "an unknown modifier is ignored but still has to parse" { const record = try parse("v=spf1 ptr-scope=example.com -all"); try testing.expectEqual(@as(?[]const u8, null), record.redirect); try testing.expect(record.has_all); // "Ignored" is about evaluation. The record as a whole either // matches the grammar or it is a permerror, so a malformed macro // inside a modifier nobody reads still breaks the record. try testing.expectError(error.InvalidMacro, parse("v=spf1 whatever=%{z} -all")); } test "a colon binds tighter than an equals sign" { // `a:foo=bar` is the `a` mechanism with a domain that happens to // contain an equals sign, not a modifier named `a:foo`, because a // modifier's name cannot hold a colon. var buffer: [4]Term = undefined; const terms = try collect("v=spf1 a:foo=bar.example.com", &buffer); try testing.expectEqualStrings("foo=bar.example.com", terms[0].directive.mechanism.a.domain.?); } test "redirect is dead when the record has an all" { // §6.1: `all` always matches, so a redirect after one can never be // reached. Not an error — a record whose redirect does nothing. const record = try parse("v=spf1 -all redirect=_spf.example.com"); try testing.expect(record.has_all); try testing.expect(record.redirect != null); } test "a domain-spec must end in a real top label or a macro" { _ = try parse("v=spf1 a:example.com"); _ = try parse("v=spf1 a:example.com."); _ = try parse("v=spf1 exists:%{ir}.%{v}._spf.%{d}"); _ = try parse("v=spf1 a:%{d}"); _ = try parse("v=spf1 a:foo.e-1"); // A top label of digits alone cannot exist, so a record naming one // would look up a name that can never resolve and report the wrong // answer rather than the syntax error it is. try testing.expectError(error.InvalidDomainSpec, parse("v=spf1 a:foo.1")); try testing.expectError(error.InvalidDomainSpec, parse("v=spf1 a:foo.-com")); try testing.expectError(error.InvalidDomainSpec, parse("v=spf1 a:foo.com-")); // `domain-end` requires the dot: a single label is not a domain-spec. try testing.expectError(error.InvalidDomainSpec, parse("v=spf1 a:foo")); } test "terms are separated by runs of spaces and nothing else" { var buffer: [8]Term = undefined; const terms = try collect("v=spf1 a -all ", &buffer); try testing.expectEqual(@as(usize, 2), terms.len); // A tab is not SP, so it is part of the term and makes it malformed // rather than separating two good ones. try testing.expectError(error.InvalidTerm, parse("v=spf1 a\t-all")); } test "a record with a malformed term at the end is malformed as a whole" { // The reason this type exists: an implementation that evaluated // terms as it parsed them would match `+all` and never reach the // typo, so a sender would never learn their record was broken. try testing.expectError(error.InvalidCidr, parse("v=spf1 +all a/99")); }