Give ban causes, limit kinds, alert events, actions and dispositions named types (closes #428)
check / check (push) Successful in 5m27s

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:
sneak committed 2026-11-19 22:13:39 +00:00
1 parent 86bb33bf07
commit de78a12801
43 files changed
+350 -232

No files matched your search

+51 -38
View File
@@ -34,65 +34,76 @@ import (
"time" "time"
) )
// Event is what an alert is for, such as EventBan.
type Event string
// The events an alert is for, as SWW_WAF_ALERT_EVENTS names them. // The events an alert is for, as SWW_WAF_ALERT_EVENTS names them.
const ( const (
// EventBan is a ban sww made. // EventBan is a ban sww made.
EventBan = "ban" EventBan Event = "ban"
// EventPermanentBan is a permanent ban sww made, or a ban it // EventPermanentBan is a permanent ban sww made, or a ban it
// made permanent. // made permanent.
EventPermanentBan = "permanent_ban" EventPermanentBan Event = "permanent_ban"
// EventAnomaly is a count of requests or bytes over an anomaly // EventAnomaly is a count of requests or bytes over an anomaly
// threshold. // threshold.
EventAnomaly = "anomaly" EventAnomaly Event = "anomaly"
// EventCoreRuleSetMatch is a request the Core Rule Set scored at or over // EventCoreRuleSetMatch is a request the Core Rule Set scored at or over
// SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD, refused in block mode, let through in // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD, refused in block mode, let through in
// detect mode. // detect mode.
EventCoreRuleSetMatch = "core_rule_set_match" EventCoreRuleSetMatch Event = "core_rule_set_match"
// EventReputationHit is a request whose client a blocklist or a DNSBL // EventReputationHit is a request whose client a blocklist or a DNSBL
// zone lists, or whose AbuseIPDB score is a hit. // zone lists, or whose AbuseIPDB score is a hit.
EventReputationHit = "reputation_hit" EventReputationHit Event = "reputation_hit"
// EventSourceFailure is GeoJS failing or refusing sww, a fetch // EventSourceFailure is GeoJS failing or refusing sww, a fetch
// of a list failing, a query to a DNSBL zone or a check with AbuseIPDB // of a list failing, a query to a DNSBL zone or a check with AbuseIPDB
// failing or refused, or the day's AbuseIPDB checks used up. // failing or refused, or the day's AbuseIPDB checks used up.
EventSourceFailure = "source_failure" EventSourceFailure Event = "source_failure"
// EventFileError is one of: // EventFileError is one of:
// - a rule file or state file edited while sww runs that does not parse; // - a rule file or state file edited while sww runs that does not parse;
// - a cache file that does not parse, at start or after an edit; // - a cache file that does not parse, at start or after an edit;
// - a state file or cache file that cannot be written; // - a state file or cache file that cannot be written;
// - a replacement of the lookup database that cannot be read. // - a replacement of the lookup database that cannot be read.
EventFileError = "file_error" EventFileError Event = "file_error"
// EventSummary is the summary sent as an hour ends: of the alerts held // EventSummary is the summary sent as an hour ends: of the alerts held
// back in it past SWW_WAF_ALERT_MAX_PER_HOUR, and of the repeats held // back in it past SWW_WAF_ALERT_MAX_PER_HOUR, and of the repeats held
// back by the cooldowns dropped as it ends, which no alert let through // back by the cooldowns dropped as it ends, which no alert let through
// has given. It is sent with SWW_WAF_ALERT_MAX_PER_HOUR off too, for // has given. It is sent with SWW_WAF_ALERT_MAX_PER_HOUR off too, for
// those repeats. SWW_WAF_ALERT_EVENTS does not name it. // those repeats. SWW_WAF_ALERT_EVENTS does not name it.
EventSummary = "summary" EventSummary Event = "summary"
) )
// Events returns every event SWW_WAF_ALERT_EVENTS can name, which is its // Events returns every event SWW_WAF_ALERT_EVENTS can name, which is its
// default. // default.
func Events() []string { func Events() []Event {
return []string{ return []Event{
EventBan, EventPermanentBan, EventCoreRuleSetMatch, EventAnomaly, EventBan, EventPermanentBan, EventCoreRuleSetMatch, EventAnomaly,
EventReputationHit, EventSourceFailure, EventFileError, EventReputationHit, EventSourceFailure, EventFileError,
} }
} }
// DestinationName is the name of a destination alerts are sent to:
// DestinationWebhook, DestinationSlack or DestinationNtfy.
type DestinationName string
// The destinations alerts are sent to, as the metrics and alerts.json // The destinations alerts are sent to, as the metrics and alerts.json
// name them. // name them.
const ( const (
// DestinationWebhook is the webhook SWW_WAF_ALERT_WEBHOOK_URL names. // DestinationWebhook is the webhook SWW_WAF_ALERT_WEBHOOK_URL names.
DestinationWebhook = "webhook" DestinationWebhook DestinationName = "webhook"
// DestinationSlack is the Slack incoming webhook // DestinationSlack is the Slack incoming webhook
// SWW_WAF_ALERT_SLACK_WEBHOOK_URL names. // SWW_WAF_ALERT_SLACK_WEBHOOK_URL names.
DestinationSlack = "slack" DestinationSlack DestinationName = "slack"
// DestinationNtfy is the ntfy topic SWW_WAF_ALERT_NTFY_URL names. // DestinationNtfy is the ntfy topic SWW_WAF_ALERT_NTFY_URL names.
DestinationNtfy = "ntfy" DestinationNtfy DestinationName = "ntfy"
) )
// Destinations returns every destination alerts can be sent to. // Destinations returns every destination alerts can be sent to.
func Destinations() []string { func Destinations() []DestinationName {
return []string{DestinationWebhook, DestinationSlack, DestinationNtfy} return []DestinationName{
DestinationWebhook,
DestinationSlack,
DestinationNtfy,
}
} }
const ( const (
@@ -135,7 +146,7 @@ type Params struct {
NtfyURL *url.URL NtfyURL *url.URL
NtfyToken string NtfyToken string
// Events are the events alerts are sent for (SWW_WAF_ALERT_EVENTS). // Events are the events alerts are sent for (SWW_WAF_ALERT_EVENTS).
Events []string Events []Event
// Cooldown is how long a repeat of an alert is held back // Cooldown is how long a repeat of an alert is held back
// (SWW_WAF_ALERT_COOLDOWN), 0 for no time. MaxPerHour is the most alerts // (SWW_WAF_ALERT_COOLDOWN), 0 for no time. MaxPerHour is the most alerts
// sent in an hour (SWW_WAF_ALERT_MAX_PER_HOUR), 0 for no limit. // sent in an hour (SWW_WAF_ALERT_MAX_PER_HOUR), 0 for no limit.
@@ -159,7 +170,7 @@ type Params struct {
type Alert struct { type Alert struct {
Instance string `json:"instance"` Instance string `json:"instance"`
Time time.Time `json:"time"` Time time.Time `json:"time"`
Event string `json:"event"` Event Event `json:"event"`
Host string `json:"host,omitempty"` Host string `json:"host,omitempty"`
Client netip.Addr `json:"client"` Client netip.Addr `json:"client"`
Netblock netip.Prefix `json:"netblock"` Netblock netip.Prefix `json:"netblock"`
@@ -186,7 +197,7 @@ type Alert struct {
// //
//nolint:tagliatelle // the state files use snake_case, as the request log does //nolint:tagliatelle // the state files use snake_case, as the request log does
type Cooldown struct { type Cooldown struct {
Event string `json:"event"` Event Event `json:"event"`
Netblock netip.Prefix `json:"netblock"` Netblock netip.Prefix `json:"netblock"`
File string `json:"file,omitempty"` File string `json:"file,omitempty"`
Source string `json:"source,omitempty"` Source string `json:"source,omitempty"`
@@ -205,7 +216,7 @@ type Cooldown struct {
type Hour struct { type Hour struct {
Start time.Time `json:"start"` Start time.Time `json:"start"`
Sent int `json:"sent"` Sent int `json:"sent"`
HeldBack map[string]int `json:"held_back"` HeldBack map[Event]int `json:"held_back"`
} }
// State is what alerts.json holds: the cooldowns, the hour under way, and // State is what alerts.json holds: the cooldowns, the hour under way, and
@@ -214,7 +225,7 @@ type Hour struct {
type State struct { type State struct {
Cooldowns []Cooldown `json:"cooldowns"` Cooldowns []Cooldown `json:"cooldowns"`
Hour Hour `json:"hour"` Hour Hour `json:"hour"`
Waiting map[string][]Alert `json:"waiting"` Waiting map[DestinationName][]Alert `json:"waiting"`
} }
// Counts are, for a destination, how many alerts it took, how many // Counts are, for a destination, how many alerts it took, how many
@@ -248,7 +259,7 @@ type destination struct {
// name is how the metrics and alerts.json name the destination, and // name is how the metrics and alerts.json name the destination, and
// setting the setting that is its URL, which the log names in place // setting the setting that is its URL, which the log names in place
// of the URL. // of the URL.
name string name DestinationName
setting string setting string
url *url.URL url *url.URL
// message returns the body an alert is posted with, and the headers // message returns the body an alert is posted with, and the headers
@@ -273,7 +284,7 @@ type destination struct {
// scope with the same AS number or name, as its detail names them. Each // scope with the same AS number or name, as its detail names them. Each
// is empty for an alert without one. // is empty for an alert without one.
type cooldownKey struct { type cooldownKey struct {
event string event Event
netblock netip.Prefix netblock netip.Prefix
file string file string
source string source string
@@ -306,7 +317,7 @@ func New(params Params) *Queue {
q := &Queue{ q := &Queue{
params: params, params: params,
cooldowns: map[cooldownKey]*Cooldown{}, cooldowns: map[cooldownKey]*Cooldown{},
hour: Hour{HeldBack: map[string]int{}}, hour: Hour{HeldBack: map[Event]int{}},
} }
if params.WebhookURL != nil { if params.WebhookURL != nil {
@@ -377,7 +388,7 @@ func (q *Queue) Raise(alert Alert) {
// event, no alert for event on netblock was let through less than // event, no alert for event on netblock was let through less than
// Cooldown before, and fewer than MaxPerHour alerts have been let through // Cooldown before, and fewer than MaxPerHour alerts have been let through
// in the hour under way. Unlike Raise, it counts nothing. // in the hour under way. Unlike Raise, it counts nothing.
func (q *Queue) WouldSend(event string, netblock netip.Prefix) bool { func (q *Queue) WouldSend(event Event, netblock netip.Prefix) bool {
if len(q.destinations) == 0 || !slices.Contains(q.params.Events, event) { if len(q.destinations) == 0 || !slices.Contains(q.params.Events, event) {
return false return false
} }
@@ -433,7 +444,7 @@ func (q *Queue) Run(ctx context.Context) {
// Counts returns the counts of the destination name, all 0 for one not // Counts returns the counts of the destination name, all 0 for one not
// set. // set.
func (q *Queue) Counts(name string) Counts { func (q *Queue) Counts(name DestinationName) Counts {
for _, d := range q.destinations { for _, d := range q.destinations {
if d.name == name { if d.name == name {
return Counts{ return Counts{
@@ -449,8 +460,8 @@ func (q *Queue) Counts(name string) Counts {
// DestinationsSet returns the destinations set, in the order of // DestinationsSet returns the destinations set, in the order of
// Destinations. // Destinations.
func (q *Queue) DestinationsSet() []string { func (q *Queue) DestinationsSet() []DestinationName {
names := make([]string, 0, len(q.destinations)) names := make([]DestinationName, 0, len(q.destinations))
for _, d := range q.destinations { for _, d := range q.destinations {
names = append(names, d.name) names = append(names, d.name)
} }
@@ -474,7 +485,7 @@ func (q *Queue) Snapshot() State {
state := State{ state := State{
Cooldowns: make([]Cooldown, 0, len(q.cooldowns)), Cooldowns: make([]Cooldown, 0, len(q.cooldowns)),
Hour: q.hour, Hour: q.hour,
Waiting: map[string][]Alert{}, Waiting: map[DestinationName][]Alert{},
} }
state.Hour.HeldBack = maps.Clone(q.hour.HeldBack) state.Hour.HeldBack = maps.Clone(q.hour.HeldBack)
@@ -525,7 +536,7 @@ func (q *Queue) Load(state State) {
q.hour.HeldBack = maps.Clone(state.Hour.HeldBack) q.hour.HeldBack = maps.Clone(state.Hour.HeldBack)
if q.hour.HeldBack == nil { if q.hour.HeldBack == nil {
q.hour.HeldBack = map[string]int{} q.hour.HeldBack = map[Event]int{}
} }
for _, d := range q.destinations { for _, d := range q.destinations {
@@ -537,7 +548,7 @@ func (q *Queue) Load(state State) {
// its URL, that URL, target, and message, which makes the messages sent // its URL, that URL, target, and message, which makes the messages sent
// to it. // to it.
func (q *Queue) addDestination( func (q *Queue) addDestination(
name, setting string, target *url.URL, name DestinationName, setting string, target *url.URL,
message func(alert *Alert) ([]byte, http.Header, error), message func(alert *Alert) ([]byte, http.Header, error),
) { ) {
q.destinations = append(q.destinations, &destination{ q.destinations = append(q.destinations, &destination{
@@ -649,7 +660,7 @@ func (q *Queue) endHour(now time.Time) {
}) })
} }
q.hour = Hour{Start: start, HeldBack: map[string]int{}} q.hour = Hour{Start: start, HeldBack: map[Event]int{}}
} }
// queue adds alert to the alerts waiting for each destination. // queue adds alert to the alerts waiting for each destination.
@@ -904,20 +915,21 @@ func (q *Queue) ntfyMessage(alert *Alert) ([]byte, http.Header, error) {
// ntfyPriority returns the priority an alert for event is published to // ntfyPriority returns the priority an alert for event is published to
// ntfy with: high for an event the admin needs to look at. // ntfy with: high for an event the admin needs to look at.
func ntfyPriority(event string) string { func ntfyPriority(event Event) string {
switch event { switch event {
case EventPermanentBan, EventAnomaly, EventSourceFailure, EventFileError: case EventPermanentBan, EventAnomaly, EventSourceFailure, EventFileError:
return "high" return "high"
case EventReputationHit: case EventReputationHit:
return "low" return "low"
default: // ban, core_rule_set_match and summary case EventBan, EventCoreRuleSetMatch, EventSummary:
return "default"
} }
return "default"
} }
// ntfyTag returns the tag an alert for event is published to ntfy with, // ntfyTag returns the tag an alert for event is published to ntfy with,
// which ntfy shows as an emoji. // which ntfy shows as an emoji.
func ntfyTag(event string) string { func ntfyTag(event Event) string {
switch event { switch event {
case EventBan, EventPermanentBan: case EventBan, EventPermanentBan:
return "no_entry" return "no_entry"
@@ -929,15 +941,16 @@ func ntfyTag(event string) string {
return "label" return "label"
case EventSourceFailure, EventFileError: case EventSourceFailure, EventFileError:
return "warning" return "warning"
default: // summary case EventSummary:
return "bar_chart"
} }
return "bar_chart"
} }
// title returns the title of alert in Slack and ntfy: the instance and // title returns the title of alert in Slack and ntfy: the instance and
// the event. // the event.
func title(alert *Alert) string { func title(alert *Alert) string {
return alert.Instance + ": " + alert.Event return alert.Instance + ": " + string(alert.Event)
} }
// text returns the text of alert in Slack and ntfy: its reason, then a // text returns the text of alert in Slack and ntfy: its reason, then a
+29 -18
View File
@@ -126,7 +126,7 @@ func TestOnlyTheChosenEventsAreSent(t *testing.T) {
synctest.Test(t, func(t *testing.T) { synctest.Test(t, func(t *testing.T) {
params := newParams() params := newParams()
params.Events = []string{ params.Events = []alerts.Event{
alerts.EventSourceFailure, alerts.EventSourceFailure,
alerts.EventFileError, alerts.EventFileError,
} }
@@ -156,7 +156,7 @@ func TestWouldSendOnlyForTheChosenEvents(t *testing.T) {
t.Parallel() t.Parallel()
params := newParams() params := newParams()
params.Events = []string{ params.Events = []alerts.Event{
alerts.EventSourceFailure, alerts.EventSourceFailure,
alerts.EventFileError, alerts.EventFileError,
} }
@@ -186,8 +186,8 @@ func TestRaiseDoesNothingWithoutADestination(t *testing.T) {
// No alert waits, no cooldown has started, and the hour counts none. // No alert waits, no cooldown has started, and the hour counts none.
want := alerts.State{ want := alerts.State{
Cooldowns: []alerts.Cooldown{}, Cooldowns: []alerts.Cooldown{},
Hour: alerts.Hour{HeldBack: map[string]int{}}, Hour: alerts.Hour{HeldBack: map[alerts.Event]int{}},
Waiting: map[string][]alerts.Alert{}, Waiting: map[alerts.DestinationName][]alerts.Alert{},
} }
if got := q.Snapshot(); !reflect.DeepEqual(got, want) { if got := q.Snapshot(); !reflect.DeepEqual(got, want) {
t.Errorf("state %+v, want %+v", got, want) t.Errorf("state %+v, want %+v", got, want)
@@ -235,7 +235,7 @@ func TestRepeatWithinTheCooldownIsHeldBackAndCountedInTheNext(t *testing.T) {
synctest.Test(t, func(t *testing.T) { synctest.Test(t, func(t *testing.T) {
webhook, q := start(t, newParams()) webhook, q := start(t, newParams())
raise := func(event string, n int) { raise := func(event alerts.Event, n int) {
q.Raise(alerts.Alert{Event: event, Netblock: netblock(n)}) q.Raise(alerts.Alert{Event: event, Netblock: netblock(n)})
} }
@@ -1014,7 +1014,7 @@ func TestNtfyTokenIsSentToNtfyAlone(t *testing.T) {
) )
synctest.Wait() synctest.Wait()
for destination, want := range map[string][]string{ for destination, want := range map[alerts.DestinationName][]string{
alerts.DestinationWebhook: nil, alerts.DestinationWebhook: nil,
alerts.DestinationSlack: nil, alerts.DestinationSlack: nil,
alerts.DestinationNtfy: tc.want, alerts.DestinationNtfy: tc.want,
@@ -1063,7 +1063,7 @@ func TestDestinationThatDoesNotAnswerHoldsUpNeitherOther(t *testing.T) {
// With no time passed, the webhook and ntfy have taken every alert, // With no time passed, the webhook and ntfy have taken every alert,
// while Slack has not answered the first. // while Slack has not answered the first.
for destination, want := range map[string]alerts.Counts{ for destination, want := range map[alerts.DestinationName]alerts.Counts{
alerts.DestinationWebhook: {Sent: 3}, alerts.DestinationWebhook: {Sent: 3},
alerts.DestinationSlack: {}, alerts.DestinationSlack: {},
alerts.DestinationNtfy: {Sent: 3}, alerts.DestinationNtfy: {Sent: 3},
@@ -1126,7 +1126,7 @@ func TestDropsAreCountedForEachDestination(t *testing.T) {
// the webhook takes every one. // the webhook takes every one.
const all = alerts.QueueSize + 1 const all = alerts.QueueSize + 1
for destination, want := range map[string]alerts.Counts{ for destination, want := range map[alerts.DestinationName]alerts.Counts{
alerts.DestinationWebhook: {Sent: all}, alerts.DestinationWebhook: {Sent: all},
alerts.DestinationSlack: {Dropped: 1}, alerts.DestinationSlack: {Dropped: 1},
alerts.DestinationNtfy: {SendFailures: all, Dropped: all}, alerts.DestinationNtfy: {SendFailures: all, Dropped: all},
@@ -1152,11 +1152,21 @@ func TestEachDestinationIsSentOnlyTheAlertsWaitingForIt(t *testing.T) {
// As read from alerts.json. The alert waiting for the webhook, which // As read from alerts.json. The alert waiting for the webhook, which
// is not set, is dropped. // is not set, is dropped.
q.Load(roundTrip(t, alerts.State{Waiting: map[string][]alerts.Alert{ q.Load(
roundTrip(
t,
alerts.State{
Waiting: map[alerts.DestinationName][]alerts.Alert{
alerts.DestinationWebhook: {waiting("first")}, alerts.DestinationWebhook: {waiting("first")},
alerts.DestinationSlack: {waiting("second"), waiting("third")}, alerts.DestinationSlack: {
waiting("second"),
waiting("third"),
},
alerts.DestinationNtfy: {waiting("fourth")}, alerts.DestinationNtfy: {waiting("fourth")},
}})) },
},
),
)
synctest.Wait() synctest.Wait()
wantBodies(t, standIns[alerts.DestinationSlack], wantBodies(t, standIns[alerts.DestinationSlack],
@@ -1165,7 +1175,7 @@ func TestEachDestinationIsSentOnlyTheAlertsWaitingForIt(t *testing.T) {
wantBodies(t, standIns[alerts.DestinationNtfy], "fourth") wantBodies(t, standIns[alerts.DestinationNtfy], "fourth")
// alerts.json then lists Slack and ntfy alone, with no alert waiting. // alerts.json then lists Slack and ntfy alone, with no alert waiting.
want := map[string][]alerts.Alert{ want := map[alerts.DestinationName][]alerts.Alert{
alerts.DestinationSlack: {}, alerts.DestinationNtfy: {}, alerts.DestinationSlack: {}, alerts.DestinationNtfy: {},
} }
if got := q.Snapshot().Waiting; !reflect.DeepEqual(got, want) { if got := q.Snapshot().Waiting; !reflect.DeepEqual(got, want) {
@@ -1334,11 +1344,11 @@ func start(t *testing.T, params alerts.Params) (*standIn, *alerts.Queue) {
func startAll( func startAll(
t *testing.T, t *testing.T,
params alerts.Params, params alerts.Params,
) (map[string]*standIn, *alerts.Queue) { ) (map[alerts.DestinationName]*standIn, *alerts.Queue) {
t.Helper() t.Helper()
q := alerts.New(params) q := alerts.New(params)
standIns := map[string]*standIn{} standIns := map[alerts.DestinationName]*standIn{}
for _, destination := range q.DestinationsSet() { for _, destination := range q.DestinationsSet() {
standIns[destination] = &standIn{answers: answering} standIns[destination] = &standIn{answers: answering}
@@ -1404,14 +1414,14 @@ func wantAlert(t *testing.T, got, want map[string]any) {
// wantEvents checks the events of the alerts the webhook was sent, in // wantEvents checks the events of the alerts the webhook was sent, in
// order. // order.
func wantEvents(t *testing.T, webhook *standIn, want ...string) { func wantEvents(t *testing.T, webhook *standIn, want ...alerts.Event) {
t.Helper() t.Helper()
got := make([]string, 0, len(webhook.received())) got := make([]alerts.Event, 0, len(webhook.received()))
for _, request := range webhook.received() { for _, request := range webhook.received() {
event, _ := request.alert["event"].(string) event, _ := request.alert["event"].(string)
got = append(got, event) got = append(got, alerts.Event(event))
} }
if !slices.Equal(got, want) { if !slices.Equal(got, want) {
@@ -1447,7 +1457,8 @@ func wantCounts(
// wantDestinationCounts checks q's counts for destination. // wantDestinationCounts checks q's counts for destination.
func wantDestinationCounts( func wantDestinationCounts(
t *testing.T, q *alerts.Queue, destination string, want alerts.Counts, t *testing.T, q *alerts.Queue, destination alerts.DestinationName,
want alerts.Counts,
) { ) {
t.Helper() t.Helper()
+3 -1
View File
@@ -7,7 +7,9 @@ const QueueSize = queueSize
// SetTransport has q's requests to the destination name go through // SetTransport has q's requests to the destination name go through
// transport instead of the network. // transport instead of the network.
func (q *Queue) SetTransport(name string, transport http.RoundTripper) { func (q *Queue) SetTransport(
name DestinationName, transport http.RoundTripper,
) {
for _, d := range q.destinations { for _, d := range q.destinations {
if d.name == name { if d.name == name {
d.httpClient.Transport = transport d.httpClient.Transport = transport
+6 -4
View File
@@ -295,7 +295,8 @@ func (c *Counters) netAround(client netip.Addr) netip.Prefix {
// overThreshold is a count over its threshold: what it counts, requests or // overThreshold is a count over its threshold: what it counts, requests or
// bytes, its window, the count and the threshold. // bytes, its window, the count and the threshold.
type overThreshold struct { type overThreshold struct {
kind, window string kind ratelimit.LimitKind
window string
count float64 count float64
threshold int64 threshold int64
} }
@@ -346,7 +347,8 @@ func (c *Counter) add(
// bucketCount is one of a counter's four counts: requests or bytes, in a // bucketCount is one of a counter's four counts: requests or bytes, in a
// window of length, and the buckets they are counted in. // window of length, and the buckets they are counted in.
type bucketCount struct { type bucketCount struct {
kind, window string kind ratelimit.LimitKind
window string
length time.Duration length time.Duration
buckets *ratelimit.Buckets buckets *ratelimit.Buckets
} }
@@ -404,8 +406,8 @@ func (k key) counter() *Counter {
// counted, the count and the threshold. // counted, the count and the threshold.
func alertFor(r Request, k key, o overThreshold) alerts.Alert { func alertFor(r Request, k key, o overThreshold) alerts.Alert {
detail := map[string]any{ detail := map[string]any{
"scope": k.scope, "window": o.window, "kind": o.kind, "count": o.count, "scope": k.scope, "window": o.window, "kind": string(o.kind),
"threshold": o.threshold, "count": o.count, "threshold": o.threshold,
} }
var counted string var counted string
+21 -14
View File
@@ -16,20 +16,26 @@ import (
"time" "time"
"github.com/hashicorp/golang-lru/v2/simplelru" "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. // The causes of bans.
const ( const (
// CauseLimit is a ban sww made for a broken limit. // 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 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 // CauseAdmin is a ban an admin made, or one sww made that an
// admin keeps. It is never dropped. // admin keeps. It is never dropped.
CauseAdmin = "admin" CauseAdmin Cause = "admin"
// CauseAbuseIPDB is a ban sww made for a client on AbuseIPDB's // CauseAbuseIPDB is a ban sww made for a client on AbuseIPDB's
// blacklist. It is forgotten once it has ended. // 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 // 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 Start time.Time
// Expires is when the ban ends, zero for a permanent ban. // Expires is when the ban ends, zero for a permanent ban.
Expires time.Time Expires time.Time
// Cause is CauseLimit, CauseAttack, CauseAdmin or CauseAbuseIPDB. Cause Cause
Cause string
// Reason is a short text: for a ban sww made, the limit broken, the // 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 // rule that matched or AbuseIPDB's score; for an admin's, what the
// admin wrote. // admin wrote.
@@ -111,7 +116,7 @@ type Notes struct {
// request that broke the limit included. // request that broke the limit included.
// These are what counted toward the ban, and the window is the time // These are what counted toward the ban, and the window is the time
// over which they came. // over which they came.
Kind string `json:"kind,omitempty"` Kind ratelimit.LimitKind `json:"kind,omitempty"`
Limit int64 `json:"limit,omitempty"` Limit int64 `json:"limit,omitempty"`
Window string `json:"window,omitempty"` Window string `json:"window,omitempty"`
Count float64 `json:"count,omitempty"` Count float64 `json:"count,omitempty"`
@@ -194,7 +199,7 @@ type Ledger struct {
// made is how many bans have been made since the start, by cause: by // 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 // the ledger, and by an admin, through BanForAdmin or in an edit of
// bans.json. // bans.json.
made map[string]int made map[Cause]int
// v4Lengths and v6Lengths are the lengths of the IPv4 and IPv6 // v4Lengths and v6Lengths are the lengths of the IPv4 and IPv6
// netblocks that have been banned. Check looks for a ban at each of // 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 // 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, params: params,
changed: make(chan struct{}, 1), changed: make(chan struct{}, 1),
netblocks: netblocks, 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 // Grounds are why a request makes a ban: its cause, CauseLimit or
// CauseAttack, and its notes. // CauseAttack, and its notes.
type Grounds struct { type Grounds struct {
Cause string Cause Cause
Notes Notes Notes Notes
} }
@@ -345,7 +350,7 @@ func (l *Ledger) WouldBan(
// or CauseAttack, made at now would be permanent, as Ban would make it. It // or CauseAttack, made at now would be permanent, as Ban would make it. It
// works out nothing else of the ban. // works out nothing else of the ban.
func (l *Ledger) WouldBePermanent( func (l *Ledger) WouldBePermanent(
netblock netip.Prefix, now time.Time, cause string, netblock netip.Prefix, now time.Time, cause Cause,
) bool { ) bool {
l.mu.Lock() l.mu.Lock()
defer l.mu.Unlock() defer l.mu.Unlock()
@@ -439,7 +444,7 @@ func (l *Ledger) BanForAbuseIPDB(
// The source is named as the request log names AbuseIPDB. // The source is named as the request log names AbuseIPDB.
notes.Reputation = []ReputationHit{ notes.Reputation = []ReputationHit{
{Source: CauseAbuseIPDB, Score: &score}, {Source: string(CauseAbuseIPDB), Score: &score},
} }
l.add(Ban{ l.add(Ban{
@@ -577,7 +582,7 @@ func (l *Ledger) AddLookup(
// for CauseLimit, CauseAttack and CauseAbuseIPDB, by the ledger; for // for CauseLimit, CauseAttack and CauseAbuseIPDB, by the ledger; for
// CauseAdmin, by an admin, with BanForAdmin or in an edit of bans.json, as // 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. // 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() l.mu.Lock()
defer l.mu.Unlock() defer l.mu.Unlock()
@@ -770,8 +775,10 @@ func (l *Ledger) ban(
case CauseAttack: case CauseAttack:
ban.Reason = attackReason(notes) ban.Reason = attackReason(notes)
ban.Expires = l.attackExpiry(held, now) ban.Expires = l.attackExpiry(held, now)
case CauseAdmin, CauseAbuseIPDB:
fallthrough
default: 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 { if !keep {
+9 -4
View File
@@ -10,6 +10,8 @@ import (
"time" "time"
"github.com/spf13/viper" "github.com/spf13/viper"
"sneak.berlin/go/smallwebwaf/internal/wafprotocol"
) )
// RProxy is the settings of `sww rproxy`. A size or time limit of zero // RProxy is the settings of `sww rproxy`. A size or time limit of zero
@@ -32,7 +34,7 @@ type RProxy struct {
// cannot be connected to, does not answer at all, or does not answer // cannot be connected to, does not answer at all, or does not answer
// within WAFBackendTimeout: allow or deny // within WAFBackendTimeout: allow or deny
// (SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION). // (SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION).
WAFBackendNoAnswerDisposition string WAFBackendNoAnswerDisposition wafprotocol.Disposition
// MaxRequestSize is the largest request body (SWW_RPROXY_MAX_REQUEST_SIZE), and // MaxRequestSize is the largest request body (SWW_RPROXY_MAX_REQUEST_SIZE), and
// MaxResponseSize the largest response body (SWW_RPROXY_MAX_RESPONSE_SIZE). // MaxResponseSize the largest response body (SWW_RPROXY_MAX_RESPONSE_SIZE).
MaxRequestSize int64 MaxRequestSize int64
@@ -166,9 +168,12 @@ func (e *environment) wafBackend(name string) *url.URL {
// disposition reads the setting that is what is done with a request when waf // disposition reads the setting that is what is done with a request when waf
// cannot be connected to, does not answer at all, or does not answer in time: // cannot be connected to, does not answer at all, or does not answer in time:
// allow or deny. // allow or deny.
func (e *environment) disposition(name, defaultValue string) string { func (e *environment) disposition(
value := e.value(name, defaultValue) name, defaultValue string,
if value != "allow" && value != "deny" { ) wafprotocol.Disposition {
value := wafprotocol.Disposition(e.value(name, defaultValue))
if value != wafprotocol.DispositionAllow &&
value != wafprotocol.DispositionDeny {
e.check(name, fmt.Errorf("%q %w", value, errNotDisposition)) e.check(name, fmt.Errorf("%q %w", value, errNotDisposition))
} }
+19 -10
View File
@@ -305,7 +305,7 @@ type WAF struct {
AlertSlackWebhookURL *url.URL AlertSlackWebhookURL *url.URL
AlertNtfyURL *url.URL AlertNtfyURL *url.URL
AlertNtfyToken string AlertNtfyToken string
AlertEvents []string AlertEvents []alerts.Event
AlertCooldown time.Duration AlertCooldown time.Duration
AlertMaxPerHour int AlertMaxPerHour int
// The anomaly thresholds, which only raise alerts: the most requests // The anomaly thresholds, which only raise alerts: the most requests
@@ -789,8 +789,7 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
), ),
AlertNtfyURL: env.webhookURL("SWW_WAF_ALERT_NTFY_URL"), AlertNtfyURL: env.webhookURL("SWW_WAF_ALERT_NTFY_URL"),
AlertNtfyToken: env.secret("SWW_WAF_ALERT_NTFY_TOKEN"), AlertNtfyToken: env.secret("SWW_WAF_ALERT_NTFY_TOKEN"),
AlertEvents: env.alertEvents("SWW_WAF_ALERT_EVENTS", AlertEvents: env.alertEvents("SWW_WAF_ALERT_EVENTS"),
strings.Join(alerts.Events(), ",")),
AlertCooldown: env.duration("SWW_WAF_ALERT_COOLDOWN", "15m"), AlertCooldown: env.duration("SWW_WAF_ALERT_COOLDOWN", "15m"),
AlertMaxPerHour: env.numberOrOff( AlertMaxPerHour: env.numberOrOff(
"SWW_WAF_ALERT_MAX_PER_HOUR", "SWW_WAF_ALERT_MAX_PER_HOUR",
@@ -1686,9 +1685,15 @@ func (e *environment) secret(name string) string {
return value return value
} }
// alertEvents reads the setting that is the events alerts are sent for. // alertEvents reads the setting that is the events alerts are sent for,
func (e *environment) alertEvents(name, defaultValue string) []string { // every event by default.
events, err := parseAlertEvents(e.value(name, defaultValue)) func (e *environment) alertEvents(name string) []alerts.Event {
every := make([]string, 0, len(alerts.Events()))
for _, event := range alerts.Events() {
every = append(every, string(event))
}
events, err := parseAlertEvents(e.value(name, strings.Join(every, ",")))
e.check(name, err) e.check(name, err)
return events return events
@@ -2548,16 +2553,20 @@ func parseWebhookHeaders(value string) (http.Header, string, error) {
// parseAlertEvents reads a comma-separated list of the events alerts can // parseAlertEvents reads a comma-separated list of the events alerts can
// be sent for. // be sent for.
func parseAlertEvents(value string) ([]string, error) { func parseAlertEvents(value string) ([]alerts.Event, error) {
events, err := parseList(value) names, err := parseList(value)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, event := range events { events := make([]alerts.Event, 0, len(names))
for _, name := range names {
event := alerts.Event(name)
if !slices.Contains(alerts.Events(), event) { if !slices.Contains(alerts.Events(), event) {
return nil, fmt.Errorf("%q %w", event, errNotAlertEvent) return nil, fmt.Errorf("%q %w", name, errNotAlertEvent)
} }
events = append(events, event)
} }
return events, nil return events, nil
+16 -2
View File
@@ -20,6 +20,7 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
"golang.org/x/net/http/httpguts" "golang.org/x/net/http/httpguts"
"sneak.berlin/go/smallwebwaf/internal/alerts"
"sneak.berlin/go/smallwebwaf/internal/anomaly" "sneak.berlin/go/smallwebwaf/internal/anomaly"
"sneak.berlin/go/smallwebwaf/internal/config" "sneak.berlin/go/smallwebwaf/internal/config"
"sneak.berlin/go/smallwebwaf/internal/config/configtest" "sneak.berlin/go/smallwebwaf/internal/config/configtest"
@@ -188,6 +189,16 @@ const (
defaultAlertCooldown = "15m" defaultAlertCooldown = "15m"
) )
// joinEvents returns events as SWW_WAF_ALERT_EVENTS lists them.
func joinEvents(events []alerts.Event) string {
names := make([]string, 0, len(events))
for _, event := range events {
names = append(names, string(event))
}
return strings.Join(names, ",")
}
// defaultIPReputationCacheTTL is the default of SWW_WAF_IP_REPUTATION_CACHE_TTL. // defaultIPReputationCacheTTL is the default of SWW_WAF_IP_REPUTATION_CACHE_TTL.
const defaultIPReputationCacheTTL = "24h" const defaultIPReputationCacheTTL = "24h"
@@ -1110,7 +1121,7 @@ func TestAlertSettingsDefaults(t *testing.T) {
if cfg.AlertWebhookURL != nil || len(cfg.AlertWebhookHeaders) != 0 || if cfg.AlertWebhookURL != nil || len(cfg.AlertWebhookHeaders) != 0 ||
cfg.AlertSlackWebhookURL != nil || cfg.AlertNtfyURL != nil || cfg.AlertSlackWebhookURL != nil || cfg.AlertNtfyURL != nil ||
cfg.AlertNtfyToken != "" || cfg.AlertNtfyToken != "" ||
strings.Join(cfg.AlertEvents, ",") != defaultAlertEvents || joinEvents(cfg.AlertEvents) != defaultAlertEvents ||
cfg.AlertCooldown != 15*time.Minute || cfg.AlertMaxPerHour != 60 { cfg.AlertCooldown != 15*time.Minute || cfg.AlertMaxPerHour != 60 {
t.Errorf( t.Errorf(
"alert settings %v, %v, %v, %v, %q, %v, %s and %d, want no URLs, "+ "alert settings %v, %v, %v, %v, %q, %v, %s and %d, want no URLs, "+
@@ -1157,7 +1168,10 @@ func TestAlertSettingsAsSet(t *testing.T) {
!reflect.DeepEqual(cfg.AlertWebhookHeaders, headers) || !reflect.DeepEqual(cfg.AlertWebhookHeaders, headers) ||
cfg.AlertSlackWebhookURL.String() != slack || cfg.AlertNtfyURL.String() != ntfy || cfg.AlertSlackWebhookURL.String() != slack || cfg.AlertNtfyURL.String() != ntfy ||
cfg.AlertNtfyToken != token || cfg.AlertNtfyToken != token ||
!slices.Equal(cfg.AlertEvents, []string{"ban", "file_error"}) || !slices.Equal(
cfg.AlertEvents,
[]alerts.Event{"ban", "file_error"},
) ||
cfg.AlertCooldown != time.Hour || cfg.AlertMaxPerHour != 10 { cfg.AlertCooldown != time.Hour || cfg.AlertMaxPerHour != 10 {
t.Errorf( t.Errorf(
"alert settings %v, %v, %v, %v, %q, %v, %s and %d", "alert settings %v, %v, %v, %v, %q, %v, %s and %d",
+14 -12
View File
@@ -17,6 +17,7 @@ import (
"sneak.berlin/go/smallwebwaf/internal/ratelimit" "sneak.berlin/go/smallwebwaf/internal/ratelimit"
"sneak.berlin/go/smallwebwaf/internal/reputation" "sneak.berlin/go/smallwebwaf/internal/reputation"
"sneak.berlin/go/smallwebwaf/internal/requestlog" "sneak.berlin/go/smallwebwaf/internal/requestlog"
"sneak.berlin/go/smallwebwaf/internal/rules"
) )
// Params are what New builds the metrics from. Each part is nil while it is // Params are what New builds the metrics from. Each part is nil while it is
@@ -279,7 +280,7 @@ func (m *Metrics) RequestReported(
// LimitHit counts a request that broke a rate limit, a byte limit or the // LimitHit counts a request that broke a rate limit, a byte limit or the
// refusal limit, by the window and the kind of hit. // refusal limit, by the window and the kind of hit.
func (m *Metrics) LimitHit(hit ratelimit.Hit) { func (m *Metrics) LimitHit(hit ratelimit.Hit) {
m.rateLimitHits.WithLabelValues(hit.Window, hit.Kind).Inc() m.rateLimitHits.WithLabelValues(hit.Window, string(hit.Kind)).Inc()
} }
// Offences counts the offences of r, a request that has ended, as its // Offences counts the offences of r, a request that has ended, as its
@@ -300,8 +301,8 @@ func (m *Metrics) Offences(r ratelimit.Request) {
// RuleMatched counts a request that matched the rule id, whose action is // RuleMatched counts a request that matched the rule id, whose action is
// action. // action.
func (m *Metrics) RuleMatched(id, action string) { func (m *Metrics) RuleMatched(id string, action rules.Action) {
m.ruleMatches.WithLabelValues(id, action).Inc() m.ruleMatches.WithLabelValues(id, string(action)).Inc()
} }
// CoreRuleSetMatched counts a request that matched the Core Rule Set's rule id, // CoreRuleSetMatched counts a request that matched the Core Rule Set's rule id,
@@ -350,7 +351,7 @@ func (m *Metrics) StateFileEditSetAside(name string) {
// banLedger is what the metrics read from the ledger of bans. // banLedger is what the metrics read from the ledger of bans.
type banLedger interface { type banLedger interface {
// Made returns the bans made since the start with cause. // Made returns the bans made since the start with cause.
Made(cause string) int Made(cause bans.Cause) int
// Count returns the bans active at now, the permanent ones included, // Count returns the bans active at now, the permanent ones included,
// and the permanent ones. // and the permanent ones.
Count(now time.Time) (active, permanent int) Count(now time.Time) (active, permanent int)
@@ -360,14 +361,14 @@ type banLedger interface {
// the bans made since the start, by cause, and the bans active and // the bans made since the start, by cause, and the bans active and
// permanent at now. // permanent at now.
func (m *Metrics) addBans(ledger banLedger, now func() time.Time) { func (m *Metrics) addBans(ledger banLedger, now func() time.Time) {
for _, cause := range []string{ for _, cause := range []bans.Cause{
bans.CauseLimit, bans.CauseAttack, bans.CauseAdmin, bans.CauseAbuseIPDB, bans.CauseLimit, bans.CauseAttack, bans.CauseAdmin, bans.CauseAbuseIPDB,
} { } {
m.registry.MustRegister( m.registry.MustRegister(
prometheus.NewCounterFunc(prometheus.CounterOpts{ prometheus.NewCounterFunc(prometheus.CounterOpts{
Name: "sww_bans_made_total", Name: "sww_bans_made_total",
Help: "Bans made, by cause.", Help: "Bans made, by cause.",
ConstLabels: prometheus.Labels{"cause": cause}, ConstLabels: prometheus.Labels{"cause": string(cause)},
}, func() float64 { }, func() float64 {
return float64(ledger.Made(cause)) return float64(ledger.Made(cause))
}), }),
@@ -601,10 +602,10 @@ func (m *Metrics) addAbuseIPDB(abuseIPDB abuseIPDBChecker) {
// alertQueue is what the metrics read from the queue of alerts. // alertQueue is what the metrics read from the queue of alerts.
type alertQueue interface { type alertQueue interface {
// DestinationsSet returns the names of the destinations set. // DestinationsSet returns the names of the destinations set.
DestinationsSet() []string DestinationsSet() []alerts.DestinationName
// Counts returns the alerts sent to the destination name, the requests // Counts returns the alerts sent to the destination name, the requests
// to it that failed, and the alerts dropped. // to it that failed, and the alerts dropped.
Counts(name string) alerts.Counts Counts(name alerts.DestinationName) alerts.Counts
// Suppressed returns the alerts held back. // Suppressed returns the alerts held back.
Suppressed() int64 Suppressed() int64
} }
@@ -616,7 +617,7 @@ type alertQueue interface {
// dropped. With no destination set, it adds none. // dropped. With no destination set, it adds none.
func (m *Metrics) addAlerts(queue alertQueue) { func (m *Metrics) addAlerts(queue alertQueue) {
for _, name := range queue.DestinationsSet() { for _, name := range queue.DestinationsSet() {
destination := prometheus.Labels{"destination": name} destination := prometheus.Labels{"destination": string(name)}
m.registry.MustRegister( m.registry.MustRegister(
prometheus.NewCounterFunc(prometheus.CounterOpts{ prometheus.NewCounterFunc(prometheus.CounterOpts{
@@ -659,10 +660,11 @@ func (m *Metrics) count(
line *requestlog.Line, duration, upstreamDuration time.Duration, line *requestlog.Line, duration, upstreamDuration time.Duration,
) { ) {
class := statusClass(line.Status) class := statusClass(line.Status)
m.requests.WithLabelValues(class, line.Action).Inc() action := string(line.Action)
m.requestBytes.WithLabelValues(class, line.Action). m.requests.WithLabelValues(class, action).Inc()
m.requestBytes.WithLabelValues(class, action).
Add(float64(line.RequestBytes)) Add(float64(line.RequestBytes))
m.responseBytes.WithLabelValues(class, line.Action). m.responseBytes.WithLabelValues(class, action).
Add(float64(line.ResponseBytes)) Add(float64(line.ResponseBytes))
m.requestDuration.Observe(duration.Seconds()) m.requestDuration.Observe(duration.Seconds())
+5 -4
View File
@@ -8,6 +8,7 @@ import (
"time" "time"
"sneak.berlin/go/smallwebwaf/internal/alerts" "sneak.berlin/go/smallwebwaf/internal/alerts"
"sneak.berlin/go/smallwebwaf/internal/bans"
"sneak.berlin/go/smallwebwaf/internal/metrics" "sneak.berlin/go/smallwebwaf/internal/metrics"
) )
@@ -22,7 +23,7 @@ const (
type ledgerStandIn struct{} type ledgerStandIn struct{}
func (ledgerStandIn) Made(string) int { return 1 } func (ledgerStandIn) Made(bans.Cause) int { return 1 }
func (ledgerStandIn) Count(time.Time) (int, int) { return 2, 1 } func (ledgerStandIn) Count(time.Time) (int, int) { return 2, 1 }
@@ -60,11 +61,11 @@ func (abuseIPDBStandIn) BudgetLeft() int { return 7 }
type alertQueueStandIn struct{} type alertQueueStandIn struct{}
func (alertQueueStandIn) DestinationsSet() []string { func (alertQueueStandIn) DestinationsSet() []alerts.DestinationName {
return []string{destination} return []alerts.DestinationName{destination}
} }
func (alertQueueStandIn) Counts(string) alerts.Counts { func (alertQueueStandIn) Counts(alerts.DestinationName) alerts.Counts {
return alerts.Counts{Sent: 8} return alerts.Counts{Sent: 8}
} }
+8 -6
View File
@@ -19,15 +19,19 @@ import (
const day = 24 * time.Hour const day = 24 * time.Hour
// LimitKind is what a limit counts: KindRequests, KindBytes or
// KindRefusals.
type LimitKind string
// The kinds of limits, as the metrics name them. // The kinds of limits, as the metrics name them.
const ( const (
// KindRequests is a rate limit, on a client's requests. // KindRequests is a rate limit, on a client's requests.
KindRequests = "requests" KindRequests LimitKind = "requests"
// KindBytes is a byte limit, on a client's bytes. // KindBytes is a byte limit, on a client's bytes.
KindBytes = "bytes" KindBytes LimitKind = "bytes"
// KindRefusals is the refusal limit, on a client's requests sww // KindRefusals is the refusal limit, on a client's requests sww
// refused after a rule file match or for a missing or wrong token. // refused after a rule file match or for a missing or wrong token.
KindRefusals = "refusals" KindRefusals LimitKind = "refusals"
) )
// Limits are the most requests a client may make in a minute, an hour and // Limits are the most requests a client may make in a minute, an hour and
@@ -200,9 +204,7 @@ func New(params Params) *Limiter {
// Hit is a request that takes a client over a rate limit or the refusal // Hit is a request that takes a client over a rate limit or the refusal
// limit, or whose bytes take it over a byte limit. // limit, or whose bytes take it over a byte limit.
type Hit struct { type Hit struct {
// Kind is KindRequests for a rate limit, KindBytes for a byte limit, Kind LimitKind
// KindRefusals for the refusal limit.
Kind string
// Window is "minute", "hour" or "day". // Window is "minute", "hour" or "day".
Window string Window string
// Limit is the window's limit, as the client's percentage of it. // Limit is the window's limit, as the client's percentage of it.
+1 -1
View File
@@ -652,7 +652,7 @@ func wantBytesCount(
client netip.Prefix, client netip.Prefix,
now time.Time, now time.Time,
bytes int64, bytes int64,
want string, want ratelimit.LimitKind,
) { ) {
t.Helper() t.Helper()
+33 -23
View File
@@ -12,82 +12,94 @@ import (
"time" "time"
"sneak.berlin/go/smallwebwaf/internal/ratelimit" "sneak.berlin/go/smallwebwaf/internal/ratelimit"
"sneak.berlin/go/smallwebwaf/internal/wafprotocol"
) )
// Action is what waf did with a request, as its request line names it,
// such as ActionForward.
type Action string
// The action a request line names: what waf did with the request. // The action a request line names: what waf did with the request.
const ( const (
// ActionForward is a request waf allowed, which rproxy passes to // ActionForward is a request waf allowed, which rproxy passes to
// the app. // the app.
ActionForward = "forward" ActionForward Action = "forward"
// ActionRequestLineTooLong is a request whose request line is too long for // ActionRequestLineTooLong is a request whose request line is too long for
// the header limits. // the header limits.
ActionRequestLineTooLong = "request_line_too_long" ActionRequestLineTooLong Action = "request_line_too_long"
// ActionRequestHeaderTooLarge is a request whose headers are over the // ActionRequestHeaderTooLarge is a request whose headers are over the
// header limits. // header limits.
ActionRequestHeaderTooLarge = "request_header_too_large" ActionRequestHeaderTooLarge Action = "request_header_too_large"
// ActionClientIsRateLimited is a request refused because it took its // ActionClientIsRateLimited is a request refused because it took its
// client over a rate limit, which bans the client. // client over a rate limit, which bans the client.
ActionClientIsRateLimited = "client_is_rate_limited" ActionClientIsRateLimited Action = "client_is_rate_limited"
// ActionTemporarilyBanned is a request refused because a ban that runs // ActionTemporarilyBanned is a request refused because a ban that runs
// out covers its client, or because it matched a ban rule or asked for a // out covers its client, or because it matched a ban rule or asked for a
// trap path, and so made such a ban. // trap path, and so made such a ban.
ActionTemporarilyBanned = "temporarily_banned" ActionTemporarilyBanned Action = "temporarily_banned"
// ActionPermanentlyBanned is a request refused because a ban that never // ActionPermanentlyBanned is a request refused because a ban that never
// runs out covers its client, or because the request made such a ban or // runs out covers its client, or because the request made such a ban or
// made its client's ban permanent. // made its client's ban permanent.
ActionPermanentlyBanned = "permanently_banned" ActionPermanentlyBanned Action = "permanently_banned"
// ActionBlockedByBlockRule is a request refused because it matched a // ActionBlockedByBlockRule is a request refused because it matched a
// block rule. // block rule.
ActionBlockedByBlockRule = "blocked_by_block_rule" ActionBlockedByBlockRule Action = "blocked_by_block_rule"
// ActionBlockedByCoreRuleSet is a request refused because the Core Rule // ActionBlockedByCoreRuleSet is a request refused because the Core Rule
// Set scored it at or over SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD. // Set scored it at or over SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD.
ActionBlockedByCoreRuleSet = "blocked_by_core_rule_set" ActionBlockedByCoreRuleSet Action = "blocked_by_core_rule_set"
// ActionClientDenied is a request refused because its client is in // ActionClientDenied is a request refused because its client is in
// SWW_WAF_DENY_NETS, in a blocklist while SWW_WAF_BLOCKLIST_ACTION is deny, // SWW_WAF_DENY_NETS, in a blocklist while SWW_WAF_BLOCKLIST_ACTION is deny,
// or listed by a DNSBL zone, or scored a hit by AbuseIPDB, while // or listed by a DNSBL zone, or scored a hit by AbuseIPDB, while
// SWW_WAF_DNSBL_ABUSEIPDB_ACTION is deny, or that carries a tag in // SWW_WAF_DNSBL_ABUSEIPDB_ACTION is deny, or that carries a tag in
// SWW_WAF_TAG_DENY. // SWW_WAF_TAG_DENY.
ActionClientDenied = "client_denied" ActionClientDenied Action = "client_denied"
// ActionCountryDenied is a request refused because its client's country // ActionCountryDenied is a request refused because its client's country
// is not in SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES. // is not in SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES.
ActionCountryDenied = "country_denied" ActionCountryDenied Action = "country_denied"
// ActionChecksTimedOut is a request refused because the trap paths, the // ActionChecksTimedOut is a request refused because the trap paths, the
// rule files and the Core Rule Set had not finished with it within 500 // rule files and the Core Rule Set had not finished with it within 500
// milliseconds. // milliseconds.
ActionChecksTimedOut = "checks_timed_out" ActionChecksTimedOut Action = "checks_timed_out"
// ActionOwnEndpoint is a request waf answered at one of its own // ActionOwnEndpoint is a request waf answered at one of its own
// endpoints, or a question about a path under /_sww/. // endpoints, or a question about a path under /_sww/.
ActionOwnEndpoint = "own_endpoint" ActionOwnEndpoint Action = "own_endpoint"
) )
// Offence is what a request was held as, as its request line names it.
type Offence string
// OffenceLimit is the offence a request line names for a request that // OffenceLimit is the offence a request line names for a request that
// broke a rate limit or the refusal limit, or whose bytes broke a byte // broke a rate limit or the refusal limit, or whose bytes broke a byte
// limit. // limit.
const OffenceLimit = "limit" const OffenceLimit Offence = "limit"
// LimitHitMinuteRefusals is the limit_hit a request line names for a request // LimitHitMinuteRefusals is the limit_hit a request line names for a request
// that broke the refusal limit. // that broke the refusal limit.
const LimitHitMinuteRefusals = "minute_refusals" const LimitHitMinuteRefusals = "minute_refusals"
// WAFLineKind is which of waf's two lines about a question a request line
// is: WAFLineDecision or WAFLineReport.
type WAFLineKind string
// The waf_line a request line names, which says which of // The waf_line a request line names, which says which of
// waf's two lines about a question it is. // waf's two lines about a question it is.
const ( const (
// WAFLineDecision is waf's line at its decision. // WAFLineDecision is waf's line at its decision.
WAFLineDecision = "decision" WAFLineDecision WAFLineKind = "decision"
// WAFLineReport is waf's line when rproxy's report // WAFLineReport is waf's line when rproxy's report
// comes, with the decision's fields and the report's together. // comes, with the decision's fields and the report's together.
WAFLineReport = "report" WAFLineReport WAFLineKind = "report"
) )
// timeLayout is RFC 3339 with milliseconds. // timeLayout is RFC 3339 with milliseconds.
@@ -100,9 +112,8 @@ const timeLayout = "2006-01-02T15:04:05.000Z07:00"
//nolint:tagliatelle // docs/request-log.md names the fields in snake_case //nolint:tagliatelle // docs/request-log.md names the fields in snake_case
type Line struct { type Line struct {
Type string `json:"type"` Type string `json:"type"`
// WAFLine is WAFLineDecision or WAFLineReport // WAFLine is set on waf's two lines about a question.
// on waf's two lines about a question. WAFLine WAFLineKind `json:"waf_line,omitempty"`
WAFLine string `json:"waf_line,omitempty"`
// The standard web log fields. Scheme is how the client reached rproxy, or // The standard web log fields. Scheme is how the client reached rproxy, or
// the trusted proxy in front of it. // the trusted proxy in front of it.
@@ -148,7 +159,7 @@ type Line struct {
Aborted bool `json:"aborted,omitempty"` Aborted bool `json:"aborted,omitempty"`
// The decision. // The decision.
Action string `json:"action"` Action Action `json:"action"`
// WouldAction is, in observe mode, the action enforce mode would have // WouldAction is, in observe mode, the action enforce mode would have
// taken with a request it would have refused: ActionClientDenied, // taken with a request it would have refused: ActionClientDenied,
// ActionTemporarilyBanned, ActionPermanentlyBanned, ActionCountryDenied, // ActionTemporarilyBanned, ActionPermanentlyBanned, ActionCountryDenied,
@@ -156,7 +167,7 @@ type Line struct {
// ActionBlockedByCoreRuleSet, or, in waf, ActionRequestLineTooLong or // ActionBlockedByCoreRuleSet, or, in waf, ActionRequestLineTooLong or
// ActionRequestHeaderTooLarge for the header limits, or // ActionRequestHeaderTooLarge for the header limits, or
// ActionChecksTimedOut. // ActionChecksTimedOut.
WouldAction string `json:"would_action,omitempty"` WouldAction Action `json:"would_action,omitempty"`
// Heuristic is the name of the heuristic that refused the request, in // Heuristic is the name of the heuristic that refused the request, in
// observe mode too, and HeuristicFigures are the figures it acted on, by // observe mode too, and HeuristicFigures are the figures it acted on, by
// name. // name.
@@ -203,8 +214,7 @@ type Line struct {
// request got no tags from routewatch: no_answer_yet, request_failed or // request got no tags from routewatch: no_answer_yet, request_failed or
// no_route. // no_route.
RouteWatchTagsMissing string `json:"routewatch_tags_missing,omitempty"` RouteWatchTagsMissing string `json:"routewatch_tags_missing,omitempty"`
// Offence is the offence the request was held as, OffenceLimit. Offence Offence `json:"offence,omitempty"`
Offence string `json:"offence,omitempty"`
// BanExpires is when the ban the request made, or was refused under, // BanExpires is when the ban the request made, or was refused under,
// ends: a time, or "permanent". // ends: a time, or "permanent".
BanExpires string `json:"ban_expires,omitempty"` BanExpires string `json:"ban_expires,omitempty"`
@@ -246,7 +256,7 @@ type RProxyLine struct {
// WAFBackendNoAnswer is true, the value of // WAFBackendNoAnswer is true, the value of
// SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION. It is "" when neither // SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION. It is "" when neither
// gave one. // gave one.
Disposition string `json:"disposition,omitempty"` Disposition wafprotocol.Disposition `json:"disposition,omitempty"`
WAFBackendNoAnswer bool `json:"waf_backend_no_answer,omitempty"` WAFBackendNoAnswer bool `json:"waf_backend_no_answer,omitempty"`
} }
+1 -1
View File
@@ -63,7 +63,7 @@ type request struct {
// disposition is what is done with the request: waf's valid disposition, // disposition is what is done with the request: waf's valid disposition,
// or SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION. It is "" when neither // or SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION. It is "" when neither
// gave one. // gave one.
disposition string disposition wafprotocol.Disposition
// answered is true when waf gave disposition, which makes a report due, // answered is true when waf gave disposition, which makes a report due,
// and wafNoAnswer when SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION did. // and wafNoAnswer when SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION did.
answered bool answered bool
+2 -1
View File
@@ -216,7 +216,8 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// This panic has Go's server close the connection without an answer, // This panic has Go's server close the connection without an answer,
// and log nothing; the deferred finish still runs. // and log nothing; the deferred finish still runs.
panic(http.ErrAbortHandler) panic(http.ErrAbortHandler)
default: // a deny, an error, or an answer without a valid disposition case wafprotocol.DispositionDeny, "":
// "" is an error, or no valid disposition
rq.answerWith(answer) rq.answerWith(answer)
} }
} }
+3 -3
View File
@@ -72,9 +72,9 @@ const (
// What the stand-in for waf answers. // What the stand-in for waf answers.
const ( const (
dispositionAllow = wafprotocol.DispositionAllow dispositionAllow = string(wafprotocol.DispositionAllow)
dispositionDeny = wafprotocol.DispositionDeny dispositionDeny = string(wafprotocol.DispositionDeny)
dispositionClose = wafprotocol.DispositionClose dispositionClose = string(wafprotocol.DispositionClose)
// deniedBody is the body of a deny. // deniedBody is the body of a deny.
deniedBody = "refused by waf" deniedBody = "refused by waf"
) )
+10 -7
View File
@@ -25,15 +25,19 @@ import (
"sneak.berlin/go/smallwebwaf/internal/dirwatch" "sneak.berlin/go/smallwebwaf/internal/dirwatch"
) )
// Action is what a rule does when it matches: ActionLog, ActionBlock or
// ActionBan.
type Action string
// The actions a rule takes when it matches. // The actions a rule takes when it matches.
const ( const (
// ActionLog notes the match in the request log, and does nothing else. // ActionLog notes the match in the request log, and does nothing else.
ActionLog = "log" ActionLog Action = "log"
// ActionBlock refuses the request with 403. // ActionBlock refuses the request with 403.
ActionBlock = "block" ActionBlock Action = "block"
// ActionBan refuses the request and bans the client's netblock: the // ActionBan refuses the request and bans the client's netblock: the
// request is a clear sign of attack. // request is a clear sign of attack.
ActionBan = "ban" ActionBan Action = "ban"
) )
// extension ends the name of every rule file. // extension ends the name of every rule file.
@@ -88,8 +92,7 @@ type Rule struct {
// Target is what the regex is matched against, such as path or // Target is what the regex is matched against, such as path or
// header:Accept. // header:Accept.
Target string Target string
// Action is ActionLog, ActionBlock or ActionBan. Action Action
Action string
regex *regexp.Regexp regex *regexp.Regexp
} }
@@ -337,7 +340,7 @@ func parse(line string) (Rule, bool, error) {
return Rule{}, false, errNotRule return Rule{}, false, errNotRule
} }
rule := Rule{ID: fields[1], Target: fields[2], Action: fields[3]} rule := Rule{ID: fields[1], Target: fields[2], Action: Action(fields[3])}
headerName, isHeader := strings.CutPrefix(rule.Target, headerTarget) headerName, isHeader := strings.CutPrefix(rule.Target, headerTarget)
switch { switch {
@@ -365,7 +368,7 @@ func parse(line string) (Rule, bool, error) {
rule.Target, rule.Target,
errHeaderTakenOut, errHeaderTakenOut,
) )
case !slices.Contains([]string{ActionLog, ActionBlock, ActionBan}, rule.Action): case !slices.Contains([]Action{ActionLog, ActionBlock, ActionBan}, rule.Action):
return Rule{}, false, fmt.Errorf( return Rule{}, false, fmt.Errorf(
"the action %q %w", "the action %q %w",
rule.Action, rule.Action,
+2 -2
View File
@@ -181,7 +181,7 @@ type BanEntry struct {
Netblock netip.Prefix `json:"netblock"` Netblock netip.Prefix `json:"netblock"`
Start time.Time `json:"start"` Start time.Time `json:"start"`
Expires *time.Time `json:"expires"` Expires *time.Time `json:"expires"`
Cause string `json:"cause"` Cause bans.Cause `json:"cause"`
Reason string `json:"reason,omitempty"` Reason string `json:"reason,omitempty"`
Lifted *time.Time `json:"lifted,omitempty"` Lifted *time.Time `json:"lifted,omitempty"`
Notes bans.Notes `json:"notes"` Notes bans.Notes `json:"notes"`
@@ -240,7 +240,7 @@ type alertsFile struct {
Version int `json:"version"` Version int `json:"version"`
Cooldowns []alerts.Cooldown `json:"cooldowns"` Cooldowns []alerts.Cooldown `json:"cooldowns"`
Hour alerts.Hour `json:"hour"` Hour alerts.Hour `json:"hour"`
Waiting map[string][]alerts.Alert `json:"waiting"` Waiting map[alerts.DestinationName][]alerts.Alert `json:"waiting"`
AnomalyCounters []anomaly.Counter `json:"anomaly_counters"` AnomalyCounters []anomaly.Counter `json:"anomaly_counters"`
} }
+3 -3
View File
@@ -1758,8 +1758,8 @@ func TestEditOfRouteWatchAndAlertsFilesTakenIn(t *testing.T) {
Event: alerts.EventBan, Netblock: netip.MustParsePrefix("198.51.100.0/24"), Event: alerts.EventBan, Netblock: netip.MustParsePrefix("198.51.100.0/24"),
Sent: midnight(), Sent: midnight(),
}}, }},
Hour: alerts.Hour{HeldBack: map[string]int{}}, Hour: alerts.Hour{HeldBack: map[alerts.Event]int{}},
Waiting: map[string][]alerts.Alert{ Waiting: map[alerts.DestinationName][]alerts.Alert{
alerts.DestinationWebhook: { alerts.DestinationWebhook: {
{Event: alerts.EventFileError, Time: midnight()}, {Event: alerts.EventFileError, Time: midnight()},
}, },
@@ -2046,7 +2046,7 @@ func TestBanWithoutACauseTakenInAsAnAdmins(t *testing.T) {
var written struct { var written struct {
Bans []struct { Bans []struct {
Cause string `json:"cause"` Cause bans.Cause `json:"cause"`
Reason string `json:"reason"` Reason string `json:"reason"`
} `json:"bans"` } `json:"bans"`
} }
+8 -8
View File
@@ -164,7 +164,7 @@ func TestABanThroughOneSiteRefusesTheClientOnTheOther(t *testing.T) {
// One request's three lines carry one id, rproxy's with waf's answer. // One request's three lines carry one id, rproxy's with waf's answer.
line := firstOut.line(t, "path", bannedProbe) line := firstOut.line(t, "path", bannedProbe)
if line["disposition"] != wafprotocol.DispositionDeny || if line["disposition"] != string(wafprotocol.DispositionDeny) ||
line["client_ip"] != placed { line["client_ip"] != placed {
t.Errorf("rproxy's line is %v", line) t.Errorf("rproxy's line is %v", line)
} }
@@ -249,7 +249,7 @@ func TestTheSiteAnswersWhileWAFIsStoppedAndStartedAgain(t *testing.T) {
wantStatus(t, site+"/after-stop", placed, http.StatusOK) wantStatus(t, site+"/after-stop", placed, http.StatusOK)
line := out.line(t, "path", "/after-stop") line := out.line(t, "path", "/after-stop")
if line["disposition"] != wafprotocol.DispositionAllow || if line["disposition"] != string(wafprotocol.DispositionAllow) ||
line["waf_backend_no_answer"] != true { line["waf_backend_no_answer"] != true {
t.Errorf("rproxy's line is %v", line) t.Errorf("rproxy's line is %v", line)
} }
@@ -608,8 +608,8 @@ func TestRProxyAsksAWAFThatSpeaksHTTP2OverHTTP1(t *testing.T) {
if r.Header.Get(wafprotocol.ClientHeader) != "" { if r.Header.Get(wafprotocol.ClientHeader) != "" {
protocols <- r.Proto protocols <- r.Proto
w.Header(). w.Header().Set(wafprotocol.DispositionHeader,
Set(wafprotocol.DispositionHeader, wafprotocol.DispositionDeny) string(wafprotocol.DispositionDeny))
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
} }
})) }))
@@ -692,8 +692,8 @@ func TestRProxyStoppingWaitsForAReportBeingSent(t *testing.T) {
backend := httptest.NewServer(http.HandlerFunc( backend := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) { func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get(wafprotocol.ClientHeader) != "" { if r.Header.Get(wafprotocol.ClientHeader) != "" {
w.Header(). w.Header().Set(wafprotocol.DispositionHeader,
Set(wafprotocol.DispositionHeader, wafprotocol.DispositionAllow) string(wafprotocol.DispositionAllow))
return return
} }
@@ -795,8 +795,8 @@ func TestRProxyStoppingCutsOffARequestAfterFiveSeconds(t *testing.T) {
backend := httptest.NewServer(http.HandlerFunc( backend := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, _ *http.Request) { func(w http.ResponseWriter, _ *http.Request) {
w.Header(). w.Header().Set(wafprotocol.DispositionHeader,
Set(wafprotocol.DispositionHeader, wafprotocol.DispositionAllow) string(wafprotocol.DispositionAllow))
})) }))
t.Cleanup(backend.Close) t.Cleanup(backend.Close)
+9 -4
View File
@@ -267,7 +267,12 @@ func runMain(t *testing.T, command string) (int, string, string) {
// ask sends waf at addr rproxy's question about a request for path from // ask sends waf at addr rproxy's question about a request for path from
// placed, checks the answer's status and disposition, and closes it. // placed, checks the answer's status and disposition, and closes it.
func ask(t *testing.T, addr, path string, status int, disposition string) { func ask(
t *testing.T,
addr, path string,
status int,
disposition wafprotocol.Disposition,
) {
t.Helper() t.Helper()
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, req, err := http.NewRequestWithContext(t.Context(), http.MethodGet,
@@ -290,12 +295,12 @@ func ask(t *testing.T, addr, path string, status int, disposition string) {
_, _ = io.Copy(io.Discard, res.Body) _, _ = io.Copy(io.Discard, res.Body)
_ = res.Body.Close() _ = res.Body.Close()
if res.StatusCode != status || answered := res.Header.Get(wafprotocol.DispositionHeader)
res.Header.Get(wafprotocol.DispositionHeader) != disposition { if res.StatusCode != status || answered != string(disposition) {
t.Errorf( t.Errorf(
"answered %d with disposition %q, want %d with %q", "answered %d with disposition %q, want %d with %q",
res.StatusCode, res.StatusCode,
res.Header.Get(wafprotocol.DispositionHeader), answered,
status, status,
disposition, disposition,
) )
+5 -3
View File
@@ -292,7 +292,7 @@ func startAppWithAlerts(
// the time raised, for ban, with its netblock, cause, reason and notes, // the time raised, for ban, with its netblock, cause, reason and notes,
// which ends at expires, as the log line gives it. // which ends at expires, as the log line gives it.
func banAlert( func banAlert(
event string, event alerts.Event,
raised time.Time, raised time.Time,
client string, client string,
ban bans.Ban, ban bans.Ban,
@@ -307,7 +307,9 @@ func banAlert(
Netblock: ban.Netblock, Netblock: ban.Netblock,
Reason: ban.Reason, Reason: ban.Reason,
Detail: map[string]any{ Detail: map[string]any{
"cause": ban.Cause, "ban_expires": expires, "notes": ban.Notes, "cause": string(
ban.Cause,
), "ban_expires": expires, "notes": ban.Notes,
}, },
} }
} }
@@ -315,7 +317,7 @@ func banAlert(
// attackAlert is banAlert for a ban for the probe rule of testRules, with // attackAlert is banAlert for a ban for the probe rule of testRules, with
// the netblock and the notes of ban. // the netblock and the notes of ban.
func attackAlert( func attackAlert(
event string, event alerts.Event,
raised time.Time, raised time.Time,
client string, client string,
ban bans.Ban, ban bans.Ban,
+5 -5
View File
@@ -86,10 +86,10 @@ func TestEachScopeAndWindowOverItsThresholdAlertsOncePerCooldown(
value int64 value int64
count float64 count float64
}{ }{
{requestsPerMinute, ratelimit.KindRequests, minute, 2, 3}, {requestsPerMinute, string(ratelimit.KindRequests), minute, 2, 3},
{requestsPerHour, ratelimit.KindRequests, hour, 2, 3}, {requestsPerHour, string(ratelimit.KindRequests), hour, 2, 3},
{bytesPerMinute, ratelimit.KindBytes, minute, 250, 300}, {bytesPerMinute, string(ratelimit.KindBytes), minute, 250, 300},
{bytesPerHour, ratelimit.KindBytes, hour, 250, 300}, {bytesPerHour, string(ratelimit.KindBytes), hour, 250, 300},
} { } {
setting := scope.prefix + threshold.end setting := scope.prefix + threshold.end
value := strconv.FormatInt(threshold.value, 10) value := strconv.FormatInt(threshold.value, 10)
@@ -186,7 +186,7 @@ func TestEveryRequestIsCountedWhateverIsDoneWithIt(t *testing.T) {
for _, sent := range []struct { for _, sent := range []struct {
from, path string from, path string
status int status int
action string action requestlog.Action
}{ }{
{allowed, "/", http.StatusOK, requestlog.ActionForward}, {allowed, "/", http.StatusOK, requestlog.ActionForward},
{exempt, "/", http.StatusOK, requestlog.ActionForward}, {exempt, "/", http.StatusOK, requestlog.ActionForward},
+3 -3
View File
@@ -135,7 +135,7 @@ func TestARequestLineOverTheLimitIsRefusedWith414BeforeAnyOtherCheck(
// cookies is how many cookies the request carries. // cookies is how many cookies the request carries.
cookies int cookies int
status int status int
action string action requestlog.Action
}{ }{
{ {
"at the limit", lineLimit, 0, http.StatusOK, "at the limit", lineLimit, 0, http.StatusOK,
@@ -768,7 +768,7 @@ func TestARefusedRequestDoesNotWaitForASlowGeoJS(t *testing.T) {
// cookie is the request's Cookie header. // cookie is the request's Cookie header.
cookie string cookie string
status int status int
action string action requestlog.Action
}{ }{
{ {
name: "SWW_WAF_DENY_NETS", env: map[string]string{denyNets: client}, name: "SWW_WAF_DENY_NETS", env: map[string]string{denyNets: client},
@@ -851,7 +851,7 @@ func TestAQuestionWaitsForASlowGeoJSOnlyAsLongAsTheDefaultSays(
// prepare gets the server ready, at now, before the question. // prepare gets the server ready, at now, before the question.
prepare func(t *testing.T, server *waf.Server, now time.Time) prepare func(t *testing.T, server *waf.Server, now time.Time)
status int status int
action string action requestlog.Action
}{ }{
{"nothing refuses it", nil, leaveAsItIs, http.StatusOK, requestlog.ActionForward}, {"nothing refuses it", nil, leaveAsItIs, http.StatusOK, requestlog.ActionForward},
{ {
+10 -7
View File
@@ -18,7 +18,7 @@ import (
// banResponse is a refusal answered with SWW_WAF_BAN_RESPONSE, and logged // banResponse is a refusal answered with SWW_WAF_BAN_RESPONSE, and logged
// with action. // with action.
func (rq *request) banResponse(action string) *refusal { func (rq *request) banResponse(action requestlog.Action) *refusal {
return &refusal{status: rq.h.config.BanResponse, action: action} return &refusal{status: rq.h.config.BanResponse, action: action}
} }
@@ -68,7 +68,7 @@ func (a activeBan) check(
// banAction is the action of a request refused under a ban or for making one: // banAction is the action of a request refused under a ban or for making one:
// permanent says the ban never runs out. // permanent says the ban never runs out.
func banAction(permanent bool) string { func banAction(permanent bool) requestlog.Action {
if permanent { if permanent {
return requestlog.ActionPermanentlyBanned return requestlog.ActionPermanentlyBanned
} }
@@ -144,7 +144,7 @@ func (b banMaker) banForLimit(
rq.line.LimitHit = hit.Window + "_bytes" // as counts names the byte totals rq.line.LimitHit = hit.Window + "_bytes" // as counts names the byte totals
case ratelimit.KindRefusals: case ratelimit.KindRefusals:
rq.line.LimitHit = requestlog.LimitHitMinuteRefusals rq.line.LimitHit = requestlog.LimitHitMinuteRefusals
default: case ratelimit.KindRequests:
rq.line.LimitHit = hit.Window rq.line.LimitHit = hit.Window
} }
@@ -164,6 +164,7 @@ func (b banMaker) banForLimit(
notes.LimitPercent, notes.LimitPercentSetting = rq.rateLimitPercent.logged() notes.LimitPercent, notes.LimitPercentSetting = rq.rateLimitPercent.logged()
case ratelimit.KindBytes: case ratelimit.KindBytes:
notes.LimitPercent, notes.LimitPercentSetting = rq.bytesLimitPercent.logged() notes.LimitPercent, notes.LimitPercentSetting = rq.bytesLimitPercent.logged()
case ratelimit.KindRefusals: // the refusal limit is not lowered
} }
b.banClient( b.banClient(
@@ -180,7 +181,7 @@ func (b banMaker) banForLimit(
// says what happens instead. // says what happens instead.
func (b banMaker) banForAttack( func (b banMaker) banForAttack(
rq *request, now time.Time, notes bans.Notes, rq *request, now time.Time, notes bans.Notes,
) string { ) requestlog.Action {
return b.banClient( return b.banClient(
rq, rq,
now, now,
@@ -201,7 +202,7 @@ func (b banMaker) banClient(
now time.Time, now time.Time,
status int, status int,
grounds bans.Grounds, grounds bans.Grounds,
) string { ) requestlog.Action {
netblock := banNetblock(b.banScopeV4Prefix, b.ipv6GroupPrefix, rq.client) netblock := banNetblock(b.banScopeV4Prefix, b.ipv6GroupPrefix, rq.client)
if b.observe { if b.observe {
@@ -275,7 +276,7 @@ func (b banMaker) withClientNotes(
// SWW_WAF_ALERT_COOLDOWN and never with no webhook set: its notes count the // SWW_WAF_ALERT_COOLDOWN and never with no webhook set: its notes count the
// netblock's requests, which can mean going through every client. // netblock's requests, which can mean going through every client.
func (b banMaker) wouldAlertBan( func (b banMaker) wouldAlertBan(
netblock netip.Prefix, now time.Time, cause string, netblock netip.Prefix, now time.Time, cause bans.Cause,
) bool { ) bool {
event := alerts.EventBan event := alerts.EventBan
if b.ledger.WouldBePermanent(netblock, now, cause) { if b.ledger.WouldBePermanent(netblock, now, cause) {
@@ -296,7 +297,9 @@ func (b banMaker) alertBan(rq *request, ban bans.Ban) {
} }
detail := map[string]any{ detail := map[string]any{
"cause": ban.Cause, "ban_expires": banExpires(ban), "notes": ban.Notes, "cause": string(ban.Cause),
"ban_expires": banExpires(ban),
"notes": ban.Notes,
} }
if b.observe { if b.observe {
detail["mode"] = "observe" detail["mode"] = "observe"
+8 -4
View File
@@ -504,7 +504,9 @@ type sender struct {
} }
// get sends a GET request for / from the client at from. // get sends a GET request for / from the client at from.
func (s *sender) get(from string, status int, action string) logLine { func (s *sender) get(
from string, status int, action requestlog.Action,
) logLine {
s.t.Helper() s.t.Helper()
return s.request(from, "/", status, action) return s.request(from, "/", status, action)
@@ -517,7 +519,7 @@ func (s *sender) get(from string, status int, action string) logLine {
func (s *sender) request( func (s *sender) request(
from, path string, from, path string,
status int, status int,
action string, action requestlog.Action,
) logLine { ) logLine {
s.t.Helper() s.t.Helper()
@@ -527,7 +529,7 @@ func (s *sender) request(
// requestWithHeader is request with header, such as "Authorization: // requestWithHeader is request with header, such as "Authorization:
// Bearer x", added to the request unless it is "". // Bearer x", added to the request unless it is "".
func (s *sender) requestWithHeader( func (s *sender) requestWithHeader(
from, path, header string, status int, action string, from, path, header string, status int, action requestlog.Action,
) logLine { ) logLine {
s.t.Helper() s.t.Helper()
@@ -553,7 +555,9 @@ func (s *sender) requestWithHeader(
// the Core Rule Set past any shorter wait. go test's -timeout ends a test // the Core Rule Set past any shorter wait. go test's -timeout ends a test
// whose answer never comes. // whose answer never comes.
func (s *sender) requestWithBody( func (s *sender) requestWithBody(
method, from, path, header, body string, status int, action string, method, from, path, header, body string,
status int,
action requestlog.Action,
) (logLine, answer) { ) (logLine, answer) {
s.t.Helper() s.t.Helper()
+1 -1
View File
@@ -198,7 +198,7 @@ type firstToRefuse struct {
env map[string]string env map[string]string
inspection time.Duration inspection time.Duration
took time.Duration took time.Duration
action string action requestlog.Action
} }
// firstToRefuseCases are the cases of // firstToRefuseCases are the cases of
+2 -2
View File
@@ -74,7 +74,7 @@ func TestEachBiasedThresholdLowersTheRateLimits(t *testing.T) {
// Half of 4 requests a minute: the third breaks the limit. // Half of 4 requests a minute: the third breaks the limit.
for _, sent := range []struct { for _, sent := range []struct {
status int status int
action string action requestlog.Action
}{ }{
{http.StatusOK, requestlog.ActionForward}, {http.StatusOK, requestlog.ActionForward},
{http.StatusOK, requestlog.ActionForward}, {http.StatusOK, requestlog.ActionForward},
@@ -453,7 +453,7 @@ func TestBanForALoweredLimitGivesThePercentageInItsNotesAndItsAlert(
// limit, which is answered with status and logged with action. // limit, which is answered with status and logged with action.
before int before int
status int status int
action string action requestlog.Action
// reason and want are the ban's reason, and its notes' limit // reason and want are the ban's reason, and its notes' limit
// percentage, as percentText gives it. // percentage, as percentText gives it.
reason, want string reason, want string
+5 -3
View File
@@ -84,7 +84,7 @@ func TestCoreRuleSetMatchAddsTheExploitSignatureTag(t *testing.T) {
name string name string
mode string mode string
status int status int
action string action requestlog.Action
}{ }{
{block, block, http.StatusForbidden, requestlog.ActionBlockedByCoreRuleSet}, {block, block, http.StatusForbidden, requestlog.ActionBlockedByCoreRuleSet},
{detect, detect, http.StatusOK, requestlog.ActionForward}, {detect, detect, http.StatusOK, requestlog.ActionForward},
@@ -158,7 +158,8 @@ func TestCoreRuleSetRefusesAttacksInBlockModeAndOnlyLogsThemInDetectMode(
t.Parallel() t.Parallel()
for _, tc := range []struct { for _, tc := range []struct {
mode, action string mode string
action requestlog.Action
status int status int
}{ }{
{block, requestlog.ActionBlockedByCoreRuleSet, http.StatusForbidden}, {block, requestlog.ActionBlockedByCoreRuleSet, http.StatusForbidden},
@@ -653,7 +654,8 @@ func TestCoreRuleSetMatchRaisesTheCoreRuleSetMatchAlert(t *testing.T) {
// with, and alertMode what the alert's detail gives as mode, if // with, and alertMode what the alert's detail gives as mode, if
// anything. // anything.
status int status int
action, alertMode string action requestlog.Action
alertMode string
}{ }{
{ {
"block", map[string]string{coreRuleSetMode: block}, "block", map[string]string{coreRuleSetMode: block},
+1 -1
View File
@@ -316,7 +316,7 @@ func TestExclusiveListRefusesAPrivateAddressUnlessAllowed(t *testing.T) {
name string name string
allowNets string allowNets string
status int status int
action string action requestlog.Action
}{ }{
{ {
"not in SWW_WAF_ALLOW_NETS", "", "not in SWW_WAF_ALLOW_NETS", "",
+5 -3
View File
@@ -3,6 +3,8 @@ package waf
import ( import (
"context" "context"
"time" "time"
"sneak.berlin/go/smallwebwaf/internal/requestlog"
) )
// To add a heuristic, write one file with a struct that implements heuristic // To add a heuristic, write one file with a struct that implements heuristic
@@ -21,10 +23,10 @@ type heuristic interface {
} }
// verdict is a heuristic refusing a request, or banning its client: the action // verdict is a heuristic refusing a request, or banning its client: the action
// the log line names, one of requestlog's Action constants, and the figures the // the log line names, and the figures the heuristic acted on, by name, such as
// heuristic acted on, by name, such as the count and the limit. // the count and the limit.
type verdict struct { type verdict struct {
action string action requestlog.Action
figures map[string]any figures map[string]any
} }
+8 -5
View File
@@ -22,12 +22,12 @@ func TestBanActionSaysWhetherTheBanRunsOut(t *testing.T) {
for _, tc := range []struct { for _, tc := range []struct {
name string name string
cause string cause bans.Cause
// endsIn is how long after the request the ban ends: negative for a // endsIn is how long after the request the ban ends: negative for a
// ban that has ended, and zero for a permanent ban. // ban that has ended, and zero for a permanent ban.
endsIn time.Duration endsIn time.Duration
path string path string
want string want requestlog.Action
}{ }{
{ {
"temporary ban", bans.CauseAdmin, time.Hour, "/", "temporary ban", bans.CauseAdmin, time.Hour, "/",
@@ -170,7 +170,10 @@ func TestObserveModeForwardsWhatEnforceModeRefuses(t *testing.T) {
// and its second breaks it. // and its second breaks it.
s.get(fromDE, http.StatusOK, requestlog.ActionForward) s.get(fromDE, http.StatusOK, requestlog.ActionForward)
for _, sent := range []struct{ from, refusal string }{ for _, sent := range []struct {
from string
refusal requestlog.Action
}{
{denied, requestlog.ActionClientDenied}, {denied, requestlog.ActionClientDenied},
{banned, requestlog.ActionTemporarilyBanned}, {banned, requestlog.ActionTemporarilyBanned},
{fromKP, requestlog.ActionClientDenied}, {fromKP, requestlog.ActionClientDenied},
@@ -253,7 +256,7 @@ func TestObserveModeMakesNoBanAndKeepsTheBansItHas(t *testing.T) {
// wantWouldAction checks the request log line's would_action, and that a // wantWouldAction checks the request log line's would_action, and that a
// line that should have none has no such field. // line that should have none has no such field.
func wantWouldAction(t *testing.T, line logLine, want string) { func wantWouldAction(t *testing.T, line logLine, want requestlog.Action) {
t.Helper() t.Helper()
got, present := line.fields["would_action"] got, present := line.fields["would_action"]
@@ -261,7 +264,7 @@ func wantWouldAction(t *testing.T, line logLine, want string) {
switch { switch {
case want == "" && present: case want == "" && present:
t.Errorf("log line has would_action %v, want none", got) t.Errorf("log line has would_action %v, want none", got)
case want != "" && got != want: case want != "" && got != string(want):
t.Errorf("log line has would_action %v, want %s", got, want) t.Errorf("log line has would_action %v, want %s", got, want)
} }
} }
+3 -1
View File
@@ -801,7 +801,9 @@ func readResponse(t *testing.T, conn net.Conn) answer {
} }
// wantLine checks the request log line's status and action. // wantLine checks the request log line's status and action.
func wantLine(t *testing.T, line logLine, status int, action string) { func wantLine(
t *testing.T, line logLine, status int, action requestlog.Action,
) {
t.Helper() t.Helper()
if line.Status != status || line.Action != action { if line.Status != status || line.Action != action {
+1 -1
View File
@@ -121,7 +121,7 @@ func TestIPv6GroupPrefixSetsTheClientTheLimitsCount(t *testing.T) {
// status and action are those of the request that breaks the // status and action are those of the request that breaks the
// limit: a rate limit refuses it, a byte limit passes it on. // limit: a rate limit refuses it, a byte limit passes it on.
status int status int
action string action requestlog.Action
}{ }{
{rateLimitPerMinute, "1", http.StatusForbidden, requestlog.ActionClientIsRateLimited}, {rateLimitPerMinute, "1", http.StatusForbidden, requestlog.ActionClientIsRateLimited},
{bytesLimitPerMinute, byteLimit, http.StatusOK, requestlog.ActionForward}, {bytesLimitPerMinute, byteLimit, http.StatusOK, requestlog.ActionForward},
+8
View File
@@ -61,6 +61,14 @@ func (r refusalLimit) check(
case requestlog.ActionTemporarilyBanned, case requestlog.ActionTemporarilyBanned,
requestlog.ActionPermanentlyBanned: requestlog.ActionPermanentlyBanned:
status = r.banResponse status = r.banResponse
case requestlog.ActionForward,
requestlog.ActionRequestLineTooLong,
requestlog.ActionRequestHeaderTooLarge,
requestlog.ActionClientIsRateLimited,
requestlog.ActionClientDenied,
requestlog.ActionCountryDenied,
requestlog.ActionChecksTimedOut,
requestlog.ActionOwnEndpoint:
} }
r.banMaker.banForLimit(rq, now, hit, status) r.banMaker.banForLimit(rq, now, hit, status)
+1 -1
View File
@@ -74,7 +74,7 @@ func TestRefusalLimitBreaksAtOneOverTheLimit(t *testing.T) {
refusals []refused refusals []refused
// nextAction is the action of the client's next request: the next // nextAction is the action of the client's next request: the next
// request after a ban for a ban rule makes it permanent. // request after a ban for a ban rule makes it permanent.
nextAction string nextAction requestlog.Action
}{ }{
{ {
"block rule", "block rule",
+13 -13
View File
@@ -58,22 +58,22 @@ func TestEachBlocklistActionForAListedAddressAndAListedNetblock(
// statuses and actions are those of a listed client's three // statuses and actions are those of a listed client's three
// requests, and percent their rate_limit_percent, as percentText gives it. // requests, and percent their rate_limit_percent, as percentText gives it.
statuses []int statuses []int
actions []string actions []requestlog.Action
percent string percent string
}{ }{
{ {
actionDeny, []int{http.StatusForbidden, http.StatusForbidden, http.StatusForbidden}, actionDeny, []int{http.StatusForbidden, http.StatusForbidden, http.StatusForbidden},
[]string{denied, denied, denied}, none, []requestlog.Action{denied, denied, denied}, none,
}, },
{ {
// Half of 4 requests a minute: the third breaks the limit. // Half of 4 requests a minute: the third breaks the limit.
limitHalf, []int{http.StatusOK, http.StatusOK, http.StatusForbidden}, limitHalf, []int{http.StatusOK, http.StatusOK, http.StatusForbidden},
[]string{forward, forward, requestlog.ActionClientIsRateLimited}, []requestlog.Action{forward, forward, requestlog.ActionClientIsRateLimited},
"50 from " + blocklistAction, "50 from " + blocklistAction,
}, },
{ {
actionLog, []int{http.StatusOK, http.StatusOK, http.StatusOK}, actionLog, []int{http.StatusOK, http.StatusOK, http.StatusOK},
[]string{forward, forward, forward}, none, []requestlog.Action{forward, forward, forward}, none,
}, },
} { } {
t.Run(tc.action, func(t *testing.T) { t.Run(tc.action, func(t *testing.T) {
@@ -442,22 +442,22 @@ func TestEachDNSBLAbuseIPDBActionForAClientADNSBLZoneLists(t *testing.T) {
// statuses and actions are those of a listed client's three // statuses and actions are those of a listed client's three
// requests, and percent their rate_limit_percent, as percentText gives it. // requests, and percent their rate_limit_percent, as percentText gives it.
statuses []int statuses []int
actions []string actions []requestlog.Action
percent string percent string
}{ }{
{ {
actionDeny, []int{http.StatusForbidden, http.StatusForbidden, http.StatusForbidden}, actionDeny, []int{http.StatusForbidden, http.StatusForbidden, http.StatusForbidden},
[]string{denied, denied, denied}, none, []requestlog.Action{denied, denied, denied}, none,
}, },
{ {
// Half of 4 requests a minute: the third breaks the limit. // Half of 4 requests a minute: the third breaks the limit.
limitHalf, []int{http.StatusOK, http.StatusOK, http.StatusForbidden}, limitHalf, []int{http.StatusOK, http.StatusOK, http.StatusForbidden},
[]string{forward, forward, requestlog.ActionClientIsRateLimited}, []requestlog.Action{forward, forward, requestlog.ActionClientIsRateLimited},
"50 from " + dnsblAbuseIPDBAction, "50 from " + dnsblAbuseIPDBAction,
}, },
{ {
actionLog, []int{http.StatusOK, http.StatusOK, http.StatusOK}, actionLog, []int{http.StatusOK, http.StatusOK, http.StatusOK},
[]string{forward, forward, forward}, none, []requestlog.Action{forward, forward, forward}, none,
}, },
} { } {
t.Run(tc.action, func(t *testing.T) { t.Run(tc.action, func(t *testing.T) {
@@ -954,7 +954,7 @@ func TestClientRefusedForAnOffenceIsCheckedWithAbuseIPDBAtItsNextRequest(
// history then counts. // history then counts.
path string path string
status int status int
action string action requestlog.Action
want ratelimit.Offences want ratelimit.Offences
}{ }{
{ {
@@ -1018,22 +1018,22 @@ func TestEachDNSBLAbuseIPDBActionForAClientAbuseIPDBScoresAtOrOverTheMinimum(
// statuses and actions are those of fromDE's three requests, and // statuses and actions are those of fromDE's three requests, and
// percent their rate_limit_percent, as percentText gives it. // percent their rate_limit_percent, as percentText gives it.
statuses []int statuses []int
actions []string actions []requestlog.Action
percent string percent string
}{ }{
{ {
actionDeny, []int{http.StatusForbidden, http.StatusForbidden, http.StatusForbidden}, actionDeny, []int{http.StatusForbidden, http.StatusForbidden, http.StatusForbidden},
[]string{denied, denied, denied}, none, []requestlog.Action{denied, denied, denied}, none,
}, },
{ {
// Half of 4 requests a minute: the third breaks the limit. // Half of 4 requests a minute: the third breaks the limit.
limitHalf, []int{http.StatusOK, http.StatusOK, http.StatusForbidden}, limitHalf, []int{http.StatusOK, http.StatusOK, http.StatusForbidden},
[]string{forward, forward, requestlog.ActionClientIsRateLimited}, []requestlog.Action{forward, forward, requestlog.ActionClientIsRateLimited},
"50 from " + dnsblAbuseIPDBAction, "50 from " + dnsblAbuseIPDBAction,
}, },
{ {
actionLog, []int{http.StatusOK, http.StatusOK, http.StatusOK}, actionLog, []int{http.StatusOK, http.StatusOK, http.StatusOK},
[]string{forward, forward, forward}, none, []requestlog.Action{forward, forward, forward}, none,
}, },
} { } {
t.Run(tc.action, func(t *testing.T) { t.Run(tc.action, func(t *testing.T) {
+1 -1
View File
@@ -21,7 +21,7 @@ import (
// to close its connection without an answer, and the action the log line names. // to close its connection without an answer, and the action the log line names.
type refusal struct { type refusal struct {
status int status int
action string action requestlog.Action
} }
// decision is what the checks note of a request: its log line, and what // decision is what the checks note of a request: its log line, and what
+3 -2
View File
@@ -72,9 +72,10 @@ func (r ruleFiles) check(
), ),
figures: figures, figures: figures,
} }
default: case rules.ActionLog:
return nil
} }
return nil
} }
// refusedByRule reports whether ruleFiles refuses a request that matched // refusedByRule reports whether ruleFiles refuses a request that matched
+1 -1
View File
@@ -184,7 +184,7 @@ func TestRateLimitExemptNetsAreNeitherCountedNorRefused(t *testing.T) {
type sentRequest struct { type sentRequest struct {
client string client string
status int status int
action string action requestlog.Action
} }
// wantAnswers sends requests to sww at addr one after another and // wantAnswers sends requests to sww at addr one after another and
+3 -3
View File
@@ -214,13 +214,13 @@ func wantDisposition(
t *testing.T, t *testing.T,
got answer, got answer,
status int, status int,
disposition string, disposition wafprotocol.Disposition,
) { ) {
t.Helper() t.Helper()
wantStatus(t, got, status) wantStatus(t, got, status)
if answered := got.header.Get(wafprotocol.DispositionHeader); answered != if answered := got.header.Get(wafprotocol.DispositionHeader); answered !=
disposition { string(disposition) {
t.Errorf( t.Errorf(
"answered with disposition %q, want %q", "answered with disposition %q, want %q",
answered, answered,
@@ -1562,7 +1562,7 @@ func TestEveryDeniedRequestsReportGetsItsLastLine(t *testing.T) {
env map[string]string env map[string]string
path string path string
status int status int
action string action requestlog.Action
}{ }{
{ {
"deny", map[string]string{denyNets: client}, "/", http.StatusForbidden, "deny", map[string]string{denyNets: client}, "/", http.StatusForbidden,
+3 -3
View File
@@ -8,7 +8,7 @@ import "net/http"
type Answer struct { type Answer struct {
// Disposition is what rproxy does with the request: DispositionAllow, // Disposition is what rproxy does with the request: DispositionAllow,
// DispositionDeny or DispositionClose. // DispositionDeny or DispositionClose.
Disposition string Disposition Disposition
// ASN and Country are the client's AS number and country, each empty // ASN and Country are the client's AS number and country, each empty
// when the answer does not give it. // when the answer does not give it.
ASN, Country string ASN, Country string
@@ -17,7 +17,7 @@ type Answer struct {
// SetAnswerHeaders sets the headers of waf's answer that tell rproxy answer, // SetAnswerHeaders sets the headers of waf's answer that tell rproxy answer,
// leaving out an ASN or Country that is empty. // leaving out an ASN or Country that is empty.
func SetAnswerHeaders(header http.Header, answer Answer) { func SetAnswerHeaders(header http.Header, answer Answer) {
header.Set(DispositionHeader, answer.Disposition) header.Set(DispositionHeader, string(answer.Disposition))
if answer.ASN != "" { if answer.ASN != "" {
header.Set(ASNHeader, answer.ASN) header.Set(ASNHeader, answer.ASN)
@@ -31,7 +31,7 @@ func SetAnswerHeaders(header http.Header, answer Answer) {
// ReadAnswer returns what the headers of waf's answer tell rproxy, and // ReadAnswer returns what the headers of waf's answer tell rproxy, and
// whether they give a valid disposition. // whether they give a valid disposition.
func ReadAnswer(header http.Header) (Answer, bool) { func ReadAnswer(header http.Header) (Answer, bool) {
disposition := header.Get(DispositionHeader) disposition := Disposition(header.Get(DispositionHeader))
switch disposition { switch disposition {
case DispositionAllow, DispositionDeny, DispositionClose: case DispositionAllow, DispositionDeny, DispositionClose:
+7 -3
View File
@@ -27,18 +27,22 @@ const (
CountryHeader = "X-Client-Country" CountryHeader = "X-Client-Country"
) )
// Disposition is what waf tells rproxy to do with a request, in
// DispositionHeader.
type Disposition string
// The values of DispositionHeader. // The values of DispositionHeader.
const ( const (
// DispositionAllow tells rproxy to pass the request to the app. // DispositionAllow tells rproxy to pass the request to the app.
DispositionAllow = "allow" DispositionAllow Disposition = "allow"
// DispositionDeny tells rproxy to give the visitor the answer's status, // DispositionDeny tells rproxy to give the visitor the answer's status,
// headers and body. // headers and body.
DispositionDeny = "deny" DispositionDeny Disposition = "deny"
// DispositionClose tells rproxy to close the visitor's connection // DispositionClose tells rproxy to close the visitor's connection
// without an answer, which SWW_WAF_BAN_RESPONSE asks for when it is close. // without an answer, which SWW_WAF_BAN_RESPONSE asks for when it is close.
// waf closing the question's connection instead would read as // waf closing the question's connection instead would read as
// waf being down, and rproxy would then let the request through. // waf being down, and rproxy would then let the request through.
DispositionClose = "close" DispositionClose Disposition = "close"
) )
// OwnEndpointPrefix starts the path of every request for sww itself: waf's // OwnEndpointPrefix starts the path of every request for sww itself: waf's