package thintest import ( "slices" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) // TestResearchFetchAuditKept (Astra security finding 5): a correctly // refused target that fails the URL contract (an IP literal, a .ts.net // redirect, raw bytes) is kept as escaped audit text with its reason, // beside the valid lines, and the findings stand; a full five-redirect // chain is kept, each hop with its address and status. A refused target // is no source findings may cite. func TestResearchFetchAuditKept(t *testing.T) { chain := []wire.ResearchHop{} for i := range wire.MaxResearchHops { chain = append(chain, wire.ResearchHop{URL: "https://shop.example.com/r" + itoa(i+1), Address: "81.2.69." + itoa(50+i), Status: 301}) } chain[len(chain)-1].Status = 200 log := []wire.ResearchFetch{ {SourceID: "s1", Origin: wire.ResearchOriginPerson, URL: "https://shop.example.com/", Address: "81.2.69.50", Status: 200, Type: "text/html", Bytes: 5120, Millis: 120}, {SourceID: "s2", Origin: wire.ResearchOriginPerson, URL: "https://shop.example.com/hours", Hops: chain, Address: "81.2.69.54", Status: 200, Type: "text/html", Bytes: 2048, Millis: 80}, {SourceID: "s3", Origin: "expanded", Parent: "s1", URL: "http://10.0.0.1/logo.png", Millis: 1, Reason: "ip_literal"}, {SourceID: "s4", Origin: "expanded", Parent: "s1", URL: "https://shop.example.com/team", Hops: []wire.ResearchHop{{URL: "https://box.tailnet-example.ts.net/"}}, Address: "81.2.69.50", Status: 302, Millis: 3, Reason: "host_refused"}, {SourceID: "s5", Origin: "expanded", Parent: "s1", URL: "https://exa\u202emple.com/a b\x01", Millis: 1, Reason: "host_refused"}, } r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) r.researchOn() r.inventoryOn() d := r.runningResearch("au") f := goodFindings() r.step("au-done", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: 1, State: wire.StepDone, Termination: true, Research: &wire.ResearchResult{Findings: findingsJSON(t, f), Fetches: log, Images: []wire.ResearchImage{}}}) if e := r.nextDispatch(); e.Specialist != wire.SpecialistEngineering { t.Fatalf("Engineering after Research %+v", e) } s := r.newest(d.TaskID, wire.SpecialistResearch) v := s.Research if v == nil || v.Findings.Summary != f.Summary || len(v.Fetches) != 5 { t.Fatalf("read shape %+v", v) } if got := v.Fetches[1].Hops; !slices.Equal(got, chain) { t.Fatalf("redirect chain %+v", got) } if l := v.Fetches[2]; l.URL != "http://10.0.0.1/logo.png" || l.Reason != "ip_literal" { t.Fatalf("IP literal %+v", l) } if l := v.Fetches[3]; len(l.Hops) != 1 || l.Hops[0].URL != "https://box.tailnet-example.ts.net/" || l.Reason != "host_refused" || l.URL != "https://shop.example.com/team" { t.Fatalf(".ts.net redirect %+v", l) } if l := v.Fetches[4]; l.URL != "https://exa%E2%80%AEmple.com/a%20b%01" { t.Fatalf("escaped audit text %q", l.URL) } // Findings citing a refused target do not check. r2 := newToolRigSaying(t, wire.ProtocolCurrent, personWords) r2.researchOn() r2.inventoryOn() d2 := r2.runningResearch("ab") bad := goodFindings() bad.Facts[0].Sources = []string{"s3"} r2.step("ab-done", wire.StepReport{TaskID: d2.TaskID, StepID: d2.StepID, Attempt: 1, State: wire.StepDone, Termination: true, Research: &wire.ResearchResult{Findings: findingsJSON(t, bad), Fetches: log, Images: []wire.ResearchImage{}}}) if s := r2.newest(d2.TaskID, wire.SpecialistResearch); s.Research == nil || !slices.Equal(s.Research.Findings.Gaps, []string{wire.ResearchNoFindingsGap}) || len(s.Research.Fetches) != 5 { t.Fatalf("findings citing a refused target %+v", s.Research) } } // TestResearchStoppedKeepsFetchLog (Astra security finding 5): an // attempt that fails after fetching (the model failed), or is cancelled, // keeps its fetch log, shown on the task with no findings; Engineering // goes on. func TestResearchStoppedKeepsFetchLog(t *testing.T) { for _, state := range []string{wire.StepFailed, wire.StepCancelled} { t.Run(state, func(t *testing.T) { r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) r.researchOn() r.inventoryOn() d := r.runningResearch("st") r.step("st-end", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: 1, State: state, Reason: "provider_failed", Termination: true, Research: &wire.ResearchResult{Fetches: fetchLog(), Images: []wire.ResearchImage{}}}) if e := r.nextDispatch(); e.Specialist != wire.SpecialistEngineering || e.ResearchNote != nil { t.Fatalf("Engineering after Research %+v", e) } if n := r.rows("SELECT count(*) FROM chat_research_fetches WHERE step_id = ? AND attempt = 1", d.StepID); n != 4 { t.Fatalf("%d fetch lines", n) } if n := r.rows("SELECT count(*) FROM chat_research WHERE step_id = ?", d.StepID); n != 0 { t.Fatalf("findings stored for a %s step", state) } s := r.newest(d.TaskID, wire.SpecialistResearch) if s.State != state || s.Research == nil || len(s.Research.Fetches) != 4 || s.Research.Findings.Summary != "" || len(s.Research.Findings.Facts) != 0 { t.Fatalf("read shape %+v %+v", s, s.Research) } page := (r.get(r.inst.SessionFor(t, Owner), "/tasks/"+d.TaskID, 200)) if !strings.Contains(page, "data-fetch-log") || !strings.Contains(page, "https://cdn.example.net/x.webp") { t.Fatal("the page does not show the fetch log") } }) } }