Release / Publish tags (push) Successful in 16s
Release / Plan (reuse the published image?) (push) Successful in 4s
Release / ci (push) Successful in 1m47s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 23s
CI / Format and vet (push) Successful in 27s
CI / Tests (Linux, race detector) (push) Successful in 1m20s
CI / Dependency audit (push) Successful in 31s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 5s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
182 lines
6.7 KiB
Go
182 lines
6.7 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/enforce"
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
|
|
)
|
|
|
|
func enforceRig(t *testing.T, share bool) (http.Handler, *enforce.Engine, string) {
|
|
t.Helper()
|
|
log := filepath.Join(t.TempDir(), "decisions.log")
|
|
st := store.New(store.Config{Name: "oracle", Version: "t", Retention: 720 * time.Hour})
|
|
e := enforce.New(enforce.Config{Mode: enforce.ModeOn, Path: log, Host: "oracle", Threshold: 3, Share: share})
|
|
st.SetIngestHook(e.Observe)
|
|
SetEnforce(e)
|
|
t.Cleanup(func() { SetEnforce(nil) })
|
|
return New(Options{Token: userTok, PeerTokens: []string{peerTok}, Version: "t", Store: st}), e, log
|
|
}
|
|
|
|
func TestEnforceAPI(t *testing.T) {
|
|
h, e, log := enforceRig(t, true)
|
|
auth := map[string]string{"Authorization": "Bearer " + userTok}
|
|
d := getJSON(t, h, "/api/v1/enforce?range=24h", auth)
|
|
if d["mode"] != "on" || d["share"] != "on" || d["threshold"].(float64) != 3 || d["log_writable"] != true {
|
|
t.Fatalf("%v", d)
|
|
}
|
|
for _, k := range []string{"coverage", "counts", "recent"} {
|
|
if d[k] == nil {
|
|
t.Fatalf("missing %s: %v", k, d)
|
|
}
|
|
}
|
|
if sk := d["counts"].(map[string]any)["skipped"].(map[string]any); len(sk) != 8 {
|
|
t.Fatalf("skipped %v", sk)
|
|
}
|
|
// feed events through the store hook and the engine worker
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
go e.Run(ctx)
|
|
e.Observe(core.Event{Time: time.Now(), IP: "203.0.114.5", Cat: core.Scanner, Source: "traefik", Method: "GET", Path: "/x", Status: 404, UA: "x"}, "")
|
|
for i := 0; i < 200 && e.Report(0).Coverage.AttackIPs == 0; i++ {
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
e.AcceptPeer("hostinger", []enforce.Wire{{T: time.Now().Unix(), IP: "203.0.114.5", Reason: "rce", Score: 90, TTL: 3600}})
|
|
d = getJSON(t, h, "/api/v1/enforce?range=24h", auth)
|
|
rec := d["recent"].([]any)
|
|
if len(rec) != 1 {
|
|
t.Fatalf("recent %v", d)
|
|
}
|
|
r := rec[0].(map[string]any)
|
|
if r["action"] != "ban" || r["src"] != "peer:hostinger" || r["ip"] != "203.0.114.5" {
|
|
t.Fatalf("%v", r)
|
|
}
|
|
if b, _ := os.ReadFile(log); !strings.Contains(string(b), "src=peer:hostinger") {
|
|
t.Fatalf("log %q", b)
|
|
}
|
|
if w := do(h, "GET", "/api/v1/enforce?range=24h", nil, nil); w.Code != 401 {
|
|
t.Fatalf("anonymous %d", w.Code)
|
|
}
|
|
if w := do(h, "GET", "/api/v1/enforce?range=24h", map[string]string{"Authorization": "Bearer " + peerTok}, nil); w.Code != 403 {
|
|
t.Fatalf("peer token %d", w.Code)
|
|
}
|
|
if w := do(h, "GET", "/api/v1/enforce?range=bad", cloneHdr(auth), nil); w.Code != 400 {
|
|
t.Fatalf("bad range %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestEnforceAPIOff(t *testing.T) {
|
|
st := store.New(store.Config{Name: "oracle", Version: "t"})
|
|
SetEnforce(nil)
|
|
h := New(Options{Token: userTok, Store: st})
|
|
d := getJSON(t, h, "/api/v1/enforce", map[string]string{"Authorization": "Bearer " + userTok})
|
|
if d["mode"] != "off" {
|
|
t.Fatalf("%v", d)
|
|
}
|
|
}
|
|
|
|
func post(h http.Handler, path, tok, body string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest("POST", path, strings.NewReader(body))
|
|
r.RemoteAddr = "198.51.100.1:1234"
|
|
r.Header.Set("Content-Type", "application/json")
|
|
if tok != "" {
|
|
r.Header.Set("Authorization", "Bearer "+tok)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
|
|
func TestPeerDecisionsEndpoints(t *testing.T) {
|
|
h, e, log := enforceRig(t, true)
|
|
good := `{"decisions":[{"t":` + strconv.FormatInt(time.Now().Unix(), 10) + `,"ip":"203.0.114.9","reason":"sqli","score":80,"ttl":3600},` +
|
|
`{"t":1,"ip":"203.0.114.10","reason":"x\ny","score":80,"ttl":3600},{"ip":"10.0.0.1","reason":"rce","ttl":60}]}`
|
|
p := "/api/v1/peer/decisions?from=hostinger"
|
|
for _, tok := range []string{"", "wrong-token-0123456789-abcdefghijklmnopqrs", userTok} {
|
|
if w := post(h, p, tok, good); w.Code != 401 && w.Code != 403 {
|
|
t.Fatalf("token %q: %d", tok, w.Code)
|
|
}
|
|
if w := do(h, "GET", "/api/v1/peer/decisions", map[string]string{"Authorization": "Bearer " + tok}, nil); w.Code != 401 && w.Code != 403 {
|
|
t.Fatalf("GET token %q: %d", tok, w.Code)
|
|
}
|
|
}
|
|
if w := post(h, p, peerTok, strings.Repeat(" ", 70<<10)); w.Code != 413 {
|
|
t.Fatalf("big body: %d", w.Code)
|
|
}
|
|
if w := post(h, p, peerTok, "{nope"); w.Code != 400 {
|
|
t.Fatalf("bad json: %d", w.Code)
|
|
}
|
|
var many strings.Builder
|
|
many.WriteString(`{"decisions":[`)
|
|
for i := 0; i < 501; i++ {
|
|
if i > 0 {
|
|
many.WriteString(",")
|
|
}
|
|
many.WriteString(`{"ip":"1.1.1.1"}`)
|
|
}
|
|
many.WriteString(`]}`)
|
|
if w := post(h, p, peerTok, many.String()); w.Code != 413 {
|
|
t.Fatalf("501 items: %d", w.Code)
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
go e.Run(ctx)
|
|
e.Observe(core.Event{Time: time.Now(), IP: "203.0.114.9", Cat: core.Scanner, Source: "traefik", Method: "GET", Path: "/x", Status: 404, UA: "x"}, "")
|
|
for i := 0; i < 200 && e.Report(0).Coverage.AttackIPs == 0; i++ {
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
w := post(h, p, peerTok, good)
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), `"accepted":1`) || !strings.Contains(w.Body.String(), `"invalid":2`) {
|
|
t.Fatalf("%d %s", w.Code, w.Body)
|
|
}
|
|
b, _ := os.ReadFile(log)
|
|
if strings.Count(string(b), "\n") != 1 || !strings.Contains(string(b), "ip=203.0.114.9 reason=sqli") || !strings.Contains(string(b), "src=peer:hostinger") {
|
|
t.Fatalf("log %q", b)
|
|
}
|
|
// GET returns local decisions only
|
|
if len(e.LocalSince(0)) != 0 {
|
|
t.Fatal("peer decision leaked into the local list")
|
|
}
|
|
rr := do(h, "GET", "/api/v1/peer/decisions?since=0", map[string]string{"Authorization": "Bearer " + peerTok}, nil)
|
|
if rr.Code != 200 || !strings.Contains(rr.Body.String(), `"decisions":[]`) {
|
|
t.Fatalf("%d %s", rr.Code, rr.Body)
|
|
}
|
|
}
|
|
|
|
func TestPeerDecisionsShareOff(t *testing.T) {
|
|
h, _, _ := enforceRig(t, false)
|
|
if w := post(h, "/api/v1/peer/decisions", peerTok, `{"decisions":[]}`); w.Code != 404 {
|
|
t.Fatalf("share off: %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestEnforceFromStoreHook(t *testing.T) {
|
|
log := filepath.Join(t.TempDir(), "d.log")
|
|
st := store.New(store.Config{Name: "oracle", Version: "t", Retention: 720 * time.Hour})
|
|
e := enforce.New(enforce.Config{Mode: enforce.ModeOn, Path: log, Host: "oracle", Threshold: 3})
|
|
st.SetIngestHook(e.Observe)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
go e.Run(ctx)
|
|
for i := 0; i < 3; i++ {
|
|
st.Ingest(core.Event{Time: time.Now(), IP: "203.0.114.9", Cat: core.Scanner, Source: "traefik", Method: "GET", Path: "/x" + strconv.Itoa(i), Status: 404, UA: "nuclei"})
|
|
}
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
if b, _ := os.ReadFile(log); strings.Contains(string(b), "ip=203.0.114.9 ") {
|
|
return
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
t.Fatal("no decision written")
|
|
}
|