Files
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

102 lines
3.2 KiB
Go

package server
import (
"net/http"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
)
// Once a client is blocked, parallel wrong attempts must not run at full
// speed: only one waits (progressively delayed), the others get 429 at once,
// and the correct token still opens the dashboard.
func TestBlockedClientIsSerializedAndAdminStillGetsIn(t *testing.T) {
old, oldMax := loginBlockedDelay, maxBlockedDelay
loginBlockedDelay, maxBlockedDelay = 40*time.Millisecond, 200*time.Millisecond
defer func() { loginBlockedDelay, maxBlockedDelay = old, oldMax }()
h, _ := setup(t)
auth := func(tok string) int {
return do(h, "GET", "/api/v1/summary", map[string]string{"Authorization": "Bearer " + tok, "X-Remote": "203.0.113.77:1"}, nil).Code
}
for i := 0; i < loginMax; i++ {
auth("wrong-guess")
}
var slow atomic.Int32
var wg sync.WaitGroup
start := time.Now()
for i := 0; i < 50; i++ {
wg.Add(1)
go func() {
defer wg.Done()
t0 := time.Now()
if c := auth("wrong-guess"); c != http.StatusTooManyRequests {
t.Errorf("blocked wrong attempt: %d", c)
}
if time.Since(t0) >= loginBlockedDelay/2 {
slow.Add(1)
}
}()
}
wg.Wait()
if n := slow.Load(); n > 3 {
t.Fatalf("%d parallel wrong attempts were evaluated slowly; only one at a time should wait", n)
}
if time.Since(start) > 5*time.Second {
t.Fatal("parallel attempts must not pile up delays")
}
if c := auth(utok); c != 200 {
t.Fatalf("the correct token must still be accepted while the client is blocked: %d", c)
}
}
func TestPenaltyGrowsAndIsCapped(t *testing.T) {
old := loginBlockedDelay
loginBlockedDelay = time.Second
defer func() { loginBlockedDelay = old }()
l := newLimiter()
var last time.Duration
for i := 0; i < loginMax+20; i++ {
l.failure("203.0.113.9")
p := l.penalty("203.0.113.9")
if p < last {
t.Fatalf("penalty must not shrink: %v after %v", p, last)
}
last = p
}
if last != maxBlockedDelay {
t.Fatalf("penalty cap: %v", last)
}
if n := len(l.fail[limiterKey("203.0.113.9")]); n > maxFailKept {
t.Fatalf("timestamps per client must be bounded: %d", n)
}
}
func TestBlocklistFamilyFilter(t *testing.T) {
st := store.New(store.Config{Name: "oracle", Version: "test"})
now := time.Now()
for i := 0; i < 30; i++ {
for _, ip := range []string{"45.94.31.4", "2a01:4f8:1:2::99"} {
st.Ingest(core.Event{Time: now, IP: ip, Cat: core.Exploit, Strong: true, Source: "traefik", Domain: "a.example", Method: "GET", Path: "/etc/passwd", Status: 404})
}
}
h := New(Options{Token: utok, Version: "test", Store: st})
get := func(q string) string {
return do(h, "GET", "/api/v1/blocklist.txt"+q, map[string]string{"Authorization": "Bearer " + utok}, nil).Body.String()
}
all, v4, v6 := get("?min_score=1"), get("?min_score=1&family=4"), get("?min_score=1&family=6")
if !strings.Contains(all, "45.94.31.4") || !strings.Contains(all, "::/64") {
t.Fatalf("default lists both families: %q", all)
}
if v4 != "45.94.31.4\n" {
t.Fatalf("family=4 must be plain IPv4 only (ipset hash:ip): %q", v4)
}
if strings.Contains(v6, "45.94.31.4") || !strings.Contains(v6, "::/64") {
t.Fatalf("family=6: %q", v6)
}
}