| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "slices" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // TestResearchFetchAuditKept (Astra security finding 5): a correctly |
| 12 | // refused target that fails the URL contract (an IP literal, a .ts.net |
| 13 | // redirect, raw bytes) is kept as escaped audit text with its reason, |
| 14 | // beside the valid lines, and the findings stand; a full five-redirect |
| 15 | // chain is kept, each hop with its address and status. A refused target |
| 16 | // is no source findings may cite. |
| 17 | func TestResearchFetchAuditKept(t *testing.T) { |
| 18 | chain := []wire.ResearchHop{} |
| 19 | for i := range wire.MaxResearchHops { |
| 20 | chain = append(chain, wire.ResearchHop{URL: "https://shop.example.com/r" + itoa(i+1), Address: "81.2.69." + itoa(50+i), Status: 301}) |
| 21 | } |
| 22 | chain[len(chain)-1].Status = 200 |
| 23 | log := []wire.ResearchFetch{ |
| 24 | {SourceID: "s1", Origin: wire.ResearchOriginPerson, URL: "https://shop.example.com/", Address: "81.2.69.50", Status: 200, Type: "text/html", Bytes: 5120, Millis: 120}, |
| 25 | {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}, |
| 26 | {SourceID: "s3", Origin: "expanded", Parent: "s1", URL: "http://10.0.0.1/logo.png", Millis: 1, Reason: "ip_literal"}, |
| 27 | {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"}, |
| 28 | {SourceID: "s5", Origin: "expanded", Parent: "s1", URL: "https://exa\u202emple.com/a b\x01", Millis: 1, Reason: "host_refused"}, |
| 29 | } |
| 30 | r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) |
| 31 | r.researchOn() |
| 32 | r.inventoryOn() |
| 33 | d := r.runningResearch("au") |
| 34 | f := goodFindings() |
| 35 | r.step("au-done", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: 1, State: wire.StepDone, Termination: true, |
| 36 | Research: &wire.ResearchResult{Findings: findingsJSON(t, f), Fetches: log, Images: []wire.ResearchImage{}}}) |
| 37 | if e := r.nextDispatch(); e.Specialist != wire.SpecialistEngineering { |
| 38 | t.Fatalf("Engineering after Research %+v", e) |
| 39 | } |
| 40 | s := r.newest(d.TaskID, wire.SpecialistResearch) |
| 41 | v := s.Research |
| 42 | if v == nil || v.Findings.Summary != f.Summary || len(v.Fetches) != 5 { |
| 43 | t.Fatalf("read shape %+v", v) |
| 44 | } |
| 45 | if got := v.Fetches[1].Hops; !slices.Equal(got, chain) { |
| 46 | t.Fatalf("redirect chain %+v", got) |
| 47 | } |
| 48 | if l := v.Fetches[2]; l.URL != "http://10.0.0.1/logo.png" || l.Reason != "ip_literal" { |
| 49 | t.Fatalf("IP literal %+v", l) |
| 50 | } |
| 51 | 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" { |
| 52 | t.Fatalf(".ts.net redirect %+v", l) |
| 53 | } |
| 54 | if l := v.Fetches[4]; l.URL != "https://exa%E2%80%AEmple.com/a%20b%01" { |
| 55 | t.Fatalf("escaped audit text %q", l.URL) |
| 56 | } |
| 57 | // Findings citing a refused target do not check. |
| 58 | r2 := newToolRigSaying(t, wire.ProtocolCurrent, personWords) |
| 59 | r2.researchOn() |
| 60 | r2.inventoryOn() |
| 61 | d2 := r2.runningResearch("ab") |
| 62 | bad := goodFindings() |
| 63 | bad.Facts[0].Sources = []string{"s3"} |
| 64 | r2.step("ab-done", wire.StepReport{TaskID: d2.TaskID, StepID: d2.StepID, Attempt: 1, State: wire.StepDone, Termination: true, |
| 65 | Research: &wire.ResearchResult{Findings: findingsJSON(t, bad), Fetches: log, Images: []wire.ResearchImage{}}}) |
| 66 | 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 { |
| 67 | t.Fatalf("findings citing a refused target %+v", s.Research) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // TestResearchStoppedKeepsFetchLog (Astra security finding 5): an |
| 72 | // attempt that fails after fetching (the model failed), or is cancelled, |
| 73 | // keeps its fetch log, shown on the task with no findings; Engineering |
| 74 | // goes on. |
| 75 | func TestResearchStoppedKeepsFetchLog(t *testing.T) { |
| 76 | for _, state := range []string{wire.StepFailed, wire.StepCancelled} { |
| 77 | t.Run(state, func(t *testing.T) { |
| 78 | r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) |
| 79 | r.researchOn() |
| 80 | r.inventoryOn() |
| 81 | d := r.runningResearch("st") |
| 82 | r.step("st-end", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: 1, State: state, Reason: "provider_failed", Termination: true, |
| 83 | Research: &wire.ResearchResult{Fetches: fetchLog(), Images: []wire.ResearchImage{}}}) |
| 84 | if e := r.nextDispatch(); e.Specialist != wire.SpecialistEngineering || e.ResearchNote != nil { |
| 85 | t.Fatalf("Engineering after Research %+v", e) |
| 86 | } |
| 87 | if n := r.rows("SELECT count(*) FROM chat_research_fetches WHERE step_id = ? AND attempt = 1", d.StepID); n != 4 { |
| 88 | t.Fatalf("%d fetch lines", n) |
| 89 | } |
| 90 | if n := r.rows("SELECT count(*) FROM chat_research WHERE step_id = ?", d.StepID); n != 0 { |
| 91 | t.Fatalf("findings stored for a %s step", state) |
| 92 | } |
| 93 | s := r.newest(d.TaskID, wire.SpecialistResearch) |
| 94 | if s.State != state || s.Research == nil || len(s.Research.Fetches) != 4 || s.Research.Findings.Summary != "" || len(s.Research.Findings.Facts) != 0 { |
| 95 | t.Fatalf("read shape %+v %+v", s, s.Research) |
| 96 | } |
| 97 | page := (r.get(r.inst.SessionFor(t, Owner), "/tasks/"+d.TaskID, 200)) |
| 98 | if !strings.Contains(page, "data-fetch-log") || !strings.Contains(page, "https://cdn.example.net/x.webp") { |
| 99 | t.Fatal("the page does not show the fetch log") |
| 100 | } |
| 101 | }) |
| 102 | } |
| 103 | } |
| 104 | |