|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net" |
| 6 | + "slices" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/TMHSDigital/subenum/internal/dnstest" |
| 12 | + "golang.org/x/net/dns/dnsmessage" |
| 13 | +) |
| 14 | + |
| 15 | +// mcpTestZone answers A for names, NS and the transfer for the apex, and |
| 16 | +// NXDOMAIN for the rest, as a scan's preflight and wildcard probes expect. |
| 17 | +func mcpTestZone(t *testing.T, apex string, names, transfer []string) *dnstest.Server { |
| 18 | + t.Helper() |
| 19 | + return dnstest.Start(t, func(q dnstest.Query) dnstest.Reply { |
| 20 | + switch { |
| 21 | + case q.Type == dnsmessage.TypeAXFR && transfer != nil: |
| 22 | + return dnstest.Reply{Transfer: transfer} |
| 23 | + case q.Name == apex && q.Type == dnsmessage.TypeNS: |
| 24 | + return dnstest.Reply{NS: []string{"ns1." + apex}} |
| 25 | + case q.Name == "ns1."+apex && q.Type == dnsmessage.TypeA: |
| 26 | + return dnstest.Reply{A: []string{"127.0.0.1"}} |
| 27 | + case q.Name == apex || q.Name == "ns1."+apex: |
| 28 | + return dnstest.Reply{} |
| 29 | + case slices.Contains(names, q.Name): |
| 30 | + if q.Type == dnsmessage.TypeA { |
| 31 | + return dnstest.Reply{A: []string{"192.0.2.10"}} |
| 32 | + } |
| 33 | + return dnstest.Reply{} |
| 34 | + } |
| 35 | + return dnstest.NXDomain |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +// callReport makes one tool call and decodes its report. |
| 40 | +func callReport(t *testing.T, call func() (string, bool)) mcpReport { |
| 41 | + t.Helper() |
| 42 | + text, _ := call() |
| 43 | + var r mcpReport |
| 44 | + if err := json.Unmarshal([]byte(text), &r); err != nil { |
| 45 | + t.Fatalf("not a scan report: %s", text) |
| 46 | + } |
| 47 | + return r |
| 48 | +} |
| 49 | + |
| 50 | +// TestMCPScanPermute covers #405: permute runs a second pass over variants |
| 51 | +// of the names found, within what is left of max_queries, and its finds join |
| 52 | +// the results. |
| 53 | +func TestMCPScanPermute(t *testing.T) { |
| 54 | + t.Parallel() |
| 55 | + srv := mcpTestZone(t, "example.test", []string{"api.example.test", "api-dev.example.test"}, nil) |
| 56 | + tool := scanTool(mcpConfig{allow: []string{"example.test"}, dnsServer: srv.Addr, maxQueries: 1000, rate: 1000, |
| 57 | + maxDuration: time.Minute, timeout: 5 * time.Second, maxCalls: 10}) |
| 58 | + call := func(args string) func() (string, bool) { |
| 59 | + return func() (string, bool) { |
| 60 | + r := tool.Call(t.Context(), json.RawMessage(args)) |
| 61 | + return r.Text, r.IsError |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + r := callReport(t, call(`{"domain": "example.test", "words": ["api", "www"], "types": ["A"], "permute": true}`)) |
| 66 | + p := r.Permutation |
| 67 | + if p == nil { |
| 68 | + t.Fatalf("no permutation in the report: %+v", r) |
| 69 | + } |
| 70 | + var names []string |
| 71 | + for _, res := range r.Results { |
| 72 | + names = append(names, res.Name) |
| 73 | + } |
| 74 | + if !slices.Contains(names, "api-dev.example.test") || r.Found != 2 { |
| 75 | + t.Errorf("results %v, want api and the variant api-dev", names) |
| 76 | + } |
| 77 | + if p.Found != 1 || p.Tested == 0 || p.Tested != int64(p.Candidates) || p.Verdict != "complete" { |
| 78 | + t.Errorf("permutation %+v, want every variant tested, one found, complete", p) |
| 79 | + } |
| 80 | + |
| 81 | + // Without permute there is no pass and no variant. |
| 82 | + r = callReport(t, call(`{"domain": "example.test", "words": ["api", "www"], "types": ["A"]}`)) |
| 83 | + if r.Permutation != nil || r.Found != 1 { |
| 84 | + t.Errorf("without permute: found %d, permutation %+v", r.Found, r.Permutation) |
| 85 | + } |
| 86 | + |
| 87 | + // A budget the scan spends leaves the pass nothing. |
| 88 | + r = callReport(t, call(`{"domain": "example.test", "words": ["api", "www"], "types": ["A"], "permute": true, "max_queries": 2}`)) |
| 89 | + if p := r.Permutation; p == nil || p.Tested != 0 || p.Skipped == 0 || p.Verdict != "degraded" { |
| 90 | + t.Errorf("max_queries spent by the scan: %+v, want every variant skipped and degraded", p) |
| 91 | + } |
| 92 | + |
| 93 | + // Part of the budget left: the pass tests that many. |
| 94 | + r = callReport(t, call(`{"domain": "example.test", "words": ["api", "www"], "types": ["A"], "permute": true, "max_queries": 5}`)) |
| 95 | + if p := r.Permutation; p == nil || p.Tested != 3 || p.Skipped != int64(p.Candidates)-3 { |
| 96 | + t.Errorf("max_queries 5: %+v, want 3 variants tested and the rest skipped", p) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// TestE2EMCPLabScanPermute covers #405: lab_scan accepts permute, so agents |
| 101 | +// can practise it; lab1 has no variants of its names, so the pass finds |
| 102 | +// nothing but tests them. |
| 103 | +func TestE2EMCPLabScanPermute(t *testing.T) { |
| 104 | + t.Parallel() |
| 105 | + _, replies := runMCPSession(t, []string{"-max-queries", "1000"}, |
| 106 | + mcpCall{"lab_scan", `{"words": ["www", "mail"], "permute": true}`}, |
| 107 | + ) |
| 108 | + r := mcpReportOf(t, replies[0]) |
| 109 | + if p := r.Permutation; p == nil || p.Candidates == 0 || p.Tested != int64(p.Candidates) || p.Found != 0 || p.Verdict != "complete" { |
| 110 | + t.Errorf("lab_scan permute: %+v", r.Permutation) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// TestMCPScanZoneTransfer covers #405: zone_transfer is refused unless the |
| 115 | +// server was started with -axfr, and with it the report says which name |
| 116 | +// servers allowed a transfer and the names it gave are verified like words. |
| 117 | +func TestMCPScanZoneTransfer(t *testing.T) { |
| 118 | + t.Parallel() |
| 119 | + srv := mcpTestZone(t, "example.test", []string{"zt.example.test", "gone.example.test"}, []string{"zt.example.test", "stale.example.test"}) |
| 120 | + _, port, err := net.SplitHostPort(srv.Addr) |
| 121 | + if err != nil { |
| 122 | + t.Fatal(err) |
| 123 | + } |
| 124 | + cfg := mcpConfig{allow: []string{"example.test"}, dnsServer: srv.Addr, maxQueries: 100, rate: 1000, |
| 125 | + maxDuration: time.Minute, timeout: 5 * time.Second, maxCalls: 10, transferPort: port} |
| 126 | + |
| 127 | + off := scanTool(cfg) |
| 128 | + r := off.Call(t.Context(), json.RawMessage(`{"domain": "example.test", "words": ["www"], "zone_transfer": true}`)) |
| 129 | + if !r.IsError || !strings.Contains(r.Text, "the operator did not start the server with -axfr") { |
| 130 | + t.Errorf("zone_transfer without -axfr: %+v", r) |
| 131 | + } |
| 132 | + if n := srv.QueriesFor("example.test", dnsmessage.TypeAXFR); n != 0 { |
| 133 | + t.Errorf("a refused call asked for %d transfers", n) |
| 134 | + } |
| 135 | + |
| 136 | + cfg.axfr = true |
| 137 | + on := scanTool(cfg) |
| 138 | + report := callReport(t, func() (string, bool) { |
| 139 | + r := on.Call(t.Context(), json.RawMessage(`{"domain": "example.test", "words": ["www"], "types": ["A"], "zone_transfer": true}`)) |
| 140 | + return r.Text, r.IsError |
| 141 | + }) |
| 142 | + z := report.ZoneTransfer |
| 143 | + if z == nil || z.Names != 2 || !slices.Equal(z.Allowed, []string{"ns1.example.test"}) || z.Error != "" { |
| 144 | + t.Fatalf("zone_transfer: %+v", z) |
| 145 | + } |
| 146 | + if report.Found != 1 || report.Results[0].Name != "zt.example.test" { |
| 147 | + t.Errorf("results %+v, want the transferred name that resolves", report.Results) |
| 148 | + } |
| 149 | + |
| 150 | + // An excluded name server is never asked. |
| 151 | + before := srv.QueriesFor("example.test", dnsmessage.TypeAXFR) |
| 152 | + report = callReport(t, func() (string, bool) { |
| 153 | + r := on.Call(t.Context(), json.RawMessage(`{"domain": "example.test", "words": ["www"], "types": ["A"], "zone_transfer": true, "exclude": ["ns1.example.test"]}`)) |
| 154 | + return r.Text, r.IsError |
| 155 | + }) |
| 156 | + if n := srv.QueriesFor("example.test", dnsmessage.TypeAXFR); n != before { |
| 157 | + t.Errorf("an excluded name server was asked for a transfer") |
| 158 | + } |
| 159 | + if report.ZoneTransfer == nil || len(report.ZoneTransfer.Allowed) != 0 { |
| 160 | + t.Errorf("excluded name server: %+v", report.ZoneTransfer) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +// TestMCPAXFRFlag covers #405: -axfr adds zone_transfer to scan's schema. |
| 165 | +func TestMCPAXFRFlag(t *testing.T) { |
| 166 | + cfg, code, ok := parseMCPFlags([]string{"-allow", "example.com", "-axfr"}, &strings.Builder{}) |
| 167 | + if !ok || code != exitOK || !cfg.axfr { |
| 168 | + t.Fatalf("-axfr: ok %v code %d axfr %v", ok, code, cfg.axfr) |
| 169 | + } |
| 170 | + if !strings.Contains(string(scanTool(cfg).InputSchema), `"zone_transfer"`) { |
| 171 | + t.Error("scan's schema has no zone_transfer with -axfr") |
| 172 | + } |
| 173 | +} |
0 commit comments