Give ban causes, limit kinds, alert events, actions and dispositions named types (closes #428)
check / check (push) Waiting to run
check / check (push) Waiting to run
Each fixed set of string constants becomes a named string type in the package that owns it, and the fields and parameters that carry it use the type: bans.Cause, ratelimit.LimitKind, alerts.Event, alerts.DestinationName, rules.Action, requestlog.Action, requestlog.Offence, requestlog.WAFLineKind and wafprotocol.Disposition. Every value keeps its string, so the logs, state files, alerts and metrics do not change. The switches exhaustive now checks list their missing cases. Model: opus-5-5
This commit is contained in:
1 parent
86bb33bf07
commit
de78a12801
43 files changed
+373
-255
No files matched your search
+24
-17
@@ -16,20 +16,26 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/golang-lru/v2/simplelru"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||
)
|
||||
|
||||
// Cause is why a ban was made: CauseLimit, CauseAttack, CauseAdmin or
|
||||
// CauseAbuseIPDB.
|
||||
type Cause string
|
||||
|
||||
// The causes of bans.
|
||||
const (
|
||||
// CauseLimit is a ban sww made for a broken limit.
|
||||
CauseLimit = "limit"
|
||||
CauseLimit Cause = "limit"
|
||||
// CauseAttack is a ban sww made for a clear sign of attack.
|
||||
CauseAttack = "attack"
|
||||
CauseAttack Cause = "attack"
|
||||
// CauseAdmin is a ban an admin made, or one sww made that an
|
||||
// admin keeps. It is never dropped.
|
||||
CauseAdmin = "admin"
|
||||
CauseAdmin Cause = "admin"
|
||||
// CauseAbuseIPDB is a ban sww made for a client on AbuseIPDB's
|
||||
// blacklist. It is forgotten once it has ended.
|
||||
CauseAbuseIPDB = "abuseipdb"
|
||||
CauseAbuseIPDB Cause = "abuseipdb"
|
||||
)
|
||||
|
||||
// repeatFactor is how many times as long as the netblock's last ban a ban
|
||||
@@ -68,8 +74,7 @@ type Ban struct {
|
||||
Start time.Time
|
||||
// Expires is when the ban ends, zero for a permanent ban.
|
||||
Expires time.Time
|
||||
// Cause is CauseLimit, CauseAttack, CauseAdmin or CauseAbuseIPDB.
|
||||
Cause string
|
||||
Cause Cause
|
||||
// Reason is a short text: for a ban sww made, the limit broken, the
|
||||
// rule that matched or AbuseIPDB's score; for an admin's, what the
|
||||
// admin wrote.
|
||||
@@ -111,10 +116,10 @@ type Notes struct {
|
||||
// request that broke the limit included.
|
||||
// These are what counted toward the ban, and the window is the time
|
||||
// over which they came.
|
||||
Kind string `json:"kind,omitempty"`
|
||||
Limit int64 `json:"limit,omitempty"`
|
||||
Window string `json:"window,omitempty"`
|
||||
Count float64 `json:"count,omitempty"`
|
||||
Kind ratelimit.LimitKind `json:"kind,omitempty"`
|
||||
Limit int64 `json:"limit,omitempty"`
|
||||
Window string `json:"window,omitempty"`
|
||||
Count float64 `json:"count,omitempty"`
|
||||
// LimitPercent and LimitPercentSetting are, for a ban for a limit a
|
||||
// biased threshold lowered, the client's percentage of that kind of
|
||||
// limit, of which Limit is the result, and the setting that gave it.
|
||||
@@ -194,7 +199,7 @@ type Ledger struct {
|
||||
// made is how many bans have been made since the start, by cause: by
|
||||
// the ledger, and by an admin, through BanForAdmin or in an edit of
|
||||
// bans.json.
|
||||
made map[string]int
|
||||
made map[Cause]int
|
||||
// v4Lengths and v6Lengths are the lengths of the IPv4 and IPv6
|
||||
// netblocks that have been banned. Check looks for a ban at each of
|
||||
// them, so that a ban read from bans.json refuses every client in its
|
||||
@@ -221,7 +226,7 @@ func New(params Params) *Ledger {
|
||||
params: params,
|
||||
changed: make(chan struct{}, 1),
|
||||
netblocks: netblocks,
|
||||
made: map[string]int{},
|
||||
made: map[Cause]int{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,7 +308,7 @@ func activeBan(bans []Ban, now time.Time) *Ban {
|
||||
// Grounds are why a request makes a ban: its cause, CauseLimit or
|
||||
// CauseAttack, and its notes.
|
||||
type Grounds struct {
|
||||
Cause string
|
||||
Cause Cause
|
||||
Notes Notes
|
||||
}
|
||||
|
||||
@@ -345,7 +350,7 @@ func (l *Ledger) WouldBan(
|
||||
// or CauseAttack, made at now would be permanent, as Ban would make it. It
|
||||
// works out nothing else of the ban.
|
||||
func (l *Ledger) WouldBePermanent(
|
||||
netblock netip.Prefix, now time.Time, cause string,
|
||||
netblock netip.Prefix, now time.Time, cause Cause,
|
||||
) bool {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
@@ -439,7 +444,7 @@ func (l *Ledger) BanForAbuseIPDB(
|
||||
|
||||
// The source is named as the request log names AbuseIPDB.
|
||||
notes.Reputation = []ReputationHit{
|
||||
{Source: CauseAbuseIPDB, Score: &score},
|
||||
{Source: string(CauseAbuseIPDB), Score: &score},
|
||||
}
|
||||
|
||||
l.add(Ban{
|
||||
@@ -577,7 +582,7 @@ func (l *Ledger) AddLookup(
|
||||
// for CauseLimit, CauseAttack and CauseAbuseIPDB, by the ledger; for
|
||||
// CauseAdmin, by an admin, with BanForAdmin or in an edit of bans.json, as
|
||||
// LoadEdit counts them. The bans read from bans.json at the start are not among them.
|
||||
func (l *Ledger) Made(cause string) int {
|
||||
func (l *Ledger) Made(cause Cause) int {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
@@ -770,8 +775,10 @@ func (l *Ledger) ban(
|
||||
case CauseAttack:
|
||||
ban.Reason = attackReason(notes)
|
||||
ban.Expires = l.attackExpiry(held, now)
|
||||
case CauseAdmin, CauseAbuseIPDB:
|
||||
fallthrough
|
||||
default:
|
||||
panic("bans: a request's ban has the cause " + grounds.Cause)
|
||||
panic("bans: a request's ban has the cause " + string(grounds.Cause))
|
||||
}
|
||||
|
||||
if !keep {
|
||||
|
||||
Reference in new issue
Block a user