Files
urutau/internal/store/review_test.go
T
Lucas RayanandClaude Opus 5.5 b4a2c2c9ec
Release / Plan (reuse the published image?) (push) Successful in 4s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 1m29s
CI / Tests (Linux, race detector) (push) Failing after 1m48s
CI / Dependency audit (push) Successful in 21s
Release / ci (push) Failing after 1m50s
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Skipped
Release / Gitea release (push) Skipped
feat: Urutau 1.0.0, lightweight threat intelligence and web analytics
Always-on Go service (stdlib only) that follows the Traefik JSON access
log, the sshd log and the fail2ban log of a host, classifies attacks,
scores sources, keeps bounded aggregates with snapshots, and serves an
embedded dashboard with live updates, offline geolocation (DB-IP Lite),
privacy-friendly traffic analytics, an exportable blocklist and
token-authenticated federation between instances.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 16:13:34 -03:00

195 lines
6.9 KiB
Go

package store
import (
"fmt"
"path/filepath"
"testing"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
)
func hostile(s *Store, ip string) bool {
for _, x := range s.AllIPs(ParseRange("7d"), false) {
if x.IP == ip && x.Level == core.LevelHostile {
return true
}
}
return false
}
// A framed browser never reaches hostile; a scanner forging the same headers
// does not disappear either: it is recorded, capped, and shows as suspicious.
func TestCrossSiteFramedRequestsNeverScore(t *testing.T) {
const chrome = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0 Safari/537.36"
line := func(ip, extra string) core.Event {
l := fmt.Sprintf(`{"ClientHost":%q,"RequestHost":"a.example","RequestMethod":"GET","RequestPath":"/etc/passwd","DownstreamStatus":404,"StartUTC":%q,"request_User-Agent":%q%s}`,
ip, t0.Add(-time.Minute).Format(time.RFC3339), chrome, extra)
e, err := core.ParseTraefik([]byte(l))
if err != nil {
t.Fatal(err)
}
return e
}
s := newStore("x", 100)
for i := 0; i < 7; i++ {
s.Ingest(line("8.8.4.4", `,"request_Sec-Fetch-Site":"cross-site","request_Sec-Fetch-Dest":"image","request_Referer":"https://evil.example/"`))
s.Ingest(line("8.8.8.8", ""))
}
if hostile(s, "8.8.4.4") {
t.Fatal("a framed browser must never become hostile")
}
// a scanner forging the headers on 1000 exploits: still visible, capped below hostile
for i := 0; i < 1000; i++ {
s.Ingest(line("9.9.9.9", `,"request_Sec-Fetch-Site":"same-site"`))
}
d, ok := s.IP("9.9.9.9")
if !ok || hostile(s, "9.9.9.9") || d.Strong || d.Score > framedCap || d.Level != core.LevelSuspicious {
t.Fatalf("forged framing must be recorded as suspicious, never hidden nor hostile: ok=%v %+v", ok, d)
}
if !hostile(s, "8.8.8.8") {
d, _ := s.IP("8.8.8.8")
t.Fatalf("without Sec-Fetch headers it scores as before: %+v", d)
}
if n := s.Summary("24h").Totals.Counts["exploit"]; n != 14+1000 {
t.Fatalf("both count in the timeline: %d", n)
}
}
func TestOldSnapshotDoesNotLoseDomains(t *testing.T) {
s := newStore("x", 100)
for i := 0; i < 5; i++ {
s.Traffic(core.Event{Time: t0.Add(-time.Hour), Source: "traefik", Domain: "real.example.com", Method: "GET", Path: "/", Status: 200, IP: "2.2.2.2"})
}
for _, d := range s.tr.dom {
d.Last = 0 // what gob decodes from a snapshot of the previous version
}
p := filepath.Join(t.TempDir(), "s.gob")
if err := s.Save(p, nil); err != nil {
t.Fatal(err)
}
b := newStore("x", 100)
if _, err := b.Load(p); err != nil {
t.Fatal(err)
}
if d := b.tr.dom["real.example.com"]; d == nil || d.Last < t0.Add(-time.Hour).Unix() {
t.Fatalf("Last not migrated: %+v", d)
}
b.Prune()
if len(b.tr.dom) != 1 {
t.Fatal("first Prune after an upgrade dropped the domain")
}
}
func TestRedirectSprayDoesNotTakeDomainSlots(t *testing.T) {
s := newStore("x", 100)
for i := 0; i < 40; i++ {
s.Traffic(core.Event{Time: t0, Source: "traefik", Domain: fmt.Sprintf("junk%d.x", i), Method: "GET", Path: "/", Status: 301, IP: "2.2.2.2"})
}
s.Traffic(core.Event{Time: t0, Source: "traefik", Domain: "real.example.com", Method: "GET", Path: "/", Status: 200, IP: "2.2.2.2"})
if _, ok := s.tr.dom["real.example.com"]; !ok || len(s.tr.dom) != 1 {
t.Fatalf("slots: %d", len(s.tr.dom))
}
}
func strongExploit(ip string, at time.Time) core.Event {
e := ev(ip, core.Exploit, 0)
e.Time, e.Path, e.Strong = at, "/etc/passwd", true
return e
}
func TestAdminImmunityIsBoundedAndCancelled(t *testing.T) {
s := newStore("x", 100)
s.SetAdminUsers([]string{"root"})
// strong attacks after the login cancel the immunity
s.Ingest(core.Event{Time: t0.Add(-2 * time.Hour), IP: "9.9.9.1", Cat: core.SSHOK, Source: "ssh", User: "root"})
for i := 0; i < 20; i++ {
s.Ingest(strongExploit("9.9.9.1", t0.Add(-time.Hour)))
}
if d, _ := s.IP("9.9.9.1"); d.Admin || !hostile(s, "9.9.9.1") {
t.Fatalf("strong attacks after ssh-ok: %+v", d)
}
// strong attacks before the login keep it
for i := 0; i < 20; i++ {
s.Ingest(strongExploit("9.9.9.2", t0.Add(-3*time.Hour)))
}
s.Ingest(core.Event{Time: t0.Add(-2 * time.Hour), IP: "9.9.9.2", Cat: core.SSHOK, Source: "ssh", User: "root"})
if d, _ := s.IP("9.9.9.2"); !d.Admin || d.Level != core.LevelLow {
t.Fatalf("login after the attacks: %+v", d)
}
// a login 25 h ago is no longer immune
s.Ingest(core.Event{Time: t0.Add(-25 * time.Hour), IP: "9.9.9.3", Cat: core.SSHOK, Source: "ssh", User: "root"})
for i := 0; i < 40; i++ {
s.Ingest(core.Event{Time: t0, IP: "9.9.9.3", Cat: core.SSHFail, Source: "ssh", User: "x"})
s.Ingest(core.Event{Time: t0, IP: "9.9.9.3", Cat: core.SSHProbe, Source: "ssh"})
}
if d, _ := s.IP("9.9.9.3"); d.Admin || !hostile(s, "9.9.9.3") {
t.Fatalf("ssh-ok 25 h ago: %+v", d)
}
}
func TestAdminUsersLimitTheImmunity(t *testing.T) {
s := newStore("x", 100)
s.SetAdminUsers([]string{"lucas", " "})
s.Ingest(core.Event{Time: t0.Add(-time.Hour), IP: "9.9.9.4", Cat: core.SSHOK, Source: "ssh", User: "git"})
s.Ingest(core.Event{Time: t0.Add(-time.Hour), IP: "9.9.9.5", Cat: core.SSHOK, Source: "ssh", User: "lucas"})
a, _ := s.IP("9.9.9.4")
b, _ := s.IP("9.9.9.5")
if a.Admin || !b.Admin {
t.Fatalf("admin users: git=%v lucas=%v", a.Admin, b.Admin)
}
}
func TestPeerCannotGrantAdminOrCheapStrong(t *testing.T) {
e := PeerExport{V: ExportVersion}
e.Summary.IPs = []IPSummary{
{IP: "1.2.3.4", Score: 999, Strong: true, Admin: true},
{IP: "1.2.3.5", Score: 10, Strong: true},
}
if err := e.Sanitize("24h"); err != nil {
t.Fatal(err)
}
got := map[string]IPSummary{}
for _, x := range e.Summary.IPs {
got[x.IP] = x
}
if got["1.2.3.4"].Admin || got["1.2.3.4"].Level != core.LevelHostile {
t.Fatalf("peer admin: %+v", got["1.2.3.4"])
}
if got["1.2.3.5"].Strong {
t.Fatalf("low score peer strong: %+v", got["1.2.3.5"])
}
// merged with a local score, the peer's cheap strong no longer tips it to hostile
m := MergeIPs([]IPSummary{{IP: "1.2.3.5", Score: 45}}, []IPSummary{got["1.2.3.5"]})
if m[0].Level == core.LevelHostile {
t.Fatalf("merge: %+v", m[0])
}
}
// Without --admin-users nobody is immune: an sshd "Accepted" line can be forged
// by a local user with logger.
func TestNoAdminImmunityByDefault(t *testing.T) {
s := newStore("x", 100)
s.Ingest(core.Event{Time: t0.Add(-2 * time.Hour), IP: "9.9.9.7", Cat: core.SSHOK, Source: "ssh", User: "root"})
for i := 0; i < 20; i++ {
s.Ingest(strongExploit("9.9.9.7", t0.Add(-3*time.Hour)))
}
if d, _ := s.IP("9.9.9.7"); d.Admin || !hostile(s, "9.9.9.7") {
t.Fatalf("no admin list means no immunity: %+v", d)
}
}
// A login in the same second as a strong attack does not shield it.
func TestAdminTieGoesToTheAttack(t *testing.T) {
s := newStore("x", 100)
s.SetAdminUsers([]string{"root"})
at := t0.Add(-time.Hour)
for i := 0; i < 20; i++ {
s.Ingest(strongExploit("9.9.9.8", at))
}
s.Ingest(core.Event{Time: at, IP: "9.9.9.8", Cat: core.SSHOK, Source: "ssh", User: "root"})
if d, _ := s.IP("9.9.9.8"); d.Admin {
t.Fatalf("a tie must not grant immunity: %+v", d)
}
}