package capability // These opt-in tests execute installed binaries against loopback fixtures only. // The host launcher runs this test binary inside Docker with --network none. import ( "context" "encoding/binary" "encoding/json" "fmt" "net" "net/http" "net/http/httptest" "os" "os/exec" "path/filepath" "strings" "sync/atomic" "testing" "time" "github.com/s41r4j/cybot/catalog" "github.com/s41r4j/cybot/internal/evidence" "github.com/s41r4j/cybot/internal/execution" "github.com/s41r4j/cybot/internal/registry" "github.com/s41r4j/cybot/pkg/schema" ) func liveDeps(t *testing.T) Dependencies { t.Helper() if os.Getenv("CYBOT_LIVE_CORE") != "1" { t.Skip("requires isolated installed core toolbox") } tools := registry.NewTools() all, err := catalog.Tools() if err != nil { t.Fatal(err) } for _, tool := range all { tool.Kind = "native" tool.DockerImage = "" if err := tools.Upsert(tool); err != nil { t.Fatal(err) } } return Dependencies{Tools: tools, Runner: execution.LocalRunner{}, Evidence: evidence.New(t.TempDir()), Timeout: 45 * time.Second} } func liveCapability(t *testing.T, id string) externalCapability { t.Helper() r := NewRegistry() if err := RegisterBuiltins(r); err != nil { t.Fatal(err) } c, _ := r.Get(id) return c.(externalCapability) } func liveRequest(target string) schema.RunRequest { return schema.RunRequest{RunID: "local-fixture", Target: target, Approved: true, Input: json.RawMessage(`{}`)} } func fixtureHTTP(t *testing.T) *httptest.Server { t.Helper() server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" && r.URL.Path != "/about" && r.URL.Path != "/admin" { http.NotFound(w, r) return } w.Header().Set("Content-Type", "text/html") fmt.Fprint(w, `
Local fixture
") })) defer clean.Close() result, err = liveCapability(t, "web.map").Run(context.Background(), liveRequest(clean.URL), d) if err != nil || len(result.Errors) != 0 || len(result.Evidence) != 2 || len(result.Findings) != 0 || len(result.Endpoints) == 0 { t.Fatalf("clean mapping failed: %+v %v", result, err) } for _, ep := range result.Endpoints { if ep.URL != clean.URL && ep.URL != clean.URL+"/" { t.Fatalf("crawler invented a link: %+v", result.Endpoints) } } clean.Close() broken := liveCapability(t, "web.map") broken.steps[0].Args = func(target string, r schema.RunRequest) []string { return append(httpxArgs(target, r), "-timeout", "2", "-retries", "0") } broken.steps[1].Args = func(target string, r schema.RunRequest) []string { return append(katanaArgs(target, r), "-timeout", "2", "-retry", "0") } result, err = broken.Run(context.Background(), liveRequest(clean.URL), d) if err == nil && result.Status != "partial" || len(result.Errors) == 0 { t.Fatalf("unreachable mapping was accepted as complete: %+v %v", result, err) } requireToolFailure(t, d, "katana", []string{"--cybot-invalid-fixture-option"}) cases := []operationCase{ passedCase("positive", "HTTPx records the reachable fixture and Katana discovers its /about link."), passedCase("negative", "A reachable page without links yields no invented endpoints or security findings."), passedCase("failure", "A closed loopback target produces an incomplete/error result; Katana rejects an invalid option."), } certifyOperation(t, "httpx", "web.map", cases) certifyOperation(t, "katana", "", cases) } func TestLiveCoreFFUF(t *testing.T) { d := liveDeps(t) server := fixtureHTTP(t) words := filepath.Join(t.TempDir(), "words.txt") if err := os.WriteFile(words, []byte("admin\nmissing-fixture\n"), 0600); err != nil { t.Fatal(err) } c := liveCapability(t, "web.content.discover") // Substitute a two-entry fixture wordlist; production flags and parser remain. c.steps[0].Args = func(target string, r schema.RunRequest) []string { return replaceArgument(ffufArgs(target, r), "-w", words) } result, err := c.Run(context.Background(), liveRequest(server.URL), d) if err != nil { t.Fatal(err) } if len(result.Endpoints) != 1 || result.Endpoints[0].URL != server.URL+"/admin" || result.Endpoints[0].Status != 200 { t.Fatalf("unexpected normalized match: %+v", result.Endpoints) } validateFixtureResult(t, result) if err := os.WriteFile(words, []byte("missing-fixture\n"), 0600); err != nil { t.Fatal(err) } result, err = c.Run(context.Background(), liveRequest(server.URL), d) if err != nil || len(result.Errors) != 0 || len(result.Endpoints) != 0 { t.Fatalf("nonexistent path reported present: %+v %v", result, err) } requireToolFailure(t, d, "ffuf", replaceArgument(ffufArgs(server.URL, schema.RunRequest{}), "-w", filepath.Join(t.TempDir(), "missing-wordlist"))) certifyOperation(t, "ffuf", "web.content.discover", []operationCase{ passedCase("positive", "Fixture wordlist finds /admin with HTTP 200 and ignores HTTP 404."), passedCase("negative", "An all-missing wordlist produces zero endpoints."), passedCase("failure", "Real FFUF rejects a nonexistent wordlist."), }) } func TestLiveCoreNuclei(t *testing.T) { d := liveDeps(t) server := fixtureHTTP(t) template := filepath.Join(t.TempDir(), "fixture.yaml") content := `id: cybot-fixture-marker info: name: CYBOT fixture marker author: cybot severity: info http: - method: GET path: - "{{BaseURL}}/" matchers: - type: word words: - "cybot-fixture-marker" ` if err := os.WriteFile(template, []byte(content), 0600); err != nil { t.Fatal(err) } c := liveCapability(t, "web.scan") c.steps[0].Args = func(target string, r schema.RunRequest) []string { return replaceArgument(nucleiArgs(target, r), "-templates", template) } result, err := c.Run(context.Background(), liveRequest(server.URL), d) if err != nil { t.Fatal(err) } if len(result.Findings) != 1 || result.Findings[0].Severity != "info" { t.Fatalf("missing fixture finding: %+v", result) } validateFixtureResult(t, result) // A nonmatching response must not create a finding. result, err = c.Run(context.Background(), liveRequest(server.URL+"/missing-fixture"), d) if err != nil || len(result.Findings) != 0 { t.Fatalf("false positive: %+v %v", result, err) } requireToolFailure(t, d, "nuclei", replaceArgument(nucleiArgs(server.URL, schema.RunRequest{}), "-templates", filepath.Join(t.TempDir(), "missing-template.yaml"))) certifyOperation(t, "nuclei", "web.scan", []operationCase{ passedCase("positive", "Pinned scanner finds the local fixture template's marker."), passedCase("negative", "A nonmatching fixture response produces zero findings."), passedCase("failure", "Real Nuclei rejects a nonexistent template."), }) } func TestLiveCoreNucleiRetainsFindingAfterCancellation(t *testing.T) { d := liveDeps(t) ctx, cancel := context.WithCancel(context.Background()) defer cancel() var marker, blocked atomic.Int64 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/marker": marker.Add(1) fmt.Fprint(w, "cybot-partial-marker") case "/blocked": blocked.Add(1) // The first request already matched. Cancel the owned scanner while // its second request is still pending, allowing its JSONL to flush. time.AfterFunc(500*time.Millisecond, cancel) select { case <-r.Context().Done(): case <-ctx.Done(): } default: http.NotFound(w, r) } })) defer server.Close() template := filepath.Join(t.TempDir(), "partial.yaml") content := `id: cybot-partial-marker info: name: CYBOT partial fixture marker author: cybot severity: info http: - method: GET path: - "{{BaseURL}}/marker" matchers: - type: word words: - "cybot-partial-marker" - method: GET path: - "{{BaseURL}}/blocked" matchers: - type: word words: - "never-a-match" ` if err := os.WriteFile(template, []byte(content), 0600); err != nil { t.Fatal(err) } c := liveCapability(t, "web.scan") c.steps[0].Args = func(target string, req schema.RunRequest) []string { args := replaceArgument(nucleiArgs(target, req), "-templates", template) return append(args, "-timeout", "30", "-retries", "0") } d.Timeout = 20 * time.Second result, err := c.Run(ctx, liveRequest(server.URL), d) if err == nil || marker.Load() != 1 || blocked.Load() != 1 || result.Status != "partial" || len(result.Errors) == 0 || len(result.Findings) != 1 || len(result.Evidence) != 1 { t.Fatalf("cancelled scanner lost its real match or failure: marker=%d blocked=%d result=%+v err=%v", marker.Load(), blocked.Load(), result, err) } if result.Evidence[0].Metadata["execution_status"] != "failed" || !strings.Contains(result.Summary, "incomplete") { t.Fatal("cancelled scan advertises completion", result) } validateFixtureResult(t, result) } func TestLiveCoreNmap(t *testing.T) { d := liveDeps(t) server := fixtureHTTP(t) _, port, err := net.SplitHostPort(strings.TrimPrefix(server.URL, "http://")) if err != nil { t.Fatal(err) } tool, _ := d.Tools.Get("nmap") // A single fixture port exercises the actual Nmap XML contract without a broad scan. out, err := d.Runner.Run(context.Background(), execution.Spec{Tool: tool, Args: []string{"-sT", "-n", "-Pn", "-p", port, "-oX", "-", "127.0.0.1"}, Timeout: 20 * time.Second}) if err != nil { t.Fatal(err) } result, err := parseNmap(out.Stdout, liveRequest("127.0.0.1")) if err != nil || len(result.Services) != 1 { t.Fatalf("port missing: %+v %v", result, err) } validateFixtureResult(t, result) server.Close() out, err = d.Runner.Run(context.Background(), execution.Spec{Tool: tool, Args: []string{"-sT", "-n", "-Pn", "-p", port, "-oX", "-", "127.0.0.1"}, Timeout: 20 * time.Second}) if err != nil { t.Fatal(err) } result, err = parseNmap(out.Stdout, liveRequest("127.0.0.1")) if err != nil || len(result.Services) != 0 { t.Fatalf("closed fixture port reported open: %+v %v", result, err) } requireToolFailure(t, d, "nmap", []string{"--cybot-invalid-fixture-option"}) certifyOperation(t, "nmap", "", []operationCase{ passedCase("positive", "Connect scan finds exactly the open loopback fixture port in Nmap XML."), passedCase("negative", "Closed fixture port is absent from normalized open services."), passedCase("failure", "Real Nmap rejects an invalid invocation."), }) } func TestLiveCoreDNS(t *testing.T) { d := liveDeps(t) conn, err := net.ListenPacket("udp", "127.0.0.1:0") if err != nil { t.Fatal(err) } defer conn.Close() var unexpected atomic.Bool var negatives atomic.Int32 go func() { buf := make([]byte, 4096) for { n, addr, e := conn.ReadFrom(buf) if e != nil { return } q := append([]byte(nil), buf[:n]...) if n < 17 { continue } end := 12 for end < n && q[end] != 0 { end += int(q[end]) + 1 } end += 5 if end > n { continue } typ := binary.BigEndian.Uint16(q[end-4 : end-2]) if typ != 1 && typ != 28 { unexpected.Store(true) } label := string(q[13 : 13+int(q[12])]) answer := append([]byte(nil), q[:end]...) answer[2] = 0x81 answer[3] = 0x80 answer[6] = 0 answer[7] = 0 switch label { case "missing": answer[3] = 0x83 // NXDOMAIN negatives.Add(1) case "failed": answer[3] = 0x82 // SERVFAIL case "empty": // NOERROR with no A/AAAA records. default: if typ == 1 { answer[7] = 1 answer = append(answer, 0xc0, 0x0c, 0, 1, 0, 1, 0, 0, 0, 60, 0, 4, 127, 0, 0, 1) } } _, _ = conn.WriteTo(answer, addr) } }() c := liveCapability(t, "dns.analyse") c.steps[0].Args = func(target string, r schema.RunRequest) []string { return append(dnsxArgs(target, r), "-resolver", conn.LocalAddr().String(), "-retry", "1", "-timeout", "1s") } result, err := c.Run(context.Background(), liveRequest("fixture.invalid"), d) if err != nil { t.Fatal(err) } found := false for _, a := range result.Assets { found = found || a.Value == "127.0.0.1" } if !found { t.Fatalf("DNS response lost: %+v", result) } validateFixtureResult(t, result) for _, host := range []string{"missing.fixture.invalid", "empty.fixture.invalid"} { result, err = c.Run(context.Background(), liveRequest(host), d) if err != nil || len(result.Errors) != 0 || len(result.Assets) != 0 || len(result.Evidence) != 1 || len(result.Observations) == 0 { t.Fatalf("definitive DNS absence was mishandled: %+v %v", result, err) } } if negatives.Load() == 0 || unexpected.Load() { t.Fatal("negative DNS query was skipped or expanded beyond A/AAAA") } result, err = c.Run(context.Background(), liveRequest("failed.fixture.invalid"), d) if err == nil && result.Status != "partial" || len(result.Errors) == 0 || len(result.Assets) != 0 { t.Fatalf("SERVFAIL was accepted as clean: %+v %v", result, err) } conn.Close() result, err = c.Run(context.Background(), liveRequest("fixture.invalid"), d) if err == nil && result.Status != "partial" || len(result.Errors) == 0 { t.Fatalf("unavailable resolver was accepted as clean: %+v %v", result, err) } certifyOperation(t, "dnsx", "dns.analyse", []operationCase{ passedCase("positive", "Real DNSx resolves loopback A records through a synthetic UDP resolver using only A/AAAA questions."), passedCase("negative", "NXDOMAIN and NOERROR without address records produce observations and zero invented assets."), passedCase("failure", "SERVFAIL and an unavailable resolver produce partial/error results rather than clean assessments."), }) } func TestLiveCorePortCapability(t *testing.T) { d := liveDeps(t) server := fixtureHTTP(t) _, port, err := net.SplitHostPort(strings.TrimPrefix(server.URL, "http://")) if err != nil { t.Fatal(err) } c := liveCapability(t, "port.scan") singlePort := func(args []string, flag string) []string { for i := 0; i < len(args)-1; i++ { if args[i] == flag { args[i], args[i+1] = "-p", port return args } } t.Fatal("production port selection flag missing", flag) return nil } c.steps[0].Args = func(target string, req schema.RunRequest) []string { return singlePort(nmapArgs(target, req), "--top-ports") } c.steps[1].Args = func(target string, req schema.RunRequest) []string { return singlePort(naabuArgs(target, req), "-top-ports") } result, err := c.Run(context.Background(), liveRequest("127.0.0.1"), d) if err != nil || len(result.Errors) != 0 || len(result.Evidence) != 2 || len(result.Services) == 0 { t.Fatalf("combined scanner capability missed local service: %+v %v", result, err) } for _, service := range result.Services { if fmt.Sprint(service.Port) != port || service.Host != "127.0.0.1" { t.Fatalf("scanner expanded beyond fixture scope: %+v", result.Services) } } validateFixtureResult(t, result) server.Close() result, err = c.Run(context.Background(), liveRequest("127.0.0.1"), d) if err != nil || len(result.Errors) != 0 || len(result.Services) != 0 || len(result.Evidence) != 2 { t.Fatalf("closed port was reported open: %+v %v", result, err) } c.steps[0].Args = func(string, schema.RunRequest) []string { return []string{"--cybot-invalid-fixture-option"} } result, err = c.Run(context.Background(), liveRequest("127.0.0.1"), d) if err == nil && result.Status != "partial" || len(result.Errors) == 0 { t.Fatalf("failed corroborating scanner was reported complete: %+v %v", result, err) } certifyCapability(t, "nmap", "port.scan", []operationCase{ passedCase("positive", "Both real scanners complete the full capability against one loopback service, with production parser and service/script flags."), passedCase("negative", "Both scanners report zero open services after the fixture port closes."), passedCase("failure", "A real Nmap execution failure remains a partial/error result even when the other scanner completes."), }) } func TestLiveCoreSubfinderArguments(t *testing.T) { _ = liveDeps(t) args := append(subfinderArgs("fixture.invalid", schema.RunRequest{}), "-list-sources") ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() out, err := exec.CommandContext(ctx, "subfinder", args...).CombinedOutput() if err != nil || !strings.Contains(strings.ToLower(string(out)), "crtsh") { t.Fatalf("subfinder flags/provider list invalid: %v %s", err, out) } // This is offline startup/argument validation, not an online provider test. } func TestLiveCoreCLIPersistence(t *testing.T) { _ = liveDeps(t) server := fixtureHTTP(t) dir := t.TempDir() config := filepath.Join(dir, "config.yaml") if err := os.WriteFile(config, []byte("data_dir: "+filepath.Join(dir, "state")+"\npacks:\n auto_update: false\n"), 0600); err != nil { t.Fatal(err) } cli := func(args ...string) []byte { t.Helper() ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second) defer cancel() cmd := exec.CommandContext(ctx, "cybot", append([]string{"--config", config}, args...)...) var diagnostics strings.Builder cmd.Stderr = &diagnostics out, err := cmd.Output() if err != nil { t.Fatalf("CLI %v: %v %s", args, err, diagnostics.String()) } return out } var result schema.Result if err := json.Unmarshal(cli("run", "web.map", "--target", server.URL, "--approve"), &result); err != nil { t.Fatal(err) } if result.Status != "completed" || result.RunID == "" { t.Fatalf("failed CLI result: %+v", result) } if !strings.Contains(string(cli("history", "list", "--limit", "1")), result.RunID) { t.Fatal("history missing run") } if !strings.Contains(string(cli("report", "generate", result.RunID, "--format", "markdown")), result.RunID) { t.Fatal("report missing run") } // Missing specialist dependencies must be visible instead of false success. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() err := exec.CommandContext(ctx, "cybot", "--config", config, "run", "code.audit", "--target", dir, "--approve").Run() if err == nil { t.Fatal("missing code-analysis tools reported success") } } func TestLiveCoreReadiness(t *testing.T) { _ = liveDeps(t) dir := t.TempDir() config := filepath.Join(dir, "config.yaml") if err := os.WriteFile(config, []byte("data_dir: "+filepath.Join(dir, "state")+"\npacks:\n auto_update: false\n"), 0600); err != nil { t.Fatal(err) } ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() out, err := exec.CommandContext(ctx, "cybot", "--config", config, "capability", "readiness").Output() if err != nil { t.Fatal(err) } var report Readiness if err := json.Unmarshal(out, &report); err != nil { t.Fatal(err) } if len(report.Tools) != 84 || len(report.Capabilities) != 71 || report.Counts["tools_available"] < 11 { t.Fatalf("unexpected inventory: %+v", report.Counts) } found := false for _, c := range report.Capabilities { if c.ID == "exploit.execute" { found = true if c.Status != "not-implemented" { t.Fatalf("help-only adapter advertised as %s", c.Status) } } } if !found { t.Fatal("capability absent from readiness report") } }