- refactor(api): use audit operation constants and the shared zone not found error in zone handlers - fix(api): return the stored content of rows with check errors - fix(api): return warnings from record deletes, zone creation and import apply - fix(api): allow repairing rows stored with a non canonical zone and invalid SOA sets - fix(check): accept JSON column formatting and an absent SOA serial as canonical content - fix(ui): match audit diff items by identity instead of array index - fix(ui): count the SOA change once, report import warnings and check the request size - fix(ui): edit the selected SOA row, keep the server order, show error rows raw and report write warnings - test(check): cover triggering and clean pairs with exact codes and record ids - test(records): cover SRV port, SOA timer and CAA value boundaries - test(cli): check the exact check output and the exit code of warning-only zones - refactor(import): order current rows with cmp.Compare - test(check): cover malformed stored content and a mailbox stored as an address - test(api): cover zone write failure paths, ambiguous SOA updates and unchanged record detection
198 lines
6.1 KiB
Go
198 lines
6.1 KiB
Go
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
package main
|
|
|
|
import (
|
|
"cmp"
|
|
"encoding/json"
|
|
"io"
|
|
"maps"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func checkZone(zone string, rows []recordRow, now time.Time) []zoneProblem {
|
|
var problems []zoneProblem
|
|
for _, row := range rows {
|
|
problems = append(problems, checkRecord(row, now)...)
|
|
}
|
|
ruleErrors, warnings := zoneRuleProblems(zone, rows, summarizeZone(rows), false)
|
|
problems = append(problems, ruleErrors...)
|
|
return append(problems, warnings...)
|
|
}
|
|
|
|
func checkRecord(row recordRow, now time.Time) []zoneProblem {
|
|
var problems []zoneProblem
|
|
addError := func(code, message string) {
|
|
problems = append(problems, recordProblem(row, "error", code, message))
|
|
}
|
|
warn := func(code, message string) {
|
|
problems = append(problems, recordProblem(row, "warning", code, message))
|
|
}
|
|
|
|
if !strings.HasSuffix(row.Zone, ".") {
|
|
addError("zone_not_fqdn", "stored zone is missing its trailing dot")
|
|
}
|
|
if row.Zone != strings.ToLower(row.Zone) {
|
|
addError("zone_case", "stored zone contains uppercase letters")
|
|
}
|
|
if _, err := normalizeZone(row.Zone); err != nil {
|
|
addError("field_invalid", "zone: "+err.Error())
|
|
}
|
|
if relative, _, err := normalizeOwner(row.Zone, row.Name); err != nil {
|
|
addError("field_invalid", "name: "+err.Error())
|
|
} else if relative != strings.ToLower(row.Name) {
|
|
addError("field_invalid", "name must be stored in relative form with an empty apex")
|
|
} else if err := validateRecordOwner(row.RecordType, relative); err != nil && row.RecordType != "SOA" && row.RecordType != "CNAME" {
|
|
addError("field_invalid", "name: "+err.Error())
|
|
}
|
|
if row.Name != strings.ToLower(row.Name) {
|
|
warn("name_case", "stored owner name contains uppercase letters")
|
|
}
|
|
switch {
|
|
case !row.TTL.Valid:
|
|
addError("ttl_null", "stored TTL is NULL")
|
|
case row.TTL.Int64 < 0:
|
|
addError("ttl_negative", "stored TTL is negative")
|
|
case row.TTL.Int64 > maxRecordSeconds:
|
|
addError("field_invalid", "ttl: exceeds 2147483647")
|
|
case row.TTL.Int64 == 0:
|
|
warn("ttl_zero", "TTL zero is served as 360 seconds")
|
|
}
|
|
|
|
_, knownType := recordRules[row.RecordType]
|
|
if !knownType {
|
|
addError("type_unknown", "stored record type is unsupported")
|
|
}
|
|
if !row.Content.Valid {
|
|
addError("content_null", "stored content is NULL")
|
|
return problems
|
|
}
|
|
if !knownType {
|
|
var object map[string]json.RawMessage
|
|
if err := json.Unmarshal([]byte(row.Content.String), &object); err != nil || object == nil {
|
|
addError("content_json", "stored content must be a JSON object")
|
|
}
|
|
return problems
|
|
}
|
|
decoded, err := inspectRecordContent(row.RecordType, row.Content.String)
|
|
if err != nil {
|
|
addError("content_json", err.Error())
|
|
return problems
|
|
}
|
|
if row.RecordType == "MX" && hasContentKey(decoded.object, "priority") && !hasContentKey(decoded.object, "preference") {
|
|
addError("mx_priority_key", "MX uses the preference key")
|
|
}
|
|
normalized, fields := normalizeRecordData(row.Zone, decoded.data)
|
|
for key, message := range decoded.fields {
|
|
fields[key] = message
|
|
}
|
|
targets := recordDomainFields(decoded.data)
|
|
for _, key := range slices.Sorted(maps.Keys(targets)) {
|
|
name := targets[key]
|
|
if name != "" && !strings.HasSuffix(name, ".") {
|
|
addError("name_not_fqdn", key+" must have a trailing dot")
|
|
} else if key == "MBox" && strings.Contains(name, "@") {
|
|
fields[key] = "must be stored as a DNS name"
|
|
}
|
|
}
|
|
for _, key := range slices.Sorted(maps.Keys(fields)) {
|
|
addError("field_invalid", key+": "+fields[key])
|
|
}
|
|
if soa, ok := decoded.data.(soaData); ok && soa.Serial == 0 && fields["serial"] == "" {
|
|
warn("soa_dynamic_serial", "SOA uses a dynamic Unix time serial")
|
|
}
|
|
if canonical, _ := encodeRecord(normalized); len(fields) == 0 && !canonicalContent(row.Content.String, canonical) {
|
|
warn("content_not_canonical", "stored content differs from its canonical form")
|
|
}
|
|
if decoded.decodeErr == nil && row.TTL.Valid && row.TTL.Int64 >= 0 && row.TTL.Int64 <= int64(^uint32(0)) {
|
|
rr, buildErr := buildRecordRR(row, decoded.data, now)
|
|
if buildErr == nil && rr != nil && packRecordRR(rr) != nil {
|
|
addError("pack_failed", "the plugin's resource record cannot be packed")
|
|
}
|
|
}
|
|
return problems
|
|
}
|
|
|
|
type contentMember struct {
|
|
key string
|
|
value any
|
|
}
|
|
|
|
func contentMembers(content string) ([]contentMember, bool) {
|
|
decoder := json.NewDecoder(strings.NewReader(content))
|
|
decoder.UseNumber()
|
|
if token, err := decoder.Token(); err != nil || token != json.Delim('{') {
|
|
return nil, false
|
|
}
|
|
var members []contentMember
|
|
for decoder.More() {
|
|
key, err := decoder.Token()
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
value, err := decoder.Token()
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
if _, nested := value.(json.Delim); nested {
|
|
return nil, false
|
|
}
|
|
members = append(members, contentMember{key.(string), value})
|
|
}
|
|
if _, err := decoder.Token(); err != nil {
|
|
return nil, false
|
|
}
|
|
if _, err := decoder.Token(); err != io.EOF {
|
|
return nil, false
|
|
}
|
|
return members, true
|
|
}
|
|
|
|
func canonicalContent(stored, canonical string) bool {
|
|
if stored == canonical {
|
|
return true
|
|
}
|
|
have, ok := contentMembers(stored)
|
|
want, _ := contentMembers(canonical)
|
|
if !ok {
|
|
return false
|
|
}
|
|
if last := len(want) - 1; last >= 0 && want[last].key == "serial" && want[last].value == json.Number("0") &&
|
|
!slices.ContainsFunc(have, func(m contentMember) bool { return m.key == "serial" }) {
|
|
want = want[:last]
|
|
}
|
|
mysqlOrder := slices.SortedStableFunc(slices.Values(want), func(a, b contentMember) int {
|
|
return cmp.Or(cmp.Compare(len(a.key), len(b.key)), strings.Compare(a.key, b.key))
|
|
})
|
|
return slices.Equal(have, want) || slices.Equal(have, mysqlOrder)
|
|
}
|
|
|
|
func hasContentKey(object map[string]json.RawMessage, key string) bool {
|
|
for name := range object {
|
|
if strings.EqualFold(name, key) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func recordDomainFields(data any) map[string]string {
|
|
switch v := data.(type) {
|
|
case cnameData:
|
|
return map[string]string{"host": v.Host}
|
|
case nsData:
|
|
return map[string]string{"host": v.Host}
|
|
case mxData:
|
|
return map[string]string{"host": v.Host}
|
|
case srvData:
|
|
return map[string]string{"target": v.Target}
|
|
case soaData:
|
|
return map[string]string{"ns": v.NS, "MBox": v.MBox}
|
|
default:
|
|
return nil
|
|
}
|
|
}
|