Files
smallwebwaf/internal/waf/trappaths.go
sneak 098bbe8878
check / check (push) Failing after 1m56s
waf: run the trap paths, rule files and Core Rule Set during the GeoJS wait (closes #405)
While the lookup waits for GeoJS, the trap paths, the rule files and the
Core Rule Set run at the same time as the checks after the lookup. The
first side to refuse answers the request; what the other side finds leaves
no trace. Those three checks have 500 ms together: past it, the request is
refused with 503 as checks_timed_out, with no ban and no offence.

Tests hold GeoJS and the Core Rule Set with stand-ins under synctest. The
test helper lifts the 500 ms limit for tests that run the real Core Rule
Set, which the race detector slows past it.

Model: opus-5-5
2026-11-17 04:27:26 +00:00

53 lines
1.3 KiB
Go

package waf
// The heuristic trap_paths. It reads SWW_WAF_TRAP_PATHS.
import (
"context"
"slices"
"time"
"sneak.berlin/go/smallwebwaf/internal/bans"
"sneak.berlin/go/smallwebwaf/internal/rules"
)
// trapPaths refuses a request for a path in SWW_WAF_TRAP_PATHS, as trapPath
// finds it. Such a request is a clear sign of attack, as a ban rule's match is:
// it bans the client's netblock, or in observe mode raises the alert for the
// ban it would have made. The ban's notes give the path as sent, and so does
// its figure.
type trapPaths struct{}
func (trapPaths) name() string { return "trap_paths" }
func (trapPaths) check(
_ context.Context,
rq *request,
now time.Time,
) *verdict {
path := rq.findings.trapPath
if path == "" {
return nil
}
rq.attack = true
return &verdict{
action: rq.banForAttack(now, bans.Notes{TrapPath: path}),
figures: map[string]any{"path": path},
}
}
// trapPath returns the request's path as sent, when its path as a path rule
// sees it, without the query, as sent or once percent-decoded, is in
// SWW_WAF_TRAP_PATHS, and "" otherwise.
func (rq *request) trapPath() string {
path := rules.Path(rq.in)
if !slices.Contains(rq.h.config.TrapPaths, path) &&
!slices.Contains(rq.h.config.TrapPaths, rules.DecodeOnce(path)) {
return ""
}
return path
}