Something went wrong. Try again.
Sender Policy Framework (RFC 7208) for Zig: the record grammar, the macro language, and an evaluator that takes its resolver from you.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! The openspf.org conformance suite, run against `Zone`.//!//! The suite is 203 cases written as zone data and expected results —//! "this domain publishes this, that host connects, the answer must be//! `fail`" — which is exactly the shape a resolver handed in as a//! parameter can be fed. It needs no network, no nameserver and no//! virtual machine, and it is the only way to find out whether this//! implementation agrees with everybody else's rather than with itself.//!//! It is a lazy dependency and nothing from it is copied into this//! repository://!//! ```sh//! zig build test -Dconformance//! ```//!//! What is here is a reader for the subset of YAML the file uses. That//! is less work than it sounds — the file is machine-written and//! regular — and much less than a general parser, which is the kind of//! dependency a test suite should not need.
const std = @import("std");const z46 = @import("z46");const Zone = @import("Zone.zig");const checker = @import("check.zig");const Result = checker.Result;
const Allocator = std.mem.Allocator;
const Case = struct { name: []const u8, helo: []const u8 = "", host: []const u8 = "", mailfrom: []const u8 = "", /// More than one when the suite accepts either answer, which it does /// where the specification leaves a choice. results: [2]?Result = .{ null, null }, /// The text the explanation must expand to. `DEFAULT` means the /// implementation's own, so there is nothing to compare. explanation: ?[]const u8 = null,
fn accepts(case: Case, result: Result) bool { for (case.results) |allowed| { if (allowed == result) return true; } return false; }};
const Section = struct { description: []const u8 = "", cases: std.ArrayList(Case) = .empty, entries: std.ArrayList(Zone.Entry) = .empty, /// Names the suite marks `TIMEOUT`, which is a nameserver that does /// not answer. failing: std.ArrayList([]const u8) = .empty,};
/// Every record in the suite, before the SPF-to-TXT rule below is/// applied. `spf` is kept apart from `txt` because one section turns on/// the difference.const Raw = struct { name: []const u8, kind: Kind, value: []const u8,
const Kind = enum { txt, spf, a, aaaa, mx, ptr, cname };};
const Parser = struct { arena: Allocator, sections: std.ArrayList(Section) = .empty, raw: std.ArrayList(Raw) = .empty,
const Mode = enum { none, tests, zonedata };
fn parse(arena: Allocator, text: []const u8) !std.ArrayList(Section) { var parser: Parser = .{ .arena = arena }; var mode: Mode = .none; var zone_name: []const u8 = ""; var section: ?*Section = null; var fold_indent: ?usize = null; var raw_start: usize = 0;
var lines = std.mem.splitScalar(u8, text, '\n'); while (lines.next()) |line| { const stripped = std.mem.trimEnd(u8, line, "\r"); const indent = leading(stripped); const content = stripped[indent..];
// A folded scalar runs until something at or left of the key // that introduced it. Only `description` and `comment` use // one, and neither is needed here. if (fold_indent) |at| { if (content.len == 0 or indent > at) continue; fold_indent = null; } if (content.len == 0 or content[0] == '#') continue;
if (std.mem.eql(u8, content, "---")) { // Each document is one section of the suite, and each // gets its own zone: the same name means different // things in different sections. try parser.finish(section, raw_start); raw_start = parser.raw.items.len; section = try parser.sections.addOne(arena); section.?.* = .{}; mode = .none; continue; } const current = section orelse continue;
// A zone record is a sequence item rather than a mapping, // and `- TIMEOUT` has no colon in it at all. if (indent == 4 and mode == .zonedata) { try parser.record(current, zone_name, content); continue; }
// A key ends at the first colon followed by a space or the // end of the line. Not simply the first colon: a zone name // may contain them — `a:foo:bar/baz.example.com` is a real // test — and so may a value. const colon = keyEnd(content) orelse continue; const key = try parser.scalar(content[0..colon]); const value = std.mem.trimStart(u8, content[colon + 1 ..], " "); if (isFold(value)) fold_indent = indent;
switch (indent) { 0 => { if (std.mem.eql(u8, key, "description") and !isFold(value)) { current.description = try parser.scalar(value); } else if (std.mem.eql(u8, key, "tests")) { mode = .tests; } else if (std.mem.eql(u8, key, "zonedata")) { mode = .zonedata; } }, 2 => switch (mode) { .tests => try current.cases.append(arena, .{ .name = key }), .zonedata => zone_name = key, .none => {}, }, 4 => switch (mode) { .tests => { if (current.cases.items.len == 0) continue; const case = ¤t.cases.items[current.cases.items.len - 1]; try parser.field(case, key, value); }, .zonedata, .none => {}, }, else => {}, } } try parser.finish(section, raw_start); return parser.sections; }
fn field(parser: *Parser, case: *Case, key: []const u8, value: []const u8) !void { if (std.mem.eql(u8, key, "helo")) { case.helo = try parser.scalar(value); } else if (std.mem.eql(u8, key, "host")) { case.host = try parser.scalar(value); } else if (std.mem.eql(u8, key, "mailfrom")) { case.mailfrom = try parser.scalar(value); } else if (std.mem.eql(u8, key, "explanation")) { case.explanation = try parser.scalar(value); } else if (std.mem.eql(u8, key, "result")) { if (value.len > 0 and value[0] == '[') { var index: usize = 0; const items = try parser.flow(value); for (items.items) |item| { if (index == case.results.len) break; case.results[index] = resultFrom(item) orelse continue; index += 1; } } else { case.results[0] = resultFrom(try parser.scalar(value)); } } }
fn record(parser: *Parser, section: *Section, name: []const u8, content: []const u8) !void { if (!std.mem.startsWith(u8, content, "- ")) return; const body = std.mem.trimStart(u8, content[2..], " "); if (std.mem.eql(u8, body, "TIMEOUT")) { try section.failing.append(parser.arena, name); return; } const colon = keyEnd(body) orelse return; const tag = body[0..colon]; const value = std.mem.trimStart(u8, body[colon + 1 ..], " ");
const kind: Raw.Kind = if (std.mem.eql(u8, tag, "TXT")) .txt else if (std.mem.eql(u8, tag, "SPF")) .spf else if (std.mem.eql(u8, tag, "A")) .a else if (std.mem.eql(u8, tag, "AAAA")) .aaaa else if (std.mem.eql(u8, tag, "MX")) .mx else if (std.mem.eql(u8, tag, "PTR")) .ptr else if (std.mem.eql(u8, tag, "CNAME")) .cname else return;
var text: []const u8 = undefined; if (value.len > 0 and value[0] == '[') { const items = try parser.flow(value); switch (kind) { // An MX is written `[preference, name]`, and SPF never // asks about the preference: `mx` is a membership test // rather than a routing decision. .mx => text = if (items.items.len > 1) items.items[1] else return, // §3.3: a record published as several character-strings // is joined with nothing between them, which is how a // record longer than 255 octets is carried and what // `v=spf1` `mx` joining to `v=spf1mx` is testing. else => { var joined: std.ArrayList(u8) = .empty; for (items.items) |item| try joined.appendSlice(parser.arena, item); text = joined.items; }, } } else { text = try parser.scalar(value); } try parser.raw.append(parser.arena, .{ .name = name, .kind = kind, .value = text }); }
/// The suite's marker for "this record type answers with nothing", /// which is not the same as a record whose text is `NONE`. fn isNone(value: []const u8) bool { return std.mem.eql(u8, value, "NONE"); }
/// Turns the suite's records into the zone the evaluator sees. /// /// RFC 7208 withdrew the SPF resource record type, so this library /// reads TXT and nothing else — but the suite still writes most of /// its zone data as `SPF:`, with a note at the top saying drivers /// are expected to duplicate those into TXT. The one section that /// tests the withdrawal publishes both types for the same name and /// expects the TXT to win, so the rule that satisfies the whole /// file is: an `SPF:` record becomes a TXT record unless the name /// already has one of its own. fn finish(parser: *Parser, section: ?*Section, from: usize) !void { const current = section orelse return; const raw = parser.raw.items[from..];
// A `TIMEOUT` beside a real TXT record is the suite saying that // the *type SPF* query times out — the case its own comment // calls "a popular braindead DNS server". RFC 7208 withdrew that // type and this library never asks for it, so such a name // answers from its TXT record rather than timing out. A TIMEOUT // with no TXT beside it is the TXT query itself failing, which // is a temperror and stays one. var kept: std.ArrayList([]const u8) = .empty; for (current.failing.items) |name| { var has_txt = false; for (raw) |entry| { if (entry.kind != .txt) continue; if (isNone(entry.value)) continue; if (!std.ascii.eqlIgnoreCase(entry.name, name)) continue; has_txt = true; } if (!has_txt) try kept.append(parser.arena, name); } current.failing = kept;
for (raw) |entry| { if (isNone(entry.value)) continue; const data: Zone.Data = switch (entry.kind) { .txt => .{ .txt = entry.value }, .a => .{ .a = entry.value }, .aaaa => .{ .aaaa = entry.value }, .mx => .{ .mx = entry.value }, .ptr => .{ .ptr = entry.value }, // A real resolver follows an alias without being asked, // so `Zone` does too and the record carries through. .cname => .{ .cname = entry.value }, .spf => continue, }; try current.entries.append(parser.arena, .{ .name = entry.name, .data = data }); } for (raw) |entry| { if (entry.kind != .spf) continue; if (isNone(entry.value)) continue; // A `NONE` counts here even though it becomes no record: it // is the suite saying the TXT type has been accounted for // at this name, which is exactly what suppresses the // duplication. `spfonly` is the case — a type SPF record // and nothing else, which a TXT-only implementation must // see as no policy at all. var has_txt = false; for (raw) |other| { if (other.kind != .txt) continue; if (!std.ascii.eqlIgnoreCase(other.name, entry.name)) continue; has_txt = true; break; } if (has_txt) continue; try current.entries.append(parser.arena, .{ .name = entry.name, .data = .{ .txt = entry.value }, }); } }
fn scalar(parser: *Parser, text: []const u8) ![]const u8 { if (text.len == 0) return ""; return switch (text[0]) { '\'' => parser.singleQuoted(text), '"' => parser.doubleQuoted(text), else => std.mem.trimEnd(u8, text, " "), }; }
fn singleQuoted(parser: *Parser, text: []const u8) ![]const u8 { var out: std.ArrayList(u8) = .empty; var index: usize = 1; while (index < text.len) : (index += 1) { if (text[index] != '\'') { try out.append(parser.arena, text[index]); continue; } // A doubled quote is one quote; a single one ends the scalar. if (index + 1 < text.len and text[index + 1] == '\'') { try out.append(parser.arena, '\''); index += 1; continue; } break; } return out.items; }
fn doubleQuoted(parser: *Parser, text: []const u8) ![]const u8 { var out: std.ArrayList(u8) = .empty; var index: usize = 1; while (index < text.len) : (index += 1) { switch (text[index]) { '"' => break, '\\' => { index += 1; if (index >= text.len) break; switch (text[index]) { 'n' => try out.append(parser.arena, '\n'), 't' => try out.append(parser.arena, '\t'), 'r' => try out.append(parser.arena, '\r'), '0' => try out.append(parser.arena, 0), // `\xEF\xBB\xBF` — the byte order mark the // non-ASCII cases hide inside a record. 'x' => { if (index + 2 >= text.len) break; const byte = std.fmt.parseInt(u8, text[index + 1 ..][0..2], 16) catch 0; try out.append(parser.arena, byte); index += 2; }, else => try out.append(parser.arena, text[index]), } }, else => try out.append(parser.arena, text[index]), } } return out.items; }
/// A flow sequence: `[a, b]`, whose elements may be quoted. fn flow(parser: *Parser, text: []const u8) !std.ArrayList([]const u8) { var items: std.ArrayList([]const u8) = .empty; var index: usize = 1; while (index < text.len and text[index] != ']') { while (index < text.len and (text[index] == ' ' or text[index] == ',')) index += 1; if (index >= text.len or text[index] == ']') break; const start = index; var quote: u8 = 0; if (text[index] == '\'' or text[index] == '"') { quote = text[index]; index += 1; while (index < text.len and text[index] != quote) index += 1; index += 1; } else { while (index < text.len and text[index] != ',' and text[index] != ']') index += 1; } try items.append(parser.arena, try parser.scalar( std.mem.trimEnd(u8, text[start..index], " "), )); } return items; }};
/// The index of the colon that ends a mapping key: the first one/// followed by a space or the end of the line.fn keyEnd(content: []const u8) ?usize { var index: usize = 0; while (std.mem.indexOfScalarPos(u8, content, index, ':')) |at| { if (at + 1 == content.len or content[at + 1] == ' ') return at; index = at + 1; } return null;}
fn leading(line: []const u8) usize { var count: usize = 0; while (count < line.len and line[count] == ' ') count += 1; return count;}
fn isFold(value: []const u8) bool { return value.len > 0 and (value[0] == '>' or value[0] == '|');}
fn resultFrom(text: []const u8) ?Result { const trimmed = std.mem.trim(u8, text, " '\""); inline for (@typeInfo(Result).@"enum".fields) |field| { if (std.mem.eql(u8, trimmed, field.name)) return @field(Result, field.name); } return null;}
/// Cases whose expected *explanation text* this implementation does not/// reproduce byte for byte, with the reason. The `result` is still/// checked; only the string comparison is skipped.////// There is one, and it is a disagreement between the suite and the RFC/// rather than a bug: `v-macro-ip6` expects `%{ir}` to expand an IPv6/// address to nibbles in *uppercase* hexadecimal, while RFC 7208 §7.4's/// own worked example writes them in lowercase — which is what the unit/// test for that macro transcribes, and what RFC 5952 makes canonical/// for an address. The suite writes DNS names in uppercase throughout/// (its reverse zone names are `...0.F.F.0.8.B.D.0.1.0.0.2.ip6.arpa`),/// which costs nothing where a name is compared, because names are/// compared without case — and costs exactly this where a name is/// compared as part of a sentence.const explanation_exceptions = [_][]const u8{"v-macro-ip6"};
fn exempt(name: []const u8) bool { for (explanation_exceptions) |exception| { if (std.mem.eql(u8, exception, name)) return true; } return false;}
const testing = std.testing;
test "the openspf.org conformance suite" { if (comptime !@import("build_options").conformance) return error.SkipZigTest; if (comptime @import("build_options").conformance) { var arena_state: std.heap.ArenaAllocator = .init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator();
const sections = try Parser.parse(arena, @embedFile("rfc7208_tests_yml")); var total: usize = 0; var failures: usize = 0; var exceptions: usize = 0;
for (sections.items) |section| { const zone: Zone = .{ .entries = section.entries.items, .failing = section.failing.items, }; for (section.cases.items) |case| { if (case.results[0] == null) continue; total += 1;
const address = z46.Address.parse(case.host) catch { std.debug.print( "{s} / {s}: cannot parse host {s}\n", .{ section.description, case.name, case.host }, ); failures += 1; continue; }; var explanation: [512]u8 = undefined; const outcome = checker.checkMailFrom( testing.io, zone.resolver(), address, case.helo, case.mailfrom, .{ .explanation = &explanation }, ); if (!case.accepts(outcome.result)) { std.debug.print("{s} / {s}: expected {t}{s}{s}, got {t}\n", .{ section.description, case.name, case.results[0].?, if (case.results[1] != null) " or " else "", if (case.results[1]) |second| @tagName(second) else "", outcome.result, }); failures += 1; continue; } // `DEFAULT` means whatever the implementation says, so // there is nothing to compare it against. if (case.explanation) |expected| { if (std.mem.eql(u8, expected, "DEFAULT")) continue; if (exempt(case.name)) { exceptions += 1; continue; } const got = outcome.explanation orelse ""; if (!std.mem.eql(u8, expected, got)) { std.debug.print("{s} / {s}: explanation \"{s}\", got \"{s}\"\n", .{ section.description, case.name, expected, got, }); failures += 1; } } } }
std.debug.print( "\nconformance: {d} of {d} cases pass, {d} explanation text exempt\n", .{ total - failures, total, exceptions }, ); try testing.expectEqual(@as(usize, 0), failures); // The exemption list is not allowed to rot into a way of // ignoring failures: every entry in it must still be a case // that reaches the comparison. try testing.expectEqual(explanation_exceptions.len, exceptions); }}