check / check (push) Waiting to run
SWWAF_ALERT_WEBHOOK_URL gets one JSON POST per alert, in SPEC.md's schema, with SWWAF_ALERT_WEBHOOK_HEADERS: ban and permanent_ban, with the ban's notes; source_failure for GeoJS; file_error for a rule or state file edit that does not parse and a failed state write. SWWAF_ALERT_EVENTS chooses, SWWAF_ALERT_COOLDOWN holds back repeats, and past SWWAF_ALERT_MAX_PER_HOUR the hour ends in one summary. A bounded queue, retried with backoff, holds up no request; alerts.json keeps it, the cooldowns and the hour. The ledger now reports whether it made a ban, or made one permanent. Judgement call: the summary's event is summary, which SPEC.md omits. Judgement call: admin bans and observe mode raise no alert. Model: opus-5-5
167 lines
4.3 KiB
Go
167 lines
4.3 KiB
Go
package proxy
|
|
|
|
import (
|
|
"net/netip"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
"sneak.berlin/go/smallwebwaf/internal/rules"
|
|
)
|
|
|
|
// banResponse is a refusal answered with SWWAF_BAN_RESPONSE, and logged
|
|
// with action.
|
|
func (rq *request) banResponse(action string) *refusal {
|
|
return &refusal{status: rq.h.config.BanResponse, action: action}
|
|
}
|
|
|
|
// banned reports whether a ban on a netblock the client is in covers the
|
|
// request at now, and notes for the log line when that ban ends. A
|
|
// request that makes the ban permanent raises the alert for it.
|
|
func (rq *request) banned(now time.Time) bool {
|
|
var (
|
|
ban bans.Ban
|
|
banned bool
|
|
madePermanent bool
|
|
)
|
|
|
|
if rq.h.config.Observe {
|
|
ban, banned = rq.h.ledger.Find(rq.client, now) // the ban refuses nothing
|
|
} else {
|
|
ban, banned, madePermanent = rq.h.ledger.Check(rq.client, now)
|
|
}
|
|
|
|
if madePermanent {
|
|
rq.alertBan(ban)
|
|
}
|
|
|
|
if banned {
|
|
rq.line.BanExpires = banExpires(ban)
|
|
}
|
|
|
|
return banned
|
|
}
|
|
|
|
// limitBroken counts the request for the rate limits at now, notes the
|
|
// client's counts for the log line, and reports whether the request takes
|
|
// the client over a limit. In enforce mode such a request bans the
|
|
// client's netblock, and sets the client's counters back to zero; in
|
|
// observe mode it does neither.
|
|
func (rq *request) limitBroken(now time.Time) bool {
|
|
group := clientGroup(rq.client)
|
|
|
|
counts, hit, over := rq.h.limiter.Count(group, now)
|
|
rq.line.Counts = counts
|
|
|
|
if !over {
|
|
return false
|
|
}
|
|
|
|
rq.line.LimitHit = hit.Window
|
|
rq.line.Offence = requestlog.OffenceLimit
|
|
|
|
if rq.h.config.Observe {
|
|
return true
|
|
}
|
|
|
|
netblock := rq.h.netblock(rq.client)
|
|
ban, made := rq.h.ledger.BanForLimit(netblock, now, bans.Notes{
|
|
Country: rq.line.Country,
|
|
Limit: hit.Limit,
|
|
Window: hit.Window,
|
|
Count: hit.Requests,
|
|
Request: rq.noted(now),
|
|
Requests: rq.netblockRequests(netblock),
|
|
})
|
|
rq.h.limiter.Reset(group)
|
|
rq.line.BanExpires = banExpires(ban)
|
|
|
|
if made {
|
|
rq.alertBan(ban)
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// banForAttack bans the client's netblock at now for a clear sign of
|
|
// attack, the match of rule, a ban rule.
|
|
func (rq *request) banForAttack(now time.Time, rule rules.Rule) {
|
|
netblock := rq.h.netblock(rq.client)
|
|
ban, made := rq.h.ledger.BanForAttack(netblock, now, bans.Notes{
|
|
Country: rq.line.Country,
|
|
RuleID: rule.ID,
|
|
Target: rule.Target,
|
|
Request: rq.noted(now),
|
|
Requests: rq.netblockRequests(netblock),
|
|
})
|
|
rq.line.BanExpires = banExpires(ban)
|
|
|
|
if made {
|
|
rq.alertBan(ban)
|
|
}
|
|
}
|
|
|
|
// alertBan raises the alert for ban, which the request made, or made
|
|
// permanent: permanent_ban for a permanent ban, ban for another. Its
|
|
// detail gives the ban's cause, when it ends, and its notes.
|
|
func (rq *request) alertBan(ban bans.Ban) {
|
|
event := alerts.EventBan
|
|
if ban.Permanent() {
|
|
event = alerts.EventPermanentBan
|
|
}
|
|
|
|
rq.h.alerts.Raise(alerts.Alert{
|
|
Event: event,
|
|
Client: rq.client,
|
|
Netblock: ban.Netblock,
|
|
Country: ban.Notes.Country,
|
|
Reason: ban.Reason,
|
|
Detail: map[string]any{
|
|
"cause": ban.Cause, "ban_expires": banExpires(ban), "notes": ban.Notes,
|
|
},
|
|
})
|
|
}
|
|
|
|
// noted is the request, refused at now with SWWAF_BAN_RESPONSE, as the
|
|
// notes of the ban it makes keep it.
|
|
func (rq *request) noted(now time.Time) bans.Request {
|
|
return bans.Request{
|
|
Time: now,
|
|
Method: rq.in.Method,
|
|
Host: rq.in.Host,
|
|
Path: rq.in.URL.RequestURI(),
|
|
Status: rq.h.config.BanResponse,
|
|
UserAgent: rq.in.UserAgent(),
|
|
}
|
|
}
|
|
|
|
// netblockRequests is how many requests netblock has sent since it was
|
|
// first seen, this one included: the histories count it only once it has
|
|
// ended.
|
|
func (rq *request) netblockRequests(netblock netip.Prefix) int64 {
|
|
return rq.h.limiter.Requests(netblock) + 1
|
|
}
|
|
|
|
// netblock is the netblock a ban on client covers: its IPv4 address,
|
|
// widened to SWWAF_BAN_SCOPE_V4_PREFIX, or the IPv6 group clientGroup
|
|
// counts it in.
|
|
func (h *handler) netblock(client netip.Addr) netip.Prefix {
|
|
addr := client.Unmap()
|
|
if addr.Is4() {
|
|
return netip.PrefixFrom(addr, h.config.BanScopeV4Prefix).Masked()
|
|
}
|
|
|
|
return clientGroup(addr)
|
|
}
|
|
|
|
// banExpires is when ban ends, as the log line gives it: a time, or
|
|
// permanent.
|
|
func banExpires(ban bans.Ban) string {
|
|
if ban.Permanent() {
|
|
return permanent
|
|
}
|
|
|
|
return requestlog.FormatTime(ban.Expires)
|
|
}
|