Files
Lucas RayanandClaude Opus 5.5 9bdfdb491b feat: report preview page, sharp report logo, time to ban by blocker
The Reports page now previews the HTML report itself (same document as the
download and the print), with only Download and Print. The report logo is
the repository logo at 800 px. Survival splits fail2ban and the Urutau
second firewall.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 00:06:29 -03:00

124 lines
6.3 KiB
Go

package server
import (
"os"
"strings"
"testing"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/egress"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/enforce"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/intel"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
)
func richReportInput(lang string) reportInput {
var in reportInput
in.Lang = lang
o := &in.Out
o.Range = store.RangeInfo{Name: "7d", From: 1763000000, To: 1763604800, Step: 3600}
o.Hosts = []string{"web-01.example.com", "web-02.example.com"}
o.Summary.reportFigures = reportFigures{Requests: 1843210, Attacks: 48211, UniqueIPs: 912, HostileIPs: 143, Bans: 97, Visitors: 5120, NonHuman: 733, PageViews: 18430}
o.Summary.Prev = reportFigures{Requests: 1500000, Attacks: 30100, UniqueIPs: 700, HostileIPs: 110, Bans: 90, Visitors: 4800, PageViews: 17000}
o.Summary.HasPrev = true
o.Summary.DeltaPct = map[string]float64{"requests": 22.9, "attacks": 60.2, "hostile_ips": 30, "bans": 7.7, "visitors": 6.7}
o.Timeline.Start, o.Timeline.Step = o.Range.From, 3600
for i := 0; i < 168; i++ {
o.Timeline.Values = append(o.Timeline.Values, uint32(200+(i*37)%400+(i%24)*10))
}
o.Families = []store.Count{{Key: "scanner", N: 21000}, {Key: "exploit", N: 14000}, {Key: "bruteforce", N: 8000}, {Key: "ssh-fail", N: 4000}, {Key: "ssh-probe", N: 1211}}
o.Techniques = []store.Count{{Key: "T1190", N: 9000, Label: "Exploit Public-Facing Application"}, {Key: "T1110", N: 4000, Label: "Brute Force"}, {Key: "T1595", N: 3000, Label: "Active Scanning"}}
o.Countries = []store.Count{{Key: "NL", N: 9000}, {Key: "US", N: 8000}, {Key: "DE", N: 5000}, {Key: "RU", N: 3000}, {Key: "BR", N: 900}}
o.ASNs = []store.Count{{Key: "AS64496", N: 9000, Label: "Example Hosting <b>Ltd</b>"}, {Key: "AS64497", N: 5000, Label: "Documentation Net"}}
for i := 0; i < 10; i++ {
o.TopIPs = append(o.TopIPs, reportTopIP{IP: "198.51.100." + string(rune('1'+i)), Score: 92.5 - float64(i), Level: "hostile", CC: "NL", Country: "Netherlands", ASOrg: "Example Hosting", Events: uint32(4000 - i*300), Banned: i%2 == 0})
}
o.Campaigns = []intel.Campaign{{ID: "camp-1", IPsN: 12, First: time.Unix(1763100000, 0), Last: time.Unix(1763500000, 0), Techs: []string{"T1190", "T1595"}}}
o.Escalations = []store.EscalationOut{{IP: "203.0.113.7", Host: "web-01.example.com", Stage: "exploit"}}
o.Exposures.Total = 3
for i := 0; i < 3; i++ {
o.Exposures.Items = append(o.Exposures.Items, reportExposure{T: 1763200000 + int64(i)*600, IP: "203.0.113.7", Domain: "app.example.com", Path: "/<script>alert(1)</script>?x=" + strings.Repeat("a", 120), Status: 200, Fam: "exploit"})
}
o.Defense.MTTBN, o.Defense.MTTBMedianS, o.Defense.MTTBP90S, o.Defense.BanCoverage = 40, 95, 700, 67.8
o.Defense.BlocklistCover, o.Defense.ExposureRate, o.Defense.ExploitsRefused = 31, 0.4, 99
in.Traffic = store.TrafficData{Visitors: 5120, Pages: 18430, NonHuman: 733, P50: 42, P95: 380, Status: [4]uint64{1500000, 200000, 140000, 3210},
TopPages: []store.Count{{Key: "/", N: 9000}, {Key: "/docs", N: 3000}}, Browsers: []store.Count{{Key: "Firefox", N: 3000}, {Key: "Chrome", N: 2000}},
Devices: []store.Count{{Key: "Desktop", N: 4000}, {Key: "Mobile", N: 1100}}, Referrers: []store.Count{{Key: "example.org", N: 400}}}
in.HasHealth = true
in.Classes = [store.NumCls]uint32{store.ClsApp: 120, store.ClsProxy: 12, store.ClsBlocked: 30000, store.ClsAttack: 9000, store.ClsBot: 4000, store.ClsClientBroken: 900, store.ClsClientAuth: 300, store.ClsClientOther: 50}
in.Enforce = &enforceView{Mode: enforce.ModeOn, Coverage: enforce.Coverage{AttackIPs: 143, BlockedByFirewall: 80, BlockedByUrutau: 20, Unblocked: 43},
Counts: enforce.Counts{Decided: 400, Written: 380, AlreadyBlocked: 20},
Recent: []enforce.Recent{{T: 1763300000, IP: "198.51.100.4", Reason: "exploit x12", Action: "ban", CC: "NL"}}}
in.Egress = &egressView{Source: egress.SourceInfo{State: "ok"}, Report: egress.Report{
Workloads: []egress.WorkloadOut{{Name: "app-1", Role: "web", Conns: 12000, Dsts: 14}, {Name: "db-1", Role: "db", Conns: 800, Dsts: 2}},
TopDst: []egress.DstOut{{IP: "192.0.2.10", CC: "US", Conns: 5000}},
Findings: []egress.Finding{{Severity: "high", Kind: "beacon", Workload: "app-1", Dst: "192.0.2.99", DPort: 4444, Proto: "tcp", Detail: "<img src=x onerror=1> periodic"}}}}
return in
}
func TestReportHTMLRenders(t *testing.T) {
for _, lang := range []string{"pt-BR", "en"} {
b, err := renderReportHTML(richReportInput(lang), 1763604800)
if err != nil {
t.Fatal(err)
}
s := string(b)
if strings.Contains(s, "<script") || strings.Contains(s, "<img src=x") || strings.Contains(s, "<b>Ltd") {
t.Fatalf("%s: unescaped attacker data", lang)
}
if !strings.Contains(s, "&lt;script&gt;") || !strings.Contains(s, "data:image/webp;base64,") {
t.Fatalf("%s: missing escaped payload or logo", lang)
}
if len(b) > 400<<10 {
t.Fatalf("%s: %d bytes", lang, len(b))
}
want := "Segundo firewall"
if lang == "en" {
want = "Second firewall"
}
if !strings.Contains(s, want) {
t.Fatalf("%s: no %q", lang, want)
}
if dir := os.Getenv("URUTAU_REPORT_OUT"); dir != "" {
_ = os.WriteFile(dir+"/rich-"+lang+".html", b, 0o600)
}
}
}
func TestReportHTMLEmpty(t *testing.T) {
for _, lang := range []string{"", "en", "xx"} {
b, err := renderReportHTML(reportInput{Lang: lang}, 1763604800)
if err != nil || len(b) == 0 {
t.Fatal(err)
}
if dir := os.Getenv("URUTAU_REPORT_OUT"); dir != "" && lang == "" {
_ = os.WriteFile(dir+"/empty.html", b, 0o600)
}
}
}
func TestReportHTMLRoute(t *testing.T) {
h, _ := setup(t)
w := do(h, "GET", "/api/v1/report?range=7d&format=html&lang=en", copyHdr(opsAuth), nil)
if w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
if ct := w.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/html") {
t.Fatal(ct)
}
cd := w.Header().Get("Content-Disposition")
if !strings.HasPrefix(cd, `attachment; filename="urutau-relatorio-oracle-`) || !strings.HasSuffix(cd, `.html"`) {
t.Fatal(cd)
}
body := w.Body.String()
if strings.Contains(body, "<img src=x") {
t.Fatal("path not escaped")
}
if dir := os.Getenv("URUTAU_REPORT_OUT"); dir != "" {
_ = os.WriteFile(dir+"/route.html", w.Body.Bytes(), 0o600)
}
if w := do(h, "GET", "/api/v1/report?format=html&lang=pt-BR", nil, nil); w.Code != 401 && w.Code != 403 {
t.Fatalf("closed without token: %d", w.Code)
}
}