diff --git a/README.md b/README.md index 1868e87..77c48cc 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ can read in full, the daily quiz question and the incident of the day. | `checklist [name]` | Tick off a checklist; progress is saved | | `incident [--fix]` | Today's real-world incident, then the fix | | `play [simulator]` | Open a simulator, a game or Opsville in your browser | +| `check tls\|dns\|headers\|email ` | Check a certificate, DNS records, security headers or email records | | `subscribe` | Open the newsletter sign-up page | | `sync` | Download the latest content now | | `update` | Show how to upgrade the way you installed it | @@ -65,6 +66,33 @@ Names are forgiving: `devops-daily quiz docker`, `devops-daily play dns` and `devops-daily read terraform-state` find the closest match or list the candidates. +## Check a host + +Four everyday checks, run from your machine. Each line says what was found +and what it means, and the last line links to a DevOps Daily page on the +topic. + +```sh +ddy check tls github.com # certificate, days left, chain, trust +ddy check dns www.github.com --server 1.1.1.1 # records with TTLs, CNAME chain +ddy check headers devops-daily.com # redirects, security and cache headers +ddy check email gmail.com # MX, SPF, DMARC, DKIM, MTA-STS, TLS-RPT +``` + +```text +$ ddy check tls expired.badssl.com +... + fail Expires in expired 4234 days ago (2015-04-12 23:59 UTC) + Browsers block the site with a warning page. Renew the certificate now. + fail Trusted no, a certificate in the chain has expired or is not valid yet +... +Result: 2 problems. +``` + +Add `--json` for scripts and `--timeout 10s` for slow hosts. Exit codes: 0 +all good, 1 warnings, 2 a problem or an error. The checks are not part of +the SSH app. See [docs/check.md](docs/check.md) for what each one looks at. + ## Offline use and files After content has been synced, the CLI works offline. On Linux, the cache is @@ -80,7 +108,9 @@ to disable color. No telemetry. The CLI downloads the public content files from `devops-daily.com/cli/v1/` and, only when you run `update`, asks GitHub for -the latest version. Nothing about you is sent. +the latest version. The `check` commands talk only to the host you name and +to your DNS resolver (or the one you pick with `--server`). Nothing about +you is sent. ## SSH app diff --git a/cmd/ssh-site/imports_test.go b/cmd/ssh-site/imports_test.go new file mode 100644 index 0000000..f89d8a9 --- /dev/null +++ b/cmd/ssh-site/imports_test.go @@ -0,0 +1,85 @@ +package main + +import ( + "bufio" + "go/parser" + "go/token" + "os" + "path/filepath" + "strconv" + "strings" + "testing" +) + +// The check commands connect to any host they are given. In the SSH app +// that would let a visitor make our server connect anywhere, so the server +// must not be able to reach them, not even through another package. +func TestSSHAppCannotReachCheckCommands(t *testing.T) { + root, module := moduleRoot(t) + deps := map[string]bool{} + walkImports(t, root, module, module+"/cmd/ssh-site", deps) + if !deps[module+"/internal/tui"] { + t.Fatalf("import walk found no internal/tui, so it is broken: %v", deps) + } + for _, banned := range []string{module + "/internal/check", module + "/internal/cli"} { + if deps[banned] { + t.Errorf("cmd/ssh-site imports %s; the network checks must stay out of the SSH app", banned) + } + } +} + +func moduleRoot(t *testing.T) (string, string) { + t.Helper() + dir, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + for { + if file, err := os.Open(filepath.Join(dir, "go.mod")); err == nil { + defer file.Close() + scanner := bufio.NewScanner(file) + for scanner.Scan() { + if module, ok := strings.CutPrefix(strings.TrimSpace(scanner.Text()), "module "); ok { + return dir, strings.TrimSpace(module) + } + } + t.Fatal("go.mod has no module line") + } + parent := filepath.Dir(dir) + if parent == dir { + t.Fatal("go.mod not found") + } + dir = parent + } +} + +// walkImports records every package of this module that pkg imports, +// directly or not. Test files do not count; files for every OS do. +func walkImports(t *testing.T, root, module, pkg string, seen map[string]bool) { + t.Helper() + dir := filepath.Join(root, filepath.FromSlash(strings.TrimPrefix(pkg, module))) + entries, err := os.ReadDir(dir) + if err != nil { + t.Fatal(err) + } + for _, entry := range entries { + name := entry.Name() + if entry.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") { + continue + } + file, err := parser.ParseFile(token.NewFileSet(), filepath.Join(dir, name), nil, parser.ImportsOnly) + if err != nil { + t.Fatal(err) + } + for _, spec := range file.Imports { + path, err := strconv.Unquote(spec.Path.Value) + if err != nil { + t.Fatal(err) + } + if (path == module || strings.HasPrefix(path, module+"/")) && !seen[path] { + seen[path] = true + walkImports(t, root, module, path, seen) + } + } + } +} diff --git a/docs/check.md b/docs/check.md new file mode 100644 index 0000000..2c44b24 --- /dev/null +++ b/docs/check.md @@ -0,0 +1,175 @@ +# Check commands + +`devops-daily check` (or `ddy check`) looks at a host from your own machine +and says, in plain words, what it found: + +| Command | What it checks | +| --- | --- | +| `check tls ` | TLS version, cipher, the certificate (names, issuer, dates, days left), the chain, trust, host name match, OCSP stapling | +| `check dns ` | A, AAAA, MX, NS, TXT and CAA records with TTLs, and the CNAME chain | +| `check headers ` | Each redirect, then the security headers and cache headers of the final response | +| `check email ` | MX, SPF, DMARC, DKIM, MTA-STS and TLS-RPT records | + +Every line has a status: + +| Status | Meaning | +| --- | --- | +| `pass` | Looks right | +| `info` | Just information | +| `warn` | Works, but worth fixing | +| `missing` | Not set, and it should be | +| `fail` | Broken, or the check could not run | + +## Flags and exit codes + +All four commands take: + +- `--json`: print the report as JSON for scripts. `details` has the raw + data (for example `details.daysLeft` for TLS) and `sections` has every + finding with a stable `id`. +- `--timeout 5s`: how long to wait for each network reply. For `check + headers` it covers each request, including its connect and TLS handshake. + +Exit codes: `0` all good, `1` warnings (`warn` or `missing`), `2` a +problem (`fail`) or an error, such as a host that does not resolve or a bad +argument. + +```sh +ddy check tls example.com --json | jq .details.daysLeft +ddy check tls example.com >/dev/null || echo "fix the certificate" +``` + +## What leaves your machine + +Only what the check needs: a connection to the host you name, and DNS +questions to your system resolver (the first `nameserver` in +`/etc/resolv.conf`) or the server you pick with `--server`. No telemetry and +no request to devops-daily.com. Where `/etc/resolv.conf` cannot be read (on +Windows, for example), DNS questions go to `1.1.1.1` and the report says so. +`check headers` sends `User-Agent: devops-daily-cli/ (check +headers)`. + +On macOS and Windows, Go asks the operating system to verify certificate +chains, and the operating system may fetch a missing intermediate +certificate itself. + +International names work in any check: `bücher.de` is looked up, sent as +the TLS server name and matched against the certificate as +`xn--bcher-kva.de`, and the report shows both forms. + +## check tls + +```sh +ddy check tls github.com +ddy check tls smtp.gmail.com:465 +``` + +The port defaults to 443, and a pasted URL works too. The check connects +without trusting the certificate first, so that a bad one is reported +instead of stopping the check; then it verifies the chain against your +system's trusted roots and the host name separately. It warns when the +certificate expires in 21 days or less: lifetimes are getting shorter (Let's +Encrypt moves to 64 days in February 2027, later 45), so renewal must be +automatic. TLS 1.0 and 1.1 are a `fail`. A missing OCSP staple is only +`info`, since Let's Encrypt ended OCSP in 2025. + +## check dns + +```sh +ddy check dns devops-daily.com +ddy check dns www.github.com --type CNAME,A --server 1.1.1.1 +``` + +`--type` takes `A`, `AAAA`, `CNAME`, `MX`, `NS`, `TXT`, `CAA` or `SOA` +(repeat it or separate with commas). Without it you get A, AAAA, MX, NS, +TXT and CAA. `--server` takes an IP address, with an optional port. TTLs +from a caching resolver count down; ask the zone's own name server (see the +NS records) for the full value. + +A name that does not exist is a `fail`, and so is an alias whose target is +gone (a dangling CNAME, which someone may be able to take over), a CNAME +loop, or a chain of 16 or more aliases. The chain is followed to its end even +when the resolver stops early, as it does for `--type CNAME`. With the +default types, a name with no A or AAAA records is a `warn`; with `--type`, +a type with no records is a `warn`. + +## check headers + +```sh +ddy check headers devops-daily.com +ddy check headers http://example.com/login +``` + +`https://` is added when there is no scheme. Redirects (301, 302, 303, 307 +and 308; not 300 or 304) are followed one at a time, at most 10, and each +hop is shown with its status and target. A +loop, too many redirects or an untrusted certificate is a `fail`; a +redirect from HTTPS to plain HTTP is a `warn`. + +The final response's headers: + +| Header | `pass` when | +| --- | --- | +| `Strict-Transport-Security` | `max-age` is 180 days or more and no directive appears twice | +| `Content-Security-Policy` | `script-src`, `script-src-elem` and `script-src-attr` (each falling back to `script-src`, then `default-src`) allow no `'unsafe-inline'` without a nonce or hash, and no `'unsafe-eval'`, `*`, `http:`, `https:`, `https://*` or `data:` | +| `X-Content-Type-Options` | `nosniff` | +| `X-Frame-Options` or CSP `frame-ancestors` | `DENY`/`SAMEORIGIN`, or a `frame-ancestors` with no wildcard source in at least one policy | +| `Referrer-Policy` | the last valid value, over all the headers, is not `unsafe-url` or `no-referrer-when-downgrade` | +| `Permissions-Policy` | present | + +A missing HSTS (on HTTPS), CSP, `X-Content-Type-Options` or frame protection +is `missing`. A missing `Referrer-Policy` or `Permissions-Policy` is only +`info`: browsers default to `strict-origin-when-cross-origin`, and +`Permissions-Policy` is optional. With several CSP headers the browser +enforces all of them, so the strictest one counts. + +`Cache-Control`, `Age`, `ETag` and `Server` are shown as `info`, with what +they mean for caches. There are no grades. + +## check email + +```sh +ddy check email gmail.com +ddy check email example.com --selector mailjet --server 8.8.8.8 +``` + +An email address or a URL works too; only the domain is used. + +- **SPF**: there must be one `v=spf1` record. Like a receiver, the check + stops at the first `all` and uses `redirect=` only when there is no `all`. + It counts the DNS lookups that needs (`include`, `a`, `mx`, `ptr`, + `exists` and an evaluated `redirect`, nested includes too) against the + limit of 10. These are a `fail` (receivers return permerror): over 10 + lookups, an `mx` with more than 10 MX hosts, a bad `ip4`/`ip6` address or + prefix length, an unknown mechanism, and an include with no SPF record. + `+all` is a `fail`, `?all` or no `all` a `warn`, `ptr` a `warn` + (deprecated). An `include` or `redirect` with macros (`%{d}`) is a `warn`: + it is filled in per message, so the count cannot be checked. +- **DMARC**: one record at `_dmarc.` whose first tag is + `v=DMARC1`, exactly in that case (spaces around `=` are fine). `p=none` is + a `warn` (monitoring only), and so is a missing `rua` (no reports). A + `pct` under 100 is a `warn`: the updated standard (DMARCbis) drops `pct`, + and `t=y` is the new testing switch. With `t=y`, receivers apply one step + less: `reject` becomes `quarantine`, `quarantine` becomes `none`. +- **DKIM**: keys cannot be listed, so the check tries common selectors: + `default`, `google`, `selector1`, `selector2`, `k1`, `s1`, `mail` and + `dkim`. Finding none is only `info`: many providers (Amazon SES, for + example) use their own selector names. Add yours with `--selector` (find + it as `s=` in the `DKIM-Signature` header of a mail you received). RSA keys + under 2048 bits are a `warn`, under 1024 a `fail`. A malformed record is a + `fail`: `v=` that is not first or not `DKIM1`, no `p=`, a key that does not + decode, an unknown `k=` or a tag given twice. An empty `p=` means the key + was revoked on purpose (`info`). +- **MTA-STS** (`_mta-sts.`) and **TLS-RPT** (`_smtp._tls.`) + are optional. When present there must be one record each, MTA-STS with an + `id=` (1 to 32 letters and digits) and TLS-RPT with a `rua=`; otherwise + senders ignore them, which is a `fail`. The MTA-STS policy file itself is + not downloaded. + +## Not in the SSH app + +The check commands exist only in the local CLI. In the SSH app +(`ssh ssh.devops-daily.com`) they would make our server connect to any host +a visitor typed. `cmd/ssh-site/imports_test.go` fails if the server ever +imports `internal/check` or `internal/cli`, directly or through another +package. diff --git a/docs/ssh-server.md b/docs/ssh-server.md index d777a78..b9c54dc 100644 --- a/docs/ssh-server.md +++ b/docs/ssh-server.md @@ -42,6 +42,11 @@ Built with [Wish](https://github.com/charmbracelet/wish) and - The app never queries the client terminal (no OSC or DA requests); colors come from `TERM` alone. All feed, quiz and config text is stripped of control codes before it is drawn. +- The app never makes a network request on a visitor's behalf. The CLI's `check` + commands (TLS, DNS, headers, email) are left out on purpose: here they + would make the server connect to any host a visitor typed. + `cmd/ssh-site/imports_test.go` fails if the server imports `internal/check` + or `internal/cli`, directly or through another package. Limits (flags on the binary, defaults shown): @@ -85,7 +90,7 @@ because a wildcard bind would clash with sshd on the tailnet address. Needs Go 1.26 or newer. ```sh -make test # unit tests: date picks, feed cache, limits, server hardening, UI layout +make test # unit tests: date picks, feed cache, limits, server hardening, UI layout, no check imports make build # static linux/amd64 binary in bin/ssh-site make run # local copy on 127.0.0.1:2222; then: ssh -p 2222 localhost ``` diff --git a/go.mod b/go.mod index 68398fa..3beea08 100644 --- a/go.mod +++ b/go.mod @@ -11,10 +11,12 @@ require ( github.com/charmbracelet/wish v1.4.7 github.com/charmbracelet/x/ansi v0.11.6 github.com/charmbracelet/x/term v0.2.2 + github.com/miekg/dns v1.1.73 github.com/muesli/termenv v0.16.0 github.com/spf13/cobra v1.10.2 github.com/yuin/goldmark v1.7.13 golang.org/x/crypto v0.58.0 + golang.org/x/net v0.60.0 ) require ( @@ -54,7 +56,6 @@ require ( github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect github.com/yuin/goldmark-emoji v1.0.6 // indirect golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect - golang.org/x/net v0.60.0 // indirect golang.org/x/sys v0.49.0 // indirect golang.org/x/term v0.47.0 // indirect golang.org/x/text v0.43.0 // indirect diff --git a/go.sum b/go.sum index d1dbe49..6a92093 100644 --- a/go.sum +++ b/go.sum @@ -84,6 +84,8 @@ github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byF github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs= github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk= github.com/microcosm-cc/bluemonday v1.0.27/go.mod h1:jFi9vgW+H7c3V0lb6nR74Ib/DIB5OBs92Dimizgw2cA= +github.com/miekg/dns v1.1.73 h1:uhT8nJxmTrPJYClxVxTCX+CVn6qnzSiybRk72Z6DgrE= +github.com/miekg/dns v1.1.73/go.mod h1:RW2Obtfd5NZHvOFe3zYG0W8koWOQtAzyHaLo8vASBuQ= github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= @@ -118,6 +120,8 @@ golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0 golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY= golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU= golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= +golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= +golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= diff --git a/internal/check/check.go b/internal/check/check.go new file mode 100644 index 0000000..2151fcc --- /dev/null +++ b/internal/check/check.go @@ -0,0 +1,165 @@ +// Package check runs the network checks behind "devops-daily check": a TLS +// certificate, DNS records, HTTP security headers and a domain's email +// records. Each check talks only to the host being checked and the DNS +// server in use. +// +// The SSH app must never import this package: there a visitor could make our +// server connect to any host. cmd/ssh-site/imports_test.go fails if it does. +package check + +import ( + "fmt" + "strings" + "time" +) + +// DefaultTimeout is how long each check waits for one network reply. +const DefaultTimeout = 5 * time.Second + +// Learn more pages on the site, one per check. +const ( + learnTLS = "https://devops-daily.com/games/ssl-tls-handshake" + learnDNS = "https://devops-daily.com/games/dns-simulator" + learnHeaders = "https://devops-daily.com/guides/owasp-top-10/05-security-misconfiguration" + learnEmail = "https://devops-daily.com/games/email-auth-lab" +) + +// Status is the result of one finding. +type Status string + +const ( + Pass Status = "pass" + Info Status = "info" + Warn Status = "warn" + Missing Status = "missing" + Fail Status = "fail" +) + +// Severity is 0 for pass and info, 1 for warn and missing, 2 for fail. It is +// also the exit code a finding asks for. +func (s Status) Severity() int { + switch s { + case Fail: + return 2 + case Warn, Missing: + return 1 + } + return 0 +} + +// Finding is one line of a report: what was looked at, what was found and, +// in plain words, what it means. +type Finding struct { + ID string `json:"id"` + Status Status `json:"status"` + Label string `json:"label"` + Value string `json:"value"` + Meaning string `json:"meaning,omitempty"` +} + +// Section groups findings under a heading, with an optional note that +// explains the whole group. +type Section struct { + Title string `json:"title"` + Note string `json:"note,omitempty"` + Findings []Finding `json:"findings"` +} + +func (s *Section) add(id string, status Status, label, value, meaning string) { + s.Findings = append(s.Findings, Finding{ID: id, Status: status, Label: label, Value: value, Meaning: meaning}) +} + +// Report is the result of one check. Details holds the raw data for scripts. +type Report struct { + Check string `json:"check"` + Target string `json:"target"` + Unicode string `json:"unicode,omitempty"` // the target's Unicode form, for international names + Status Status `json:"status"` + ExitCode int `json:"exitCode"` + Warnings int `json:"warnings"` + Failures int `json:"failures"` + Sections []Section `json:"sections"` + Details any `json:"details"` + LearnMore string `json:"learnMore,omitempty"` +} + +func (r *Report) add(s Section) { + if len(s.Findings) > 0 { + r.Sections = append(r.Sections, s) + } +} + +// finish counts warnings and failures and sets the status and exit code: +// 0 when all is well, 1 with warnings, 2 with at least one failure. +func (r *Report) finish() *Report { + r.Warnings, r.Failures = 0, 0 + for _, s := range r.Sections { + for _, f := range s.Findings { + switch f.Status.Severity() { + case 2: + r.Failures++ + case 1: + r.Warnings++ + } + } + } + switch { + case r.Failures > 0: + r.Status, r.ExitCode = Fail, 2 + case r.Warnings > 0: + r.Status, r.ExitCode = Warn, 1 + default: + r.Status, r.ExitCode = Pass, 0 + } + return r +} + +func timeoutOr(d time.Duration) time.Duration { + if d <= 0 { + return DefaultTimeout + } + return d +} + +// span formats a number of seconds as "45s", "5m", "1h30m" or "2d". +func span(seconds int64) string { + if seconds < 60 { + return fmt.Sprintf("%ds", seconds) + } + units := []struct { + name string + size int64 + }{{"d", 86400}, {"h", 3600}, {"m", 60}, {"s", 1}} + var parts []string + for _, u := range units { + if seconds >= u.size && len(parts) < 2 { + parts = append(parts, fmt.Sprintf("%d%s", seconds/u.size, u.name)) + seconds %= u.size + } else if len(parts) > 0 { + break + } + } + return strings.Join(parts, "") +} + +func ttl(seconds uint32) string { + if seconds < 60 { + return fmt.Sprintf("TTL %ds", seconds) + } + return fmt.Sprintf("TTL %ds (%s)", seconds, span(int64(seconds))) +} + +func plural(n int, one, many string) string { + if n == 1 { + return "1 " + one + } + return fmt.Sprintf("%d %s", n, many) +} + +// list joins values, keeping at most max and saying how many were left out. +func list(values []string, max int) string { + if len(values) <= max { + return strings.Join(values, ", ") + } + return strings.Join(values[:max], ", ") + fmt.Sprintf(" and %d more", len(values)-max) +} diff --git a/internal/check/check_test.go b/internal/check/check_test.go new file mode 100644 index 0000000..c8267af --- /dev/null +++ b/internal/check/check_test.go @@ -0,0 +1,136 @@ +package check + +import ( + "testing" +) + +func finding(t *testing.T, r *Report, id string) Finding { + t.Helper() + for _, s := range r.Sections { + for _, f := range s.Findings { + if f.ID == id { + return f + } + } + } + t.Fatalf("no finding %q in report: %+v", id, r.Sections) + return Finding{} +} + +func hasFinding(r *Report, id string) bool { + for _, s := range r.Sections { + for _, f := range s.Findings { + if f.ID == id { + return true + } + } + } + return false +} + +func TestFinishSetsExitCode(t *testing.T) { + cases := []struct { + statuses []Status + code int + status Status + }{ + {[]Status{Pass, Info}, 0, Pass}, + {[]Status{Pass, Missing}, 1, Warn}, + {[]Status{Warn, Info}, 1, Warn}, + {[]Status{Warn, Fail, Pass}, 2, Fail}, + {nil, 0, Pass}, + } + for _, c := range cases { + s := Section{Title: "x"} + for i, status := range c.statuses { + s.add(string(rune('a'+i)), status, "", "", "") + } + r := &Report{} + r.add(s) + r.finish() + if r.ExitCode != c.code || r.Status != c.status { + t.Errorf("%v: exit %d status %s, want %d %s", c.statuses, r.ExitCode, r.Status, c.code, c.status) + } + } +} + +func TestHostPort(t *testing.T) { + cases := []struct{ in, host, port string }{ + {"github.com", "github.com", "443"}, + {"GitHub.com.", "github.com", "443"}, + {"example.com:8443", "example.com", "8443"}, + {"https://example.com/path?q=1", "example.com", "443"}, + {"https://example.com:444/", "example.com", "444"}, + {"127.0.0.1:9000", "127.0.0.1", "9000"}, + {"[::1]:9000", "::1", "9000"}, + {"[::1]", "::1", "443"}, + {"::1", "::1", "443"}, + {"https://[::1]:9443/x", "::1", "9443"}, + {"bücher.de", "xn--bcher-kva.de", "443"}, + {"https://Bücher.de:8443/", "xn--bcher-kva.de", "8443"}, + } + for _, c := range cases { + host, port, err := hostPort(c.in, "443") + if err != nil || host != c.host || port != c.port { + t.Errorf("hostPort(%q) = %q, %q, %v; want %q, %q", c.in, host, port, err, c.host, c.port) + } + } + for _, bad := range []string{"", "exa mple.com", "example.com:0", "example.com:99999", "example.com:http", "-bad-.com:x"} { + if _, _, err := hostPort(bad, "443"); err == nil { + t.Errorf("hostPort(%q) gave no error", bad) + } + } +} + +func TestDomainName(t *testing.T) { + for in, want := range map[string]string{ + "Example.COM.": "example.com", + "me@example.com": "example.com", + "a/b@example.com": "example.com", + "https://example.com/x": "example.com", + "https://example.com:8443/path": "example.com", + "example.com/path": "example.com", + "example.com:25": "example.com", + "_dmarc.example.com": "_dmarc.example.com", + "info@Bücher.de": "xn--bcher-kva.de", + "xn--bcher-kva.de": "xn--bcher-kva.de", + } { + if got, err := domainName(in); err != nil || got != want { + t.Errorf("domainName(%q) = %q, %v; want %q", in, got, err, want) + } + } + for _, bad := range []string{"", "1.1.1.1", "a..b", "bad name.com", "me@", "bü cher.de"} { + if _, err := domainName(bad); err == nil { + t.Errorf("domainName(%q) gave no error", bad) + } + } + if got := unicodeName("xn--bcher-kva.de"); got != "bücher.de" { + t.Errorf("unicodeName = %q", got) + } + if got := unicodeName("example.com"); got != "" { + t.Errorf("unicodeName(example.com) = %q", got) + } +} + +func TestSpanAndTTL(t *testing.T) { + for seconds, want := range map[int64]string{0: "0s", 45: "45s", 60: "1m", 90: "1m30s", 3600: "1h", 5400: "1h30m", 86400: "1d", 31536000: "365d"} { + if got := span(seconds); got != want { + t.Errorf("span(%d) = %q, want %q", seconds, got, want) + } + } + if got := ttl(300); got != "TTL 300s (5m)" { + t.Errorf("ttl(300) = %q", got) + } + if got := ttl(30); got != "TTL 30s" { + t.Errorf("ttl(30) = %q", got) + } +} + +func TestList(t *testing.T) { + if got := list([]string{"a", "b", "c"}, 2); got != "a, b and 1 more" { + t.Errorf("list = %q", got) + } + if got := list([]string{"a", "b"}, 2); got != "a, b" { + t.Errorf("list = %q", got) + } +} diff --git a/internal/check/dns.go b/internal/check/dns.go new file mode 100644 index 0000000..7f4552d --- /dev/null +++ b/internal/check/dns.go @@ -0,0 +1,293 @@ +package check + +import ( + "context" + "fmt" + "slices" + "strings" + "sync" + "time" + + "github.com/miekg/dns" +) + +type DNSOptions struct { + Server string // an IP, optionally with a port; empty: the system resolver + Types []string // empty: DefaultDNSTypes + Timeout time.Duration +} + +// DefaultDNSTypes are the record types "check dns" shows without --type. +var DefaultDNSTypes = []string{"A", "AAAA", "MX", "NS", "TXT", "CAA"} + +var dnsTypes = map[string]uint16{ + "A": dns.TypeA, "AAAA": dns.TypeAAAA, "CNAME": dns.TypeCNAME, "MX": dns.TypeMX, + "NS": dns.TypeNS, "TXT": dns.TypeTXT, "CAA": dns.TypeCAA, "SOA": dns.TypeSOA, +} + +var typeNotes = map[string]string{ + "A": "IPv4 addresses. Browsers and other clients connect to these.", + "AAAA": "IPv6 addresses.", + "CNAME": "An alias: this name points to another name, and the other records come from there.", + "MX": "The servers that receive mail for this domain, lowest number tried first.", + "NS": "The name servers that hold this zone's records.", + "TXT": "Free text: SPF, site verification tokens and other settings.", + "CAA": "Which certificate authorities may issue certificates. Looked up at this name only; CAs also check parent names.", + "SOA": "The zone's main name server, contact address and timers.", +} + +type Record struct { + Name string `json:"name"` + Type string `json:"type"` + TTL uint32 `json:"ttl"` + Value string `json:"value"` +} + +type DNSDetails struct { + Name string `json:"name"` + Server string `json:"server"` + Records []Record `json:"records"` + CNAMEChain []Record `json:"cnameChain"` + Errors []string `json:"errors,omitempty"` +} + +// DNS looks up name's records and shows them with their TTLs. The error is +// only for bad input; lookup problems are findings in the report. +func DNS(ctx context.Context, input string, opts DNSOptions) (*Report, error) { + name, err := domainName(input) + if err != nil { + return nil, err + } + types, err := parseTypes(opts.Types) + if err != nil { + return nil, err + } + res, err := newResolver(opts.Server, opts.Timeout) + if err != nil { + return nil, err + } + d := &DNSDetails{Name: name, Server: res.server, Records: []Record{}, CNAMEChain: []Record{}} + r := &Report{Check: "dns", Target: name, Unicode: unicodeName(name), Details: d, LearnMore: learnDNS} + server := Section{Title: "DNS server"} + server.add("server", Info, "Asked", res.server+" ("+res.source+")", "") + r.add(server) + + answers := make([]*dns.Msg, len(types)) + errs := make([]error, len(types)) + var wg sync.WaitGroup + for i, t := range types { + wg.Add(1) + go func() { + defer wg.Done() + answers[i], errs[i] = res.query(ctx, name, dnsTypes[t]) + }() + } + wg.Wait() + + var cnames []*dns.CNAME + nxdomain := false + for _, in := range answers { + if in == nil { + continue + } + nxdomain = nxdomain || in.Rcode == dns.RcodeNameError + for _, rr := range in.Answer { + if c, ok := rr.(*dns.CNAME); ok { + cnames = append(cnames, c) + } + } + } + chain, loop := cnameChain(name, cnames) + // A resolver answers a CNAME question with the first alias only, so + // follow the chain to a name that has data, does not exist or repeats. + seen := answers + for len(chain) > 0 && !loop && !nxdomain && len(chain) < maxAliases { + last := chain[len(chain)-1].Target + if hasData(seen, last) { + break + } + in, err := res.query(ctx, trimDot(last), dns.TypeA) + if err != nil { + break + } + seen = append(seen[:len(seen):len(seen)], in) + if in.Rcode == dns.RcodeNameError { + nxdomain = true + break + } + before := len(chain) + for _, rr := range in.Answer { + if c, ok := rr.(*dns.CNAME); ok { + cnames = append(cnames, c) + } + } + if chain, loop = cnameChain(name, cnames); len(chain) == before { + break + } + } + for _, c := range chain { + d.CNAMEChain = append(d.CNAMEChain, Record{Name: trimDot(c.Hdr.Name), Type: "CNAME", TTL: c.Hdr.Ttl, Value: trimDot(c.Target)}) + } + alias := Section{Title: "CNAME chain"} + if len(chain) == 0 { + alias.add("cname", Info, "", "none: "+name+" is not an alias", "") + } else { + for _, c := range d.CNAMEChain { + alias.add("cname", Info, ttl(c.TTL), c.Name+" -> "+c.Value, "") + } + alias.Note = "This name is an alias. The records below come from " + d.CNAMEChain[len(d.CNAMEChain)-1].Value + "." + } + r.add(alias) + + result := Section{Title: "Result"} + switch { + case loop: + result.add("cname.loop", Fail, "Alias loop", "the aliases lead back to "+d.CNAMEChain[len(d.CNAMEChain)-1].Value, "The name never resolves. Point one of the CNAMEs at a real name.") + case len(chain) >= maxAliases: + result.add("cname.long", Fail, "Alias chain", fmt.Sprintf("%d or more aliases", maxAliases), "Resolvers give up on chains this long. Point the name closer to its target.") + case nxdomain && len(chain) > 0: + last := d.CNAMEChain[len(d.CNAMEChain)-1].Value + result.add("nxdomain", Fail, "Dangling alias", last+" does not exist", "The alias points to a name that is gone. Fix or delete the CNAME: if someone can register the target, they can take over this name.") + case nxdomain: + result.add("nxdomain", Fail, "Not found", name+" does not exist (NXDOMAIN)", "No DNS records exist for this name. Check the spelling, or that the zone is set up.") + } + if len(result.Findings) > 0 { + r.add(result) + return r.finish(), nil + } + + addresses := 0 + for i, t := range types { + s := Section{Title: t + " records", Note: typeNotes[t]} + switch { + case errs[i] != nil: + d.Errors = append(d.Errors, t+": "+errs[i].Error()) + s.add(strings.ToLower(t), Fail, "", "lookup failed: "+errs[i].Error(), "") + case answers[i].Rcode != dns.RcodeSuccess: + problem := rcodeProblem(answers[i].Rcode).Error() + d.Errors = append(d.Errors, t+": "+problem) + s.add(strings.ToLower(t), Fail, "", problem, "") + default: + for _, rr := range answers[i].Answer { + if rr.Header().Rrtype != dnsTypes[t] { + continue + } + record := Record{Name: trimDot(rr.Header().Name), Type: t, TTL: rr.Header().Ttl, Value: rdata(rr)} + d.Records = append(d.Records, record) + s.add(strings.ToLower(t), Info, ttl(record.TTL), record.Value, "") + if t == "A" || t == "AAAA" { + addresses++ + } + } + if len(s.Findings) == 0 { + status := Info + if len(opts.Types) > 0 { + status = Warn + } + s.add(strings.ToLower(t), status, "", "none", "") + } + } + r.add(s) + } + if len(opts.Types) == 0 && addresses == 0 && len(d.Errors) == 0 { + s := Section{Title: "Result"} + s.add("address", Warn, "No address", "no A or AAAA records", "Browsers and most clients cannot connect to this name. That is fine for a name used only for mail or TXT records.") + r.add(s) + } + return r.finish(), nil +} + +func parseTypes(values []string) ([]string, error) { + if len(values) == 0 { + return DefaultDNSTypes, nil + } + var types []string + for _, value := range values { + for _, t := range strings.Split(value, ",") { + t = strings.ToUpper(strings.TrimSpace(t)) + if t == "" { + continue + } + if _, ok := dnsTypes[t]; !ok { + return nil, fmt.Errorf("unknown record type %q; use A, AAAA, CNAME, MX, NS, TXT, CAA or SOA", t) + } + if !slices.Contains(types, t) { + types = append(types, t) + } + } + } + if len(types) == 0 { + return DefaultDNSTypes, nil + } + return types, nil +} + +// maxAliases is the longest CNAME chain the check follows. +const maxAliases = 16 + +// cnameChain follows the aliases from name. loop is true when an alias +// points back to a name already in the chain. +func cnameChain(name string, cnames []*dns.CNAME) (chain []*dns.CNAME, loop bool) { + current := strings.ToLower(dns.Fqdn(name)) + seen := map[string]bool{current: true} + for len(chain) < maxAliases { + var next *dns.CNAME + for _, c := range cnames { + if strings.EqualFold(c.Hdr.Name, current) { + next = c + break + } + } + if next == nil { + return chain, false + } + chain = append(chain, next) + current = strings.ToLower(dns.Fqdn(next.Target)) + if seen[current] { + return chain, true + } + seen[current] = true + } + return chain, false +} + +// hasData is true when an answer holds a record other than a CNAME for +// name, which proves the end of the chain exists. +func hasData(answers []*dns.Msg, name string) bool { + for _, in := range answers { + if in == nil { + continue + } + for _, rr := range in.Answer { + if rr.Header().Rrtype != dns.TypeCNAME && strings.EqualFold(rr.Header().Name, name) { + return true + } + } + } + return false +} + +func rdata(rr dns.RR) string { + switch v := rr.(type) { + case *dns.A: + return v.A.String() + case *dns.AAAA: + return v.AAAA.String() + case *dns.CNAME: + return trimDot(v.Target) + case *dns.MX: + return fmt.Sprintf("%d %s", v.Preference, trimDot(v.Mx)) + case *dns.NS: + return trimDot(v.Ns) + case *dns.TXT: + return strings.Join(v.Txt, "") + case *dns.CAA: + return fmt.Sprintf("%d %s %q", v.Flag, v.Tag, v.Value) + case *dns.SOA: + return fmt.Sprintf("%s %s serial %d refresh %s retry %s expire %s minimum %s", trimDot(v.Ns), trimDot(v.Mbox), v.Serial, span(int64(v.Refresh)), span(int64(v.Retry)), span(int64(v.Expire)), span(int64(v.Minttl))) + } + header := rr.Header().String() + return strings.TrimSpace(strings.TrimPrefix(rr.String(), header)) +} + +func trimDot(s string) string { return strings.TrimSuffix(s, ".") } diff --git a/internal/check/dns_test.go b/internal/check/dns_test.go new file mode 100644 index 0000000..71713f3 --- /dev/null +++ b/internal/check/dns_test.go @@ -0,0 +1,203 @@ +package check + +import ( + "context" + "net" + "strings" + "testing" + "time" +) + +func TestDNSDefaultRecords(t *testing.T) { + server := fakeDNS(t, + "example.test. 300 IN A 192.0.2.1", + "example.test. 300 IN AAAA 2001:db8::1", + "example.test. 3600 IN MX 10 mail.example.test.", + "example.test. 86400 IN NS ns1.example.test.", + `example.test. 300 IN TXT "v=spf1 -all"`, + `example.test. 3600 IN CAA 0 issue "letsencrypt.org"`, + ) + r, err := DNS(context.Background(), "Example.test.", DNSOptions{Server: server, Timeout: 2 * time.Second}) + if err != nil { + t.Fatal(err) + } + if r.ExitCode != 0 || r.Target != "example.test" || r.LearnMore != learnDNS { + t.Fatalf("report = %+v", r) + } + d := r.Details.(*DNSDetails) + if len(d.Records) != 6 || len(d.CNAMEChain) != 0 || d.Server != server { + t.Fatalf("details = %+v", d) + } + want := map[string]string{"a": "192.0.2.1", "aaaa": "2001:db8::1", "mx": "10 mail.example.test", "ns": "ns1.example.test", "txt": "v=spf1 -all", "caa": `0 issue "letsencrypt.org"`} + for id, value := range want { + if f := finding(t, r, id); f.Value != value || !strings.HasPrefix(f.Label, "TTL ") { + t.Errorf("%s = %+v, want %q", id, f, value) + } + } + if f := finding(t, r, "mx"); f.Label != "TTL 3600s (1h)" { + t.Errorf("mx label = %q", f.Label) + } + if f := finding(t, r, "cname"); !strings.Contains(f.Value, "not an alias") { + t.Errorf("cname = %+v", f) + } +} + +func TestDNSCNAMEChainAndDangling(t *testing.T) { + server := fakeDNS(t, + "www.example.test. 60 IN CNAME edge.example.net.", + "edge.example.net. 120 IN CNAME host.example.org.", + "host.example.org. 30 IN A 192.0.2.10", + "old.example.test. 60 IN CNAME gone.example.net.", + ) + r, err := DNS(context.Background(), "www.example.test", DNSOptions{Server: server, Types: []string{"A"}}) + if err != nil { + t.Fatal(err) + } + d := r.Details.(*DNSDetails) + if len(d.CNAMEChain) != 2 || d.CNAMEChain[0].Value != "edge.example.net" || d.CNAMEChain[1].Value != "host.example.org" { + t.Fatalf("chain = %+v", d.CNAMEChain) + } + if len(d.Records) != 1 || d.Records[0].Name != "host.example.org" || d.Records[0].Value != "192.0.2.10" || r.ExitCode != 0 { + t.Fatalf("records = %+v, exit %d", d.Records, r.ExitCode) + } + + r, err = DNS(context.Background(), "old.example.test", DNSOptions{Server: server}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "nxdomain"); f.Status != Fail || f.Label != "Dangling alias" || !strings.Contains(f.Value, "gone.example.net") || r.ExitCode != 2 { + t.Fatalf("dangling = %+v, exit %d", f, r.ExitCode) + } + + r, err = DNS(context.Background(), "nope.example.test", DNSOptions{Server: server}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "nxdomain"); f.Label != "Not found" || r.ExitCode != 2 { + t.Fatalf("nxdomain = %+v", f) + } +} + +func TestDNSFollowsCNAMEOnlyAnswers(t *testing.T) { + server := fakeDNS(t, + "alias.test. 60 IN CNAME missing.test.", + "loop1.test. 60 IN CNAME loop2.test.", + "loop2.test. 60 IN CNAME loop1.test.", + "www.example.test. 60 IN CNAME edge.example.net.", + "edge.example.net. 120 IN CNAME host.example.org.", + "host.example.org. 30 IN A 192.0.2.10", + ) + r, err := DNS(context.Background(), "alias.test", DNSOptions{Server: server, Types: []string{"CNAME"}}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "nxdomain"); f.Label != "Dangling alias" || !strings.Contains(f.Value, "missing.test") || r.ExitCode != 2 { + t.Fatalf("dangling = %+v, exit %d", f, r.ExitCode) + } + r, err = DNS(context.Background(), "loop1.test", DNSOptions{Server: server, Types: []string{"CNAME"}}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "cname.loop"); f.Status != Fail || r.ExitCode != 2 { + t.Fatalf("loop = %+v", f) + } + r, err = DNS(context.Background(), "www.example.test", DNSOptions{Server: server, Types: []string{"CNAME"}}) + if err != nil { + t.Fatal(err) + } + if d := r.Details.(*DNSDetails); len(d.CNAMEChain) != 2 || r.ExitCode != 0 { + t.Fatalf("chain = %+v, exit %d", d.CNAMEChain, r.ExitCode) + } +} + +func TestDNSInternationalName(t *testing.T) { + server := fakeDNS(t, "xn--bcher-kva.test. 300 IN A 192.0.2.7") + r, err := DNS(context.Background(), "Bücher.test", DNSOptions{Server: server, Types: []string{"A"}}) + if err != nil { + t.Fatal(err) + } + if r.Target != "xn--bcher-kva.test" || r.Unicode != "bücher.test" || finding(t, r, "a").Value != "192.0.2.7" { + t.Fatalf("report = %+v", r) + } +} + +func TestDNSTypesAndWarnings(t *testing.T) { + server := fakeDNS(t, + "mail-only.test. 300 IN MX 10 mx.example.test.", + "example.test. 3600 IN SOA ns1.example.test. hostmaster.example.test. 2026111401 7200 3600 1209600 300", + ) + r, err := DNS(context.Background(), "mail-only.test", DNSOptions{Server: server}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "address"); f.Status != Warn || r.ExitCode != 1 { + t.Fatalf("address = %+v, exit %d", f, r.ExitCode) + } + if f := finding(t, r, "txt"); f.Status != Info || f.Value != "none" { + t.Fatalf("txt = %+v", f) + } + + r, err = DNS(context.Background(), "mail-only.test", DNSOptions{Server: server, Types: []string{"txt"}}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "txt"); f.Status != Warn || r.ExitCode != 1 || hasFinding(r, "address") || hasFinding(r, "mx") { + t.Fatalf("explicit type report = %+v", r.Sections) + } + + r, err = DNS(context.Background(), "example.test", DNSOptions{Server: server, Types: []string{"soa"}}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "soa"); !strings.HasPrefix(f.Value, "ns1.example.test hostmaster.example.test serial 2026111401 refresh 2h") { + t.Fatalf("soa = %+v", f) + } + + for _, opts := range []DNSOptions{{Server: server, Types: []string{"PTR"}}, {Server: "dns.google"}, {Server: "1.1.1.1:99999"}} { + if _, err := DNS(context.Background(), "example.test", opts); err == nil { + t.Errorf("%+v gave no error", opts) + } + } + if _, err := DNS(context.Background(), "192.0.2.1", DNSOptions{Server: server}); err == nil { + t.Error("an IP address gave no error") + } +} + +func TestDNSLargeAnswerUsesTCP(t *testing.T) { + long := strings.Repeat("x", 250) + record := "big.test. 300 IN TXT" + for i := 0; i < 8; i++ { + record += ` "` + long + `"` + } + server := fakeDNS(t, record) + r, err := DNS(context.Background(), "big.test", DNSOptions{Server: server, Types: []string{"TXT"}}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "txt"); len(f.Value) != 2000 { + t.Fatalf("TXT value has %d characters, want 2000: %+v", len(f.Value), r.Sections) + } +} + +func TestDNSTimeout(t *testing.T) { + pc, err := net.ListenPacket("udp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer pc.Close() + r, err := DNS(context.Background(), "example.test", DNSOptions{Server: pc.LocalAddr().String(), Types: []string{"A"}, Timeout: 200 * time.Millisecond}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "a"); f.Status != Fail || !strings.Contains(f.Value, "within 200ms") || r.ExitCode != 2 { + t.Fatalf("timeout = %+v", f) + } +} + +func TestServerAddress(t *testing.T) { + for in, want := range map[string]string{"1.1.1.1": "1.1.1.1:53", "8.8.8.8:5353": "8.8.8.8:5353", "2606:4700:4700::1111": "[2606:4700:4700::1111]:53", "[::1]:53": "[::1]:53"} { + if got, err := serverAddress(in); err != nil || got != want { + t.Errorf("serverAddress(%q) = %q, %v; want %q", in, got, err, want) + } + } +} diff --git a/internal/check/dnsserver_test.go b/internal/check/dnsserver_test.go new file mode 100644 index 0000000..b222c5b --- /dev/null +++ b/internal/check/dnsserver_test.go @@ -0,0 +1,89 @@ +package check + +import ( + "net" + "strings" + "testing" + + "github.com/miekg/dns" +) + +// fakeDNS serves the records (in zone file syntax) on 127.0.0.1 over UDP +// and TCP and answers like a recursive resolver: it follows CNAMEs and +// says NXDOMAIN for names with no records. UDP answers that do not fit +// are truncated, so clients must retry over TCP. +func fakeDNS(t *testing.T, records ...string) string { + t.Helper() + var rrs []dns.RR + for _, record := range records { + rr, err := dns.NewRR(record) + if err != nil { + t.Fatalf("bad test record %q: %v", record, err) + } + rrs = append(rrs, rr) + } + handler := dns.HandlerFunc(func(w dns.ResponseWriter, req *dns.Msg) { + m := new(dns.Msg) + m.SetReply(req) + q := req.Question[0] + name := q.Name + for hops := 0; hops < 8; hops++ { + exists, alias := false, "" + for _, rr := range rrs { + h := rr.Header() + if !strings.EqualFold(h.Name, name) { + continue + } + exists = true + if h.Rrtype == q.Qtype { + m.Answer = append(m.Answer, rr) + } else if c, ok := rr.(*dns.CNAME); ok && q.Qtype != dns.TypeCNAME { + m.Answer = append(m.Answer, rr) + alias = c.Target + } + } + if !exists { + m.Rcode = dns.RcodeNameError + } + if alias == "" { + break + } + name = alias + } + if _, udp := w.RemoteAddr().(*net.UDPAddr); udp { + size := dns.MinMsgSize + if opt := req.IsEdns0(); opt != nil { + size = int(opt.UDPSize()) + } + m.Truncate(size) + } + _ = w.WriteMsg(m) + }) + + var udp, tcp *dns.Server + for attempt := 0; ; attempt++ { + pc, err := net.ListenPacket("udp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + ln, err := net.Listen("tcp", pc.LocalAddr().String()) + if err != nil { + pc.Close() + if attempt < 5 { + continue + } + t.Fatal(err) + } + udp = &dns.Server{PacketConn: pc, Handler: handler} + tcp = &dns.Server{Listener: ln, Handler: handler} + break + } + for _, srv := range []*dns.Server{udp, tcp} { + started := make(chan struct{}) + srv.NotifyStartedFunc = func() { close(started) } + go func() { _ = srv.ActivateAndServe() }() + <-started + t.Cleanup(func() { _ = srv.Shutdown() }) + } + return udp.PacketConn.LocalAddr().String() +} diff --git a/internal/check/email.go b/internal/check/email.go new file mode 100644 index 0000000..579c725 --- /dev/null +++ b/internal/check/email.go @@ -0,0 +1,703 @@ +package check + +import ( + "context" + "crypto/ed25519" + "crypto/rsa" + "crypto/x509" + "encoding/base64" + "fmt" + "net/netip" + "slices" + "sort" + "strconv" + "strings" + "sync" + "time" + + "github.com/miekg/dns" +) + +// SPFLookupLimit is the most DNS lookups an SPF check may need (RFC 7208). +const SPFLookupLimit = 10 + +// DKIMSelectors are the selectors "check email" tries. DKIM keys live at +// ._domainkey. and there is no way to list them. +var DKIMSelectors = []string{"default", "google", "selector1", "selector2", "k1", "s1", "mail", "dkim"} + +type EmailOptions struct { + Server string + Selectors []string // tried after DKIMSelectors + Timeout time.Duration +} + +type MX struct { + Preference uint16 `json:"preference"` + Host string `json:"host"` +} + +type SPFDetails struct { + Records []string `json:"records"` + Lookups int `json:"lookups"` + All string `json:"all,omitempty"` + Problems []string `json:"problems,omitempty"` + Macros []string `json:"macros,omitempty"` +} + +type DMARCDetails struct { + Records []string `json:"records"` + Tags map[string]string `json:"tags,omitempty"` +} + +type DKIMKey struct { + Selector string `json:"selector"` + Found bool `json:"found"` + KeyType string `json:"keyType,omitempty"` + Bits int `json:"bits,omitempty"` + Revoked bool `json:"revoked,omitempty"` + Problem string `json:"problem,omitempty"` + Error string `json:"error,omitempty"` +} + +type EmailDetails struct { + Domain string `json:"domain"` + Server string `json:"server"` + MX []MX `json:"mx"` + SPF SPFDetails `json:"spf"` + DMARC DMARCDetails `json:"dmarc"` + DKIM []DKIMKey `json:"dkim"` + MTASTS string `json:"mtaSts,omitempty"` + TLSRPT string `json:"tlsRpt,omitempty"` +} + +// Email checks the DNS records that decide whether mail from and to domain +// is trusted: MX, SPF, DMARC, DKIM, MTA-STS and TLS-RPT. The error is only +// for bad input. +func Email(ctx context.Context, input string, opts EmailOptions) (*Report, error) { + domain, err := domainName(input) + if err != nil { + return nil, err + } + res, err := newResolver(opts.Server, opts.Timeout) + if err != nil { + return nil, err + } + d := &EmailDetails{Domain: domain, Server: res.server, MX: []MX{}, SPF: SPFDetails{Records: []string{}}, DMARC: DMARCDetails{Records: []string{}}, DKIM: []DKIMKey{}} + r := &Report{Check: "email", Target: domain, Unicode: unicodeName(domain), Details: d, LearnMore: learnEmail} + server := Section{Title: "DNS server"} + server.add("server", Info, "Asked", res.server+" ("+res.source+")", "") + r.add(server) + + mx := Section{Title: "Mail servers (MX)", Note: "The servers that receive mail for this domain. Senders try the lowest number first."} + in, err := res.query(ctx, domain, dns.TypeMX) + switch { + case err != nil: + mx.add("mx", Fail, "", "lookup failed: "+err.Error(), "") + case in.Rcode == dns.RcodeNameError: + mx.add("mx", Fail, "", domain+" does not exist (NXDOMAIN)", "There are no DNS records for this name. Check the spelling.") + r.add(mx) + return r.finish(), nil + case in.Rcode != dns.RcodeSuccess: + mx.add("mx", Fail, "", rcodeProblem(in.Rcode).Error(), "") + default: + for _, rr := range in.Answer { + if m, ok := rr.(*dns.MX); ok { + d.MX = append(d.MX, MX{Preference: m.Preference, Host: trimDot(m.Mx)}) + } + } + sort.SliceStable(d.MX, func(i, j int) bool { return d.MX[i].Preference < d.MX[j].Preference }) + switch { + case len(d.MX) == 1 && (d.MX[0].Host == "" || d.MX[0].Host == "."): + mx.add("mx", Info, "", "null MX: this domain accepts no mail", "The owner says no mail should be sent here (RFC 7505).") + case len(d.MX) == 0: + mx.add("mx", Warn, "", "none", "Without MX records, mail for this domain goes to its A record, if there is one, or bounces.") + default: + for _, m := range d.MX { + mx.add("mx", Info, fmt.Sprint(m.Preference), m.Host, "") + } + } + } + r.add(mx) + + r.add(checkSPF(ctx, res, domain, &d.SPF)) + r.add(checkDMARC(ctx, res, domain, &d.DMARC)) + r.add(checkDKIM(ctx, res, domain, opts.Selectors, &d.DKIM)) + r.add(checkTransport(ctx, res, domain, d)) + return r.finish(), nil +} + +func checkSPF(ctx context.Context, res *resolver, domain string, d *SPFDetails) Section { + s := Section{Title: "SPF", Note: "Lists the servers allowed to send mail as this domain. Receivers compare it with the sending server."} + records, err := res.txt(ctx, domain) + if err != nil { + s.add("spf", Fail, "Record", "lookup failed: "+err.Error(), "") + return s + } + d.Records = append(d.Records, withPrefix(records, "v=spf1")...) + switch len(d.Records) { + case 0: + s.add("spf", Missing, "Record", "missing", "Without SPF, receivers cannot tell which servers may send as this domain, and more of your mail lands in spam.") + return s + case 1: + s.add("spf", Pass, "Record", d.Records[0], "") + default: + s.add("spf", Fail, "Record", fmt.Sprintf("%d SPF records", len(d.Records)), "Receivers treat more than one SPF record as an error (permerror), so SPF fails. Merge them into one.") + for _, record := range d.Records { + s.add("spf", Info, "", record, "") + } + return s + } + + w := &spfWalk{res: res, path: map[string]bool{domain: true}} + all, via := w.record(ctx, domain, d.Records[0], 0) + d.Lookups, d.All, d.Problems, d.Macros = w.lookups, all, w.problems, w.macros + lookupNote := "Each include, a, mx, ptr, exists and redirect costs one DNS lookup, nested ones too. Over 10 and SPF fails." + switch { + case w.lookups > SPFLookupLimit: + s.add("spf.lookups", Fail, "DNS lookups", fmt.Sprintf("more than %d", SPFLookupLimit), lookupNote+" Remove includes you no longer use, or replace some with ip4/ip6.") + case len(w.macros) > 0: + s.add("spf.lookups", Warn, "DNS lookups", fmt.Sprintf("%d of %d, not counting %s", w.lookups, SPFLookupLimit, strings.Join(w.macros, ", ")), "The record uses macros (%{...}), which are filled in for each message, so the lookup count cannot be checked.") + default: + s.add("spf.lookups", Pass, "DNS lookups", fmt.Sprintf("%d of %d", w.lookups, SPFLookupLimit), lookupNote) + } + for _, problem := range w.problems { + s.add("spf.problem", Fail, "Problem", problem, "Receivers treat this as an error (permerror), so SPF fails.") + } + if w.ptr { + s.add("spf.ptr", Warn, "ptr", "uses the ptr mechanism", "ptr is deprecated: slow, unreliable and some receivers skip it. Use ip4, ip6 or include.") + } + value := all + if via != "" { + value += " (from redirect=" + via + ")" + } + switch strings.TrimPrefix(all, "+") { + case "-all": + s.add("spf.all", Pass, "Other servers", value, "Fail: mail from any server not listed fails SPF.") + case "~all": + s.add("spf.all", Pass, "Other servers", value, "Softfail: mail from servers not listed is marked suspicious. Fine together with a DMARC policy.") + case "?all": + s.add("spf.all", Warn, "Other servers", value, "Neutral: says nothing about servers not listed, so SPF gives no protection. Use ~all or -all.") + case "all": + s.add("spf.all", Fail, "Other servers", value, "Anyone may send mail as this domain. Use ~all or -all.") + default: + s.add("spf.all", Warn, "Other servers", "no all at the end", "Servers not listed get neutral, which gives no protection. End the record with ~all or -all.") + } + return s +} + +type spfWalk struct { + res *resolver + lookups int + problems []string + macros []string + ptr bool + path map[string]bool +} + +// record checks one SPF record of domain and the records it includes, and +// returns its "all" term. Like a receiver, it reads every term for syntax +// errors but evaluates only up to the first "all", and follows redirect= +// only when there is no "all". +func (w *spfWalk) record(ctx context.Context, domain, record string, depth int) (all, via string) { + where := "" + if depth > 0 { + where = " (in the record of " + domain + ")" + } + redirect := "" + for _, raw := range strings.Fields(record)[1:] { + term := raw + qualifier := "" + if strings.ContainsRune("+-~?", rune(term[0])) { + qualifier, term = term[:1], term[1:] + } + i := strings.IndexAny(term, ":=/") + name, sep, arg := term, byte(0), "" + if i >= 0 { + name, sep, arg = term[:i], term[i], term[i+1:] + } + name = strings.ToLower(name) + if sep == '=' { + if name == "redirect" && redirect == "" { + redirect = strings.ToLower(arg) + } + continue // a modifier + } + if problem := spfSyntax(name, sep, arg); problem != "" { + w.problems = append(w.problems, raw+" "+problem+where) + continue + } + if all != "" { + continue // never evaluated: receivers stop at "all" + } + target := domain + if sep == ':' { + target, _, _ = strings.Cut(strings.ToLower(arg), "/") + } + switch name { + case "all": + all = qualifier + "all" + case "include": + w.lookups++ + w.include(ctx, raw, target, depth) + case "a", "exists": + w.lookups++ + case "mx": + w.lookups++ + w.mx(ctx, raw, target, where) + case "ptr": + w.lookups++ + w.ptr = true + } + } + if all != "" || redirect == "" { + return all, "" + } + w.lookups++ + if strings.Contains(redirect, "%") { + w.macros = append(w.macros, "redirect="+redirect) + return "", redirect + } + target, ok := w.fetch(ctx, "redirect="+redirect, redirect, depth) + if !ok { + return "", redirect + } + all, _ = w.record(ctx, redirect, target, depth+1) + delete(w.path, redirect) + return all, redirect +} + +// spfSyntax checks a mechanism's name and argument and says what is wrong. +func spfSyntax(name string, sep byte, arg string) string { + switch name { + case "all": + if sep != 0 { + return "is not valid: all takes no argument" + } + case "include", "exists": + if sep != ':' || arg == "" { + return "needs a domain, like " + name + ":example.com" + } + case "ptr": + case "a", "mx": + cidr := arg + if sep == ':' { + _, rest, found := strings.Cut(arg, "/") + cidr = "" + if found { + cidr = rest + } + } + if sep == '/' || sep == ':' && cidr != "" { + if !validDualCIDR("/" + cidr) { + return "has a bad prefix length (use /0 to /32 for IPv4 and //0 to //128 for IPv6)" + } + } + case "ip4", "ip6": + if sep != ':' || !validIPRange(name, arg) { + if name == "ip4" { + return "is not a valid IPv4 address or range, like ip4:192.0.2.0/24" + } + return "is not a valid IPv6 address or range, like ip6:2001:db8::/32" + } + default: + return "is not an SPF mechanism" + } + return "" +} + +func validIPRange(kind, value string) bool { + address, length, hasLength := strings.Cut(value, "/") + ip, err := netip.ParseAddr(address) + if err != nil || ip.Zone() != "" { + return false + } + max := 128 + if kind == "ip4" { + if !ip.Is4() { + return false + } + max = 32 + } else if !ip.Is6() { + return false + } + return !hasLength || validLength(length, max) +} + +// validDualCIDR accepts "", "/24", "//64" and "/24//64". +func validDualCIDR(c string) bool { + v4, v6, has6 := c, "", false + if i := strings.Index(c, "//"); i >= 0 { + v4, v6, has6 = c[:i], c[i+2:], true + } + if v4 != "" && !validLength(strings.TrimPrefix(v4, "/"), 32) { + return false + } + return !has6 || validLength(v6, 128) +} + +func validLength(s string, max int) bool { + if s == "" || len(s) > 3 || strings.Trim(s, "0123456789") != "" { + return false + } + n, _ := strconv.Atoi(s) + return n <= max +} + +func (w *spfWalk) include(ctx context.Context, term, domain string, depth int) { + if strings.Contains(domain, "%") { + w.macros = append(w.macros, term) + return + } + if record, ok := w.fetch(ctx, term, domain, depth); ok { + w.record(ctx, domain, record, depth+1) + delete(w.path, domain) + } +} + +// mx checks the separate limit of the mx mechanism: receivers look up the +// address of every MX host, and more than 10 hosts is an error. +func (w *spfWalk) mx(ctx context.Context, term, domain, where string) { + if strings.Contains(domain, "%") || w.lookups > SPFLookupLimit { + return + } + in, err := w.res.query(ctx, domain, dns.TypeMX) + if err != nil { + return + } + hosts := 0 + for _, rr := range in.Answer { + if _, ok := rr.(*dns.MX); ok { + hosts++ + } + } + if hosts > SPFLookupLimit { + w.problems = append(w.problems, fmt.Sprintf("%s needs the addresses of %d MX hosts; more than %d is an error%s", term, hosts, SPFLookupLimit, where)) + } +} + +// fetch reads the SPF record a term points to. It stops once the count is +// over the limit, since the result is already known. +func (w *spfWalk) fetch(ctx context.Context, term, domain string, depth int) (string, bool) { + if w.lookups > SPFLookupLimit || depth >= SPFLookupLimit { + return "", false + } + if w.path[domain] { + w.problems = append(w.problems, term+" loops back to a record that includes it") + return "", false + } + records, err := w.res.txt(ctx, domain) + if err != nil { + w.problems = append(w.problems, term+": lookup failed: "+err.Error()) + return "", false + } + spf := withPrefix(records, "v=spf1") + switch len(spf) { + case 0: + w.problems = append(w.problems, term+" has no SPF record") + return "", false + case 1: + w.path[domain] = true + return spf[0], true + } + w.problems = append(w.problems, fmt.Sprintf("%s has %d SPF records", term, len(spf))) + return "", false +} + +func checkDMARC(ctx context.Context, res *resolver, domain string, d *DMARCDetails) Section { + s := Section{Title: "DMARC", Note: "Tells receivers what to do with mail that fails both SPF and DKIM for this domain, and where to send reports."} + records, err := res.txt(ctx, "_dmarc."+domain) + if err != nil { + s.add("dmarc", Fail, "Record", "lookup failed: "+err.Error(), "") + return s + } + valid, bad := dmarcRecords(records) + d.Records = append(d.Records, valid...) + switch len(d.Records) { + case 0: + if len(bad) > 0 { + s.add("dmarc", Fail, "Record", bad[0], "The version must be written exactly v=DMARC1 (upper case), so receivers ignore this record.") + return s + } + meaning := "No DMARC record at _dmarc." + domain + ": spoofed mail is not blocked and you get no reports. Start with v=DMARC1; p=none; rua=mailto:." + if strings.Count(domain, ".") > 1 { + meaning += " For a subdomain, the parent domain's record may apply." + } + s.add("dmarc", Missing, "Record", "missing", meaning) + return s + case 1: + s.add("dmarc", Pass, "Record", d.Records[0], "") + default: + s.add("dmarc", Fail, "Record", fmt.Sprintf("%d DMARC records", len(d.Records)), "With more than one record, receivers ignore DMARC for this domain. Keep one.") + return s + } + tags := tagList(d.Records[0]) + d.Tags = tags + + switch strings.ToLower(tags["p"]) { + case "reject": + s.add("dmarc.p", Pass, "Policy (p)", "reject", "Mail that fails is refused.") + case "quarantine": + s.add("dmarc.p", Pass, "Policy (p)", "quarantine", "Mail that fails goes to spam.") + case "none": + s.add("dmarc.p", Warn, "Policy (p)", "none", "Monitoring only: mail that fails is still delivered. Move to quarantine, then reject, once the reports look clean.") + case "": + s.add("dmarc.p", Fail, "Policy (p)", "missing", "p= is required. Without it receivers ignore the record.") + default: + s.add("dmarc.p", Fail, "Policy (p)", tags["p"], "Not a valid policy; use none, quarantine or reject.") + } + if sp, ok := tags["sp"]; ok { + s.add("dmarc.sp", Info, "Subdomains (sp)", sp, "The policy for subdomains.") + } + if np, ok := tags["np"]; ok { + s.add("dmarc.np", Info, "Missing names (np)", np, "The policy for subdomains that do not exist, a common spoofing target.") + } + if pct, ok := tags["pct"]; ok { + if n, err := strconv.Atoi(pct); err == nil && n == 100 { + s.add("dmarc.pct", Info, "Percent (pct)", "100", "The policy applies to all failing mail. The updated DMARC standard (DMARCbis) drops pct, so you can delete it.") + } else { + s.add("dmarc.pct", Warn, "Percent (pct)", pct, "Only that share of failing mail gets the policy. The updated DMARC standard (DMARCbis) drops pct: receivers that follow it apply the full policy. Use t=y while testing instead.") + } + } else { + s.add("dmarc.pct", Info, "Percent (pct)", "not set (all mail)", "The policy applies to all failing mail.") + } + if strings.EqualFold(tags["t"], "y") { + switch strings.ToLower(tags["p"]) { + case "reject": + s.add("dmarc.t", Warn, "Testing (t)", "y", "Testing mode: receivers apply quarantine instead of reject, so failing mail goes to spam instead of being refused.") + case "quarantine": + s.add("dmarc.t", Warn, "Testing (t)", "y", "Testing mode: receivers apply none instead of quarantine, so failing mail is still delivered.") + default: + s.add("dmarc.t", Info, "Testing (t)", "y", "Testing mode. With p=none it changes nothing: receivers only report.") + } + } + if rua := tags["rua"]; rua != "" { + s.add("dmarc.rua", Pass, "Reports (rua)", rua, "Daily summary reports of who sends mail as you go here.") + } else { + s.add("dmarc.rua", Warn, "Reports (rua)", "not set", "Without rua you get no reports, so you cannot see who sends mail as you or whether it passes.") + } + return s +} + +func checkDKIM(ctx context.Context, res *resolver, domain string, extra []string, keys *[]DKIMKey) Section { + s := Section{Title: "DKIM", Note: "Public keys that receivers use to check the signature on your mail. Each key sits under a selector name."} + selectors := append([]string{}, DKIMSelectors...) + for _, sel := range extra { + sel = strings.ToLower(strings.TrimSpace(sel)) + if sel != "" && !slices.Contains(selectors, sel) { + selectors = append(selectors, sel) + } + } + results := make([]DKIMKey, len(selectors)) + var wg sync.WaitGroup + for i, sel := range selectors { + wg.Add(1) + go func() { + defer wg.Done() + results[i] = dkimKey(ctx, res, sel, domain) + }() + } + wg.Wait() + *keys = results + + var missing []string + found := 0 + for _, k := range results { + label := k.Selector + switch { + case k.Error != "": + s.add("dkim", Warn, label, "lookup failed: "+k.Error, "") + case !k.Found: + missing = append(missing, k.Selector) + case k.Problem != "": + found++ + s.add("dkim", Fail, label, "malformed: "+k.Problem, "Receivers cannot use this key, so mail signed with this selector fails DKIM.") + case k.Revoked: + found++ + s.add("dkim", Info, label, "revoked (empty p=)", "This selector was switched off on purpose.") + case k.KeyType == "rsa" && k.Bits < 1024: + found++ + s.add("dkim", Fail, label, fmt.Sprintf("RSA %d-bit", k.Bits), "Too short: receivers ignore keys under 1024 bits. Make a 2048-bit key.") + case k.KeyType == "rsa" && k.Bits < 2048: + found++ + s.add("dkim", Warn, label, fmt.Sprintf("RSA %d-bit", k.Bits), "Works, but 2048 bits is the current advice.") + case k.KeyType == "rsa": + found++ + s.add("dkim", Pass, label, fmt.Sprintf("RSA %d-bit", k.Bits), "") + default: + found++ + s.add("dkim", Pass, label, "Ed25519", "Not every receiver checks Ed25519 yet, so also sign with an RSA key.") + } + } + if len(missing) > 0 { + s.add("dkim.missing", Info, "Not found", strings.Join(missing, ", "), "") + } + if found == 0 { + s.add("dkim.none", Info, "Result", "no key at the common selectors", "That does not prove DKIM is missing: many providers (Amazon SES, for example) use their own selector names. Find s= in the DKIM-Signature header of a message from this domain and run again with --selector .") + } + return s +} + +func dkimKey(ctx context.Context, res *resolver, selector, domain string) DKIMKey { + key := DKIMKey{Selector: selector} + records, err := res.txt(ctx, selector+"._domainkey."+domain) + if err != nil { + key.Error = err.Error() + return key + } + for _, record := range records { + tags, order, dup := parseTags(record) + version, hasVersion := tags["v"] + p, hasKey := tags["p"] + if !hasVersion && !hasKey { + continue // not a DKIM record (a wildcard TXT, for example) + } + key.Found = true + key.KeyType = strings.ToLower(tags["k"]) + if key.KeyType == "" { + key.KeyType = "rsa" + } + p = strings.Join(strings.Fields(p), "") + switch { + case dup != "": + key.Problem = dup + "= appears twice" + case hasVersion && (version != "DKIM1" || order[0] != "v"): + key.Problem = "v= must come first and be exactly DKIM1" + case !hasKey: + key.Problem = "there is no p= tag, so there is no key" + case p == "": + key.Revoked = true + } + if key.Problem != "" || key.Revoked { + return key + } + der, err := base64.StdEncoding.DecodeString(p) + if err != nil { + key.Problem = "p= is not valid base64" + return key + } + switch key.KeyType { + case "rsa": + if pub, err := x509.ParsePKIXPublicKey(der); err == nil { + if rsaKey, ok := pub.(*rsa.PublicKey); ok { + key.Bits = rsaKey.N.BitLen() + } + } else if rsaKey, err := x509.ParsePKCS1PublicKey(der); err == nil { + key.Bits = rsaKey.N.BitLen() + } + if key.Bits == 0 { + key.Problem = "p= is not an RSA public key" + } + case "ed25519": + if len(der) != ed25519.PublicKeySize { + key.Problem = "p= is not a 32-byte Ed25519 key" + } else { + key.Bits = 256 + } + default: + key.Problem = "k=" + key.KeyType + " is not a key type receivers know (rsa or ed25519)" + } + return key + } + return key +} + +func checkTransport(ctx context.Context, res *resolver, domain string, d *EmailDetails) Section { + s := Section{Title: "MTA-STS and TLS-RPT", Note: "Optional. They make other mail servers use TLS when they deliver to you, and report when that fails."} + policy := "https://mta-sts." + domain + "/.well-known/mta-sts.txt" + records, err := res.txt(ctx, "_mta-sts."+domain) + sts := withPrefix(records, "v=STSv1") + switch { + case err != nil: + s.add("mta-sts", Warn, "MTA-STS", "lookup failed: "+err.Error(), "") + case len(sts) > 1: + s.add("mta-sts", Fail, "MTA-STS", fmt.Sprintf("%d records", len(sts)), "With more than one record, senders ignore MTA-STS. Keep one.") + case len(sts) == 1 && !validSTSID(tagList(sts[0])["id"]): + s.add("mta-sts", Fail, "MTA-STS", sts[0], "The record needs id= (1 to 32 letters and digits). Senders compare it to notice a new policy, and ignore a record without it.") + case len(sts) == 1: + d.MTASTS = sts[0] + s.add("mta-sts", Pass, "MTA-STS", sts[0], "Senders fetch the policy from "+policy+" (not downloaded by this check).") + default: + s.add("mta-sts", Info, "MTA-STS", "not set", "Without it, a sender may deliver in plain text when TLS to your servers fails. The policy would live at "+policy+".") + } + records, err = res.txt(ctx, "_smtp._tls."+domain) + rpt := withPrefix(records, "v=TLSRPTv1") + switch { + case err != nil: + s.add("tls-rpt", Warn, "TLS-RPT", "lookup failed: "+err.Error(), "") + case len(rpt) > 1: + s.add("tls-rpt", Fail, "TLS-RPT", fmt.Sprintf("%d records", len(rpt)), "With more than one record, senders ignore TLS-RPT. Keep one.") + case len(rpt) == 1 && tagList(rpt[0])["rua"] == "": + s.add("tls-rpt", Fail, "TLS-RPT", rpt[0], "The record needs rua= (a mailto: or https: address for the reports), so senders ignore it.") + case len(rpt) == 1: + d.TLSRPT = rpt[0] + s.add("tls-rpt", Pass, "TLS-RPT", rpt[0], "Senders report TLS problems delivering to you to the rua address.") + default: + s.add("tls-rpt", Info, "TLS-RPT", "not set", "Lets senders report TLS problems when they deliver to you.") + } + return s +} + +// validSTSID accepts the MTA-STS id: 1 to 32 letters and digits. +func validSTSID(id string) bool { + if id == "" || len(id) > 32 { + return false + } + for _, r := range id { + if !(r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9') { + return false + } + } + return true +} + +// dmarcRecords picks the DMARC records: the first tag must be v, with +// optional spaces around "=", and the value exactly DMARC1. bad holds the +// records that would be DMARC but for the case of the version. +func dmarcRecords(records []string) (valid, bad []string) { + for _, record := range records { + first, _, _ := strings.Cut(record, ";") + key, value, ok := strings.Cut(first, "=") + if !ok || !strings.EqualFold(strings.TrimSpace(key), "v") { + continue + } + switch value = strings.TrimSpace(value); { + case value == "DMARC1": + valid = append(valid, strings.TrimSpace(record)) + case strings.EqualFold(value, "DMARC1"): + bad = append(bad, strings.TrimSpace(record)) + } + } + return valid, bad +} + +// tagList parses "k=v; k2=v2" records (DMARC, DKIM, MTA-STS). Keys are +// lower case; for a repeated key the first value counts. +func tagList(record string) map[string]string { + tags, _, _ := parseTags(record) + return tags +} + +// parseTags is tagList that also returns the keys in order and the first +// key that appears twice. +func parseTags(record string) (tags map[string]string, order []string, dup string) { + tags = map[string]string{} + for _, part := range strings.Split(record, ";") { + key, value, ok := strings.Cut(part, "=") + if !ok { + continue + } + key = strings.ToLower(strings.TrimSpace(key)) + if key == "" { + continue + } + if _, seen := tags[key]; seen { + if dup == "" { + dup = key + } + continue + } + tags[key] = strings.TrimSpace(value) + order = append(order, key) + } + return tags, order, dup +} diff --git a/internal/check/email_test.go b/internal/check/email_test.go new file mode 100644 index 0000000..8251f72 --- /dev/null +++ b/internal/check/email_test.go @@ -0,0 +1,410 @@ +package check + +import ( + "context" + "crypto/rsa" + "crypto/x509" + "encoding/base64" + "fmt" + "math/big" + "slices" + "strings" + "testing" +) + +// rsaKey returns a base64 public key of the given size. Only the size is +// read, so the modulus does not need to be a real one. +func rsaKey(t *testing.T, bits int) string { + t.Helper() + n := new(big.Int).Lsh(big.NewInt(1), uint(bits-1)) + n.SetBit(n, 0, 1) + der, err := x509.MarshalPKIXPublicKey(&rsa.PublicKey{N: n, E: 65537}) + if err != nil { + t.Fatal(err) + } + return base64.StdEncoding.EncodeToString(der) +} + +// txt writes a TXT record, split into strings of at most 200 characters. +func txt(name, value string) string { + record := name + " 300 IN TXT" + for len(value) > 200 { + record += ` "` + value[:200] + `"` + value = value[200:] + } + return record + ` "` + value + `"` +} + +func dkimFor(d *EmailDetails, selector string) DKIMKey { + for _, k := range d.DKIM { + if k.Selector == selector { + return k + } + } + return DKIMKey{} +} + +func TestEmailGoodDomain(t *testing.T) { + ed25519Key := base64.StdEncoding.EncodeToString(make([]byte, 32)) + server := fakeDNS(t, + "good.test. 300 IN MX 20 mx2.good.test.", + "good.test. 300 IN MX 10 mx1.good.test.", + txt("good.test.", "v=spf1 include:_spf.good.test ip4:192.0.2.0/24 ~all"), + txt("good.test.", "google-site-verification=abc"), + txt("_spf.good.test.", "v=spf1 include:a.mail.test include:b.mail.test -all"), + txt("a.mail.test.", "v=spf1 ip4:198.51.100.0/24 -all"), + txt("b.mail.test.", "v=spf1 a mx -all"), + txt("_dmarc.good.test.", "v=DMARC1; p=reject; rua=mailto:dmarc@good.test"), + txt("selector1._domainkey.good.test.", "v=DKIM1; k=rsa; p="+rsaKey(t, 2048)), + txt("s1._domainkey.good.test.", "v=DKIM1; k=ed25519; p="+ed25519Key), + txt("custom._domainkey.good.test.", "v=DKIM1; p="+rsaKey(t, 2048)), + txt("_mta-sts.good.test.", "v=STSv1; id=20261114"), + txt("_smtp._tls.good.test.", "v=TLSRPTv1; rua=mailto:tls@good.test"), + ) + r, err := Email(context.Background(), "postmaster@good.test", EmailOptions{Server: server, Selectors: []string{"Custom", "selector1"}}) + if err != nil { + t.Fatal(err) + } + if r.ExitCode != 0 || r.Target != "good.test" || r.LearnMore != learnEmail { + t.Fatalf("exit %d, report = %+v", r.ExitCode, r.Sections) + } + d := r.Details.(*EmailDetails) + if len(d.MX) != 2 || d.MX[0].Host != "mx1.good.test" || d.MX[1].Preference != 20 { + t.Fatalf("mx = %+v", d.MX) + } + if d.SPF.Lookups != 5 || d.SPF.All != "~all" || len(d.SPF.Problems) != 0 { + t.Fatalf("spf = %+v", d.SPF) + } + if f := finding(t, r, "spf.lookups"); f.Value != "5 of 10" || f.Status != Pass { + t.Fatalf("lookups = %+v", f) + } + if d.DMARC.Tags["p"] != "reject" || d.MTASTS == "" || d.TLSRPT == "" { + t.Fatalf("details = %+v", d) + } + if k := dkimFor(d, "selector1"); !k.Found || k.KeyType != "rsa" || k.Bits != 2048 { + t.Fatalf("selector1 = %+v", k) + } + if k := dkimFor(d, "s1"); !k.Found || k.KeyType != "ed25519" { + t.Fatalf("s1 = %+v", k) + } + if k := dkimFor(d, "custom"); !k.Found || k.Bits != 2048 { + t.Fatalf("custom = %+v", k) + } + if len(d.DKIM) != len(DKIMSelectors)+1 { + t.Fatalf("selectors tried = %d", len(d.DKIM)) + } + if f := finding(t, r, "dkim.missing"); !strings.Contains(f.Value, "default, google") { + t.Fatalf("missing selectors = %+v", f) + } +} + +func TestEmailProblems(t *testing.T) { + server := fakeDNS(t, + txt("open.test.", "v=spf1 include:nospf.test +all"), + txt("nospf.test.", "hello"), + txt("_dmarc.open.test.", "v=DMARC1; p=none; pct=50"), + txt("default._domainkey.open.test.", "v=DKIM1; p="+rsaKey(t, 1024)), + txt("k1._domainkey.open.test.", "v=DKIM1; p="), + txt("mail._domainkey.open.test.", "v=DKIM1; p="+rsaKey(t, 512)), + txt("two.test.", "v=spf1 -all"), + txt("two.test.", "v=spf1 ip4:192.0.2.1 -all"), + ) + r, err := Email(context.Background(), "open.test", EmailOptions{Server: server}) + if err != nil { + t.Fatal(err) + } + if r.ExitCode != 2 { + t.Fatalf("exit code = %d", r.ExitCode) + } + want := map[string]Status{"mx": Warn, "spf": Pass, "spf.problem": Fail, "spf.all": Fail, "dmarc.p": Warn, "dmarc.pct": Warn, "dmarc.rua": Warn, "mta-sts": Info, "tls-rpt": Info} + for id, status := range want { + if f := finding(t, r, id); f.Status != status { + t.Errorf("%s = %+v, want %s", id, f, status) + } + } + d := r.Details.(*EmailDetails) + if k := dkimFor(d, "default"); k.Bits != 1024 { + t.Errorf("default = %+v", k) + } + if k := dkimFor(d, "k1"); !k.Revoked { + t.Errorf("k1 = %+v", k) + } + if k := dkimFor(d, "mail"); k.Bits != 512 { + t.Errorf("mail = %+v", k) + } + statuses := map[string]Status{} + for _, s := range r.Sections { + for _, f := range s.Findings { + if f.ID == "dkim" { + statuses[f.Label] = f.Status + } + } + } + if statuses["default"] != Warn || statuses["k1"] != Info || statuses["mail"] != Fail { + t.Errorf("DKIM statuses = %v", statuses) + } + + r, err = Email(context.Background(), "two.test", EmailOptions{Server: server}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "spf"); f.Status != Fail || f.Value != "2 SPF records" { + t.Fatalf("spf = %+v", f) + } + if f := finding(t, r, "dmarc"); f.Status != Missing { + t.Fatalf("dmarc = %+v", f) + } + if f := finding(t, r, "dkim.none"); f.Status != Info || !strings.Contains(f.Meaning, "--selector") { + t.Fatalf("dkim.none = %+v", f) + } +} + +func TestSPFLookupsLoopsAndRedirects(t *testing.T) { + records := []string{ + txt("loop.test.", "v=spf1 include:loop2.test -all"), + txt("loop2.test.", "v=spf1 include:loop.test -all"), + txt("redir.test.", "v=spf1 redirect=_spf.redir.test"), + txt("_spf.redir.test.", "v=spf1 ip4:192.0.2.1 -all"), + txt("noall.test.", "v=spf1 ip4:192.0.2.1"), + txt("ptr.test.", "v=spf1 ptr ?all"), + txt("deep.test.", "v=spf1 include:la.test -all"), + } + // la.test includes lb.test and so on to lk.test: 11 lookups in all. + letters := "abcdefghijk" + for i := range len(letters) { + value := "v=spf1 -all" + if i+1 < len(letters) { + value = "v=spf1 include:l" + letters[i+1:i+2] + ".test -all" + } + records = append(records, txt("l"+letters[i:i+1]+".test.", value)) + } + server := fakeDNS(t, records...) + + spfOf := func(domain string) *SPFDetails { + t.Helper() + r, err := Email(context.Background(), domain, EmailOptions{Server: server}) + if err != nil { + t.Fatal(err) + } + return &r.Details.(*EmailDetails).SPF + } + if spf := spfOf("loop.test"); len(spf.Problems) != 1 || !strings.Contains(spf.Problems[0], "loops back") { + t.Errorf("loop = %+v", spf) + } + if spf := spfOf("redir.test"); spf.All != "-all" || spf.Lookups != 1 { + t.Errorf("redirect = %+v", spf) + } + if spf := spfOf("noall.test"); spf.All != "" || spf.Lookups != 0 { + t.Errorf("no all = %+v", spf) + } + if spf := spfOf("deep.test"); spf.Lookups <= SPFLookupLimit { + t.Errorf("deep = %+v", spf) + } + + r, err := Email(context.Background(), "ptr.test", EmailOptions{Server: server}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "spf.ptr"); f.Status != Warn { + t.Errorf("ptr = %+v", f) + } + if f := finding(t, r, "spf.all"); f.Status != Warn || f.Value != "?all" { + t.Errorf("?all = %+v", f) + } + r, err = Email(context.Background(), "deep.test", EmailOptions{Server: server}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "spf.lookups"); f.Status != Fail { + t.Errorf("deep lookups = %+v", f) + } + r, err = Email(context.Background(), "redir.test", EmailOptions{Server: server}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "spf.all"); f.Value != "-all (from redirect=_spf.redir.test)" || f.Status != Pass { + t.Errorf("redirect all = %+v", f) + } +} + +func TestEmailMissingDomain(t *testing.T) { + server := fakeDNS(t, txt("other.test.", "x")) + r, err := Email(context.Background(), "missing.test", EmailOptions{Server: server}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "mx"); f.Status != Fail || !strings.Contains(f.Value, "does not exist") || r.ExitCode != 2 || hasFinding(r, "spf") { + t.Fatalf("report = %+v", r.Sections) + } + if _, err := Email(context.Background(), "", EmailOptions{Server: server}); err == nil { + t.Fatal("empty domain gave no error") + } +} + +func TestTagList(t *testing.T) { + tags := tagList("v=DMARC1; P=reject ;rua=mailto:a@example.com,mailto:b@example.com; p=none") + if tags["v"] != "DMARC1" || tags["p"] != "reject" || tags["rua"] != "mailto:a@example.com,mailto:b@example.com" { + t.Fatalf("tags = %v", tags) + } +} + +func TestSPFEvaluationRules(t *testing.T) { + var records []string + for i := 1; i <= 11; i++ { + records = append(records, fmt.Sprintf("manymx.test. 300 IN MX %d mx%d.manymx.test.", i, i)) + } + records = append(records, + txt("order.test.", "v=spf1 +all -all"), + txt("after.test.", "v=spf1 -all include:missing.test"), + txt("redirall.test.", "v=spf1 redirect=_spf.redirall.test -all"), + txt("manymx.test.", "v=spf1 mx -all"), + txt("badip.test.", "v=spf1 ip4:999.999.999.999 ip6:2001:db8::/129 ip4:192.0.2.0/24 ip6:2001:db8::/32 a/33 mx//64 a:x.test/24//64 ip4:2001:db8::1 -all"), + txt("unknown.test.", "v=spf1 foo:bar -all"), + txt("macro.test.", "v=spf1 include:%{d}._spf.test exists:%{i}._spf.test -all"), + txt("macroredir.test.", "v=spf1 redirect=%{d}._spf.test"), + txt("nested.test.", "v=spf1 include:inner.test -all"), + txt("inner.test.", "v=spf1 ip4:300.1.1.1 -all"), + ) + server := fakeDNS(t, records...) + spfOf := func(domain string) (*SPFDetails, *Report) { + t.Helper() + r, err := Email(context.Background(), domain, EmailOptions{Server: server}) + if err != nil { + t.Fatal(err) + } + return &r.Details.(*EmailDetails).SPF, r + } + + if spf, r := spfOf("order.test"); spf.All != "+all" || finding(t, r, "spf.all").Status != Fail { + t.Errorf("first all must win: %+v", spf) + } + if spf, _ := spfOf("after.test"); spf.All != "-all" || spf.Lookups != 0 || len(spf.Problems) != 0 { + t.Errorf("terms after all must not be evaluated: %+v", spf) + } + if spf, _ := spfOf("redirall.test"); spf.Lookups != 0 || spf.All != "-all" { + t.Errorf("redirect with all must be ignored: %+v", spf) + } + if spf, r := spfOf("manymx.test"); len(spf.Problems) != 1 || !strings.Contains(spf.Problems[0], "11 MX hosts") || r.ExitCode != 2 { + t.Errorf("mx with 11 hosts = %+v", spf) + } + spf, _ := spfOf("badip.test") + if len(spf.Problems) != 4 { + t.Errorf("bad addresses: %q", spf.Problems) + } + for _, bad := range []string{"ip4:999.999.999.999", "ip6:2001:db8::/129", "a/33", "ip4:2001:db8::1"} { + if !slices.ContainsFunc(spf.Problems, func(p string) bool { return strings.HasPrefix(p, bad+" ") }) { + t.Errorf("no problem for %s: %q", bad, spf.Problems) + } + } + if spf, _ := spfOf("unknown.test"); len(spf.Problems) != 1 || !strings.Contains(spf.Problems[0], "not an SPF mechanism") { + t.Errorf("unknown mechanism: %+v", spf) + } + if spf, r := spfOf("macro.test"); len(spf.Macros) != 1 || spf.Macros[0] != "include:%{d}._spf.test" || spf.Lookups != 2 || finding(t, r, "spf.lookups").Status != Warn { + t.Errorf("macros: %+v", spf) + } + if spf, _ := spfOf("macroredir.test"); len(spf.Macros) != 1 || !strings.HasPrefix(spf.Macros[0], "redirect=") || spf.Lookups != 1 { + t.Errorf("redirect macro: %+v", spf) + } + if spf, _ := spfOf("nested.test"); len(spf.Problems) != 1 || !strings.Contains(spf.Problems[0], "in the record of inner.test") { + t.Errorf("nested syntax error: %+v", spf) + } +} + +func TestDMARCRecordRules(t *testing.T) { + server := fakeDNS(t, + txt("case.test.", "v=spf1 -all"), + txt("_dmarc.case.test.", "v=dmarc1; p=reject; rua=mailto:r@case.test"), + txt("space.test.", "v=spf1 -all"), + txt("_dmarc.space.test.", "v = DMARC1; p=reject; rua=mailto:r@space.test"), + txt("rej.test.", "v=spf1 -all"), + txt("_dmarc.rej.test.", "v=DMARC1; p=reject; t=y; rua=mailto:r@rej.test"), + txt("quar.test.", "v=spf1 -all"), + txt("_dmarc.quar.test.", "v=DMARC1; p=quarantine; t=y; rua=mailto:r@quar.test"), + ) + email := func(domain string) *Report { + t.Helper() + r, err := Email(context.Background(), domain, EmailOptions{Server: server}) + if err != nil { + t.Fatal(err) + } + return r + } + if f := finding(t, email("case.test"), "dmarc"); f.Status != Fail || !strings.Contains(f.Meaning, "exactly v=DMARC1") { + t.Errorf("lower-case version = %+v", f) + } + if r := email("space.test"); finding(t, r, "dmarc").Status != Pass || finding(t, r, "dmarc.p").Value != "reject" { + t.Errorf("spaces around = should be fine: %+v", r.Sections) + } + if f := finding(t, email("rej.test"), "dmarc.t"); f.Status != Warn || !strings.Contains(f.Meaning, "quarantine instead of reject") { + t.Errorf("t=y with reject = %+v", f) + } + if f := finding(t, email("quar.test"), "dmarc.t"); !strings.Contains(f.Meaning, "none instead of quarantine") { + t.Errorf("t=y with quarantine = %+v", f) + } +} + +func TestDKIMAndTransportRecordRules(t *testing.T) { + key := rsaKey(t, 2048) + server := fakeDNS(t, + txt("dk.test.", "v=spf1 -all"), + txt("default._domainkey.dk.test.", "v=DKIM1; p="), + txt("v2._domainkey.dk.test.", "v=DKIM2; p="+key), + txt("nop._domainkey.dk.test.", "v=DKIM1; k=rsa"), + txt("bang._domainkey.dk.test.", "v=DKIM1; p=!"), + txt("dupe._domainkey.dk.test.", "v=DKIM1; p="+key+"; p="+key), + txt("kx._domainkey.dk.test.", "v=DKIM1; k=dsa; p=AAAA"), + txt("late._domainkey.dk.test.", "k=rsa; v=DKIM1; p="+key), + txt("junk._domainkey.dk.test.", "v=DKIM1; p=AAAA"), + txt("_mta-sts.dk.test.", "v=STSv1"), + txt("_smtp._tls.dk.test.", "v=TLSRPTv1"), + txt("two.test.", "v=spf1 -all"), + txt("_mta-sts.two.test.", "v=STSv1; id=a1"), + txt("_mta-sts.two.test.", "v=STSv1; id=b2"), + txt("_smtp._tls.two.test.", "v=TLSRPTv1; rua=mailto:a@two.test"), + txt("_smtp._tls.two.test.", "v=TLSRPTv1; rua=mailto:b@two.test"), + ) + r, err := Email(context.Background(), "dk.test", EmailOptions{Server: server, Selectors: []string{"v2", "nop", "bang", "dupe", "kx", "late", "junk"}}) + if err != nil { + t.Fatal(err) + } + d := r.Details.(*EmailDetails) + if k := dkimFor(d, "default"); !k.Revoked || k.Problem != "" { + t.Errorf("default = %+v", k) + } + for _, sel := range []string{"v2", "nop", "bang", "dupe", "kx", "late", "junk"} { + if k := dkimFor(d, sel); !k.Found || k.Problem == "" { + t.Errorf("%s = %+v, want a problem", sel, k) + } + } + statuses := map[string]Status{} + for _, s := range r.Sections { + for _, f := range s.Findings { + if f.ID == "dkim" { + statuses[f.Label] = f.Status + } + } + } + if statuses["default"] != Info || statuses["nop"] != Fail || statuses["bang"] != Fail { + t.Errorf("DKIM statuses = %v", statuses) + } + if f := finding(t, r, "mta-sts"); f.Status != Fail || !strings.Contains(f.Meaning, "id=") { + t.Errorf("mta-sts without id = %+v", f) + } + if f := finding(t, r, "tls-rpt"); f.Status != Fail || !strings.Contains(f.Meaning, "rua=") { + t.Errorf("tls-rpt without rua = %+v", f) + } + if r.ExitCode != 2 { + t.Errorf("exit = %d", r.ExitCode) + } + + r, err = Email(context.Background(), "two.test", EmailOptions{Server: server}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "mta-sts"); f.Status != Fail || f.Value != "2 records" { + t.Errorf("two MTA-STS records = %+v", f) + } + if f := finding(t, r, "tls-rpt"); f.Status != Fail || f.Value != "2 records" { + t.Errorf("two TLS-RPT records = %+v", f) + } +} diff --git a/internal/check/headers.go b/internal/check/headers.go new file mode 100644 index 0000000..61eb2ba --- /dev/null +++ b/internal/check/headers.go @@ -0,0 +1,547 @@ +package check + +import ( + "context" + "crypto/tls" + "crypto/x509" + "errors" + "fmt" + "net" + "net/http" + "net/url" + "regexp" + "slices" + "strconv" + "strings" + "time" +) + +// MaxRedirects is how many redirects "check headers" follows. +const MaxRedirects = 10 + +type HeadersOptions struct { + Timeout time.Duration + RootCAs *x509.CertPool // nil: the system's trusted roots + UserAgent string +} + +type Hop struct { + URL string `json:"url"` + Status int `json:"status"` + Location string `json:"location,omitempty"` +} + +type HeadersDetails struct { + URL string `json:"url"` + FinalURL string `json:"finalUrl,omitempty"` + Hops []Hop `json:"hops"` + Headers map[string]string `json:"headers"` + Error string `json:"error,omitempty"` +} + +// reportedHeaders are copied to the JSON details as the server sent them. +var reportedHeaders = []string{ + "Strict-Transport-Security", "Content-Security-Policy", "Content-Security-Policy-Report-Only", + "X-Content-Type-Options", "X-Frame-Options", "Referrer-Policy", "Permissions-Policy", + "Cache-Control", "Age", "ETag", "Server", +} + +// Headers requests rawURL (https:// is added when there is no scheme), +// follows redirects one at a time and checks the final response's headers. +// The error is only for a URL that cannot be used. +func Headers(ctx context.Context, rawURL string, opts HeadersOptions) (*Report, error) { + target, err := parseURL(rawURL) + if err != nil { + return nil, err + } + timeout := timeoutOr(opts.Timeout) + client := newClient(timeout, opts.RootCAs) + defer client.CloseIdleConnections() + + d := &HeadersDetails{URL: target.String(), Hops: []Hop{}, Headers: map[string]string{}} + r := &Report{Check: "headers", Target: target.String(), Unicode: unicodeName(target.Hostname()), Details: d, LearnMore: learnHeaders} + hops := Section{Title: "Requests", Note: "Each request, its HTTP status and where a redirect sent you next."} + seen := map[string]bool{} + current := target + var final *http.Response + for i := 0; ; i++ { + if i > MaxRedirects { + hops.add("redirects", Fail, "Too many", fmt.Sprintf("more than %d redirects", MaxRedirects), "Browsers give up too. Look for a redirect loop between two addresses.") + break + } + if seen[current.String()] { + hops.add("redirects", Fail, "Loop", "redirects back to "+current.String(), "The redirects go round in a circle, so the page never loads.") + break + } + seen[current.String()] = true + resp, err := get(ctx, client, current, opts.UserAgent) + if err != nil { + d.Error = err.Error() + hops.add("request", Fail, fmt.Sprintf("%d.", i+1), current.String(), requestProblem(err, current, timeout)) + break + } + resp.Body.Close() + hop := Hop{URL: current.String(), Status: resp.StatusCode} + location := resp.Header.Get("Location") + if !redirectStatus(resp.StatusCode) || location == "" { + d.Hops = append(d.Hops, hop) + hops.add("hop", Info, fmt.Sprintf("%d.", i+1), fmt.Sprintf("%d %s", resp.StatusCode, current), "") + final = resp + break + } + next, err := current.Parse(location) + if err != nil || (next.Scheme != "http" && next.Scheme != "https") { + d.Hops = append(d.Hops, hop) + hops.add("hop", Fail, fmt.Sprintf("%d.", i+1), fmt.Sprintf("%d %s -> %s", resp.StatusCode, current, location), "The redirect target is not a usable http or https URL.") + break + } + next.Fragment = "" + hop.Location = next.String() + d.Hops = append(d.Hops, hop) + hops.add("hop", Info, fmt.Sprintf("%d.", i+1), fmt.Sprintf("%d %s -> %s", resp.StatusCode, current, next), "") + if current.Scheme == "https" && next.Scheme == "http" { + hops.add("downgrade", Warn, "Downgrade", "redirects from HTTPS to plain HTTP", "Anyone on the network can read and change the plain HTTP request.") + } + current = next + } + r.add(hops) + if final == nil { + return r.finish(), nil + } + d.FinalURL = current.String() + for _, h := range reportedHeaders { + if values := final.Header.Values(h); len(values) > 0 { + d.Headers[h] = strings.Join(values, ", ") + } + } + + page := Section{Title: "Final response"} + if current.Scheme == "https" { + page.add("https", Pass, "HTTPS", "yes", "The page is encrypted in transit.") + } else { + page.add("https", Warn, "HTTPS", "no, plain HTTP", "Anyone on the network can read and change the page. Serve it over HTTPS and redirect HTTP to it.") + } + if final.StatusCode >= 400 { + page.add("status", Warn, "Status", fmt.Sprint(final.StatusCode), "An error page. Your real pages may send different headers; try one that loads.") + } else { + page.add("status", Info, "Status", fmt.Sprint(final.StatusCode), "") + } + r.add(page) + r.add(securityHeaders(final.Header, current.Scheme == "https")) + r.add(cacheHeaders(final.Header)) + return r.finish(), nil +} + +// newClient makes a client that follows no redirects by itself and uses +// timeout for the whole request and for the dial and TLS handshake too. +func newClient(timeout time.Duration, roots *x509.CertPool) *http.Client { + transport := http.DefaultTransport.(*http.Transport).Clone() + transport.TLSClientConfig = &tls.Config{RootCAs: roots} + transport.DialContext = (&net.Dialer{Timeout: timeout, KeepAlive: 30 * time.Second}).DialContext + transport.TLSHandshakeTimeout = timeout + transport.ResponseHeaderTimeout = timeout + return &http.Client{ + Transport: transport, + Timeout: timeout, + CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }, + } +} + +// redirectStatus is true for the statuses browsers follow. A 304 or 300 +// with a Location header is not a redirect. +func redirectStatus(code int) bool { + switch code { + case http.StatusMovedPermanently, http.StatusFound, http.StatusSeeOther, http.StatusTemporaryRedirect, http.StatusPermanentRedirect: + return true + } + return false +} + +func parseURL(raw string) (*url.URL, error) { + value := strings.TrimSpace(raw) + if !strings.Contains(value, "://") { + value = "https://" + value + } + u, err := url.Parse(value) + if err != nil || u.Host == "" { + return nil, fmt.Errorf("%q is not a URL, like https://example.com", raw) + } + if u.Scheme != "http" && u.Scheme != "https" { + return nil, fmt.Errorf("%q: only http:// and https:// URLs can be checked", raw) + } + host, _, err := hostPort(u.Host, "443") + if err != nil { + return nil, err + } + if strings.Contains(host, ":") { + host = "[" + host + "]" + } + if port := u.Port(); port != "" { + host += ":" + port + } + u.Host, u.Fragment, u.User = host, "", nil + if u.Path == "" { + u.Path = "/" + } + return u, nil +} + +func get(ctx context.Context, client *http.Client, u *url.URL, userAgent string) (*http.Response, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) + if err != nil { + return nil, err + } + if userAgent != "" { + req.Header.Set("User-Agent", userAgent) + } + return client.Do(req) +} + +func requestProblem(err error, u *url.URL, timeout time.Duration) string { + var unknown x509.UnknownAuthorityError + var invalid x509.CertificateInvalidError + var hostname x509.HostnameError + switch { + case errors.As(err, &unknown), errors.As(err, &invalid), errors.As(err, &hostname): + return "The certificate is not valid, so browsers would stop here. Run: devops-daily check tls " + u.Host + case isTimeout(err): + return fmt.Sprintf("No answer within %s.", timeout) + } + var urlErr *url.Error + if errors.As(err, &urlErr) { + err = urlErr.Err + } + return "The request failed: " + err.Error() +} + +func securityHeaders(h http.Header, https bool) Section { + s := Section{Title: "Security headers", Note: "Headers that turn on browser protections. Each one closes a common attack."} + csp := strings.Join(h.Values("Content-Security-Policy"), ", ") + policies := parseCSP(csp) + + hsts := h.Get("Strict-Transport-Security") + switch { + case !https: + s.add("hsts", Info, "Strict-Transport-Security", "not checked: the page is plain HTTP", "HSTS only works over HTTPS. Move the site to HTTPS first.") + case hsts == "": + s.add("hsts", Missing, "Strict-Transport-Security", "missing", "Tells browsers to always use HTTPS here, so a visit cannot be downgraded to plain HTTP.") + default: + age, ok, dup := maxAge(hsts) + switch { + case dup != "": + s.add("hsts", Warn, "Strict-Transport-Security", hsts, dup+" appears more than once, which makes the header invalid, so browsers ignore it.") + case !ok: + s.add("hsts", Warn, "Strict-Transport-Security", hsts, "There is no valid max-age, so browsers ignore it. Use max-age=31536000 (one year).") + case age == 0: + s.add("hsts", Warn, "Strict-Transport-Security", hsts, "max-age=0 switches HSTS off.") + case age < 15552000: + s.add("hsts", Warn, "Strict-Transport-Security", hsts, fmt.Sprintf("Browsers remember HTTPS-only for %s; 180 days or more is the usual advice (one year is common).", span(age))) + default: + s.add("hsts", Pass, "Strict-Transport-Security", hsts, fmt.Sprintf("Browsers will use only HTTPS here for %s after a visit.", plural(int(age/86400), "day", "days"))) + } + } + + switch { + case csp != "": + status, reason, scripts := judgeCSP(policies) + value := csp + if len(csp) > 200 && scripts != "" { + others := -(strings.Count(scripts, "; ") + 1) + for _, p := range policies { + others += len(p) + } + value = scripts + " (and " + plural(others, "more directive", "more directives") + "; --json shows the whole policy)" + } + s.add("csp", status, "Content-Security-Policy", value, reason) + case h.Get("Content-Security-Policy-Report-Only") != "": + s.add("csp", Warn, "Content-Security-Policy", "report-only", "Content-Security-Policy-Report-Only reports problems but blocks nothing. Switch to Content-Security-Policy when the reports are clean.") + default: + s.add("csp", Missing, "Content-Security-Policy", "missing", "Limits where scripts and other content may load from, which stops most cross-site scripting.") + } + + nosniff := strings.TrimSpace(h.Get("X-Content-Type-Options")) + switch { + case strings.EqualFold(nosniff, "nosniff"): + s.add("nosniff", Pass, "X-Content-Type-Options", nosniff, "Browsers will not guess a file's type, so an uploaded text file cannot run as a script.") + case nosniff == "": + s.add("nosniff", Missing, "X-Content-Type-Options", "missing", "Set it to nosniff, so browsers do not guess file types and run, say, an uploaded text file as a script.") + default: + s.add("nosniff", Warn, "X-Content-Type-Options", nosniff, "The only valid value is nosniff.") + } + + frame := strings.TrimSpace(h.Get("X-Frame-Options")) + ancestors, hasAncestors, strict := frameAncestors(policies) + switch { + case hasAncestors && !strict: + s.add("framing", Warn, "Frame protection", "frame-ancestors "+ancestors, "A wildcard source lets any site show this page in a frame, which allows clickjacking.") + case hasAncestors: + s.add("framing", Pass, "Frame protection", "frame-ancestors "+ancestors, "The CSP decides which sites may show this page in a frame (stops clickjacking).") + case strings.EqualFold(frame, "DENY") || strings.EqualFold(frame, "SAMEORIGIN"): + s.add("framing", Pass, "Frame protection", "X-Frame-Options: "+frame, "Other sites cannot show this page in a frame (stops clickjacking).") + case frame != "": + s.add("framing", Warn, "Frame protection", "X-Frame-Options: "+frame, "Browsers only know DENY and SAMEORIGIN (ALLOW-FROM is ignored). Use the CSP frame-ancestors directive instead.") + default: + s.add("framing", Missing, "Frame protection", "missing (no X-Frame-Options or CSP frame-ancestors)", "Without it, other sites can show this page in a hidden frame and trick clicks (clickjacking).") + } + + referrer := referrerPolicy(strings.Join(h.Values("Referrer-Policy"), ",")) + switch referrer { + case "": + s.add("referrer", Info, "Referrer-Policy", "not set", "Browsers then use strict-origin-when-cross-origin, a safe default. Set it to make the choice explicit.") + case "unsafe-url", "no-referrer-when-downgrade": + s.add("referrer", Warn, "Referrer-Policy", referrer, "Sends the full URL, with paths and query strings, to other sites. Use strict-origin-when-cross-origin.") + case "no-referrer", "same-origin", "strict-origin", "strict-origin-when-cross-origin", "origin", "origin-when-cross-origin": + s.add("referrer", Pass, "Referrer-Policy", referrer, "Controls how much of the page URL other sites see when a visitor follows a link.") + default: + s.add("referrer", Warn, "Referrer-Policy", referrer, "Not a value browsers know, so they ignore it.") + } + + permissions := h.Get("Permissions-Policy") + switch { + case permissions != "": + s.add("permissions", Pass, "Permissions-Policy", permissions, "Turns off browser features (camera, microphone, location and so on) the page does not use.") + case h.Get("Feature-Policy") != "": + s.add("permissions", Info, "Permissions-Policy", "not set (only the old Feature-Policy)", "Feature-Policy is the old name. Browsers now read Permissions-Policy.") + default: + s.add("permissions", Info, "Permissions-Policy", "not set", "Optional. Lets you turn off browser features (camera, microphone, location) the page does not use.") + } + return s +} + +var versionPattern = regexp.MustCompile(`/v?\d+(\.\d+)*`) + +func cacheHeaders(h http.Header) Section { + s := Section{Title: "Caching and server"} + if cc := strings.Join(h.Values("Cache-Control"), ", "); cc != "" { + s.add("cache-control", Info, "Cache-Control", cc, cacheMeaning(cc)) + } else { + s.add("cache-control", Info, "Cache-Control", "not set", "Browsers and CDNs guess how long to keep the page from Last-Modified.") + } + if age := h.Get("Age"); age != "" { + meaning := "How long this copy has been in a cache: a CDN or proxy served it." + if seconds, err := strconv.ParseInt(age, 10, 64); err == nil { + meaning = fmt.Sprintf("This copy has been in a cache (a CDN or proxy) for %s.", span(seconds)) + } + s.add("age", Info, "Age", age, meaning) + } else { + s.add("age", Info, "Age", "not set", "No cache in front of the server says it served this copy, so it probably came from the server itself.") + } + if etag := h.Get("ETag"); etag != "" { + s.add("etag", Info, "ETag", etag, "A version tag. Browsers send it back to ask \"has this changed?\" and get a short 304 reply if not.") + } else { + s.add("etag", Info, "ETag", "not set", "Without it (or Last-Modified), browsers download the page again instead of revalidating it.") + } + if server := h.Get("Server"); server != "" { + meaning := "The software that answered, as the server reports it." + if versionPattern.MatchString(server) { + meaning = "It shows a version number, which helps attackers match known bugs. Most servers can hide it (nginx: server_tokens off)." + } + s.add("server", Info, "Server", server, meaning) + } else { + s.add("server", Info, "Server", "not set", "") + } + return s +} + +func cacheMeaning(cc string) string { + var parts []string + for _, item := range strings.Split(strings.ToLower(cc), ",") { + key, value, _ := strings.Cut(strings.TrimSpace(item), "=") + seconds, err := strconv.ParseInt(strings.Trim(value, `"`), 10, 64) + switch { + case key == "no-store": + parts = append(parts, "nothing may store it") + case key == "no-cache": + parts = append(parts, "caches must check with the server before each reuse") + case key == "private": + parts = append(parts, "only the browser may keep it, not shared caches or CDNs") + case key == "public": + parts = append(parts, "shared caches and CDNs may keep it") + case key == "max-age" && err == nil && seconds == 0: + parts = append(parts, "stale at once") + case key == "max-age" && err == nil: + parts = append(parts, "fresh for "+span(seconds)) + case key == "s-maxage" && err == nil: + parts = append(parts, "CDNs keep it for "+span(seconds)) + case key == "must-revalidate": + parts = append(parts, "once stale, it must be checked before reuse") + case key == "immutable": + parts = append(parts, "it never changes while fresh") + } + } + if len(parts) == 0 { + return "Tells browsers and CDNs how long they may keep a copy." + } + text := strings.Join(parts, "; ") + return strings.ToUpper(text[:1]) + text[1:] + "." +} + +// maxAge reads max-age from an HSTS header. dup names a directive that +// appears twice, which makes the whole header invalid. +func maxAge(hsts string) (age int64, ok bool, dup string) { + seen := map[string]bool{} + for _, part := range strings.Split(hsts, ";") { + key, value, _ := strings.Cut(strings.TrimSpace(part), "=") + key = strings.ToLower(strings.TrimSpace(key)) + if key == "" { + continue + } + if seen[key] { + return 0, false, key + } + seen[key] = true + if key == "max-age" { + n, err := strconv.ParseInt(strings.Trim(strings.TrimSpace(value), `"`), 10, 64) + age, ok = n, err == nil && n >= 0 + } + } + return age, ok, "" +} + +// parseCSP splits one or more policies (comma separated) into directives. +func parseCSP(value string) []map[string][]string { + var policies []map[string][]string + for _, policy := range strings.Split(value, ",") { + directives := map[string][]string{} + for _, part := range strings.Split(policy, ";") { + fields := strings.Fields(part) + if len(fields) == 0 { + continue + } + name := strings.ToLower(fields[0]) + if _, dup := directives[name]; !dup { + directives[name] = fields[1:] + } + } + if len(directives) > 0 { + policies = append(policies, directives) + } + } + return policies +} + +// frameAncestors combines frame-ancestors from every policy. The browser +// enforces all of them, so one without a wildcard source blocks framing. +func frameAncestors(policies []map[string][]string) (value string, found, strict bool) { + for _, p := range policies { + sources, ok := p["frame-ancestors"] + if !ok { + continue + } + found = true + joined := strings.Join(sources, " ") + if !slices.ContainsFunc(sources, wildcardSource) { + return joined, true, true + } + if value == "" { + value = joined + } + } + return value, found, false +} + +// wildcardSource matches any site: *, a bare scheme like https:, or a +// scheme with a * host. +func wildcardSource(src string) bool { + l := strings.ToLower(src) + return l == "*" || l == "http:" || l == "https:" || strings.HasSuffix(l, "://*") +} + +// weakSources lists the sources that let an injected script run. +// 'unsafe-inline' does not count when a nonce, hash or 'strict-dynamic' is +// present, since browsers then ignore it. +func weakSources(sources []string) []string { + hasNonce := false + for _, src := range sources { + l := strings.ToLower(src) + if strings.HasPrefix(l, "'nonce-") || strings.HasPrefix(l, "'sha256-") || strings.HasPrefix(l, "'sha384-") || strings.HasPrefix(l, "'sha512-") || l == "'strict-dynamic'" { + hasNonce = true + } + } + var weak []string + for _, src := range sources { + l := strings.ToLower(src) + if l == "'unsafe-inline'" && !hasNonce || l == "'unsafe-eval'" || l == "data:" || wildcardSource(l) { + weak = append(weak, src) + } + } + return weak +} + +var scriptDirectives = []string{"script-src", "script-src-elem", "script-src-attr"} + +// scriptSources returns the directive that controls kind in p, following +// the fallback script-src-elem/-attr -> script-src -> default-src. +func scriptSources(p map[string][]string, kind string) (string, []string, bool) { + for _, name := range []string{kind, "script-src", "default-src"} { + if sources, ok := p[name]; ok { + return name, sources, true + } + } + return "", nil, false +} + +// judgeCSP checks the sources allowed for scripts: script-src and, when +// set, script-src-elem (script tags) and script-src-attr (inline event +// handlers). Each kind is safe when at least one policy restricts it, +// since the browser enforces every policy. It also returns the directives +// it judged, for display. +func judgeCSP(policies []map[string][]string) (Status, string, string) { + safe := map[string]bool{} + var weak []string + shown := "" + restricted := false + for _, p := range policies { + var parts []string + for _, kind := range scriptDirectives { + name, sources, ok := scriptSources(p, kind) + if !ok { + continue + } + restricted = true + if _, own := p[kind]; own || kind == "script-src" { + part := strings.TrimSpace(name + " " + strings.Join(sources, " ")) + if !slices.Contains(parts, part) { + parts = append(parts, part) + } + } + if w := weakSources(sources); len(w) == 0 { + safe[kind] = true + } else if desc := strings.Join(w, " ") + " (" + name + ")"; !slices.Contains(weak, desc) { + weak = append(weak, desc) + } + } + if shown == "" { + shown = strings.Join(parts, "; ") + } + } + if !restricted { + return Warn, "No script-src or default-src, so scripts may load from anywhere.", shown + } + if safe["script-src"] && safe["script-src-elem"] && safe["script-src-attr"] { + return Pass, "Scripts may load only from the sources listed, which stops most cross-site scripting.", shown + } + return Warn, "Scripts are allowed from " + strings.Join(weak, ", ") + ", so an injected script can still run. Use nonces or hashes instead.", shown +} + +// referrerPolicy returns the policy browsers use: the last value they know. +func referrerPolicy(value string) string { + known := []string{"no-referrer", "no-referrer-when-downgrade", "origin", "origin-when-cross-origin", "same-origin", "strict-origin", "strict-origin-when-cross-origin", "unsafe-url"} + last, other := "", "" + for _, v := range strings.Split(value, ",") { + v = strings.ToLower(strings.TrimSpace(v)) + if v == "" { + continue + } + other = v + for _, k := range known { + if v == k { + last = v + } + } + } + if last == "" { + return other + } + return last +} diff --git a/internal/check/headers_test.go b/internal/check/headers_test.go new file mode 100644 index 0000000..898daae --- /dev/null +++ b/internal/check/headers_test.go @@ -0,0 +1,313 @@ +package check + +import ( + "context" + "crypto/x509" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" +) + +func goodHeaders(w http.ResponseWriter) { + h := w.Header() + h.Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains") + h.Set("Content-Security-Policy", "default-src 'self'; script-src 'self' 'nonce-abc' 'unsafe-inline'; frame-ancestors 'none'") + h.Set("X-Content-Type-Options", "nosniff") + h.Set("Referrer-Policy", "strict-origin-when-cross-origin") + h.Set("Permissions-Policy", "camera=(), geolocation=()") + h.Set("Cache-Control", "public, max-age=300") + h.Set("Age", "42") + h.Set("ETag", `"v1"`) + h.Set("Server", "nginx/1.27.0") +} + +func TestHeadersFollowsRedirectsAndPasses(t *testing.T) { + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/": + http.Redirect(w, r, "/next", http.StatusMovedPermanently) + case "/next": + http.Redirect(w, r, "/final#frag", http.StatusFound) + default: + if r.Header.Get("User-Agent") != "test-agent" { + t.Errorf("User-Agent = %q", r.Header.Get("User-Agent")) + } + goodHeaders(w) + } + })) + defer srv.Close() + pool := x509.NewCertPool() + pool.AddCert(srv.Certificate()) + r, err := Headers(context.Background(), srv.URL, HeadersOptions{RootCAs: pool, UserAgent: "test-agent"}) + if err != nil { + t.Fatal(err) + } + if r.ExitCode != 0 { + t.Fatalf("exit %d: %+v", r.ExitCode, r.Sections) + } + d := r.Details.(*HeadersDetails) + if len(d.Hops) != 3 || d.Hops[0].Status != 301 || d.Hops[1].Location != srv.URL+"/final" || d.Hops[2].Status != 200 || d.FinalURL != srv.URL+"/final" { + t.Fatalf("hops = %+v, final %s", d.Hops, d.FinalURL) + } + for _, id := range []string{"https", "hsts", "csp", "nosniff", "framing", "referrer", "permissions"} { + if f := finding(t, r, id); f.Status != Pass { + t.Errorf("%s = %+v", id, f) + } + } + if f := finding(t, r, "framing"); f.Value != "frame-ancestors 'none'" { + t.Errorf("framing = %+v", f) + } + if f := finding(t, r, "cache-control"); f.Meaning != "Shared caches and CDNs may keep it; fresh for 5m." { + t.Errorf("cache-control = %+v", f) + } + if f := finding(t, r, "server"); !strings.Contains(f.Meaning, "version number") { + t.Errorf("server = %+v", f) + } + if d.Headers["ETag"] != `"v1"` || r.LearnMore != learnHeaders { + t.Errorf("headers = %v", d.Headers) + } +} + +func TestHeadersMissingAndWeak(t *testing.T) { + plain := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) + defer plain.Close() + r, err := Headers(context.Background(), plain.URL, HeadersOptions{}) + if err != nil { + t.Fatal(err) + } + if r.ExitCode != 1 { + t.Fatalf("exit = %d", r.ExitCode) + } + want := map[string]Status{"https": Warn, "hsts": Info, "csp": Missing, "nosniff": Missing, "framing": Missing, "referrer": Info, "permissions": Info} + for id, status := range want { + if f := finding(t, r, id); f.Status != status { + t.Errorf("%s = %+v, want %s", id, f, status) + } + } + + weak := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h := w.Header() + h.Set("Strict-Transport-Security", "max-age=300") + h.Set("Content-Security-Policy", "default-src * 'unsafe-inline'") + h.Set("X-Content-Type-Options", "yes") + h.Set("X-Frame-Options", "ALLOW-FROM https://example.com") + h.Set("Referrer-Policy", "no-referrer, unsafe-url") + h.Set("Feature-Policy", "camera 'none'") + w.WriteHeader(http.StatusNotFound) + })) + defer weak.Close() + pool := x509.NewCertPool() + pool.AddCert(weak.Certificate()) + r, err = Headers(context.Background(), strings.TrimPrefix(weak.URL, "https://"), HeadersOptions{RootCAs: pool}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "permissions"); f.Status != Info || !strings.Contains(f.Value, "Feature-Policy") { + t.Errorf("permissions = %+v", f) + } + for _, id := range []string{"status", "hsts", "csp", "nosniff", "framing", "referrer"} { + if f := finding(t, r, id); f.Status != Warn { + t.Errorf("%s = %+v, want warn", id, f) + } + } + if f := finding(t, r, "csp"); !strings.Contains(f.Meaning, "* 'unsafe-inline'") { + t.Errorf("csp = %+v", f) + } + if f := finding(t, r, "referrer"); f.Value != "unsafe-url" { + t.Errorf("referrer = %+v", f) + } +} + +func TestHeadersRedirectProblems(t *testing.T) { + var plainURL string + plain := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.URL.Path == "/a": + http.Redirect(w, r, "/b", http.StatusFound) + case r.URL.Path == "/b": + http.Redirect(w, r, "/a", http.StatusFound) + case strings.HasPrefix(r.URL.Path, "/n/"): + var n int + fmt.Sscanf(r.URL.Path, "/n/%d", &n) + http.Redirect(w, r, fmt.Sprintf("/n/%d", n+1), http.StatusFound) + } + })) + defer plain.Close() + plainURL = plain.URL + + r, err := Headers(context.Background(), plainURL+"/a", HeadersOptions{}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "redirects"); f.Status != Fail || f.Label != "Loop" || r.ExitCode != 2 { + t.Fatalf("loop = %+v", f) + } + r, err = Headers(context.Background(), plainURL+"/n/0", HeadersOptions{}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "redirects"); f.Label != "Too many" || len(r.Details.(*HeadersDetails).Hops) != MaxRedirects+1 { + t.Fatalf("too many = %+v", f) + } + + tlsSrv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, plainURL+"/", http.StatusMovedPermanently) + })) + defer tlsSrv.Close() + pool := x509.NewCertPool() + pool.AddCert(tlsSrv.Certificate()) + r, err = Headers(context.Background(), tlsSrv.URL, HeadersOptions{RootCAs: pool}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "downgrade"); f.Status != Warn { + t.Fatalf("downgrade = %+v", f) + } + + r, err = Headers(context.Background(), tlsSrv.URL, HeadersOptions{RootCAs: x509.NewCertPool(), Timeout: 2 * time.Second}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "request"); f.Status != Fail || !strings.Contains(f.Meaning, "devops-daily check tls 127.0.0.1:") { + t.Fatalf("untrusted = %+v", f) + } + + for _, bad := range []string{"ftp://example.com", "https://", "exa mple.com"} { + if _, err := Headers(context.Background(), bad, HeadersOptions{}); err == nil { + t.Errorf("%q gave no error", bad) + } + } +} + +func TestReferrerPolicyAndCSP(t *testing.T) { + for in, want := range map[string]string{"": "", "no-referrer": "no-referrer", "unsafe-url, strict-origin": "strict-origin", "bogus": "bogus", "same-origin, bogus": "same-origin"} { + if got := referrerPolicy(in); got != want { + t.Errorf("referrerPolicy(%q) = %q, want %q", in, got, want) + } + } + cases := map[string]Status{ + "default-src 'self'": Pass, + "script-src 'self' 'unsafe-eval'": Warn, + "script-src 'unsafe-inline'": Warn, + "script-src 'unsafe-inline' 'sha256-abc='": Pass, + "img-src 'self'": Warn, + "script-src https:, default-src 'self'": Pass, + "default-src 'none'; script-src 'self' data:": Warn, + "script-src 'none'; script-src-elem * 'unsafe-inline'": Warn, + "script-src 'self'; script-src-attr 'unsafe-inline'": Warn, + "script-src 'self'; script-src-elem 'self' 'nonce-a' 'unsafe-inline'": Pass, + "script-src 'self' https://*": Warn, + "script-src-elem *, script-src 'self'": Pass, + } + for policy, want := range cases { + if got, _, _ := judgeCSP(parseCSP(policy)); got != want { + t.Errorf("judgeCSP(%q) = %s, want %s", policy, got, want) + } + } + if age, ok, dup := maxAge(`max-age="600"; preload`); !ok || age != 600 || dup != "" { + t.Errorf("maxAge = %d, %v, %q", age, ok, dup) + } + if _, _, dup := maxAge("max-age=31536000; max-age=0"); dup != "max-age" { + t.Errorf("duplicate max-age not found: %q", dup) + } + if _, _, dup := maxAge("max-age=600; includeSubDomains; IncludeSubDomains"); dup != "includesubdomains" { + t.Errorf("duplicate includeSubDomains not found: %q", dup) + } +} + +func TestSecurityHeaderRules(t *testing.T) { + h := http.Header{} + h.Set("Strict-Transport-Security", "max-age=31536000; max-age=0") + h.Add("Content-Security-Policy", "default-src 'self'; frame-ancestors *") + h.Add("Content-Security-Policy", "frame-ancestors 'none'") + h.Add("Referrer-Policy", "no-referrer") + h.Add("Referrer-Policy", "unsafe-url") + r := &Report{} + r.add(securityHeaders(h, true)) + if f := finding(t, r, "hsts"); f.Status != Warn || !strings.Contains(f.Meaning, "max-age appears more than once") { + t.Errorf("hsts = %+v", f) + } + if f := finding(t, r, "framing"); f.Status != Pass || f.Value != "frame-ancestors 'none'" { + t.Errorf("framing over two policies = %+v", f) + } + if f := finding(t, r, "referrer"); f.Status != Warn || f.Value != "unsafe-url" { + t.Errorf("referrer = %+v", f) + } + + h = http.Header{} + h.Set("Content-Security-Policy", "default-src 'self'; frame-ancestors * 'self'") + r = &Report{} + r.add(securityHeaders(h, true)) + if f := finding(t, r, "framing"); f.Status != Warn { + t.Errorf("frame-ancestors with * = %+v", f) + } + for _, id := range []string{"referrer", "permissions"} { + if f := finding(t, r, id); f.Status != Info || f.Value != "not set" { + t.Errorf("%s = %+v, want info", id, f) + } + } + for _, id := range []string{"hsts", "nosniff"} { + if f := finding(t, r, id); f.Status != Missing { + t.Errorf("%s = %+v, want missing", id, f) + } + } +} + +func TestHeadersFollowsOnlyRedirectStatuses(t *testing.T) { + var other int + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/304": + w.Header().Set("Location", "/other") + w.WriteHeader(http.StatusNotModified) + case "/300": + w.Header().Set("Location", "/other") + w.WriteHeader(http.StatusMultipleChoices) + case "/303": + http.Redirect(w, r, "/other", http.StatusSeeOther) + default: + other++ + } + })) + defer srv.Close() + for path, hops := range map[string]int{"/304": 1, "/300": 1, "/303": 2} { + r, err := Headers(context.Background(), srv.URL+path, HeadersOptions{}) + if err != nil { + t.Fatal(err) + } + if got := len(r.Details.(*HeadersDetails).Hops); got != hops { + t.Errorf("%s: %d hops, want %d", path, got, hops) + } + } + if other != 1 { + t.Errorf("/other was requested %d times, want 1", other) + } +} + +func TestClientUsesTimeoutForDialAndHandshake(t *testing.T) { + client := newClient(30*time.Second, nil) + transport := client.Transport.(*http.Transport) + if client.Timeout != 30*time.Second || transport.TLSHandshakeTimeout != 30*time.Second || transport.ResponseHeaderTimeout != 30*time.Second || transport.DialContext == nil { + t.Fatalf("client timeout %s, handshake %s, headers %s", client.Timeout, transport.TLSHandshakeTimeout, transport.ResponseHeaderTimeout) + } +} + +func TestParseURLInternationalHost(t *testing.T) { + u, err := parseURL("Bücher.de:8443/x#top") + if err != nil { + t.Fatal(err) + } + if u.String() != "https://xn--bcher-kva.de:8443/x" { + t.Fatalf("url = %s", u) + } + u, err = parseURL("https://user:secret@[::1]:9000") + if err != nil { + t.Fatal(err) + } + if u.String() != "https://[::1]:9000/" { + t.Fatalf("url = %s", u) + } +} diff --git a/internal/check/resolver.go b/internal/check/resolver.go new file mode 100644 index 0000000..9165fff --- /dev/null +++ b/internal/check/resolver.go @@ -0,0 +1,117 @@ +package check + +import ( + "context" + "fmt" + "net" + "strconv" + "strings" + "time" + + "github.com/miekg/dns" +) + +// fallbackServer is used only when the system resolver cannot be read +// (for example on Windows, which has no /etc/resolv.conf). +const fallbackServer = "1.1.1.1:53" + +type resolver struct { + server string + source string + timeout time.Duration +} + +// newResolver uses server (an IP, optionally with a port) or, when it is +// empty, the first name server in /etc/resolv.conf. +func newResolver(server string, timeout time.Duration) (*resolver, error) { + r := &resolver{timeout: timeoutOr(timeout)} + if server != "" { + address, err := serverAddress(server) + if err != nil { + return nil, err + } + r.server, r.source = address, "chosen with --server" + return r, nil + } + if config, err := dns.ClientConfigFromFile("/etc/resolv.conf"); err == nil && len(config.Servers) > 0 { + r.server, r.source = net.JoinHostPort(config.Servers[0], config.Port), "your system resolver" + return r, nil + } + r.server, r.source = fallbackServer, "Cloudflare; your system resolver could not be read, so pick one with --server" + return r, nil +} + +func serverAddress(value string) (string, error) { + if ip := net.ParseIP(strings.Trim(value, "[]")); ip != nil { + return net.JoinHostPort(ip.String(), "53"), nil + } + host, port, err := net.SplitHostPort(value) + if n, perr := strconv.Atoi(port); err == nil && net.ParseIP(host) != nil && perr == nil && n > 0 && n < 65536 { + return net.JoinHostPort(host, port), nil + } + return "", fmt.Errorf("--server must be an IP address, like 1.1.1.1 or 8.8.8.8:53 (got %q)", value) +} + +// query asks one question, over UDP first and again over TCP when the +// answer does not fit. +func (r *resolver) query(ctx context.Context, name string, qtype uint16) (*dns.Msg, error) { + m := new(dns.Msg) + m.SetQuestion(dns.Fqdn(name), qtype) + m.SetEdns0(1232, false) + ctx, cancel := context.WithTimeout(ctx, r.timeout) + defer cancel() + c := &dns.Client{Net: "udp", UDPSize: 1232, Timeout: r.timeout} + in, _, err := c.ExchangeContext(ctx, m, r.server) + if err == nil && in.Truncated { + c.Net = "tcp" + in, _, err = c.ExchangeContext(ctx, m, r.server) + } + if err != nil { + if isTimeout(err) { + return nil, fmt.Errorf("no answer from %s within %s", r.server, r.timeout) + } + return nil, err + } + return in, nil +} + +// txt returns the TXT records at name, each one's strings joined. A name +// that does not exist gives no records and no error. +func (r *resolver) txt(ctx context.Context, name string) ([]string, error) { + in, err := r.query(ctx, name, dns.TypeTXT) + if err != nil { + return nil, err + } + if in.Rcode != dns.RcodeSuccess && in.Rcode != dns.RcodeNameError { + return nil, rcodeProblem(in.Rcode) + } + var values []string + for _, rr := range in.Answer { + if t, ok := rr.(*dns.TXT); ok { + values = append(values, strings.Join(t.Txt, "")) + } + } + return values, nil +} + +func rcodeProblem(rcode int) error { + switch rcode { + case dns.RcodeServerFailure: + return fmt.Errorf("the DNS server failed to answer (SERVFAIL; often a broken DNSSEC signature or a dead name server)") + case dns.RcodeRefused: + return fmt.Errorf("the DNS server refused the question (REFUSED; it may not answer for you)") + } + return fmt.Errorf("the DNS server answered %s", dns.RcodeToString[rcode]) +} + +// withPrefix keeps the records that start with prefix, ignoring case. +func withPrefix(records []string, prefix string) []string { + var out []string + for _, record := range records { + value := strings.TrimSpace(record) + if len(value) >= len(prefix) && strings.EqualFold(value[:len(prefix)], prefix) && (len(value) == len(prefix) || value[len(prefix)] == ' ' || value[len(prefix)] == ';') { + out = append(out, value) + } + } + return out +} diff --git a/internal/check/target.go b/internal/check/target.go new file mode 100644 index 0000000..a28b6ac --- /dev/null +++ b/internal/check/target.go @@ -0,0 +1,140 @@ +package check + +import ( + "errors" + "fmt" + "net" + "net/url" + "strconv" + "strings" + + "golang.org/x/net/idna" +) + +// hostPort reads "host", "host:port", "[::1]:port" or a pasted URL and +// returns the host in ASCII (lower case, no trailing dot) and the port. +func hostPort(input, defaultPort string) (string, string, error) { + s := strings.TrimSpace(input) + host, port := s, defaultPort + switch { + case strings.Contains(s, "://") || strings.ContainsAny(s, "/?#"): + raw := s + if !strings.Contains(raw, "://") { + raw = "https://" + raw + } + u, err := url.Parse(raw) + if err != nil || u.Hostname() == "" { + return "", "", fmt.Errorf("%q is not a host or host:port", input) + } + host = u.Hostname() + if u.Port() != "" { + port = u.Port() + } + case strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]"): + host = s[1 : len(s)-1] + case strings.HasPrefix(s, "[") || strings.Count(s, ":") == 1: + h, p, err := net.SplitHostPort(s) + if err != nil { + return "", "", fmt.Errorf("%q is not a host or host:port", input) + } + host, port = h, p + } + if n, err := strconv.Atoi(port); err != nil || n < 1 || n > 65535 { + return "", "", fmt.Errorf("%q is not a valid port", port) + } + host, err := asciiName(host) + if err != nil { + return "", "", err + } + return host, port, nil +} + +// domainName reads a domain, an email address or a pasted URL and returns +// the domain in ASCII, lower case and without a trailing dot. +func domainName(input string) (string, error) { + s := strings.TrimSpace(input) + switch { + case strings.Contains(s, "://"): + u, err := url.Parse(s) + if err != nil { + return "", fmt.Errorf("%q is not a valid domain name", input) + } + s = u.Hostname() + case strings.Contains(s, "@"): + s = s[strings.LastIndex(s, "@")+1:] + case strings.ContainsAny(s, "/?#"): + u, err := url.Parse("https://" + s) + if err != nil { + return "", fmt.Errorf("%q is not a valid domain name", input) + } + s = u.Hostname() + case strings.Count(s, ":") == 1: + if h, _, err := net.SplitHostPort(s); err == nil { + s = h + } + } + if s == "" { + return "", errors.New("give a domain name, like example.com") + } + if net.ParseIP(s) != nil { + return "", fmt.Errorf("%s is an IP address; give a domain name, like example.com", s) + } + return asciiName(s) +} + +// asciiName lowers host and drops a trailing dot. International names +// (bücher.de) become their ASCII form (xn--bcher-kva.de), the one DNS, +// TLS and certificates use. IP addresses are returned as they are. +func asciiName(host string) (string, error) { + host = strings.TrimSuffix(host, ".") + if net.ParseIP(host) != nil { + return strings.ToLower(host), nil + } + for _, r := range host { + if r >= 0x80 { + ascii, err := idna.Lookup.ToASCII(host) + if err != nil { + return "", fmt.Errorf("%q is not a valid international domain name", host) + } + host = ascii + break + } + } + host = strings.ToLower(host) + if !validName(host) { + return "", fmt.Errorf("%q is not a valid host name", host) + } + return host, nil +} + +// unicodeName returns the Unicode form of an xn-- name, or "" when the +// name has none, so reports can show both. +func unicodeName(host string) string { + if !strings.Contains(host, "xn--") { + return "" + } + u, err := idna.Lookup.ToUnicode(host) + if err != nil || u == host { + return "" + } + return u +} + +// validName accepts DNS names made of letters, digits, hyphens and +// underscores (for names like _dmarc.example.com). +func validName(s string) bool { + if s == "" || len(s) > 253 { + return false + } + for _, label := range strings.Split(s, ".") { + if label == "" || len(label) > 63 { + return false + } + for _, r := range label { + if !(r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '-' || r == '_') { + return false + } + } + } + return true +} diff --git a/internal/check/tls.go b/internal/check/tls.go new file mode 100644 index 0000000..4bf3741 --- /dev/null +++ b/internal/check/tls.go @@ -0,0 +1,289 @@ +package check + +import ( + "bytes" + "context" + "crypto/tls" + "crypto/x509" + "errors" + "fmt" + "math" + "net" + "os" + "slices" + "syscall" + "time" +) + +// WarnDays is when an expiring certificate becomes a warning. +const WarnDays = 21 + +type TLSOptions struct { + Timeout time.Duration + Now func() time.Time + Roots *x509.CertPool // nil: the system's trusted roots +} + +type CertInfo struct { + Subject string `json:"subject"` + Issuer string `json:"issuer"` + DNSNames []string `json:"dnsNames,omitempty"` + IPAddresses []string `json:"ipAddresses,omitempty"` + NotBefore time.Time `json:"notBefore"` + NotAfter time.Time `json:"notAfter"` + SerialNumber string `json:"serialNumber"` +} + +type TLSDetails struct { + Host string `json:"host"` + Port string `json:"port"` + Address string `json:"address,omitempty"` + Version string `json:"version,omitempty"` + CipherSuite string `json:"cipherSuite,omitempty"` + Certificate *CertInfo `json:"certificate,omitempty"` + DaysLeft *int `json:"daysLeft,omitempty"` + Chain []CertInfo `json:"chain,omitempty"` + Trusted bool `json:"trusted"` + TrustError string `json:"trustError,omitempty"` + TrustedRoot string `json:"trustedRoot,omitempty"` + HostnameMatch bool `json:"hostnameMatch"` + OCSPStapled bool `json:"ocspStapled"` + Error string `json:"error,omitempty"` +} + +// TLS connects to target (host or host:port, port 443 by default), reads the +// certificate the server sends and checks it. The error is only for a target +// that cannot be parsed; connection problems are findings in the report. +func TLS(ctx context.Context, target string, opts TLSOptions) (*Report, error) { + host, port, err := hostPort(target, "443") + if err != nil { + return nil, err + } + now := time.Now + if opts.Now != nil { + now = opts.Now + } + timeout := timeoutOr(opts.Timeout) + address := net.JoinHostPort(host, port) + d := &TLSDetails{Host: host, Port: port} + r := &Report{Check: "tls", Target: address, Unicode: unicodeName(host), Details: d, LearnMore: learnTLS} + conn := Section{Title: "Connection"} + + dialCtx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + raw, err := (&net.Dialer{}).DialContext(dialCtx, "tcp", address) + if err != nil { + d.Error = err.Error() + conn.add("connect", Fail, "Connect", dialProblem(err, port, timeout), "") + r.add(conn) + return r.finish(), nil + } + defer raw.Close() + d.Address = raw.RemoteAddr().String() + conn.add("address", Info, "Address", d.Address, "The IP address and port this check connected to.") + + // The chain is verified below, against the roots, so that a bad + // certificate is reported instead of ending the check. + config := &tls.Config{InsecureSkipVerify: true, MinVersion: tls.VersionTLS10} // #nosec G402 + if net.ParseIP(host) == nil { + config.ServerName = host + } + client := tls.Client(raw, config) + handshakeCtx, cancelHandshake := context.WithTimeout(ctx, timeout) + defer cancelHandshake() + if err := client.HandshakeContext(handshakeCtx); err != nil { + d.Error = err.Error() + conn.add("handshake", Fail, "TLS handshake", handshakeProblem(err, timeout), "") + r.add(conn) + return r.finish(), nil + } + state := client.ConnectionState() + d.Version = tls.VersionName(state.Version) + d.CipherSuite = tls.CipherSuiteName(state.CipherSuite) + switch state.Version { + case tls.VersionTLS13: + conn.add("version", Pass, "TLS version", d.Version, "The newest version of TLS.") + case tls.VersionTLS12: + conn.add("version", Pass, "TLS version", d.Version, "Still safe. TLS 1.3 is newer and a little faster.") + default: + conn.add("version", Fail, "TLS version", d.Version, "Old and unsafe: TLS 1.0 and 1.1 were retired in 2021 and browsers refuse them. Turn on TLS 1.2 and 1.3.") + } + if insecureSuite(state.CipherSuite) { + conn.add("cipher", Warn, "Cipher suite", d.CipherSuite, "The algorithms that encrypt this connection. This one is weak; prefer AES-GCM or ChaCha20 suites.") + } else { + conn.add("cipher", Pass, "Cipher suite", d.CipherSuite, "The algorithms that encrypt this connection.") + } + r.add(conn) + + peers := state.PeerCertificates + if len(peers) == 0 { + cert := Section{Title: "Certificate"} + cert.add("certificate", Fail, "Certificate", "the server sent no certificate", "") + r.add(cert) + return r.finish(), nil + } + leaf := peers[0] + info := certInfo(leaf) + d.Certificate = &info + for _, c := range peers { + d.Chain = append(d.Chain, certInfo(c)) + } + at := now() + + cert := Section{Title: "Certificate"} + cert.add("subject", Info, "Subject", info.Subject, "Who the certificate was issued to.") + names := append(append([]string{}, info.DNSNames...), info.IPAddresses...) + if len(names) == 0 { + cert.add("names", Warn, "Names (SANs)", "none", "The certificate lists no host names, so browsers will reject it.") + } else { + cert.add("names", Info, "Names (SANs)", list(names, 8), "The host names this certificate is valid for.") + } + cert.add("issuer", Info, "Issuer", info.Issuer, "The certificate authority (CA) that signed it.") + lifetime := int(math.Round(leaf.NotAfter.Sub(leaf.NotBefore).Hours() / 24)) + cert.add("validity", Info, "Valid", fmt.Sprintf("%s to %s (%s in total)", date(leaf.NotBefore), date(leaf.NotAfter), plural(lifetime, "day", "days")), "") + + days := int(math.Floor(leaf.NotAfter.Sub(at).Hours() / 24)) + d.DaysLeft = &days + renew := fmt.Sprintf("A warning starts at %d days. Certificates are getting shorter (Let's Encrypt: 64 days from February 2027, later 45), so renewal must be automatic.", WarnDays) + switch { + case at.Before(leaf.NotBefore): + cert.add("expiry", Fail, "Expires in", "not valid yet (starts "+date(leaf.NotBefore)+")", "Browsers reject a certificate before its start date. Check the clock on the server that issued it.") + case days < 0: + cert.add("expiry", Fail, "Expires in", fmt.Sprintf("expired %s ago (%s)", plural(-days, "day", "days"), date(leaf.NotAfter)), "Browsers block the site with a warning page. Renew the certificate now.") + case days <= WarnDays: + cert.add("expiry", Warn, "Expires in", fmt.Sprintf("%s (%s)", plural(days, "day", "days"), date(leaf.NotAfter)), "Renew soon. "+renew) + default: + cert.add("expiry", Pass, "Expires in", fmt.Sprintf("%s (%s)", plural(days, "day", "days"), date(leaf.NotAfter)), renew) + } + + intermediates := x509.NewCertPool() + for _, c := range peers[1:] { + intermediates.AddCert(c) + } + chains, err := leaf.Verify(x509.VerifyOptions{Roots: opts.Roots, Intermediates: intermediates, CurrentTime: at}) + if err != nil { + d.TrustError = err.Error() + cert.add("trusted", Fail, "Trusted", trustProblem(err, peers), "Browsers show a warning page for an untrusted certificate.") + } else { + d.Trusted = true + chain := chains[0] + d.TrustedRoot = name(chain[len(chain)-1]) + cert.add("trusted", Pass, "Trusted", "yes, chains to "+d.TrustedRoot, "Your system trusts the CA that signed this chain.") + } + if err := leaf.VerifyHostname(host); err != nil { + cert.add("hostname", Fail, "Host name", "does not match "+host, "The certificate is for other names (see Names above), so browsers reject it for "+host+".") + } else { + d.HostnameMatch = true + cert.add("hostname", Pass, "Host name", "matches "+host, "The certificate covers the name you asked for.") + } + d.OCSPStapled = len(state.OCSPResponse) > 0 + if d.OCSPStapled { + cert.add("ocsp", Info, "OCSP stapling", "yes", "The server attaches a recent \"not revoked\" proof from the CA.") + } else { + cert.add("ocsp", Info, "OCSP stapling", "no", "No revocation proof is attached. That is common now: browsers rarely ask, and Let's Encrypt ended OCSP in 2025.") + } + r.add(cert) + + chain := Section{Title: "Chain", Note: "The certificates the server sent, its own first. Each is signed by the next one; the root comes from your system."} + for i, c := range peers { + chain.add(fmt.Sprintf("chain.%d", i+1), Info, fmt.Sprint(i+1), fmt.Sprintf("%s, issued by %s, expires %s", name(c), issuerName(c), c.NotAfter.UTC().Format("2006-01-02")), "") + } + if d.Trusted { + chain.add("chain.root", Info, "root", d.TrustedRoot+" (from your system)", "") + } + r.add(chain) + return r.finish(), nil +} + +func certInfo(c *x509.Certificate) CertInfo { + info := CertInfo{Subject: name(c), Issuer: issuerName(c), DNSNames: c.DNSNames, NotBefore: c.NotBefore.UTC(), NotAfter: c.NotAfter.UTC(), SerialNumber: fmt.Sprintf("%X", c.SerialNumber)} + if len(c.Subject.Organization) > 0 && c.Subject.CommonName != "" { + info.Subject += " (" + c.Subject.Organization[0] + ")" + } + if len(c.Issuer.Organization) > 0 && c.Issuer.CommonName != "" { + info.Issuer += " (" + c.Issuer.Organization[0] + ")" + } + for _, ip := range c.IPAddresses { + info.IPAddresses = append(info.IPAddresses, ip.String()) + } + return info +} + +func name(c *x509.Certificate) string { + switch { + case c.Subject.CommonName != "": + return c.Subject.CommonName + case len(c.DNSNames) > 0: + return c.DNSNames[0] + case len(c.Subject.Organization) > 0: + return c.Subject.Organization[0] + } + return c.Subject.String() +} + +func issuerName(c *x509.Certificate) string { + switch { + case c.Issuer.CommonName != "": + return c.Issuer.CommonName + case len(c.Issuer.Organization) > 0: + return c.Issuer.Organization[0] + } + return c.Issuer.String() +} + +func date(t time.Time) string { return t.UTC().Format("2006-01-02 15:04 UTC") } + +func selfSigned(c *x509.Certificate) bool { + return bytes.Equal(c.RawIssuer, c.RawSubject) && c.CheckSignature(c.SignatureAlgorithm, c.RawTBSCertificate, c.Signature) == nil +} + +func insecureSuite(id uint16) bool { + return slices.ContainsFunc(tls.InsecureCipherSuites(), func(s *tls.CipherSuite) bool { return s.ID == id }) +} + +func trustProblem(err error, peers []*x509.Certificate) string { + var invalid x509.CertificateInvalidError + var unknown x509.UnknownAuthorityError + switch { + case errors.As(err, &invalid) && invalid.Reason == x509.Expired: + return "no, a certificate in the chain has expired or is not valid yet" + case selfSigned(peers[0]): + return "no, the certificate is self-signed, so no trusted CA vouches for it" + case errors.As(err, &unknown) && len(peers) == 1: + return "no, the server sent only its own certificate and your system does not know the CA that signed it (a missing intermediate?)" + case errors.As(err, &unknown): + return "no, the chain does not lead to a root your system trusts" + } + return "no, " + err.Error() +} + +func dialProblem(err error, port string, timeout time.Duration) string { + var dnsErr *net.DNSError + switch { + case errors.As(err, &dnsErr) && dnsErr.IsNotFound: + return "could not find the host: the name does not resolve to an address" + case errors.As(err, &dnsErr): + return "could not look up the host: " + dnsErr.Err + case errors.Is(err, syscall.ECONNREFUSED): + return "connection refused: nothing is listening on port " + port + case isTimeout(err): + return fmt.Sprintf("no answer within %s (a firewall may be dropping the traffic)", timeout) + } + return err.Error() +} + +func handshakeProblem(err error, timeout time.Duration) string { + var record tls.RecordHeaderError + switch { + case errors.As(err, &record): + return "failed: the server did not answer with TLS (is this a plain HTTP port?)" + case isTimeout(err): + return fmt.Sprintf("failed: no TLS answer within %s", timeout) + } + return "failed: " + err.Error() +} + +func isTimeout(err error) bool { + var netErr net.Error + return errors.Is(err, context.DeadlineExceeded) || errors.Is(err, os.ErrDeadlineExceeded) || errors.As(err, &netErr) && netErr.Timeout() +} diff --git a/internal/check/tls_test.go b/internal/check/tls_test.go new file mode 100644 index 0000000..e40ed1a --- /dev/null +++ b/internal/check/tls_test.go @@ -0,0 +1,245 @@ +package check + +import ( + "context" + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" + "crypto/tls" + "crypto/x509" + "crypto/x509/pkix" + "math/big" + "net" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" +) + +var testNow = time.Date(2026, time.November, 14, 12, 0, 0, 0, time.UTC) + +type testCA struct { + cert *x509.Certificate + key *ecdsa.PrivateKey +} + +func newTestCA(t *testing.T) *testCA { + t.Helper() + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatal(err) + } + template := &x509.Certificate{ + SerialNumber: big.NewInt(1), + Subject: pkix.Name{CommonName: "Test Root CA", Organization: []string{"DevOps Daily Tests"}}, + NotBefore: testNow.AddDate(-1, 0, 0), + NotAfter: testNow.AddDate(5, 0, 0), + IsCA: true, + BasicConstraintsValid: true, + KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageDigitalSignature, + } + der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key) + if err != nil { + t.Fatal(err) + } + cert, err := x509.ParseCertificate(der) + if err != nil { + t.Fatal(err) + } + return &testCA{cert: cert, key: key} +} + +func (ca *testCA) pool() *x509.CertPool { + pool := x509.NewCertPool() + pool.AddCert(ca.cert) + return pool +} + +// leaf issues a server certificate. A nil ca makes it self-signed. +func leaf(t *testing.T, ca *testCA, notAfter time.Time, dnsNames []string, ips ...net.IP) tls.Certificate { + t.Helper() + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatal(err) + } + template := &x509.Certificate{ + SerialNumber: big.NewInt(time.Now().UnixNano()), + Subject: pkix.Name{CommonName: "server.test"}, + NotBefore: notAfter.AddDate(0, 0, -90), + NotAfter: notAfter, + DNSNames: dnsNames, + IPAddresses: ips, + KeyUsage: x509.KeyUsageDigitalSignature, + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, + } + parent, signer := template, any(key) + if ca != nil { + parent, signer = ca.cert, ca.key + } + der, err := x509.CreateCertificate(rand.Reader, template, parent, &key.PublicKey, signer) + if err != nil { + t.Fatal(err) + } + return tls.Certificate{Certificate: [][]byte{der}, PrivateKey: key} +} + +func tlsServer(t *testing.T, cert tls.Certificate) string { + t.Helper() + srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) + srv.TLS = &tls.Config{Certificates: []tls.Certificate{cert}} + srv.StartTLS() + t.Cleanup(srv.Close) + return srv.Listener.Addr().String() +} + +func now() time.Time { return testNow } + +var localhost = net.ParseIP("127.0.0.1") + +func TestTLSGoodCertificate(t *testing.T) { + ca := newTestCA(t) + addr := tlsServer(t, leaf(t, ca, testNow.AddDate(0, 0, 60), []string{"server.test"}, localhost)) + r, err := TLS(context.Background(), addr, TLSOptions{Roots: ca.pool(), Now: now, Timeout: 2 * time.Second}) + if err != nil { + t.Fatal(err) + } + if r.ExitCode != 0 { + t.Fatalf("exit code = %d, report = %+v", r.ExitCode, r.Sections) + } + d := r.Details.(*TLSDetails) + if !d.Trusted || !d.HostnameMatch || d.DaysLeft == nil || *d.DaysLeft != 60 || d.TrustedRoot != "Test Root CA" { + t.Fatalf("details = %+v", d) + } + if d.Version != "TLS 1.3" || d.CipherSuite == "" || d.OCSPStapled { + t.Fatalf("details = %+v", d) + } + if f := finding(t, r, "expiry"); f.Status != Pass || !strings.HasPrefix(f.Value, "60 days") { + t.Fatalf("expiry = %+v", f) + } + if f := finding(t, r, "names"); f.Value != "server.test, 127.0.0.1" { + t.Fatalf("names = %+v", f) + } + if r.LearnMore != learnTLS || !hasFinding(r, "chain.root") { + t.Fatalf("report = %+v", r) + } +} + +func TestTLSExpiry(t *testing.T) { + ca := newTestCA(t) + notAfter := testNow.AddDate(0, 0, 30) + addr := tlsServer(t, leaf(t, ca, notAfter, nil, localhost)) + cases := []struct { + at time.Time + status Status + code int + value string + }{ + {notAfter.AddDate(0, 0, -22), Pass, 0, "22 days"}, + {notAfter.AddDate(0, 0, -21), Warn, 1, "21 days"}, + {notAfter.Add(-36 * time.Hour), Warn, 1, "1 day"}, + {notAfter.AddDate(0, 0, 3), Fail, 2, "expired 3 days ago"}, + {notAfter.AddDate(0, 0, -100), Fail, 2, "not valid yet"}, + } + for _, c := range cases { + at := c.at + r, err := TLS(context.Background(), addr, TLSOptions{Roots: ca.pool(), Now: func() time.Time { return at }}) + if err != nil { + t.Fatal(err) + } + f := finding(t, r, "expiry") + if f.Status != c.status || r.ExitCode != c.code || !strings.HasPrefix(f.Value, c.value) { + t.Errorf("at %s: expiry %+v, exit %d; want %s %q exit %d", at, f, r.ExitCode, c.status, c.value, c.code) + } + } +} + +func TestTLSUntrustedAndWrongName(t *testing.T) { + ca := newTestCA(t) + addr := tlsServer(t, leaf(t, ca, testNow.AddDate(0, 0, 60), []string{"server.test"})) + + r, err := TLS(context.Background(), addr, TLSOptions{Roots: x509.NewCertPool(), Now: now}) + if err != nil { + t.Fatal(err) + } + if r.ExitCode != 2 { + t.Fatalf("exit code = %d", r.ExitCode) + } + if f := finding(t, r, "trusted"); f.Status != Fail || !strings.Contains(f.Value, "intermediate") { + t.Fatalf("trusted = %+v", f) + } + if f := finding(t, r, "hostname"); f.Status != Fail || !strings.Contains(f.Value, "127.0.0.1") { + t.Fatalf("hostname = %+v", f) + } +} + +func TestTLSSelfSignedAndStapled(t *testing.T) { + cert := leaf(t, nil, testNow.AddDate(0, 0, 60), nil, localhost) + cert.OCSPStaple = []byte{0x30, 0x03, 0x0a, 0x01, 0x00} + addr := tlsServer(t, cert) + r, err := TLS(context.Background(), addr, TLSOptions{Roots: x509.NewCertPool(), Now: now}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "trusted"); f.Status != Fail || !strings.Contains(f.Value, "self-signed") { + t.Fatalf("trusted = %+v", f) + } + if f := finding(t, r, "ocsp"); f.Value != "yes" { + t.Fatalf("ocsp = %+v", f) + } + if f := finding(t, r, "hostname"); f.Status != Pass { + t.Fatalf("hostname = %+v", f) + } +} + +func TestTLSConnectionProblems(t *testing.T) { + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + closed := ln.Addr().String() + ln.Close() + r, err := TLS(context.Background(), closed, TLSOptions{Now: now, Timeout: time.Second}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "connect"); f.Status != Fail || r.ExitCode != 2 || !strings.Contains(f.Value, "refused") { + t.Fatalf("connect = %+v, exit %d", f, r.ExitCode) + } + + plain := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) + defer plain.Close() + r, err = TLS(context.Background(), plain.Listener.Addr().String(), TLSOptions{Now: now, Timeout: time.Second}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "handshake"); f.Status != Fail || !strings.Contains(f.Value, "did not answer with TLS") { + t.Fatalf("handshake = %+v", f) + } + + silent, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer silent.Close() + go func() { + for { + conn, err := silent.Accept() + if err != nil { + return + } + defer conn.Close() + } + }() + r, err = TLS(context.Background(), silent.Addr().String(), TLSOptions{Now: now, Timeout: 200 * time.Millisecond}) + if err != nil { + t.Fatal(err) + } + if f := finding(t, r, "handshake"); f.Status != Fail || !strings.Contains(f.Value, "no TLS answer within 200ms") { + t.Fatalf("handshake = %+v", f) + } + + if _, err := TLS(context.Background(), "not a host", TLSOptions{}); err == nil { + t.Fatal("bad target gave no error") + } +} diff --git a/internal/cli/check.go b/internal/cli/check.go new file mode 100644 index 0000000..478f619 --- /dev/null +++ b/internal/cli/check.go @@ -0,0 +1,276 @@ +package cli + +import ( + "context" + "encoding/json" + "fmt" + "strings" + "time" + "unicode/utf8" + + "github.com/charmbracelet/lipgloss" + "github.com/spf13/cobra" + + "github.com/The-DevOps-Daily/devops-daily-cli/internal/check" + "github.com/The-DevOps-Daily/devops-daily-cli/internal/text" + "github.com/The-DevOps-Daily/devops-daily-cli/internal/tui" +) + +const checkLong = `Check a host from your own machine: its TLS certificate, DNS records, HTTP +security headers or email records. Only the host you name and the DNS server +are contacted. These checks are not part of the SSH app. + +Exit codes: 0 all good, 1 warnings, 2 a problem or an error.` + +func (a *App) checkCmd() *cobra.Command { + var jsonOut bool + var timeout time.Duration + cmd := &cobra.Command{ + Use: "check", + Short: "Check a site's TLS, DNS, headers or email records", + Long: checkLong, + Example: " devops-daily check tls github.com\n devops-daily check dns devops-daily.com --type MX\n devops-daily check headers https://devops-daily.com\n devops-daily check email gmail.com", + Args: cobra.ArbitraryArgs, + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + return usageError{fmt.Errorf("unknown check %q; use tls, dns, headers or email", args[0])} + } + return cmd.Help() + }, + } + cmd.PersistentFlags().BoolVar(&jsonOut, "json", false, "print JSON") + cmd.PersistentFlags().DurationVar(&timeout, "timeout", check.DefaultTimeout, "how long to wait for each network reply") + run := func(cmd *cobra.Command, get func(context.Context) (*check.Report, error)) error { + if timeout <= 0 { + return usageError{fmt.Errorf("--timeout must be more than 0")} + } + report, err := get(cmd.Context()) + if err != nil { + return usageError{err} + } + return a.printCheck(report, jsonOut) + } + + tlsCmd := &cobra.Command{ + Use: "tls ", + Short: "Check a TLS certificate: expiry, chain, trust and host name", + Example: " devops-daily check tls github.com\n devops-daily check tls smtp.gmail.com:465", + Args: exactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return run(cmd, func(ctx context.Context) (*check.Report, error) { + return check.TLS(ctx, args[0], check.TLSOptions{Timeout: timeout, Now: a.Now}) + }) + }, + } + + var types []string + var server string + dnsCmd := &cobra.Command{ + Use: "dns ", + Short: "Look up DNS records with their TTLs and the CNAME chain", + Example: " devops-daily check dns devops-daily.com\n devops-daily check dns www.github.com --type CNAME,A --server 1.1.1.1", + Args: exactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return run(cmd, func(ctx context.Context) (*check.Report, error) { + return check.DNS(ctx, args[0], check.DNSOptions{Server: server, Types: types, Timeout: timeout}) + }) + }, + } + dnsCmd.Flags().StringSliceVar(&types, "type", nil, "record types: A, AAAA, CNAME, MX, NS, TXT, CAA or SOA (default A,AAAA,MX,NS,TXT,CAA)") + dnsCmd.Flags().StringVar(&server, "server", "", "DNS server IP to ask, like 1.1.1.1 (default: your system resolver)") + + headersCmd := &cobra.Command{ + Use: "headers ", + Short: "Follow redirects and check security and cache headers", + Example: " devops-daily check headers devops-daily.com\n devops-daily check headers http://example.com/login", + Args: exactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return run(cmd, func(ctx context.Context) (*check.Report, error) { + return check.Headers(ctx, args[0], check.HeadersOptions{Timeout: timeout, UserAgent: "devops-daily-cli/" + a.version() + " (check headers)"}) + }) + }, + } + + var selectors []string + var emailServer string + emailCmd := &cobra.Command{ + Use: "email ", + Short: "Check MX, SPF, DMARC, DKIM, MTA-STS and TLS-RPT records", + Example: " devops-daily check email gmail.com\n devops-daily check email example.com --selector mailjet", + Args: exactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return run(cmd, func(ctx context.Context) (*check.Report, error) { + return check.Email(ctx, args[0], check.EmailOptions{Server: emailServer, Selectors: selectors, Timeout: timeout}) + }) + }, + } + emailCmd.Flags().StringSliceVar(&selectors, "selector", nil, "extra DKIM selector to try (repeat or comma-separate)") + emailCmd.Flags().StringVar(&emailServer, "server", "", "DNS server IP to ask, like 1.1.1.1 (default: your system resolver)") + + for _, sub := range []*cobra.Command{tlsCmd, dnsCmd, headersCmd, emailCmd} { + sub.Long = sub.Short + ".\n\n" + checkLong + } + cmd.AddCommand(tlsCmd, dnsCmd, headersCmd, emailCmd) + return cmd +} + +func (a *App) version() string { + if a.Build.Version == "" { + return "dev" + } + return a.Build.Version +} + +// printCheck prints the report and turns its result into the exit code. +func (a *App) printCheck(report *check.Report, jsonOut bool) error { + if jsonOut { + if err := json.NewEncoder(a.Stdout).Encode(report); err != nil { + return exitError{code: 2, err: err} + } + } else { + a.printReport(report) + } + if report.ExitCode != 0 { + return exitError{code: report.ExitCode} + } + return nil +} + +var checkTitles = map[string]string{"tls": "TLS check", "dns": "DNS check", "headers": "Header check", "email": "Email check"} + +const statusWidth = 9 // "missing" and two spaces + +func (a *App) printReport(r *check.Report) { + width := max(a.width(), 40) + out := a.Stdout + title := checkTitles[r.Check] + " for " + text.Line(r.Target, 200) + if r.Unicode != "" { + title += " (" + text.Line(r.Unicode, 200) + ")" + } + fmt.Fprintln(out, a.bold(title)) + for _, s := range r.Sections { + fmt.Fprintln(out) + fmt.Fprintln(out, a.bold(text.Line(s.Title, 80))) + if s.Note != "" { + for _, line := range wrapHard(text.Line(s.Note, 0), width-2) { + fmt.Fprintln(out, " "+a.muted(line)) + } + } + statusCol, labelWidth := false, 0 + for _, f := range s.Findings { + statusCol = statusCol || f.Status != check.Info + labelWidth = max(labelWidth, utf8.RuneCountInString(f.Label)) + } + labelWidth = min(labelWidth, 26) + for _, f := range s.Findings { + prefix, plain := " ", 2 + if statusCol { + prefix += a.status(f.Status, tui.PadRight(string(f.Status), statusWidth)) + plain += statusWidth + } + if labelWidth > 0 { + prefix += tui.PadRight(text.Line(f.Label, labelWidth), labelWidth) + " " + plain += labelWidth + 2 + } + value := text.Line(f.Value, 0) + if width-plain < 24 { + fmt.Fprintln(out, strings.TrimRight(prefix, " ")) + prefix, plain = " ", 6 + } + lines := wrapHard(value, width-plain) + if len(lines) == 0 { + lines = []string{""} + } + fmt.Fprintln(out, strings.TrimRight(prefix+lines[0], " ")) + for _, line := range lines[1:] { + fmt.Fprintln(out, strings.Repeat(" ", plain)+line) + } + if f.Meaning != "" { + // Under the label when there is a status column, else under the value. + indent := 2 + statusWidth + if !statusCol { + indent = plain + } + for _, line := range wrapHard(text.Line(f.Meaning, 0), width-indent) { + fmt.Fprintln(out, strings.Repeat(" ", indent)+a.muted(line)) + } + } + } + } + fmt.Fprintln(out) + fmt.Fprintln(out, a.status(r.Status, summary(r))) + if r.LearnMore != "" { + fmt.Fprintln(out, "Learn more: "+r.LearnMore) + } +} + +func summary(r *check.Report) string { + warnings := fmt.Sprintf("%d warnings", r.Warnings) + if r.Warnings == 1 { + warnings = "1 warning" + } + switch { + case r.Failures > 0: + problems := fmt.Sprintf("%d problems", r.Failures) + if r.Failures == 1 { + problems = "1 problem" + } + if r.Warnings > 0 { + return "Result: " + problems + " and " + warnings + "." + } + return "Result: " + problems + "." + case r.Warnings > 0: + return "Result: " + warnings + ", no problems." + } + return "Result: all good." +} + +func (a *App) status(s check.Status, value string) string { + if a.NoColor || !a.IsTTY() { + return value + } + style := a.renderer().NewStyle() + switch s { + case check.Pass: + style = style.Foreground(lipgloss.Color("#22C55E")) + case check.Warn, check.Missing: + style = style.Foreground(lipgloss.Color("#F6A823")) + case check.Fail: + style = style.Foreground(lipgloss.Color("#EF4444")).Bold(true) + default: + style = style.Foreground(lipgloss.Color("#A1A1AA")) + } + return style.Render(value) +} + +// wrapHard wraps value at spaces and also breaks words longer than width, +// such as long TXT records. +func wrapHard(value string, width int) []string { + width = max(width, 10) + var lines []string + line := "" + for _, word := range strings.Fields(value) { + for utf8.RuneCountInString(word) > width { + if line != "" { + lines = append(lines, line) + line = "" + } + runes := []rune(word) + lines = append(lines, string(runes[:width])) + word = string(runes[width:]) + } + switch { + case line == "": + line = word + case utf8.RuneCountInString(line)+1+utf8.RuneCountInString(word) > width: + lines = append(lines, line) + line = word + default: + line += " " + word + } + } + if line != "" { + lines = append(lines, line) + } + return lines +} diff --git a/internal/cli/check_test.go b/internal/cli/check_test.go new file mode 100644 index 0000000..982f605 --- /dev/null +++ b/internal/cli/check_test.go @@ -0,0 +1,139 @@ +package cli + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/The-DevOps-Daily/devops-daily-cli/internal/check" +) + +func TestCheckHeadersTextAndJSON(t *testing.T) { + srv := newTestServer(t) + app, calls := newTestApp(t, srv) + site := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("X-Content-Type-Options", "nosniff") + })) + defer site.Close() + + if code := run(t, app, "check", "headers", site.URL); code != 1 { + t.Fatalf("exit code = %d, stderr = %s", code, calls.stderr.String()) + } + out := calls.stdout.String() + for _, want := range []string{"Header check for " + site.URL + "/", "Requests", "Security headers", "pass X-Content-Type-Options", "missing Content-Security-Policy", "Result: ", "warnings, no problems.", "Learn more: https://devops-daily.com/guides/owasp-top-10/05-security-misconfiguration"} { + if !strings.Contains(out, want) { + t.Errorf("output has no %q:\n%s", want, out) + } + } + if strings.Contains(out, "\x1b") || strings.Contains(strings.ToLower(out), "sponsor") || strings.Contains(out, "Acme") { + t.Errorf("output has escape codes or a sponsor line:\n%s", out) + } + + if code := run(t, app, "check", "headers", site.URL, "--json"); code != 1 { + t.Fatalf("JSON exit code = %d", code) + } + var report struct { + Check string `json:"check"` + ExitCode int `json:"exitCode"` + Details struct { + Hops []struct { + Status int `json:"status"` + } `json:"hops"` + } `json:"details"` + } + if err := json.Unmarshal(calls.stdout.Bytes(), &report); err != nil { + t.Fatalf("JSON: %v\n%s", err, calls.stdout.String()) + } + if report.Check != "headers" || report.ExitCode != 1 || len(report.Details.Hops) != 1 || report.Details.Hops[0].Status != 200 { + t.Fatalf("report = %+v", report) + } + + if got := srv.count("index.json"); got != 0 { + t.Fatalf("check downloaded content: %d index requests", got) + } +} + +func TestCheckTLSUntrustedExitsTwo(t *testing.T) { + srv := newTestServer(t) + app, calls := newTestApp(t, srv) + site := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) + defer site.Close() + if code := run(t, app, "check", "tls", site.Listener.Addr().String(), "--timeout", "2s"); code != 2 { + t.Fatalf("exit code = %d, stdout = %s, stderr = %s", code, calls.stdout.String(), calls.stderr.String()) + } + out := calls.stdout.String() + for _, want := range []string{"TLS check for 127.0.0.1:", "fail Trusted", "Expires in", "Chain", "Result: ", "Learn more: https://devops-daily.com/games/ssl-tls-handshake"} { + if !strings.Contains(out, want) { + t.Errorf("output has no %q:\n%s", want, out) + } + } +} + +func TestCheckUsageErrors(t *testing.T) { + srv := newTestServer(t) + app, calls := newTestApp(t, srv) + cases := [][]string{ + {"check", "nope"}, + {"check", "tls"}, + {"check", "tls", "a", "b"}, + {"check", "tls", "bad host"}, + {"check", "dns", "example.com", "--type", "PTR"}, + {"check", "dns", "example.com", "--server", "dns.google"}, + {"check", "email", "1.1.1.1"}, + {"check", "headers", "ftp://example.com"}, + {"check", "headers", "example.com", "--timeout", "0s"}, + {"check", "headers", "example.com", "--timeout", "soon"}, + } + for _, args := range cases { + if code := run(t, app, args...); code != 2 { + t.Errorf("%v exit code = %d, stderr = %s", args, code, calls.stderr.String()) + } + } + if code := run(t, app, "check"); code != 0 { + t.Fatalf("check help exit code = %d", code) + } + out := calls.stdout.String() + for _, want := range []string{"Exit codes: 0 all good, 1 warnings, 2 a problem or an error.", "not part of the SSH app", "tls", "dns", "headers", "email"} { + if !strings.Contains(out, want) { + t.Errorf("check help has no %q:\n%s", want, out) + } + } + if code := run(t, app, "--help"); code != 0 || !strings.Contains(calls.stdout.String(), "check") { + t.Fatalf("root help = %s", calls.stdout.String()) + } +} + +func TestPrintReportCleansAndWraps(t *testing.T) { + srv := newTestServer(t) + app, calls := newTestApp(t, srv) + app.Width = func() int { return 50 } + report := &check.Report{Check: "dns", Target: "evil\x1b]2;owned\x07.test", Status: check.Warn, Warnings: 1, ExitCode: 1, Sections: []check.Section{{ + Title: "TXT records", + Findings: []check.Finding{ + {ID: "txt", Status: check.Warn, Label: "TTL 300s (5m)", Value: "v=spf1 \x1b[2Jinclude:" + strings.Repeat("x", 80), Meaning: "A long meaning that has to wrap onto more than one line at this width."}, + }, + }}} + if err := app.printCheck(report, false); err == nil { + t.Fatal("a report with warnings returned no exit error") + } + out := calls.stdout.String() + if strings.ContainsAny(out, "\x1b\x07") || strings.Contains(out, "owned") || strings.Contains(out, "[2J") { + t.Fatalf("output has escape sequences: %q", out) + } + for _, line := range strings.Split(out, "\n") { + if len([]rune(line)) > 50 { + t.Errorf("line longer than 50: %q", line) + } + } + if !strings.Contains(out, "Result: 1 warning, no problems.") { + t.Errorf("summary missing:\n%s", out) + } + if err := app.printCheck(&check.Report{Check: "dns", Target: "xn--bcher-kva.de", Unicode: "bücher.de", Status: check.Pass}, false); err != nil { + t.Fatalf("a clean report returned %v", err) + } + if !strings.Contains(calls.stdout.String(), "DNS check for xn--bcher-kva.de (bücher.de)") { + t.Errorf("title does not show both forms:\n%s", calls.stdout.String()) + } +} diff --git a/internal/cli/helpers_test.go b/internal/cli/helpers_test.go index 6bedaae..429bd69 100644 --- a/internal/cli/helpers_test.go +++ b/internal/cli/helpers_test.go @@ -4,7 +4,6 @@ import ( "bytes" "crypto/sha256" "encoding/json" - "errors" "fmt" "net/http" "net/http/httptest" @@ -237,21 +236,5 @@ func run(t *testing.T, app *App, args ...string) int { if output, ok := app.Stderr.(*bytes.Buffer); ok { output.Reset() } - command := app.Command() - command.SetArgs(args) - if _, _, err := command.Find(args); err != nil { - fmt.Fprintf(app.Stderr, "devops-daily: %s\n", err) - fmt.Fprintln(app.Stderr, "Run 'devops-daily --help' for usage.") - return 2 - } - if err := command.Execute(); err != nil { - fmt.Fprintf(app.Stderr, "devops-daily: %s\n", err) - var use usageError - if errors.As(err, &use) { - fmt.Fprintln(app.Stderr, "Run 'devops-daily --help' for usage.") - return 2 - } - return 1 - } - return 0 + return app.Run(args) } diff --git a/internal/cli/root.go b/internal/cli/root.go index ce32eaf..cb6b12f 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -52,20 +52,46 @@ type usageError struct{ err error } func (e usageError) Error() string { return e.err.Error() } func (e usageError) Unwrap() error { return e.err } +// exitError ends the program with a chosen exit code. With a nil err +// nothing more is printed. +type exitError struct { + code int + err error +} + +func (e exitError) Error() string { + if e.err == nil { + return fmt.Sprintf("exit code %d", e.code) + } + return e.err.Error() +} +func (e exitError) Unwrap() error { return e.err } + func Main(args []string, info BuildInfo) int { - app := DefaultApp(info) - command := app.Command() + return DefaultApp(info).Run(args) +} + +// Run runs one command line and returns the exit code. +func (a *App) Run(args []string) int { + command := a.Command() command.SetArgs(args) if _, _, err := command.Find(args); err != nil { - printError(app.Stderr, err) - fmt.Fprintln(app.Stderr, "Run 'devops-daily --help' for usage.") + printError(a.Stderr, err) + fmt.Fprintln(a.Stderr, "Run 'devops-daily --help' for usage.") return 2 } if err := command.Execute(); err != nil { - printError(app.Stderr, err) + var exit exitError + if errors.As(err, &exit) { + if exit.err != nil { + printError(a.Stderr, exit.err) + } + return exit.code + } + printError(a.Stderr, err) var use usageError if errors.As(err, &use) { - fmt.Fprintln(app.Stderr, "Run 'devops-daily --help' for usage.") + fmt.Fprintln(a.Stderr, "Run 'devops-daily --help' for usage.") return 2 } return 1 @@ -139,7 +165,7 @@ func (a *App) Command() *cobra.Command { root := &cobra.Command{ Use: "devops-daily", Short: "Learn DevOps from your terminal", - Example: " devops-daily the home menu\n devops-daily posts --tag k8s\n devops-daily read \n devops-daily quiz docker\n devops-daily play dns", + Example: " devops-daily the home menu\n devops-daily posts --tag k8s\n devops-daily read \n devops-daily quiz docker\n devops-daily play dns\n devops-daily check tls github.com", SilenceErrors: true, SilenceUsage: true, Args: func(cmd *cobra.Command, args []string) error { @@ -165,17 +191,21 @@ func (a *App) Command() *cobra.Command { root.PersistentFlags().BoolVar(&offline, "offline", false, "use cached content only") root.PersistentFlags().BoolVar(&noColor, "no-color", false, "disable colour") root.SetFlagErrorFunc(func(_ *cobra.Command, err error) error { return usageError{err} }) - root.AddCommand(a.postsCmd(), a.readCmd(), a.incidentCmd(), a.playCmd(), a.subscribeCmd(), a.syncCmd(), a.versionCmd(), a.updateCmd(), a.quizCmd(), a.flashcardsCmd(), a.checklistCmd()) + root.AddCommand(a.postsCmd(), a.readCmd(), a.incidentCmd(), a.playCmd(), a.subscribeCmd(), a.syncCmd(), a.versionCmd(), a.updateCmd(), a.quizCmd(), a.flashcardsCmd(), a.checklistCmd(), a.checkCmd()) root.SetHelpFunc(func(cmd *cobra.Command, args []string) { if cmd == root { fmt.Fprintln(a.Stdout, "Learn DevOps from your terminal: posts, quizzes, flashcards, checklists and") fmt.Fprintln(a.Stdout, "a daily incident from DevOps Daily. Run it with no command for the menu.") fmt.Fprintln(a.Stdout) cmd.Print(cmd.UsageString()) - fmt.Fprintln(a.Stdout, "Content: https://devops-daily.com\nNo tracking: the only network requests are the content download and \"update\".") + fmt.Fprintln(a.Stdout, "Content: https://devops-daily.com\nNo tracking: the only network requests are the content download, \"update\" and\nthe hosts you name in \"check\".") return } - fmt.Fprintln(a.Stdout, cmd.Short+".") + if cmd.Long != "" { + fmt.Fprintln(a.Stdout, cmd.Long) + } else { + fmt.Fprintln(a.Stdout, cmd.Short+".") + } fmt.Fprintln(a.Stdout) cmd.Print(cmd.UsageString()) })