Use Error.underlying instead of adding a new field
diff --git a/bdns/dns.go b/bdns/dns.go index 1d31267..1375f99 100644 --- a/bdns/dns.go +++ b/bdns/dns.go
@@ -316,6 +316,14 @@ return resultFromMsg[*dns.CAA](resp), resolver, nil } + // Truncated responses can never be trusted because the truncated portion + // may contain a CAA record that forbids issuance, such as one with the + // critical bit set and an unknown property tag. For example: + // CAA 128 unknown "foo" + if resp.Truncated { + err = errCAATruncated + } + err = wrapErr(dns.TypeCAA, hostname, resp, err) if err != nil { return nil, resolver, err
diff --git a/bdns/dns_test.go b/bdns/dns_test.go index 7bd9f7d..3d40d4d 100644 --- a/bdns/dns_test.go +++ b/bdns/dns_test.go
@@ -543,7 +543,7 @@ test.AssertContains(t, err.Error(), "NXDOMAIN looking up AAAA for") _, _, err = obj.LookupTXT(context.Background(), hostname) - expected := Error{dns.TypeTXT, hostname, nil, dns.RcodeNameError, nil, false} + expected := Error{dns.TypeTXT, hostname, nil, dns.RcodeNameError, nil} test.AssertDeepEquals(t, err, expected) }
diff --git a/bdns/problem.go b/bdns/problem.go index 19dfe89..36379de 100644 --- a/bdns/problem.go +++ b/bdns/problem.go
@@ -14,18 +14,13 @@ type Error struct { recordType uint16 hostname string - // Exactly one of underlying, rCode, or truncated should be set. + // Exactly one of underlying or rCode should be set. underlying error rCode int // Optional: If the resolver returned extended error information, it will be stored here. // https://www.rfc-editor.org/rfc/rfc8914 extended *dns.EDNS0_EDE - - // truncated is set when the response to a CAA query had the TC bit set. We - // don't implement fallback to TCP, so we treat a truncated response as an - // error rather than risk silently acting on an incomplete set of records. - truncated bool } // extendedDNSError returns non-nil if the input message contained an OPT RR @@ -57,13 +52,6 @@ extended: nil, } } - if queryType == dns.TypeCAA && resp.Truncated { - return Error{ - recordType: queryType, - hostname: hostname, - truncated: true, - } - } if resp.Rcode != dns.RcodeSuccess { return Error{ recordType: queryType, @@ -131,11 +119,11 @@ detail = detailDNSTimeout } else if errors.Is(d.underlying, context.Canceled) { detail = detailCanceled + } else if errors.Is(d.underlying, errCAATruncated) { + detail = d.underlying.Error() } else { detail = detailServerFailure } - } else if d.truncated { - detail = detailDNSTruncated } else if d.rCode != dns.RcodeSuccess { detail = dns.RcodeToString[d.rCode] if explanation, ok := rcodeExplanations[d.rCode]; ok { @@ -166,7 +154,8 @@ const detailCanceled = "query timed out (and was canceled)" const detailDNSNetFailure = "networking error" const detailServerFailure = "server failure at resolver" -const detailDNSTruncated = "response was truncated" + +var errCAATruncated = errors.New("response was truncated") // rcodeExplanations provide additional friendly explanatory text to be included in DNS // error messages, for select inscrutable RCODEs.
diff --git a/bdns/problem_test.go b/bdns/problem_test.go index 1c534c9..c6d94d3 100644 --- a/bdns/problem_test.go +++ b/bdns/problem_test.go
@@ -18,43 +18,43 @@ expected string }{ { - &Error{dns.TypeMX, "hostname", &net.OpError{Err: errors.New("some net error")}, -1, nil, false}, + &Error{dns.TypeMX, "hostname", &net.OpError{Err: errors.New("some net error")}, -1, nil}, "DNS problem: networking error looking up MX for hostname", }, { - &Error{dns.TypeTXT, "hostname", nil, dns.RcodeNameError, nil, false}, + &Error{dns.TypeTXT, "hostname", nil, dns.RcodeNameError, nil}, "DNS problem: NXDOMAIN looking up TXT for hostname - check that a DNS record exists for this domain", }, { - &Error{dns.TypeTXT, "hostname", context.DeadlineExceeded, -1, nil, false}, + &Error{dns.TypeTXT, "hostname", context.DeadlineExceeded, -1, nil}, "DNS problem: query timed out looking up TXT for hostname", }, { - &Error{dns.TypeTXT, "hostname", context.Canceled, -1, nil, false}, + &Error{dns.TypeTXT, "hostname", context.Canceled, -1, nil}, "DNS problem: query timed out (and was canceled) looking up TXT for hostname", }, { - &Error{dns.TypeCAA, "hostname", nil, dns.RcodeServerFailure, nil, false}, + &Error{dns.TypeCAA, "hostname", nil, dns.RcodeServerFailure, nil}, "DNS problem: SERVFAIL looking up CAA for hostname - the domain's nameservers may be malfunctioning", }, { - &Error{dns.TypeA, "hostname", nil, dns.RcodeServerFailure, &dns.EDNS0_EDE{InfoCode: 1, ExtraText: "oh no"}, false}, + &Error{dns.TypeA, "hostname", nil, dns.RcodeServerFailure, &dns.EDNS0_EDE{InfoCode: 1, ExtraText: "oh no"}}, "DNS problem: looking up A for hostname: DNSSEC: Unsupported DNSKEY Algorithm: oh no", }, { - &Error{dns.TypeA, "hostname", nil, dns.RcodeServerFailure, &dns.EDNS0_EDE{InfoCode: 6, ExtraText: ""}, false}, + &Error{dns.TypeA, "hostname", nil, dns.RcodeServerFailure, &dns.EDNS0_EDE{InfoCode: 6, ExtraText: ""}}, "DNS problem: looking up A for hostname: DNSSEC: Bogus", }, { - &Error{dns.TypeA, "hostname", nil, dns.RcodeServerFailure, &dns.EDNS0_EDE{InfoCode: 1337, ExtraText: "mysterious"}, false}, + &Error{dns.TypeA, "hostname", nil, dns.RcodeServerFailure, &dns.EDNS0_EDE{InfoCode: 1337, ExtraText: "mysterious"}}, "DNS problem: looking up A for hostname: Unknown Extended DNS Error code 1337: mysterious", }, { - &Error{dns.TypeCAA, "hostname", nil, dns.RcodeServerFailure, nil, false}, + &Error{dns.TypeCAA, "hostname", nil, dns.RcodeServerFailure, nil}, "DNS problem: SERVFAIL looking up CAA for hostname - the domain's nameservers may be malfunctioning", }, { - &Error{dns.TypeCAA, "hostname", nil, dns.RcodeServerFailure, nil, false}, + &Error{dns.TypeCAA, "hostname", nil, dns.RcodeServerFailure, nil}, "DNS problem: SERVFAIL looking up CAA for hostname - the domain's nameservers may be malfunctioning", }, { - &Error{dns.TypeA, "hostname", nil, dns.RcodeFormatError, nil, false}, + &Error{dns.TypeA, "hostname", nil, dns.RcodeFormatError, nil}, "DNS problem: FORMERR looking up A for hostname", }, { - &Error{dns.TypeA, "hostname", &url.Error{Op: "GET", URL: "https://example.com/", Err: dohTimeoutError{}}, -1, nil, false}, + &Error{dns.TypeA, "hostname", &url.Error{Op: "GET", URL: "https://example.com/", Err: dohTimeoutError{}}, -1, nil}, "DNS problem: query timed out looking up A for hostname", }, { - &Error{dns.TypeCAA, "hostname", nil, dns.RcodeSuccess, nil, true}, + &Error{dns.TypeCAA, "hostname", errCAATruncated, dns.RcodeSuccess, nil}, "DNS problem: response was truncated looking up CAA for hostname", }, } @@ -90,14 +90,4 @@ MsgHdr: dns.MsgHdr{Rcode: dns.RcodeSuccess}, }, errors.New("oh no")) test.AssertError(t, err, "expected error") - - // A truncated response should be treated as an error, even though its - // Rcode is RcodeSuccess: a truncated CAA response could be silently - // missing the issue/issuewild records that would otherwise forbid - // issuance. - err = wrapErr(dns.TypeCAA, "hostname", &dns.Msg{ - MsgHdr: dns.MsgHdr{Rcode: dns.RcodeSuccess, Truncated: true}, - }, nil) - test.AssertError(t, err, "expected error for truncated response") - test.AssertContains(t, err.Error(), "response was truncated") }