feat: record own and trusted web traffic in its own classes instead of dropping it

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas RayanandClaude Sonnet 5.5 committed 2026-11-23 09:03:57 -03:00
1 parent 4143bf599e
commit 3511a7141c
18 files changed
+434 -17

No files matched your search

+99 -2
View File
@@ -62,13 +62,13 @@ func TestMonitoringTrustedAndForged(t *testing.T) {
t.Fatalf("feed %+v", rec)
}
// trusted origins are dropped entirely, even for exploits and with other UAs
// trusted origins are only counted (see TestOwnTrafficIsCountedNeverAnalysed), even for exploits
h(line("136.248.78.83", "GET", "/?x=../../etc/passwd", "curl/8", 200))
h(line("2001:db8:1::5", "GET", "/.env", "curl/8", 404))
h(line("127.0.0.1", "GET", "/.env", "curl/8", 404))
for _, a := range []string{"136.248.78.83", "2001:db8:1::5", "127.0.0.1"} {
if _, ok := st.IP(a); ok {
t.Fatalf("%s must be dropped", a)
t.Fatalf("%s must not become an IP record", a)
}
}
// a non-matching UA from an external IP is a normal request
@@ -374,3 +374,100 @@ func TestLocationFlagIsValidated(t *testing.T) {
}
}
}
func TestOwnTrafficIsCountedNeverAnalysed(t *testing.T) {
r, st := runner(t, nil)
h := r.handle(&source{name: "traefik", parse: traefikParse})
h(line("198.51.100.4", "GET", "/", "Mozilla/5.0 (X11; Linux x86_64) Firefox/120.0", 200)) // external: counted as usual
before := st.Summary("1h")
// trusted (--trusted), including an attack-looking request, and a private address (--ignore default)
h(line("136.248.78.83", "GET", "/.env", "curl/8", 404))
h(line("136.248.78.83", "GET", "/api/x?token=secret", "curl/8", 200))
h(line("2001:db8:1::5", "POST", "/login", "curl/8", 401))
h(line("172.18.0.1", "GET", "/", "Mozilla/5.0 (X11; Linux x86_64) Firefox/120.0", 200))
h(line("127.0.0.1", "GET", "/", "x", 502))
after := st.Summary("2h") // another range: the summary of "1h" is cached for 2 s
if after.Totals.Trusted != 3 || after.Totals.Internal != 2 {
t.Fatalf("own counts: %+v", after.Totals)
}
if after.Totals.Requests != before.Totals.Requests || after.Totals.Malicious != before.Totals.Malicious ||
after.Totals.RequestsAll != before.Totals.Requests+5 {
t.Fatalf("totals: before %+v after %+v", before.Totals, after.Totals)
}
if after.Totals.Counts["trusted"] != 3 || after.Totals.Counts["internal"] != 2 {
t.Fatalf("counts: %v", after.Totals.Counts)
}
if after.Totals.UniqueIPs != before.Totals.UniqueIPs || after.Totals.HostileIPs != 0 || after.Totals.SuspiciousIPs != 0 {
t.Fatalf("own traffic must not create IPs: %+v", after.Totals)
}
for _, a := range []string{"136.248.78.83", "2001:db8:1::5", "172.18.0.1", "127.0.0.1"} {
if _, ok := st.IP(a); ok {
t.Fatalf("%s must not have an IP record", a)
}
}
if len(st.Recent(10)) != len(before.Recent) || len(after.IPs) != len(before.IPs) || len(after.TopPaths) != len(before.TopPaths) {
t.Fatal("own traffic must not reach the feed, the IP table or the attack tops")
}
if tr := after; tr.Totals.Monitoring != 0 {
t.Fatalf("monitoring: %+v", tr.Totals)
}
if v := st.TrafficFor(store.ParseRange("1h"), "all"); v.Requests != 1 || v.Visitors != 0 {
t.Fatalf("web analytics must not count own traffic (one external request, no visitor without an enricher): %+v", v)
}
o := after.Own
if o.Status[0] != 2 || o.Status[2] != 2 || o.Status[3] != 1 {
t.Fatalf("status classes: %v", o.Status)
}
var sum uint32
for _, c := range []string{"trusted", "internal"} {
for _, v := range o.Series[c] {
sum += v
}
}
if sum != 5 {
t.Fatalf("series sum %d", sum)
}
got := map[string]string{}
for _, c := range o.Origins {
got[c.Key] = c.Label
}
if got["136.248.78.83"] != "trusted" || got["172.18.0.1"] != "internal" || got["2001:db8:1::/64"] != "trusted" {
t.Fatalf("origins: %+v", o.Origins)
}
for _, p := range o.Paths {
if strings.Contains(p.Key, "secret") {
t.Fatalf("query strings must not be kept: %+v", o.Paths)
}
}
// a snapshot keeps the own traffic
dir := t.TempDir()
if err := st.Save(SnapshotPath(dir), nil); err != nil {
t.Fatal(err)
}
st2 := store.New(store.Config{Name: "t", Version: "t"})
if _, err := st2.Load(SnapshotPath(dir)); err != nil {
t.Fatal(err)
}
if s2 := st2.Summary("3h"); s2.Totals.Trusted != 3 || s2.Totals.Internal != 2 {
t.Fatalf("after reload: %+v", s2.Totals)
}
}
func TestOwnTrafficThroughFeed(t *testing.T) {
// the probe sends every line; the hub runner (remote mode) classifies it
c := Config{Ignore: DefaultIgnore, Trusted: "10.9.9.9", Retention: 168 * time.Hour, Name: "n"}
st := store.New(store.Config{Name: "n", Version: "t"})
r, err := NewRemoteRunner(c, st, slog.New(slog.NewTextHandler(io.Discard, nil)), t.TempDir())
if err != nil {
t.Fatal(err)
}
if !r.Feed("traefik", line("10.9.9.9", "GET", "/", "x", 200), store.Offset{Inode: 1, Offset: 10}) ||
!r.Feed("traefik", line("203.0.113.9", "GET", "/", "x", 200), store.Offset{Inode: 1, Offset: 20}) ||
!r.Feed("traefik", line("192.168.1.2", "GET", "/", "x", 200), store.Offset{Inode: 1, Offset: 30}) {
t.Fatal("feed refused")
}
s := st.Summary("1h")
if s.Totals.Trusted != 1 || s.Totals.Internal != 1 || s.Totals.Requests != 1 || s.Totals.RequestsAll != 3 {
t.Fatalf("totals: %+v", s.Totals)
}
}