Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
828f1c7bc5 | ||
|
|
8e3879a0c9 |
No files matched your search
@@ -817,8 +817,8 @@ The supported apps are:
|
||||
- `vaultwarden`;
|
||||
- `wordpress`.
|
||||
|
||||
Each app's fixes are in one file, such as `internal/coreruleset/gitea.conf`, and
|
||||
one entry of `apps` in `internal/coreruleset/coreruleset.go`. gitea's fixes are:
|
||||
Each app's fixes are in one file named for the app, such as
|
||||
`internal/coreruleset/apps/gitea.conf`. gitea's fixes are:
|
||||
|
||||
1. Ten parameters are not checked against the Core Rule Set's lists of system
|
||||
files (930120), shell paths (932160) and command names (932260), on any path:
|
||||
@@ -851,8 +851,8 @@ one entry of `apps` in `internal/coreruleset/coreruleset.go`. gitea's fixes are:
|
||||
3. On gitea's paths, some fields skip the rules that refused ordinary gitea
|
||||
requests there. They are the fields where gitea sends a search, a file's name
|
||||
or text, a commit message, a branch name or a repository's name.
|
||||
`internal/coreruleset/gitea.conf` lists each rule and the request it refused.
|
||||
The fields are:
|
||||
`internal/coreruleset/apps/gitea.conf` lists each rule and the request it
|
||||
refused. The fields are:
|
||||
- `q` on gitea's searches of code, of issues and pull requests, and of
|
||||
commits. A code search holds code, such as `<script>`,
|
||||
`SELECT * FROM users WHERE` or `../`. The searches of issues and pull
|
||||
@@ -1037,7 +1037,7 @@ one entry of `apps` in `internal/coreruleset/coreruleset.go`. gitea's fixes are:
|
||||
- The `Content-Range` header of an artifact upload from
|
||||
`actions/upload-artifact@v3` (920450).
|
||||
- The passwords in a body, for each rule that random passwords set off. The
|
||||
numbers are in `internal/coreruleset/gitea.conf`.
|
||||
numbers are in `internal/coreruleset/apps/gitea.conf`.
|
||||
- `password` on these paths:
|
||||
- `/user/login`, `/user/sign_up`, `/user/activate`;
|
||||
- `/user/link_account_signin` and `/user/link_account_signup`;
|
||||
@@ -1069,8 +1069,8 @@ one entry of `apps` in `internal/coreruleset/coreruleset.go`. gitea's fixes are:
|
||||
such as `owner/python3-app` would have that push refused.
|
||||
|
||||
WordPress's fixes: on WordPress's paths, some fields skip the rules that refused
|
||||
ordinary WordPress requests there. `internal/coreruleset/wordpress.conf` lists
|
||||
each rule and the request it refused. The REST API has four addresses:
|
||||
ordinary WordPress requests there. `internal/coreruleset/apps/wordpress.conf`
|
||||
lists each rule and the request it refused. The REST API has four addresses:
|
||||
|
||||
- `/wp-json/...`
|
||||
- `/index.php/wp-json/...`
|
||||
@@ -1162,9 +1162,9 @@ Elsewhere WordPress's fixes do not apply. The paths are those of WordPress at
|
||||
the site root.
|
||||
|
||||
Drupal's fixes: on Drupal's paths, some fields skip the rules that refused
|
||||
ordinary Drupal requests there. `internal/coreruleset/drupal.conf` lists each
|
||||
rule and the request it refused. These query fields skip the lists of system
|
||||
files and command names (930120, 932120, 932250, 932260):
|
||||
ordinary Drupal requests there. `internal/coreruleset/apps/drupal.conf` lists
|
||||
each rule and the request it refused. These query fields skip the lists of
|
||||
system files and command names (930120, 932120, 932250, 932260):
|
||||
|
||||
- On every page under `/admin`: `title`, `name`, `user`, `filename`, `filemime`,
|
||||
`info`, `subject`, `author_name`, `search`, `q` and `destination`.
|
||||
@@ -1254,10 +1254,10 @@ first path segment of two or three lowercase letters, optionally followed by a
|
||||
hyphen and more letters, so `/api` counts too.
|
||||
|
||||
MediaWiki's fixes: on MediaWiki's paths, some fields skip the rules that refused
|
||||
ordinary MediaWiki requests there. `internal/coreruleset/mediawiki.conf` lists
|
||||
each rule and the request it refused. These fields skip the lists of system
|
||||
files and command names (930120, 932120, 932250, 932260), in the query or the
|
||||
body:
|
||||
ordinary MediaWiki requests there. `internal/coreruleset/apps/mediawiki.conf`
|
||||
lists each rule and the request it refused. These fields skip the lists of
|
||||
system files and command names (930120, 932120, 932250, 932260), in the query or
|
||||
the body:
|
||||
|
||||
- On MediaWiki's pages, which are `/index.php` and the paths under `/index.php/`
|
||||
and `/wiki/`:
|
||||
@@ -1363,9 +1363,9 @@ Elsewhere these fields keep every rule. The paths are those of MediaWiki at the
|
||||
site root or under `/w/`.
|
||||
|
||||
Mastodon's fixes: on Mastodon's paths, some fields skip the rules that refused
|
||||
ordinary Mastodon requests there. `internal/coreruleset/mastodon.conf` lists
|
||||
each rule and the request it refused. These fields skip the lists of system
|
||||
files and command names (930120, 932120, 932250, 932260), in the query:
|
||||
ordinary Mastodon requests there. `internal/coreruleset/apps/mastodon.conf`
|
||||
lists each rule and the request it refused. These fields skip the lists of
|
||||
system files and command names (930120, 932120, 932250, 932260), in the query:
|
||||
|
||||
- On the API's search, accounts and admin accounts, under `/api/v1/` and
|
||||
`/api/v2/`, the searches and names `q`, `acct`, `username`, `display_name`,
|
||||
@@ -1526,7 +1526,7 @@ every rule.
|
||||
|
||||
Vaultwarden's fixes: on Vaultwarden's paths, some fields skip the rules that
|
||||
refused ordinary Vaultwarden requests there.
|
||||
`internal/coreruleset/vaultwarden.conf` lists each rule and the request it
|
||||
`internal/coreruleset/apps/vaultwarden.conf` lists each rule and the request it
|
||||
refused. The apps encrypt vault items, folders, collections and sends before
|
||||
sending them, so those fields skip no rules. The fields below hold plain text.
|
||||
|
||||
@@ -1577,8 +1577,8 @@ has a parameter of the same name.
|
||||
|
||||
`token` in the body of `/admin`, the admin token typed on the admin page, skips
|
||||
each rule that random passwords set off. The numbers are in
|
||||
`internal/coreruleset/vaultwarden.conf`. It applies only when the query has no
|
||||
`token` parameter. `' OR '1'='1`, `../../etc/passwd`, `; cat /etc/passwd` and
|
||||
`internal/coreruleset/apps/vaultwarden.conf`. It applies only when the query has
|
||||
no `token` parameter. `' OR '1'='1`, `../../etc/passwd`, `; cat /etc/passwd` and
|
||||
`<img src=x onerror=x()>` sent as the token reach Vaultwarden;
|
||||
`<script>alert(1)</script>` and `javascript:alert(1)` are still refused.
|
||||
|
||||
@@ -1600,7 +1600,7 @@ Elsewhere these fields keep every rule.
|
||||
|
||||
Nextcloud's fixes: on Nextcloud's paths, some fields skip the rules that refused
|
||||
ordinary Nextcloud requests there, and its WebDAV methods are allowed.
|
||||
`internal/coreruleset/nextcloud.conf` lists each rule and the request it
|
||||
`internal/coreruleset/apps/nextcloud.conf` lists each rule and the request it
|
||||
refused.
|
||||
|
||||
The sync client and the Files app use WebDAV on these paths:
|
||||
@@ -1610,7 +1610,8 @@ The sync client and the Files app use WebDAV on these paths:
|
||||
- `/remote.php/webdav/`;
|
||||
- `/public.php/webdav/`.
|
||||
|
||||
These methods are allowed there, besides those of the first change:
|
||||
These methods are allowed there, besides `PUT`, `PATCH` and `DELETE`, which
|
||||
every app gets:
|
||||
|
||||
- `PROPFIND` and `PROPPATCH`;
|
||||
- `MKCOL`;
|
||||
@@ -1754,7 +1755,7 @@ gets gitea's fixes too. Forgejo's own fixes hold only what Forgejo sends and
|
||||
gitea does not.
|
||||
|
||||
On Forgejo's paths, some fields skip the rules that refused ordinary Forgejo
|
||||
requests there. `internal/coreruleset/forgejo.conf` lists each rule and the
|
||||
requests there. `internal/coreruleset/apps/forgejo.conf` lists each rule and the
|
||||
request it refused.
|
||||
|
||||
`q` skips rules on some of Forgejo's searches. The searches are:
|
||||
@@ -1800,10 +1801,10 @@ The Core Rule Set reads the request without the `flash` cookie, in which Forgejo
|
||||
puts what gitea puts in `gitea_flash` (gitea's second fix).
|
||||
|
||||
Ghost's fixes: on Ghost's paths, some fields skip the rules that refused
|
||||
ordinary Ghost requests there. `internal/coreruleset/ghost.conf` lists each rule
|
||||
and the request it refused. Ghost's admin and integrations call the Admin API
|
||||
under `/ghost/api/admin/`. Themes and Ghost's scripts call the Content API under
|
||||
`/ghost/api/content/`, and members call `/members/api/`. The admin calls
|
||||
ordinary Ghost requests there. `internal/coreruleset/apps/ghost.conf` lists each
|
||||
rule and the request it refused. Ghost's admin and integrations call the Admin
|
||||
API under `/ghost/api/admin/`. Themes and Ghost's scripts call the Content API
|
||||
under `/ghost/api/content/`, and members call `/members/api/`. The admin calls
|
||||
ActivityPub, a service of its own, under `/.ghost/activitypub/`.
|
||||
|
||||
Eight rules refused words that name commands and files, such as `python3`,
|
||||
@@ -1941,9 +1942,9 @@ That name holds `.profile`, a file on the list of system files, so the name
|
||||
skips 930120 when a profile is saved.
|
||||
|
||||
The passwords skip each rule that random passwords set off. The numbers are in
|
||||
`internal/coreruleset/ghost.conf`. They are `password` when signing in, and the
|
||||
passwords sent when creating the first staff user, accepting an invitation, and
|
||||
resetting or changing a password. `' OR '1'='1`, `../../etc/passwd`,
|
||||
`internal/coreruleset/apps/ghost.conf`. They are `password` when signing in, and
|
||||
the passwords sent when creating the first staff user, accepting an invitation,
|
||||
and resetting or changing a password. `' OR '1'='1`, `../../etc/passwd`,
|
||||
`; cat /etc/passwd` and `<img src=x onerror=x()>` sent as a password reach
|
||||
Ghost; `<script>alert(1)</script>` and `javascript:alert(1)` are still refused.
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ require (
|
||||
github.com/prometheus/client_golang v1.24.1
|
||||
github.com/spf13/viper v1.21.0
|
||||
go.uber.org/fx v1.24.0
|
||||
golang.org/x/net v0.60.0
|
||||
golang.org/x/sys v0.48.0
|
||||
)
|
||||
|
||||
@@ -51,6 +50,7 @@ require (
|
||||
go.uber.org/zap v1.26.0 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
||||
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
|
||||
golang.org/x/net v0.60.0 // indirect
|
||||
golang.org/x/sync v0.23.0 // indirect
|
||||
golang.org/x/text v0.42.0 // indirect
|
||||
google.golang.org/protobuf v1.36.11 // indirect
|
||||
|
||||
+42
-55
@@ -34,76 +34,65 @@ import (
|
||||
"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.
|
||||
const (
|
||||
// EventBan is a ban sww made.
|
||||
EventBan Event = "ban"
|
||||
EventBan = "ban"
|
||||
// EventPermanentBan is a permanent ban sww made, or a ban it
|
||||
// made permanent.
|
||||
EventPermanentBan Event = "permanent_ban"
|
||||
EventPermanentBan = "permanent_ban"
|
||||
// EventAnomaly is a count of requests or bytes over an anomaly
|
||||
// threshold.
|
||||
EventAnomaly Event = "anomaly"
|
||||
EventAnomaly = "anomaly"
|
||||
// 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
|
||||
// detect mode.
|
||||
EventCoreRuleSetMatch Event = "core_rule_set_match"
|
||||
EventCoreRuleSetMatch = "core_rule_set_match"
|
||||
// EventReputationHit is a request whose client a blocklist or a DNSBL
|
||||
// zone lists, or whose AbuseIPDB score is a hit.
|
||||
EventReputationHit Event = "reputation_hit"
|
||||
EventReputationHit = "reputation_hit"
|
||||
// EventSourceFailure is GeoJS failing or refusing sww, a fetch
|
||||
// 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.
|
||||
EventSourceFailure Event = "source_failure"
|
||||
EventSourceFailure = "source_failure"
|
||||
// EventFileError is one of:
|
||||
// - 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 state file or cache file that cannot be written;
|
||||
// - a replacement of the lookup database that cannot be read.
|
||||
EventFileError Event = "file_error"
|
||||
EventFileError = "file_error"
|
||||
// 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 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
|
||||
// those repeats. SWW_WAF_ALERT_EVENTS does not name it.
|
||||
EventSummary Event = "summary"
|
||||
EventSummary = "summary"
|
||||
)
|
||||
|
||||
// Events returns every event SWW_WAF_ALERT_EVENTS can name, which is its
|
||||
// default.
|
||||
func Events() []Event {
|
||||
return []Event{
|
||||
func Events() []string {
|
||||
return []string{
|
||||
EventBan, EventPermanentBan, EventCoreRuleSetMatch, EventAnomaly,
|
||||
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
|
||||
// name them.
|
||||
const (
|
||||
// DestinationWebhook is the webhook SWW_WAF_ALERT_WEBHOOK_URL names.
|
||||
DestinationWebhook DestinationName = "webhook"
|
||||
DestinationWebhook = "webhook"
|
||||
// DestinationSlack is the Slack incoming webhook
|
||||
// SWW_WAF_ALERT_SLACK_WEBHOOK_URL names.
|
||||
DestinationSlack DestinationName = "slack"
|
||||
DestinationSlack = "slack"
|
||||
// DestinationNtfy is the ntfy topic SWW_WAF_ALERT_NTFY_URL names.
|
||||
DestinationNtfy DestinationName = "ntfy"
|
||||
DestinationNtfy = "ntfy"
|
||||
)
|
||||
|
||||
// Destinations returns every destination alerts can be sent to.
|
||||
func Destinations() []DestinationName {
|
||||
return []DestinationName{
|
||||
DestinationWebhook,
|
||||
DestinationSlack,
|
||||
DestinationNtfy,
|
||||
}
|
||||
func Destinations() []string {
|
||||
return []string{DestinationWebhook, DestinationSlack, DestinationNtfy}
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -146,7 +135,7 @@ type Params struct {
|
||||
NtfyURL *url.URL
|
||||
NtfyToken string
|
||||
// Events are the events alerts are sent for (SWW_WAF_ALERT_EVENTS).
|
||||
Events []Event
|
||||
Events []string
|
||||
// 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
|
||||
// sent in an hour (SWW_WAF_ALERT_MAX_PER_HOUR), 0 for no limit.
|
||||
@@ -170,7 +159,7 @@ type Params struct {
|
||||
type Alert struct {
|
||||
Instance string `json:"instance"`
|
||||
Time time.Time `json:"time"`
|
||||
Event Event `json:"event"`
|
||||
Event string `json:"event"`
|
||||
Host string `json:"host,omitempty"`
|
||||
Client netip.Addr `json:"client"`
|
||||
Netblock netip.Prefix `json:"netblock"`
|
||||
@@ -197,7 +186,7 @@ type Alert struct {
|
||||
//
|
||||
//nolint:tagliatelle // the state files use snake_case, as the request log does
|
||||
type Cooldown struct {
|
||||
Event Event `json:"event"`
|
||||
Event string `json:"event"`
|
||||
Netblock netip.Prefix `json:"netblock"`
|
||||
File string `json:"file,omitempty"`
|
||||
Source string `json:"source,omitempty"`
|
||||
@@ -214,18 +203,18 @@ type Cooldown struct {
|
||||
//
|
||||
//nolint:tagliatelle // the state files use snake_case, as the request log does
|
||||
type Hour struct {
|
||||
Start time.Time `json:"start"`
|
||||
Sent int `json:"sent"`
|
||||
HeldBack map[Event]int `json:"held_back"`
|
||||
Start time.Time `json:"start"`
|
||||
Sent int `json:"sent"`
|
||||
HeldBack map[string]int `json:"held_back"`
|
||||
}
|
||||
|
||||
// State is what alerts.json holds: the cooldowns, the hour under way, and
|
||||
// for each destination set, the alerts waiting to be sent to it, oldest
|
||||
// first.
|
||||
type State struct {
|
||||
Cooldowns []Cooldown `json:"cooldowns"`
|
||||
Hour Hour `json:"hour"`
|
||||
Waiting map[DestinationName][]Alert `json:"waiting"`
|
||||
Cooldowns []Cooldown `json:"cooldowns"`
|
||||
Hour Hour `json:"hour"`
|
||||
Waiting map[string][]Alert `json:"waiting"`
|
||||
}
|
||||
|
||||
// Counts are, for a destination, how many alerts it took, how many
|
||||
@@ -259,7 +248,7 @@ type destination struct {
|
||||
// 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
|
||||
// of the URL.
|
||||
name DestinationName
|
||||
name string
|
||||
setting string
|
||||
url *url.URL
|
||||
// message returns the body an alert is posted with, and the headers
|
||||
@@ -284,7 +273,7 @@ type destination struct {
|
||||
// scope with the same AS number or name, as its detail names them. Each
|
||||
// is empty for an alert without one.
|
||||
type cooldownKey struct {
|
||||
event Event
|
||||
event string
|
||||
netblock netip.Prefix
|
||||
file string
|
||||
source string
|
||||
@@ -317,7 +306,7 @@ func New(params Params) *Queue {
|
||||
q := &Queue{
|
||||
params: params,
|
||||
cooldowns: map[cooldownKey]*Cooldown{},
|
||||
hour: Hour{HeldBack: map[Event]int{}},
|
||||
hour: Hour{HeldBack: map[string]int{}},
|
||||
}
|
||||
|
||||
if params.WebhookURL != nil {
|
||||
@@ -388,7 +377,7 @@ func (q *Queue) Raise(alert Alert) {
|
||||
// event, no alert for event on netblock was let through less than
|
||||
// Cooldown before, and fewer than MaxPerHour alerts have been let through
|
||||
// in the hour under way. Unlike Raise, it counts nothing.
|
||||
func (q *Queue) WouldSend(event Event, netblock netip.Prefix) bool {
|
||||
func (q *Queue) WouldSend(event string, netblock netip.Prefix) bool {
|
||||
if len(q.destinations) == 0 || !slices.Contains(q.params.Events, event) {
|
||||
return false
|
||||
}
|
||||
@@ -444,7 +433,7 @@ func (q *Queue) Run(ctx context.Context) {
|
||||
|
||||
// Counts returns the counts of the destination name, all 0 for one not
|
||||
// set.
|
||||
func (q *Queue) Counts(name DestinationName) Counts {
|
||||
func (q *Queue) Counts(name string) Counts {
|
||||
for _, d := range q.destinations {
|
||||
if d.name == name {
|
||||
return Counts{
|
||||
@@ -460,8 +449,8 @@ func (q *Queue) Counts(name DestinationName) Counts {
|
||||
|
||||
// DestinationsSet returns the destinations set, in the order of
|
||||
// Destinations.
|
||||
func (q *Queue) DestinationsSet() []DestinationName {
|
||||
names := make([]DestinationName, 0, len(q.destinations))
|
||||
func (q *Queue) DestinationsSet() []string {
|
||||
names := make([]string, 0, len(q.destinations))
|
||||
for _, d := range q.destinations {
|
||||
names = append(names, d.name)
|
||||
}
|
||||
@@ -485,7 +474,7 @@ func (q *Queue) Snapshot() State {
|
||||
state := State{
|
||||
Cooldowns: make([]Cooldown, 0, len(q.cooldowns)),
|
||||
Hour: q.hour,
|
||||
Waiting: map[DestinationName][]Alert{},
|
||||
Waiting: map[string][]Alert{},
|
||||
}
|
||||
state.Hour.HeldBack = maps.Clone(q.hour.HeldBack)
|
||||
|
||||
@@ -536,7 +525,7 @@ func (q *Queue) Load(state State) {
|
||||
q.hour.HeldBack = maps.Clone(state.Hour.HeldBack)
|
||||
|
||||
if q.hour.HeldBack == nil {
|
||||
q.hour.HeldBack = map[Event]int{}
|
||||
q.hour.HeldBack = map[string]int{}
|
||||
}
|
||||
|
||||
for _, d := range q.destinations {
|
||||
@@ -548,7 +537,7 @@ func (q *Queue) Load(state State) {
|
||||
// its URL, that URL, target, and message, which makes the messages sent
|
||||
// to it.
|
||||
func (q *Queue) addDestination(
|
||||
name DestinationName, setting string, target *url.URL,
|
||||
name, setting string, target *url.URL,
|
||||
message func(alert *Alert) ([]byte, http.Header, error),
|
||||
) {
|
||||
q.destinations = append(q.destinations, &destination{
|
||||
@@ -660,7 +649,7 @@ func (q *Queue) endHour(now time.Time) {
|
||||
})
|
||||
}
|
||||
|
||||
q.hour = Hour{Start: start, HeldBack: map[Event]int{}}
|
||||
q.hour = Hour{Start: start, HeldBack: map[string]int{}}
|
||||
}
|
||||
|
||||
// queue adds alert to the alerts waiting for each destination.
|
||||
@@ -915,21 +904,20 @@ func (q *Queue) ntfyMessage(alert *Alert) ([]byte, http.Header, error) {
|
||||
|
||||
// ntfyPriority returns the priority an alert for event is published to
|
||||
// ntfy with: high for an event the admin needs to look at.
|
||||
func ntfyPriority(event Event) string {
|
||||
func ntfyPriority(event string) string {
|
||||
switch event {
|
||||
case EventPermanentBan, EventAnomaly, EventSourceFailure, EventFileError:
|
||||
return "high"
|
||||
case EventReputationHit:
|
||||
return "low"
|
||||
case EventBan, EventCoreRuleSetMatch, EventSummary:
|
||||
default: // ban, core_rule_set_match and summary
|
||||
return "default"
|
||||
}
|
||||
|
||||
return "default"
|
||||
}
|
||||
|
||||
// ntfyTag returns the tag an alert for event is published to ntfy with,
|
||||
// which ntfy shows as an emoji.
|
||||
func ntfyTag(event Event) string {
|
||||
func ntfyTag(event string) string {
|
||||
switch event {
|
||||
case EventBan, EventPermanentBan:
|
||||
return "no_entry"
|
||||
@@ -941,16 +929,15 @@ func ntfyTag(event Event) string {
|
||||
return "label"
|
||||
case EventSourceFailure, EventFileError:
|
||||
return "warning"
|
||||
case EventSummary:
|
||||
default: // summary
|
||||
return "bar_chart"
|
||||
}
|
||||
|
||||
return "bar_chart"
|
||||
}
|
||||
|
||||
// title returns the title of alert in Slack and ntfy: the instance and
|
||||
// the event.
|
||||
func title(alert *Alert) string {
|
||||
return alert.Instance + ": " + string(alert.Event)
|
||||
return alert.Instance + ": " + alert.Event
|
||||
}
|
||||
|
||||
// text returns the text of alert in Slack and ntfy: its reason, then a
|
||||
|
||||
@@ -126,7 +126,7 @@ func TestOnlyTheChosenEventsAreSent(t *testing.T) {
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
params := newParams()
|
||||
params.Events = []alerts.Event{
|
||||
params.Events = []string{
|
||||
alerts.EventSourceFailure,
|
||||
alerts.EventFileError,
|
||||
}
|
||||
@@ -156,7 +156,7 @@ func TestWouldSendOnlyForTheChosenEvents(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
params := newParams()
|
||||
params.Events = []alerts.Event{
|
||||
params.Events = []string{
|
||||
alerts.EventSourceFailure,
|
||||
alerts.EventFileError,
|
||||
}
|
||||
@@ -186,8 +186,8 @@ func TestRaiseDoesNothingWithoutADestination(t *testing.T) {
|
||||
// No alert waits, no cooldown has started, and the hour counts none.
|
||||
want := alerts.State{
|
||||
Cooldowns: []alerts.Cooldown{},
|
||||
Hour: alerts.Hour{HeldBack: map[alerts.Event]int{}},
|
||||
Waiting: map[alerts.DestinationName][]alerts.Alert{},
|
||||
Hour: alerts.Hour{HeldBack: map[string]int{}},
|
||||
Waiting: map[string][]alerts.Alert{},
|
||||
}
|
||||
if got := q.Snapshot(); !reflect.DeepEqual(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) {
|
||||
webhook, q := start(t, newParams())
|
||||
raise := func(event alerts.Event, n int) {
|
||||
raise := func(event string, n int) {
|
||||
q.Raise(alerts.Alert{Event: event, Netblock: netblock(n)})
|
||||
}
|
||||
|
||||
@@ -1014,7 +1014,7 @@ func TestNtfyTokenIsSentToNtfyAlone(t *testing.T) {
|
||||
)
|
||||
synctest.Wait()
|
||||
|
||||
for destination, want := range map[alerts.DestinationName][]string{
|
||||
for destination, want := range map[string][]string{
|
||||
alerts.DestinationWebhook: nil,
|
||||
alerts.DestinationSlack: nil,
|
||||
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,
|
||||
// while Slack has not answered the first.
|
||||
for destination, want := range map[alerts.DestinationName]alerts.Counts{
|
||||
for destination, want := range map[string]alerts.Counts{
|
||||
alerts.DestinationWebhook: {Sent: 3},
|
||||
alerts.DestinationSlack: {},
|
||||
alerts.DestinationNtfy: {Sent: 3},
|
||||
@@ -1126,7 +1126,7 @@ func TestDropsAreCountedForEachDestination(t *testing.T) {
|
||||
// the webhook takes every one.
|
||||
const all = alerts.QueueSize + 1
|
||||
|
||||
for destination, want := range map[alerts.DestinationName]alerts.Counts{
|
||||
for destination, want := range map[string]alerts.Counts{
|
||||
alerts.DestinationWebhook: {Sent: all},
|
||||
alerts.DestinationSlack: {Dropped: 1},
|
||||
alerts.DestinationNtfy: {SendFailures: all, Dropped: all},
|
||||
@@ -1152,21 +1152,11 @@ func TestEachDestinationIsSentOnlyTheAlertsWaitingForIt(t *testing.T) {
|
||||
|
||||
// As read from alerts.json. The alert waiting for the webhook, which
|
||||
// is not set, is dropped.
|
||||
q.Load(
|
||||
roundTrip(
|
||||
t,
|
||||
alerts.State{
|
||||
Waiting: map[alerts.DestinationName][]alerts.Alert{
|
||||
alerts.DestinationWebhook: {waiting("first")},
|
||||
alerts.DestinationSlack: {
|
||||
waiting("second"),
|
||||
waiting("third"),
|
||||
},
|
||||
alerts.DestinationNtfy: {waiting("fourth")},
|
||||
},
|
||||
},
|
||||
),
|
||||
)
|
||||
q.Load(roundTrip(t, alerts.State{Waiting: map[string][]alerts.Alert{
|
||||
alerts.DestinationWebhook: {waiting("first")},
|
||||
alerts.DestinationSlack: {waiting("second"), waiting("third")},
|
||||
alerts.DestinationNtfy: {waiting("fourth")},
|
||||
}}))
|
||||
synctest.Wait()
|
||||
|
||||
wantBodies(t, standIns[alerts.DestinationSlack],
|
||||
@@ -1175,7 +1165,7 @@ func TestEachDestinationIsSentOnlyTheAlertsWaitingForIt(t *testing.T) {
|
||||
wantBodies(t, standIns[alerts.DestinationNtfy], "fourth")
|
||||
|
||||
// alerts.json then lists Slack and ntfy alone, with no alert waiting.
|
||||
want := map[alerts.DestinationName][]alerts.Alert{
|
||||
want := map[string][]alerts.Alert{
|
||||
alerts.DestinationSlack: {}, alerts.DestinationNtfy: {},
|
||||
}
|
||||
if got := q.Snapshot().Waiting; !reflect.DeepEqual(got, want) {
|
||||
@@ -1344,11 +1334,11 @@ func start(t *testing.T, params alerts.Params) (*standIn, *alerts.Queue) {
|
||||
func startAll(
|
||||
t *testing.T,
|
||||
params alerts.Params,
|
||||
) (map[alerts.DestinationName]*standIn, *alerts.Queue) {
|
||||
) (map[string]*standIn, *alerts.Queue) {
|
||||
t.Helper()
|
||||
|
||||
q := alerts.New(params)
|
||||
standIns := map[alerts.DestinationName]*standIn{}
|
||||
standIns := map[string]*standIn{}
|
||||
|
||||
for _, destination := range q.DestinationsSet() {
|
||||
standIns[destination] = &standIn{answers: answering}
|
||||
@@ -1414,14 +1404,14 @@ func wantAlert(t *testing.T, got, want map[string]any) {
|
||||
|
||||
// wantEvents checks the events of the alerts the webhook was sent, in
|
||||
// order.
|
||||
func wantEvents(t *testing.T, webhook *standIn, want ...alerts.Event) {
|
||||
func wantEvents(t *testing.T, webhook *standIn, want ...string) {
|
||||
t.Helper()
|
||||
|
||||
got := make([]alerts.Event, 0, len(webhook.received()))
|
||||
got := make([]string, 0, len(webhook.received()))
|
||||
|
||||
for _, request := range webhook.received() {
|
||||
event, _ := request.alert["event"].(string)
|
||||
got = append(got, alerts.Event(event))
|
||||
got = append(got, event)
|
||||
}
|
||||
|
||||
if !slices.Equal(got, want) {
|
||||
@@ -1457,8 +1447,7 @@ func wantCounts(
|
||||
|
||||
// wantDestinationCounts checks q's counts for destination.
|
||||
func wantDestinationCounts(
|
||||
t *testing.T, q *alerts.Queue, destination alerts.DestinationName,
|
||||
want alerts.Counts,
|
||||
t *testing.T, q *alerts.Queue, destination string, want alerts.Counts,
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
|
||||
@@ -7,9 +7,7 @@ const QueueSize = queueSize
|
||||
|
||||
// SetTransport has q's requests to the destination name go through
|
||||
// transport instead of the network.
|
||||
func (q *Queue) SetTransport(
|
||||
name DestinationName, transport http.RoundTripper,
|
||||
) {
|
||||
func (q *Queue) SetTransport(name string, transport http.RoundTripper) {
|
||||
for _, d := range q.destinations {
|
||||
if d.name == name {
|
||||
d.httpClient.Transport = transport
|
||||
|
||||
@@ -295,10 +295,9 @@ func (c *Counters) netAround(client netip.Addr) netip.Prefix {
|
||||
// overThreshold is a count over its threshold: what it counts, requests or
|
||||
// bytes, its window, the count and the threshold.
|
||||
type overThreshold struct {
|
||||
kind ratelimit.LimitKind
|
||||
window string
|
||||
count float64
|
||||
threshold int64
|
||||
kind, window string
|
||||
count float64
|
||||
threshold int64
|
||||
}
|
||||
|
||||
// add counts a request of bytes at now in each of c's counts whose
|
||||
@@ -347,10 +346,9 @@ func (c *Counter) add(
|
||||
// bucketCount is one of a counter's four counts: requests or bytes, in a
|
||||
// window of length, and the buckets they are counted in.
|
||||
type bucketCount struct {
|
||||
kind ratelimit.LimitKind
|
||||
window string
|
||||
length time.Duration
|
||||
buckets *ratelimit.Buckets
|
||||
kind, window string
|
||||
length time.Duration
|
||||
buckets *ratelimit.Buckets
|
||||
}
|
||||
|
||||
// counts returns c's counts: requests and bytes in the minute, then in
|
||||
@@ -406,8 +404,8 @@ func (k key) counter() *Counter {
|
||||
// counted, the count and the threshold.
|
||||
func alertFor(r Request, k key, o overThreshold) alerts.Alert {
|
||||
detail := map[string]any{
|
||||
"scope": k.scope, "window": o.window, "kind": string(o.kind),
|
||||
"count": o.count, "threshold": o.threshold,
|
||||
"scope": k.scope, "window": o.window, "kind": o.kind, "count": o.count,
|
||||
"threshold": o.threshold,
|
||||
}
|
||||
|
||||
var counted string
|
||||
|
||||
@@ -54,11 +54,7 @@ func TestBlacklistLeavesANetblockWithAnActiveBanAsItIs(t *testing.T) {
|
||||
now := midnight()
|
||||
scores := map[netip.Prefix]int64{netblock: 100}
|
||||
|
||||
limitBan, _ := ledger.Ban(
|
||||
netblock,
|
||||
now,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
limitBan, _ := ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
|
||||
if made := ledger.BanForAbuseIPDB(now, now.Add(30*day), scores); made != 0 {
|
||||
t.Errorf("made %d bans over a ban for a limit, want none", made)
|
||||
@@ -123,7 +119,7 @@ func TestEndedBlacklistBansAreForgottenAtTheNextFetch(t *testing.T) {
|
||||
expires := now.Add(30 * day)
|
||||
|
||||
ledger.BanForAbuseIPDB(now, expires, map[netip.Prefix]int64{listed: 100})
|
||||
ledger.Ban(limited, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(limited, now, bans.Notes{})
|
||||
|
||||
// listed is off the blacklist when it is fetched after the ban's end.
|
||||
if made := ledger.BanForAbuseIPDB(expires, expires.Add(30*day), nil); made != 0 {
|
||||
@@ -148,7 +144,7 @@ func TestBlacklistBansDoNotCountTowardMaxBans(t *testing.T) {
|
||||
scores[netip.PrefixFrom(netip.AddrFrom4([4]byte{198, 51, 100, byte(i)}), 32)] = 100
|
||||
}
|
||||
|
||||
ledger.Ban(limited, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(limited, now, bans.Notes{})
|
||||
|
||||
if made := ledger.BanForAbuseIPDB(now, now.Add(30*day), scores); made != 3 {
|
||||
t.Errorf("made %d bans for 3 netblocks, want 3", made)
|
||||
@@ -158,7 +154,7 @@ func TestBlacklistBansDoNotCountTowardMaxBans(t *testing.T) {
|
||||
|
||||
// The ban for an attack takes the place of the one for a limit, and no
|
||||
// blacklist ban goes.
|
||||
ledger.Ban(attacker, now, bans.Grounds{Cause: bans.CauseAttack})
|
||||
ledger.BanForAttack(attacker, now, bans.Notes{})
|
||||
|
||||
want := map[netip.Prefix]int{limited: 0, attacker: 1}
|
||||
for netblock := range scores {
|
||||
|
||||
+23
-64
@@ -61,7 +61,7 @@ func TestAdminsBansAreNeverDroppedAndDoNotCountTowardMaxBans(t *testing.T) {
|
||||
wantBans(t, ledger, map[netip.Prefix]int{adminsOnly: 2, both: 2})
|
||||
|
||||
// A new ban drops the ban sww made, and only that one.
|
||||
ledger.Ban(second, midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(second, midnight(), bans.Notes{})
|
||||
wantBans(
|
||||
t,
|
||||
ledger,
|
||||
@@ -77,7 +77,7 @@ func TestAdminsBansAreNeverDroppedAndDoNotCountTowardMaxBans(t *testing.T) {
|
||||
}
|
||||
|
||||
// And the next drops that one.
|
||||
ledger.Ban(third, midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(third, midnight(), bans.Notes{})
|
||||
wantBans(
|
||||
t,
|
||||
ledger,
|
||||
@@ -98,28 +98,20 @@ func TestEarlierBansCountABanDroppedAfterAnAdminsBan(t *testing.T) {
|
||||
// twice, each time after its last ban has ended.
|
||||
ledger.BanForAdmin(netblock, midnight(), midnight().Add(time.Minute),
|
||||
"probes for logins")
|
||||
first, _ := ledger.Ban(
|
||||
first, _ := ledger.BanForLimit(
|
||||
netblock,
|
||||
midnight().Add(time.Minute),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
second, _ := ledger.Ban(
|
||||
netblock,
|
||||
first.Expires,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
second, _ := ledger.BanForLimit(netblock, first.Expires, bans.Notes{})
|
||||
|
||||
// A ban on another netblock fills the ledger, and the netblock's first
|
||||
// ban for a limit, its earliest that is not an admin's, goes.
|
||||
ledger.Ban(other, second.Start, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(other, second.Start, bans.Notes{})
|
||||
wantBans(t, ledger, map[netip.Prefix]int{netblock: 2, other: 1})
|
||||
|
||||
// The netblock's next ban still counts it.
|
||||
third, _ := ledger.Ban(
|
||||
netblock,
|
||||
second.Expires,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
third, _ := ledger.BanForLimit(netblock, second.Expires, bans.Notes{})
|
||||
if third.Notes.EarlierBans != (bans.EarlierBans{Limit: 2, Admin: 1}) {
|
||||
t.Errorf("the third ban for a limit has earlier bans %+v, "+
|
||||
"want 2 for a limit and 1 an admin's", third.Notes.EarlierBans)
|
||||
@@ -135,27 +127,15 @@ func TestEarlierBansCountABanAnAdminKeepsOnce(t *testing.T) {
|
||||
// The netblock breaks a limit twice, the second time after its first
|
||||
// ban has ended, and the second ban's notes count the first. An admin
|
||||
// then keeps the first ban.
|
||||
first, _ := ledger.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
second, _ := ledger.Ban(
|
||||
netblock,
|
||||
first.Expires,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
first, _ := ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
second, _ := ledger.BanForLimit(netblock, first.Expires, bans.Notes{})
|
||||
|
||||
kept := first
|
||||
kept.Cause = bans.CauseAdmin
|
||||
ledger.LoadEdit([]bans.Ban{kept, second})
|
||||
|
||||
// The netblock's next ban counts the kept ban once, as an admin's.
|
||||
third, _ := ledger.Ban(
|
||||
netblock,
|
||||
second.Expires,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
third, _ := ledger.BanForLimit(netblock, second.Expires, bans.Notes{})
|
||||
if third.Notes.EarlierBans != (bans.EarlierBans{Limit: 1, Admin: 1}) {
|
||||
t.Errorf("the third ban for a limit has earlier bans %+v, "+
|
||||
"want 1 for a limit and 1 an admin's", third.Notes.EarlierBans)
|
||||
@@ -167,37 +147,20 @@ func TestReasonOfTheBansSwwMakes(t *testing.T) {
|
||||
|
||||
ledger := bans.New(defaultParams())
|
||||
|
||||
limit, _ := ledger.Ban(
|
||||
limit, _ := ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.1/32"),
|
||||
midnight(),
|
||||
bans.Grounds{
|
||||
Cause: bans.CauseLimit,
|
||||
Notes: bans.Notes{
|
||||
Kind: "requests",
|
||||
Limit: 1000,
|
||||
Window: "minute",
|
||||
},
|
||||
},
|
||||
bans.Notes{Kind: "requests", Limit: 1000, Window: "minute"},
|
||||
)
|
||||
byteLimit, _ := ledger.Ban(
|
||||
byteLimit, _ := ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.3/32"),
|
||||
midnight(),
|
||||
bans.Grounds{
|
||||
Cause: bans.CauseLimit,
|
||||
Notes: bans.Notes{
|
||||
Kind: "bytes",
|
||||
Limit: 10 << 30,
|
||||
Window: "hour",
|
||||
},
|
||||
},
|
||||
bans.Notes{Kind: "bytes", Limit: 10 << 30, Window: "hour"},
|
||||
)
|
||||
attack, _ := ledger.Ban(
|
||||
attack, _ := ledger.BanForAttack(
|
||||
netip.MustParsePrefix("203.0.113.2/32"),
|
||||
midnight(),
|
||||
bans.Grounds{
|
||||
Cause: bans.CauseAttack,
|
||||
Notes: bans.Notes{RuleID: "git-dir", Target: "path"},
|
||||
},
|
||||
bans.Notes{RuleID: "git-dir", Target: "path"},
|
||||
)
|
||||
|
||||
for _, tc := range []struct{ got, want string }{
|
||||
@@ -237,7 +200,7 @@ func TestLiftedBanForALimitRefusesNothingAndMakesNoBanLonger(t *testing.T) {
|
||||
t.Error("the lifted ban refuses")
|
||||
}
|
||||
|
||||
ban, _ := ledger.Ban(netblock, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
if ban.Expires.Sub(ban.Start) != time.Hour ||
|
||||
ban.Notes.EarlierBans != (bans.EarlierBans{Limit: 1}) {
|
||||
t.Errorf(
|
||||
@@ -288,11 +251,7 @@ func TestLiftedBanForAnAttackRefusesNothingAndMakesNoBanLonger(
|
||||
}
|
||||
|
||||
// The next clear sign of attack bans for seven days, as a first does.
|
||||
ban, _ := ledger.Ban(
|
||||
netblock,
|
||||
now,
|
||||
bans.Grounds{Cause: bans.CauseAttack},
|
||||
)
|
||||
ban, _ := ledger.BanForAttack(netblock, now, bans.Notes{})
|
||||
if ban.Expires.Sub(ban.Start) != 7*day {
|
||||
t.Errorf(
|
||||
"the next ban for an attack ends at %s, want seven days on",
|
||||
@@ -305,10 +264,10 @@ func TestLoadEditCountsTheBansAnAdminMade(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ledger := bans.New(defaultParams())
|
||||
made, _ := ledger.Ban(
|
||||
made, _ := ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.1/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
atStart := bans.Ban{
|
||||
Netblock: netip.MustParsePrefix("203.0.113.2/32"),
|
||||
@@ -352,7 +311,7 @@ func TestAdminsBanIsMadeWhileAnotherLasts(t *testing.T) {
|
||||
ledger := bans.New(defaultParams())
|
||||
|
||||
// An hour's ban for a broken limit.
|
||||
ledger.Ban(netblock, midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
wantChanged(t, ledger, true)
|
||||
|
||||
// A minute later an admin bans the netblock for good, named by an
|
||||
@@ -472,10 +431,10 @@ func TestPermanentBanOnAWiderNetblockIsReturnedOverATemporaryBan(
|
||||
ledger := bans.New(defaultParams())
|
||||
|
||||
// The temporary ban is made first, so that its netblock is looked at first.
|
||||
ledger.Ban(
|
||||
ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.9/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
ledger.BanForAdmin(
|
||||
netip.MustParsePrefix("203.0.113.0/24"),
|
||||
|
||||
+97
-76
@@ -16,26 +16,20 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/golang-lru/v2/simplelru"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||
)
|
||||
|
||||
// Cause is why a ban was made: CauseLimit, CauseAttack, CauseAdmin or
|
||||
// CauseAbuseIPDB.
|
||||
type Cause string
|
||||
|
||||
// The causes of bans.
|
||||
const (
|
||||
// CauseLimit is a ban sww made for a broken limit.
|
||||
CauseLimit Cause = "limit"
|
||||
CauseLimit = "limit"
|
||||
// CauseAttack is a ban sww made for a clear sign of attack.
|
||||
CauseAttack Cause = "attack"
|
||||
CauseAttack = "attack"
|
||||
// CauseAdmin is a ban an admin made, or one sww made that an
|
||||
// admin keeps. It is never dropped.
|
||||
CauseAdmin Cause = "admin"
|
||||
CauseAdmin = "admin"
|
||||
// CauseAbuseIPDB is a ban sww made for a client on AbuseIPDB's
|
||||
// blacklist. It is forgotten once it has ended.
|
||||
CauseAbuseIPDB Cause = "abuseipdb"
|
||||
CauseAbuseIPDB = "abuseipdb"
|
||||
)
|
||||
|
||||
// repeatFactor is how many times as long as the netblock's last ban a ban
|
||||
@@ -74,7 +68,8 @@ type Ban struct {
|
||||
Start time.Time
|
||||
// Expires is when the ban ends, zero for a permanent ban.
|
||||
Expires time.Time
|
||||
Cause Cause
|
||||
// Cause is CauseLimit, CauseAttack, CauseAdmin or CauseAbuseIPDB.
|
||||
Cause string
|
||||
// Reason is a short text: for a ban sww made, the limit broken, the
|
||||
// rule that matched or AbuseIPDB's score; for an admin's, what the
|
||||
// admin wrote.
|
||||
@@ -116,10 +111,10 @@ type Notes struct {
|
||||
// request that broke the limit included.
|
||||
// These are what counted toward the ban, and the window is the time
|
||||
// over which they came.
|
||||
Kind ratelimit.LimitKind `json:"kind,omitempty"`
|
||||
Limit int64 `json:"limit,omitempty"`
|
||||
Window string `json:"window,omitempty"`
|
||||
Count float64 `json:"count,omitempty"`
|
||||
Kind string `json:"kind,omitempty"`
|
||||
Limit int64 `json:"limit,omitempty"`
|
||||
Window string `json:"window,omitempty"`
|
||||
Count float64 `json:"count,omitempty"`
|
||||
// LimitPercent and LimitPercentSetting are, for a ban for a limit a
|
||||
// biased threshold lowered, the client's percentage of that kind of
|
||||
// limit, of which Limit is the result, and the setting that gave it.
|
||||
@@ -199,7 +194,7 @@ type Ledger struct {
|
||||
// made is how many bans have been made since the start, by cause: by
|
||||
// the ledger, and by an admin, through BanForAdmin or in an edit of
|
||||
// bans.json.
|
||||
made map[Cause]int
|
||||
made map[string]int
|
||||
// v4Lengths and v6Lengths are the lengths of the IPv4 and IPv6
|
||||
// netblocks that have been banned. Check looks for a ban at each of
|
||||
// them, so that a ban read from bans.json refuses every client in its
|
||||
@@ -226,7 +221,7 @@ func New(params Params) *Ledger {
|
||||
params: params,
|
||||
changed: make(chan struct{}, 1),
|
||||
netblocks: netblocks,
|
||||
made: map[Cause]int{},
|
||||
made: map[string]int{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -305,52 +300,83 @@ func activeBan(bans []Ban, now time.Time) *Ban {
|
||||
return newest
|
||||
}
|
||||
|
||||
// Grounds are why a request makes a ban: its cause, CauseLimit or
|
||||
// CauseAttack, and its notes.
|
||||
type Grounds struct {
|
||||
Cause Cause
|
||||
Notes Notes
|
||||
// BanForLimit bans netblock at now for a broken limit, with notes, and
|
||||
// returns the ban, and true. A first ban lasts LimitBanDuration. A ban
|
||||
// made within LimitBanRepeatWindow after the netblock's ban that ended
|
||||
// last, other than one for a clear sign of attack or a lifted one, lasts
|
||||
// repeatFactor times as long as that one. A ban that would be longer
|
||||
// than LimitBanPermanentAbove is permanent instead. If a ban on netblock is still
|
||||
// active, as when two of its requests break a limit at once, that ban is
|
||||
// returned with false, and no other is made. The ledger fills in the
|
||||
// notes' Refused and EarlierBans itself, and gives the ban the reason
|
||||
// "<Kind> per <Window> over the limit of <Limit>", from the notes, such
|
||||
// as "requests per minute over the limit of 1000".
|
||||
func (l *Ledger) BanForLimit(
|
||||
netblock netip.Prefix, now time.Time, notes Notes,
|
||||
) (Ban, bool) {
|
||||
return l.ban(
|
||||
netblock,
|
||||
now,
|
||||
CauseLimit,
|
||||
limitReason(notes),
|
||||
notes,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
// Ban bans netblock at now on grounds, and returns the ban, and true. If
|
||||
// a ban on netblock is still active, as when two of its requests break a
|
||||
// limit at once, that ban is returned with false, and no other is made.
|
||||
// The ledger fills in the notes' Refused and EarlierBans itself.
|
||||
//
|
||||
// A ban for a broken limit:
|
||||
// - lasts LimitBanDuration the first time;
|
||||
// - lasts repeatFactor times as long as the netblock's ban that ended
|
||||
// last, when made within LimitBanRepeatWindow after it, unless that
|
||||
// ban was for a clear sign of attack or was lifted;
|
||||
// - is permanent instead when it would be longer than
|
||||
// LimitBanPermanentAbove;
|
||||
// - has the reason "<Kind> per <Window> over the limit of <Limit>", from
|
||||
// the notes, such as "requests per minute over the limit of 1000".
|
||||
//
|
||||
// A ban for a clear sign of attack:
|
||||
// - lasts AttackBanDuration the first time;
|
||||
// - is permanent once the netblock has had one that was not lifted;
|
||||
// - has the reason "matched the rule <RuleID>", or "asked for the trap
|
||||
// path <TrapPath>".
|
||||
func (l *Ledger) Ban(
|
||||
netblock netip.Prefix, now time.Time, grounds Grounds,
|
||||
// WouldBanForLimit returns what BanForLimit would, without making the ban:
|
||||
// what observe mode would have done.
|
||||
func (l *Ledger) WouldBanForLimit(
|
||||
netblock netip.Prefix, now time.Time, notes Notes,
|
||||
) (Ban, bool) {
|
||||
return l.ban(netblock, now, grounds, true)
|
||||
return l.ban(
|
||||
netblock,
|
||||
now,
|
||||
CauseLimit,
|
||||
limitReason(notes),
|
||||
notes,
|
||||
false,
|
||||
)
|
||||
}
|
||||
|
||||
// WouldBan returns what Ban would, without making the ban: what observe
|
||||
// mode would have done.
|
||||
func (l *Ledger) WouldBan(
|
||||
netblock netip.Prefix, now time.Time, grounds Grounds,
|
||||
// BanForAttack bans netblock at now for a clear sign of attack, with
|
||||
// notes, and returns the ban, and whether it made it, as BanForLimit
|
||||
// does. A first ban lasts AttackBanDuration; once the netblock has had
|
||||
// one that was not lifted, the next is permanent. Its reason is "matched
|
||||
// the rule <RuleID>", or "asked for the trap path <TrapPath>".
|
||||
func (l *Ledger) BanForAttack(
|
||||
netblock netip.Prefix, now time.Time, notes Notes,
|
||||
) (Ban, bool) {
|
||||
return l.ban(netblock, now, grounds, false)
|
||||
return l.ban(
|
||||
netblock,
|
||||
now,
|
||||
CauseAttack,
|
||||
attackReason(notes),
|
||||
notes,
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
// WouldBanForAttack returns what BanForAttack would, without making the
|
||||
// ban: what observe mode would have done.
|
||||
func (l *Ledger) WouldBanForAttack(
|
||||
netblock netip.Prefix, now time.Time, notes Notes,
|
||||
) (Ban, bool) {
|
||||
return l.ban(
|
||||
netblock,
|
||||
now,
|
||||
CauseAttack,
|
||||
attackReason(notes),
|
||||
notes,
|
||||
false,
|
||||
)
|
||||
}
|
||||
|
||||
// WouldBePermanent reports whether a ban on netblock for cause, CauseLimit
|
||||
// or CauseAttack, made at now would be permanent, as Ban would make it. It
|
||||
// works out nothing else of the ban.
|
||||
// or CauseAttack, made at now would be permanent, as BanForLimit or
|
||||
// BanForAttack would make it. It works out nothing else of the ban.
|
||||
func (l *Ledger) WouldBePermanent(
|
||||
netblock netip.Prefix, now time.Time, cause Cause,
|
||||
netblock netip.Prefix, now time.Time, cause string,
|
||||
) bool {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
@@ -385,9 +411,10 @@ func attackReason(notes Notes) string {
|
||||
|
||||
// BanForAdmin bans netblock at now for an admin, with reason, until
|
||||
// expires, or for good when expires is zero, and returns the ban, whose
|
||||
// cause is CauseAdmin. Unlike Ban, it makes the ban even while another
|
||||
// on netblock is active, since the admin asked for this one. The ledger
|
||||
// fills in the notes' EarlierBans, and counts the ban among those made.
|
||||
// cause is CauseAdmin. Unlike BanForLimit and BanForAttack, it makes the
|
||||
// ban even while another on netblock is active, since the admin asked
|
||||
// for this one. The ledger fills in the notes' EarlierBans, and counts
|
||||
// the ban among those made.
|
||||
func (l *Ledger) BanForAdmin(
|
||||
netblock netip.Prefix, now, expires time.Time, reason string,
|
||||
) Ban {
|
||||
@@ -444,7 +471,7 @@ func (l *Ledger) BanForAbuseIPDB(
|
||||
|
||||
// The source is named as the request log names AbuseIPDB.
|
||||
notes.Reputation = []ReputationHit{
|
||||
{Source: string(CauseAbuseIPDB), Score: &score},
|
||||
{Source: CauseAbuseIPDB, Score: &score},
|
||||
}
|
||||
|
||||
l.add(Ban{
|
||||
@@ -582,7 +609,7 @@ func (l *Ledger) AddLookup(
|
||||
// for CauseLimit, CauseAttack and CauseAbuseIPDB, by the ledger; for
|
||||
// CauseAdmin, by an admin, with BanForAdmin or in an edit of bans.json, as
|
||||
// LoadEdit counts them. The bans read from bans.json at the start are not among them.
|
||||
func (l *Ledger) Made(cause Cause) int {
|
||||
func (l *Ledger) Made(cause string) int {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
@@ -732,13 +759,15 @@ func (l *Ledger) forgetEndedAbuseIPDBBans(now time.Time) bool {
|
||||
return forgot
|
||||
}
|
||||
|
||||
// ban bans netblock at now on grounds, as Ban describes, and returns the
|
||||
// ban, and whether it made it. Unless keep is true, the ban is not made,
|
||||
// only returned: it is the ban that would have been made.
|
||||
// ban bans netblock at now for cause, CauseLimit or CauseAttack, with
|
||||
// reason and notes, as BanForLimit and BanForAttack describe, and returns
|
||||
// the ban, and whether it made it. Unless keep is true, the ban is not
|
||||
// made, only returned: it is the ban that would have been made.
|
||||
func (l *Ledger) ban(
|
||||
netblock netip.Prefix,
|
||||
now time.Time,
|
||||
grounds Grounds,
|
||||
cause, reason string,
|
||||
notes Notes,
|
||||
keep bool,
|
||||
) (Ban, bool) {
|
||||
l.mu.Lock()
|
||||
@@ -747,8 +776,6 @@ func (l *Ledger) ban(
|
||||
// held are the netblock's bans, none of them active.
|
||||
var held []Ban
|
||||
|
||||
notes := grounds.Notes
|
||||
|
||||
bans, found := l.netblocks.Get(netblock)
|
||||
if found {
|
||||
active := activeBan(*bans, now)
|
||||
@@ -764,21 +791,15 @@ func (l *Ledger) ban(
|
||||
ban := Ban{
|
||||
Netblock: netblock,
|
||||
Start: now,
|
||||
Cause: grounds.Cause,
|
||||
Cause: cause,
|
||||
Reason: reason,
|
||||
Notes: notes,
|
||||
}
|
||||
|
||||
switch grounds.Cause {
|
||||
case CauseLimit:
|
||||
ban.Reason = limitReason(notes)
|
||||
ban.Expires = l.limitExpiry(held, now)
|
||||
case CauseAttack:
|
||||
ban.Reason = attackReason(notes)
|
||||
if cause == CauseAttack {
|
||||
ban.Expires = l.attackExpiry(held, now)
|
||||
case CauseAdmin, CauseAbuseIPDB:
|
||||
fallthrough
|
||||
default:
|
||||
panic("bans: a request's ban has the cause " + string(grounds.Cause))
|
||||
} else {
|
||||
ban.Expires = l.limitExpiry(held, now)
|
||||
}
|
||||
|
||||
if !keep {
|
||||
@@ -786,7 +807,7 @@ func (l *Ledger) ban(
|
||||
}
|
||||
|
||||
l.add(ban)
|
||||
l.made[grounds.Cause]++
|
||||
l.made[cause]++
|
||||
l.markChanged()
|
||||
|
||||
return ban, true
|
||||
|
||||
+51
-121
@@ -22,11 +22,7 @@ func TestRepeatsTripleUntilPermanent(t *testing.T) {
|
||||
// Each ban is followed by another as soon as it ends: 1, 3, 9, 27 and
|
||||
// 81 hours.
|
||||
for i, hours := range []int{1, 3, 9, 27, 81} {
|
||||
ban, _ := ledger.Ban(
|
||||
netblock,
|
||||
now,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
|
||||
length := time.Duration(hours) * time.Hour
|
||||
if !ban.Expires.Equal(now.Add(length)) ||
|
||||
@@ -46,7 +42,7 @@ func TestRepeatsTripleUntilPermanent(t *testing.T) {
|
||||
|
||||
// The sixth would last 243 hours, more than seven days: it is
|
||||
// permanent, and never ends.
|
||||
ban, _ := ledger.Ban(netblock, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
if !ban.Permanent() {
|
||||
t.Fatalf("sixth ban ends at %s, want a permanent one", ban.Expires)
|
||||
}
|
||||
@@ -75,15 +71,15 @@ func TestRepeatWindowRunsOut(t *testing.T) {
|
||||
ledger := bans.New(defaultParams())
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
|
||||
first, _ := ledger.Ban(
|
||||
first, _ := ledger.BanForLimit(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
second, _ := ledger.Ban(
|
||||
second, _ := ledger.BanForLimit(
|
||||
netblock,
|
||||
first.Expires.Add(tc.gap),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
|
||||
if second.Expires.Sub(second.Start) != tc.want ||
|
||||
@@ -108,10 +104,10 @@ func TestFirstBanLongerThanLimitBanPermanentAboveIsPermanent(t *testing.T) {
|
||||
params.LimitBanDuration = params.LimitBanPermanentAbove + time.Hour
|
||||
ledger := bans.New(params)
|
||||
|
||||
ban, _ := ledger.Ban(
|
||||
ban, _ := ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.9/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
if !ban.Permanent() {
|
||||
t.Errorf("first ban ends at %s, want a permanent one", ban.Expires)
|
||||
@@ -131,11 +127,7 @@ func TestLimitBanPermanentAboveSetFarOffDoesNotOverflow(t *testing.T) {
|
||||
now := midnight()
|
||||
|
||||
for i := range 14 {
|
||||
ban, _ := ledger.Ban(
|
||||
netblock,
|
||||
now,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
if !ban.Expires.After(ban.Start) {
|
||||
t.Fatalf(
|
||||
"ban %d starts at %s and ends at %s",
|
||||
@@ -148,7 +140,7 @@ func TestLimitBanPermanentAboveSetFarOffDoesNotOverflow(t *testing.T) {
|
||||
now = ban.Expires
|
||||
}
|
||||
|
||||
ban, _ := ledger.Ban(netblock, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
if !ban.Permanent() {
|
||||
t.Errorf("15th ban ends at %s, want a permanent one", ban.Expires)
|
||||
}
|
||||
@@ -160,19 +152,15 @@ func TestBrokenLimitDuringABanMakesNoOther(t *testing.T) {
|
||||
ledger := bans.New(defaultParams())
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
|
||||
first, made := ledger.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
first, made := ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
if !made {
|
||||
t.Error("the first ban was not made")
|
||||
}
|
||||
|
||||
again, made := ledger.Ban(
|
||||
again, made := ledger.BanForLimit(
|
||||
netblock,
|
||||
midnight().Add(time.Minute),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
|
||||
if made || !reflect.DeepEqual(again, first) ||
|
||||
@@ -187,10 +175,10 @@ func TestBrokenLimitDuringABanMakesNoOther(t *testing.T) {
|
||||
)
|
||||
}
|
||||
|
||||
again, made = ledger.Ban(
|
||||
again, made = ledger.BanForAttack(
|
||||
netblock,
|
||||
midnight().Add(time.Minute),
|
||||
bans.Grounds{Cause: bans.CauseAttack},
|
||||
bans.Notes{},
|
||||
)
|
||||
if made || !reflect.DeepEqual(again, first) {
|
||||
t.Errorf(
|
||||
@@ -207,10 +195,10 @@ func TestCheckRefusesWhileTheBanLastsAndCountsTheRefusals(t *testing.T) {
|
||||
|
||||
ledger := bans.New(defaultParams())
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
ban, _ := ledger.Ban(
|
||||
ban, _ := ledger.BanForLimit(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit, Notes: bans.Notes{Requests: 5}},
|
||||
bans.Notes{Requests: 5},
|
||||
)
|
||||
|
||||
for range 3 {
|
||||
@@ -249,10 +237,10 @@ func TestFindCountsNothing(t *testing.T) {
|
||||
|
||||
ledger := bans.New(defaultParams())
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
ban, _ := ledger.Ban(
|
||||
ban, _ := ledger.BanForLimit(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit, Notes: bans.Notes{Requests: 5}},
|
||||
bans.Notes{Requests: 5},
|
||||
)
|
||||
|
||||
got, banned, _ := ledger.Find(
|
||||
@@ -299,24 +287,24 @@ func TestMaxBansDropsTheEarliestBanOfTheNetblockSeenLongestAgo(
|
||||
d := netip.MustParsePrefix("2001:db8::/64")
|
||||
now := midnight()
|
||||
|
||||
first, _ := ledger.Ban(a, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.Ban(b, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.Ban(c, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
first, _ := ledger.BanForLimit(a, now, bans.Notes{})
|
||||
ledger.BanForLimit(b, now, bans.Notes{})
|
||||
ledger.BanForLimit(c, now, bans.Notes{})
|
||||
|
||||
// A request from a makes b the netblock seen longest ago, and its ban
|
||||
// goes to make room for d's.
|
||||
ledger.Check(a.Addr(), now)
|
||||
ledger.Ban(d, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(d, now, bans.Notes{})
|
||||
wantBans(t, ledger, map[netip.Prefix]int{a: 1, b: 0, c: 1, d: 1})
|
||||
|
||||
// a is banned again once its ban has ended; c, seen longest ago, goes.
|
||||
ledger.Ban(a, first.Expires, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(a, first.Expires, bans.Notes{})
|
||||
wantBans(t, ledger, map[netip.Prefix]int{a: 2, c: 0, d: 1})
|
||||
|
||||
// With d seen since, a is seen longest ago, and its earlier ban goes
|
||||
// first.
|
||||
ledger.Check(d.Addr(), first.Expires)
|
||||
ledger.Ban(b, first.Expires, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(b, first.Expires, bans.Notes{})
|
||||
wantBans(t, ledger, map[netip.Prefix]int{a: 1, b: 1, d: 1})
|
||||
|
||||
if !ledger.Bans(a)[0].Start.Equal(first.Expires) {
|
||||
@@ -337,16 +325,8 @@ func TestFullLedgerDropsTheEarlierBanOfTheNetblockBannedAgain(t *testing.T) {
|
||||
ledger := bans.New(params)
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
|
||||
first, _ := ledger.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
second, _ := ledger.Ban(
|
||||
netblock,
|
||||
first.Expires,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
first, _ := ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
second, _ := ledger.BanForLimit(netblock, first.Expires, bans.Notes{})
|
||||
|
||||
held := ledger.Bans(netblock)
|
||||
if len(held) != 1 || !reflect.DeepEqual(held[0], second) ||
|
||||
@@ -363,11 +343,7 @@ func TestRequestDuringAnAttackBanMakesItPermanent(t *testing.T) {
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
notes := bans.Notes{RuleID: "env-file", Target: "path"}
|
||||
|
||||
ban, _ := ledger.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseAttack, Notes: notes},
|
||||
)
|
||||
ban, _ := ledger.BanForAttack(netblock, midnight(), notes)
|
||||
if !ban.Expires.Equal(midnight().Add(7*day)) ||
|
||||
ban.Cause != bans.CauseAttack ||
|
||||
ban.Notes.RuleID != "env-file" ||
|
||||
@@ -442,16 +418,8 @@ func TestAttackAfterAnAttackBanHasEndedBansPermanently(t *testing.T) {
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
|
||||
// A ban for a broken limit before does not count.
|
||||
first, _ := ledger.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
second, _ := ledger.Ban(
|
||||
netblock,
|
||||
first.Expires,
|
||||
bans.Grounds{Cause: bans.CauseAttack},
|
||||
)
|
||||
first, _ := ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
second, _ := ledger.BanForAttack(netblock, first.Expires, bans.Notes{})
|
||||
|
||||
if second.Expires.Sub(second.Start) != 7*day {
|
||||
t.Fatalf("the first ban for an attack lasts %s, want 7 days",
|
||||
@@ -467,10 +435,10 @@ func TestAttackAfterAnAttackBanHasEndedBansPermanently(t *testing.T) {
|
||||
|
||||
// Its notes show the earlier ban for an attack that makes it permanent,
|
||||
// beside the one for a limit.
|
||||
third, _ := ledger.Ban(
|
||||
third, _ := ledger.BanForAttack(
|
||||
netblock,
|
||||
second.Expires.Add(30*day),
|
||||
bans.Grounds{Cause: bans.CauseAttack},
|
||||
bans.Notes{},
|
||||
)
|
||||
if !third.Permanent() ||
|
||||
third.Notes.EarlierBans != (bans.EarlierBans{Limit: 1, Attack: 1}) {
|
||||
@@ -484,18 +452,14 @@ func TestWouldBanGivesTheBanWithoutMakingIt(t *testing.T) {
|
||||
|
||||
ledger := bans.New(defaultParams())
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
first, _ := ledger.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
first, _ := ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
wantChanged(t, ledger, true)
|
||||
|
||||
// While the first ban lasts, none would be made.
|
||||
during, would := ledger.WouldBan(
|
||||
during, would := ledger.WouldBanForAttack(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseAttack},
|
||||
bans.Notes{},
|
||||
)
|
||||
if would || !reflect.DeepEqual(during, first) {
|
||||
t.Errorf(
|
||||
@@ -508,19 +472,13 @@ func TestWouldBanGivesTheBanWithoutMakingIt(t *testing.T) {
|
||||
|
||||
// As it ends, a clear sign of attack would ban for seven days, and a
|
||||
// limit broken again for three hours, but neither is made.
|
||||
limitGrounds := bans.Grounds{
|
||||
Cause: bans.CauseLimit,
|
||||
Notes: bans.Notes{Kind: "requests", Limit: 1, Window: "minute"},
|
||||
}
|
||||
attack, wouldAttack := ledger.WouldBan(netblock, first.Expires,
|
||||
bans.Grounds{
|
||||
Cause: bans.CauseAttack,
|
||||
Notes: bans.Notes{RuleID: "git-dir"},
|
||||
})
|
||||
limit, wouldLimit := ledger.WouldBan(
|
||||
limitNotes := bans.Notes{Kind: "requests", Limit: 1, Window: "minute"}
|
||||
attack, wouldAttack := ledger.WouldBanForAttack(netblock, first.Expires,
|
||||
bans.Notes{RuleID: "git-dir"})
|
||||
limit, wouldLimit := ledger.WouldBanForLimit(
|
||||
netblock,
|
||||
first.Expires,
|
||||
limitGrounds,
|
||||
limitNotes,
|
||||
)
|
||||
|
||||
if !wouldAttack || !attack.Expires.Equal(first.Expires.Add(7*day)) ||
|
||||
@@ -547,7 +505,7 @@ func TestWouldBanGivesTheBanWithoutMakingIt(t *testing.T) {
|
||||
wantChanged(t, ledger, false)
|
||||
|
||||
// The ban made is the one that would have been.
|
||||
made, _ := ledger.Ban(netblock, first.Expires, limitGrounds)
|
||||
made, _ := ledger.BanForLimit(netblock, first.Expires, limitNotes)
|
||||
if !reflect.DeepEqual(made, limit) {
|
||||
t.Errorf("the ban made is %+v, want %+v", made, limit)
|
||||
}
|
||||
@@ -568,11 +526,7 @@ func TestWouldBePermanentAnswersAsTheBanWouldBeMade(t *testing.T) {
|
||||
t.Fatalf("ban %d for a limit would be permanent", i+1)
|
||||
}
|
||||
|
||||
ban, _ := ledger.Ban(
|
||||
netblock,
|
||||
now,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
now = ban.Expires
|
||||
}
|
||||
|
||||
@@ -585,11 +539,7 @@ func TestWouldBePermanentAnswersAsTheBanWouldBeMade(t *testing.T) {
|
||||
}
|
||||
|
||||
// Once a first ban for an attack has ended, the next would be permanent.
|
||||
attack, _ := ledger.Ban(
|
||||
netblock,
|
||||
now,
|
||||
bans.Grounds{Cause: bans.CauseAttack},
|
||||
)
|
||||
attack, _ := ledger.BanForAttack(netblock, now, bans.Notes{})
|
||||
if !ledger.WouldBePermanent(netblock, attack.Expires, bans.CauseAttack) {
|
||||
t.Error("a second ban for an attack would not be permanent")
|
||||
}
|
||||
@@ -603,16 +553,8 @@ func TestAttackBanDoesNotLengthenTheNextBanForALimit(t *testing.T) {
|
||||
|
||||
// Three times the seven days would be permanent; a limit broken as the
|
||||
// ban for an attack ends bans for an hour, as a first broken limit does.
|
||||
attack, _ := ledger.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseAttack},
|
||||
)
|
||||
limit, _ := ledger.Ban(
|
||||
netblock,
|
||||
attack.Expires,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
attack, _ := ledger.BanForAttack(netblock, midnight(), bans.Notes{})
|
||||
limit, _ := ledger.BanForLimit(netblock, attack.Expires, bans.Notes{})
|
||||
|
||||
if limit.Expires.Sub(limit.Start) != time.Hour ||
|
||||
limit.Cause != bans.CauseLimit {
|
||||
@@ -636,13 +578,10 @@ func TestRequestTextsAreCutTo256Bytes(t *testing.T) {
|
||||
Time: midnight(), Method: long, Host: long, Path: long, Status: 403, UserAgent: long,
|
||||
}
|
||||
|
||||
ban, _ := ledger.Ban(
|
||||
ban, _ := ledger.BanForLimit(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{
|
||||
Cause: bans.CauseLimit,
|
||||
Notes: bans.Notes{Request: request},
|
||||
},
|
||||
bans.Notes{Request: request},
|
||||
)
|
||||
|
||||
cut := long[:256]
|
||||
@@ -668,20 +607,11 @@ func TestLookupFillsTheNotesOfTheNetblocksBansWithoutOne(t *testing.T) {
|
||||
|
||||
// A ban made with the client's lookup, one made before it came, after
|
||||
// the first ended, and one on another netblock.
|
||||
ledger.Ban(netblock, midnight(), bans.Grounds{
|
||||
Cause: bans.CauseLimit,
|
||||
Notes: bans.Notes{
|
||||
ASN: "AS64497",
|
||||
ASName: "Other Net",
|
||||
Country: "FR",
|
||||
},
|
||||
ledger.BanForLimit(netblock, midnight(), bans.Notes{
|
||||
ASN: "AS64497", ASName: "Other Net", Country: "FR",
|
||||
})
|
||||
ledger.Ban(
|
||||
netblock,
|
||||
midnight().Add(time.Hour),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
ledger.Ban(other, midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(netblock, midnight().Add(time.Hour), bans.Notes{})
|
||||
ledger.BanForLimit(other, midnight(), bans.Notes{})
|
||||
|
||||
ledger.AddLookup(netblock, "AS64496", "Example Net", "DE")
|
||||
|
||||
|
||||
@@ -19,29 +19,25 @@ func TestChangedAfterABanIsMade(t *testing.T) {
|
||||
|
||||
wantChanged(t, ledger, false)
|
||||
|
||||
ledger.Ban(netblock, midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
wantChanged(t, ledger, true)
|
||||
|
||||
// A limit broken during the ban makes no other, and a refusal changes
|
||||
// only the counts in the notes, which wait for the interval's write.
|
||||
ledger.Ban(
|
||||
netblock,
|
||||
midnight().Add(time.Minute),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
ledger.BanForLimit(netblock, midnight().Add(time.Minute), bans.Notes{})
|
||||
ledger.Check(netblock.Addr(), midnight().Add(time.Minute))
|
||||
wantChanged(t, ledger, false)
|
||||
|
||||
// Two bans before the value is read leave one.
|
||||
ledger.Ban(
|
||||
ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.10/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
ledger.Ban(
|
||||
ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.11/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
wantChanged(t, ledger, true)
|
||||
wantChanged(t, ledger, false)
|
||||
@@ -55,14 +51,10 @@ func TestSnapshotListsEveryBanByNetblock(t *testing.T) {
|
||||
high := netip.MustParsePrefix("203.0.113.10/32")
|
||||
low := netip.MustParsePrefix("203.0.113.9/32")
|
||||
|
||||
first, _ := ledger.Ban(
|
||||
v6,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
ledger.Ban(high, midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.Ban(low, midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.Ban(v6, first.Expires, bans.Grounds{Cause: bans.CauseLimit})
|
||||
first, _ := ledger.BanForLimit(v6, midnight(), bans.Notes{})
|
||||
ledger.BanForLimit(high, midnight(), bans.Notes{})
|
||||
ledger.BanForLimit(low, midnight(), bans.Notes{})
|
||||
ledger.BanForLimit(v6, first.Expires, bans.Notes{})
|
||||
|
||||
snapshot := ledger.Snapshot()
|
||||
|
||||
@@ -88,10 +80,7 @@ func TestLoadedBansCarryOn(t *testing.T) {
|
||||
|
||||
before := bans.New(defaultParams())
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
ban, _ := before.Ban(netblock, midnight(), bans.Grounds{
|
||||
Cause: bans.CauseLimit,
|
||||
Notes: bans.Notes{Limit: 1},
|
||||
})
|
||||
ban, _ := before.BanForLimit(netblock, midnight(), bans.Notes{Limit: 1})
|
||||
|
||||
// Loaded into a new ledger, as across a restart, the ban still refuses
|
||||
// while it lasts, and once it has ended a broken limit bans for three
|
||||
@@ -107,11 +96,7 @@ func TestLoadedBansCarryOn(t *testing.T) {
|
||||
t.Error("the loaded ban does not refuse")
|
||||
}
|
||||
|
||||
again, _ := after.Ban(
|
||||
netblock,
|
||||
ban.Expires,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
again, _ := after.BanForLimit(netblock, ban.Expires, bans.Notes{})
|
||||
if again.Expires.Sub(again.Start) != 3*time.Hour ||
|
||||
again.Notes.EarlierBans != (bans.EarlierBans{Limit: 1}) {
|
||||
t.Errorf(
|
||||
@@ -139,10 +124,10 @@ func TestLoadedBanRefusesEveryClientInItsNetblock(t *testing.T) {
|
||||
Start: midnight(),
|
||||
},
|
||||
})
|
||||
ledger.Ban(
|
||||
ledger.BanForLimit(
|
||||
netip.MustParsePrefix("198.51.100.7/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
|
||||
for client, want := range map[string]bool{
|
||||
@@ -215,7 +200,7 @@ func TestPermanentBanStartedBeforeAnEndedOneRefuses(t *testing.T) {
|
||||
}
|
||||
|
||||
// A limit broken now makes no shorter ban over the permanent one.
|
||||
ban, _ = ledger.Ban(netblock, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ban, _ = ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
if !ban.Permanent() || len(ledger.Bans(netblock)) != 2 {
|
||||
t.Errorf(
|
||||
"a broken limit returned %+v and left the netblock %d bans, "+
|
||||
@@ -251,10 +236,10 @@ func TestNextBanWorkedOutFromTheBanThatEndedLast(t *testing.T) {
|
||||
// Once both have ended, a limit broken within the repeat window bans
|
||||
// for three times the 9 hours, and the notes count the two bans
|
||||
// before the 9-hour one and it, for a limit, and the admin's.
|
||||
ban, _ := ledger.Ban(
|
||||
ban, _ := ledger.BanForLimit(
|
||||
netblock,
|
||||
nineHours.Expires.Add(time.Hour),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
if ban.Expires.Sub(ban.Start) != 27*time.Hour ||
|
||||
ban.Notes.EarlierBans != (bans.EarlierBans{Limit: 3, Admin: 1}) {
|
||||
@@ -328,15 +313,15 @@ func TestLoadReplacesTheBansHeld(t *testing.T) {
|
||||
}
|
||||
|
||||
// The ledger holds one ban, so it makes two more without dropping any.
|
||||
first, _ := ledger.Ban(
|
||||
first, _ := ledger.BanForLimit(
|
||||
netip.MustParsePrefix("198.51.100.7/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
second, _ := ledger.Ban(
|
||||
second, _ := ledger.BanForLimit(
|
||||
netip.MustParsePrefix("198.51.100.8/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
|
||||
want := []bans.Ban{first, second, kept}
|
||||
|
||||
@@ -19,8 +19,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
||||
)
|
||||
|
||||
// off is the value that switches a timeout, a size limit or a rate limit
|
||||
@@ -42,7 +40,9 @@ var (
|
||||
errNotPositive = errors.New("must be more than zero, or off")
|
||||
errNotMilliseconds = errors.New(
|
||||
"is not a whole number of milliseconds above zero, such as 2000")
|
||||
errEmptyItem = errors.New("has an empty item in its list")
|
||||
errEmptyItem = errors.New("has an empty item in its list")
|
||||
errNotNetblock = errors.New(
|
||||
"is not a netblock such as 10.0.0.0/8, or an address")
|
||||
errNotListenAddr = errors.New(
|
||||
"is not an address to listen on, such as :8080")
|
||||
errNotCommand = errors.New("is not rproxy or waf")
|
||||
@@ -253,7 +253,7 @@ func parseNetblocks(value string) ([]netip.Prefix, error) {
|
||||
netblocks := make([]netip.Prefix, 0, len(items))
|
||||
|
||||
for _, item := range items {
|
||||
netblock, err := reputation.ParseNetblock(item)
|
||||
netblock, err := ParseNetblock(item)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -264,6 +264,27 @@ func parseNetblocks(value string) ([]netip.Prefix, error) {
|
||||
return netblocks, nil
|
||||
}
|
||||
|
||||
// ParseNetblock reads a netblock in CIDR form, such as 10.0.0.0/8. A bare
|
||||
// address is a netblock of that address alone, a /32 or a /128. A
|
||||
// blocklist's lines are read with it too.
|
||||
func ParseNetblock(value string) (netip.Prefix, error) {
|
||||
if strings.Contains(value, "/") {
|
||||
netblock, err := netip.ParsePrefix(value)
|
||||
if err != nil {
|
||||
return netip.Prefix{}, fmt.Errorf("%q %w", value, errNotNetblock)
|
||||
}
|
||||
|
||||
return netblock.Masked(), nil
|
||||
}
|
||||
|
||||
addr, err := netip.ParseAddr(value)
|
||||
if err != nil || addr.Zone() != "" {
|
||||
return netip.Prefix{}, fmt.Errorf("%q %w", value, errNotNetblock)
|
||||
}
|
||||
|
||||
return netip.PrefixFrom(addr, addr.BitLen()), nil
|
||||
}
|
||||
|
||||
// parseListenAddr checks an address to listen on: an optional host and a
|
||||
// port number.
|
||||
func parseListenAddr(value string) (string, error) {
|
||||
|
||||
+163
-189
@@ -20,26 +20,11 @@ import (
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"golang.org/x/net/http/httpguts"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||
"sneak.berlin/go/smallwebwaf/internal/anomaly"
|
||||
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
|
||||
"sneak.berlin/go/smallwebwaf/internal/remotelog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
||||
)
|
||||
|
||||
// The names of the settings that waf writes into log lines and ban notes,
|
||||
// as the setting that refused a request or lowered a client's limits.
|
||||
const (
|
||||
ExclusivelyAllowedCountriesSetting = "SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES"
|
||||
TagLimitPercentSetting = "SWW_WAF_TAG_LIMIT_PERCENT"
|
||||
ASNBytesLimitPercentSetting = "SWW_WAF_ASN_BYTES_LIMIT_PERCENT"
|
||||
CountryBytesLimitPercentSetting = "SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT"
|
||||
UnknownCountryLimitPercentSetting = "SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT"
|
||||
ASNLimitPercentURLSetting = "SWW_WAF_ASN_LIMIT_PERCENT_URL"
|
||||
BlocklistActionSetting = "SWW_WAF_BLOCKLIST_ACTION"
|
||||
DNSBLAbuseIPDBActionSetting = "SWW_WAF_DNSBL_ABUSEIPDB_ACTION"
|
||||
)
|
||||
|
||||
// WAF is the settings of `sww waf`. A time or size limit of zero is off.
|
||||
@@ -90,7 +75,7 @@ type WAF struct {
|
||||
BytesLimitPerMinute int64
|
||||
BytesLimitPerHour int64
|
||||
BytesLimitPerDay int64
|
||||
BytesCounted BytesCounted
|
||||
BytesCounted string
|
||||
// LookupSource is where each client's AS number and country are looked
|
||||
// up (SWW_WAF_LOOKUP_SOURCE): geojs, file, or off for nowhere.
|
||||
// LookupDBPath is the lookup database, the IPinfo Lite file looked up in
|
||||
@@ -101,7 +86,7 @@ type WAF struct {
|
||||
// AddLookupHeaders is true when the app is passed the client's AS number
|
||||
// and country in headers (SWW_WAF_ADD_LOOKUP_HEADERS), by default unless
|
||||
// LookupSource is off.
|
||||
LookupSource LookupSource
|
||||
LookupSource string
|
||||
LookupDBPath string
|
||||
ASNAndCountryLookupTimeout time.Duration
|
||||
AddLookupHeaders bool
|
||||
@@ -115,7 +100,7 @@ type WAF struct {
|
||||
// with a tag that percentage of every rate limit and byte limit, from 1
|
||||
// to 99 (SWW_WAF_TAG_LIMIT_PERCENT). All hold tags as requests carry them: in
|
||||
// lower case, a country code in capitals.
|
||||
TagLists []reputation.TagList
|
||||
TagLists []TagList
|
||||
TagDeny []string
|
||||
TagLimitPercent map[string]int64
|
||||
// RouteWatchURL is where the routewatch instance is that adds the asn=
|
||||
@@ -146,7 +131,7 @@ type WAF struct {
|
||||
// BlocklistLimitPercent is the percentage of every limit it gets.
|
||||
BlocklistURLs []string
|
||||
BlocklistRefresh time.Duration
|
||||
BlocklistAction ReputationAction
|
||||
BlocklistAction string
|
||||
BlocklistLimitPercent int64
|
||||
// DNSBLZones are the DNSBL zones clients are asked about
|
||||
// (SWW_WAF_DNSBL_ZONES), through DNSBLResolver (SWW_WAF_DNSBL_RESOLVER), or
|
||||
@@ -169,7 +154,7 @@ type WAF struct {
|
||||
AbuseIPDBKey string
|
||||
AbuseIPDBMinScore int64
|
||||
AbuseIPDBDailyBudget int
|
||||
DNSBLAbuseIPDBAction ReputationAction
|
||||
DNSBLAbuseIPDBAction string
|
||||
DNSBLAbuseIPDBLimitPercent int64
|
||||
IPReputationCacheTTL time.Duration
|
||||
DNSBLAbuseIPDBTimeout time.Duration
|
||||
@@ -252,7 +237,7 @@ type WAF struct {
|
||||
// reads none.
|
||||
// CoreRuleSetExclusionsFile is the path of the file of its exclusions
|
||||
// (SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE), "" while there is none.
|
||||
CoreRuleSetMode CoreRuleSetMode
|
||||
CoreRuleSetMode string
|
||||
CoreRuleSetParanoiaLevel int
|
||||
CoreRuleSetScoreThreshold int
|
||||
CoreRuleSetDisabledRules []int
|
||||
@@ -305,7 +290,7 @@ type WAF struct {
|
||||
AlertSlackWebhookURL *url.URL
|
||||
AlertNtfyURL *url.URL
|
||||
AlertNtfyToken string
|
||||
AlertEvents []alerts.Event
|
||||
AlertEvents []string
|
||||
AlertCooldown time.Duration
|
||||
AlertMaxPerHour int
|
||||
// The anomaly thresholds, which only raise alerts: the most requests
|
||||
@@ -331,66 +316,20 @@ type WAF struct {
|
||||
settings []slog.Attr
|
||||
}
|
||||
|
||||
// BytesCounted is which body bytes count toward the byte limits and the
|
||||
// byte anomaly thresholds, a value of SWW_WAF_BYTES_COUNTED.
|
||||
type BytesCounted string
|
||||
|
||||
// The values of SWW_WAF_BYTES_COUNTED.
|
||||
const (
|
||||
// BytesCountedResponse counts the response's body bytes.
|
||||
BytesCountedResponse BytesCounted = "response"
|
||||
// BytesCountedRequest counts the request's body bytes.
|
||||
BytesCountedRequest BytesCounted = "request"
|
||||
// BytesCountedBoth counts the body bytes of both.
|
||||
BytesCountedBoth BytesCounted = "both"
|
||||
)
|
||||
|
||||
// LookupSource is where each client's AS number and country are looked up,
|
||||
// a value of SWW_WAF_LOOKUP_SOURCE.
|
||||
type LookupSource string
|
||||
|
||||
// The values of SWW_WAF_LOOKUP_SOURCE.
|
||||
const (
|
||||
// LookupSourceGeoJS asks GeoJS.
|
||||
LookupSourceGeoJS LookupSource = "geojs"
|
||||
// LookupSourceFile looks clients up in the lookup database, the file
|
||||
// SWW_WAF_LOOKUP_DB_PATH names.
|
||||
LookupSourceFile LookupSource = "file"
|
||||
// LookupSourceOff looks no client up.
|
||||
LookupSourceOff LookupSource = "off"
|
||||
)
|
||||
|
||||
// ReputationAction is what is done with a client a reputation source
|
||||
// lists, a value of SWW_WAF_BLOCKLIST_ACTION and of
|
||||
// SWW_WAF_DNSBL_ABUSEIPDB_ACTION.
|
||||
type ReputationAction string
|
||||
|
||||
// The values of SWW_WAF_BLOCKLIST_ACTION and SWW_WAF_DNSBL_ABUSEIPDB_ACTION.
|
||||
const (
|
||||
// ReputationActionDeny refuses the client's requests.
|
||||
ReputationActionDeny ReputationAction = "deny"
|
||||
// ReputationActionLimit gives the client a percentage of every limit,
|
||||
// written limit:<percent> in the setting.
|
||||
ReputationActionLimit ReputationAction = "limit"
|
||||
// ReputationActionLog only notes the hit in the log line and raises an
|
||||
// alert.
|
||||
ReputationActionLog ReputationAction = "log"
|
||||
)
|
||||
|
||||
// CoreRuleSetMode is what the Core Rule Set does, a value of
|
||||
// SWW_WAF_CORE_RULE_SET_MODE.
|
||||
type CoreRuleSetMode string
|
||||
|
||||
// The values of SWW_WAF_CORE_RULE_SET_MODE.
|
||||
const (
|
||||
// CoreRuleSetModeOff runs no request through the Core Rule Set.
|
||||
CoreRuleSetModeOff CoreRuleSetMode = "off"
|
||||
CoreRuleSetModeOff = off
|
||||
// CoreRuleSetModeDetect logs and alerts a match, and refuses nothing.
|
||||
CoreRuleSetModeDetect CoreRuleSetMode = "detect"
|
||||
CoreRuleSetModeDetect = "detect"
|
||||
// CoreRuleSetModeBlock refuses a match with 403.
|
||||
CoreRuleSetModeBlock CoreRuleSetMode = "block"
|
||||
CoreRuleSetModeBlock = "block"
|
||||
)
|
||||
|
||||
// fileSource is the SWW_WAF_LOOKUP_SOURCE that looks clients up in the
|
||||
// lookup database, the file SWW_WAF_LOOKUP_DB_PATH names.
|
||||
const fileSource = "file"
|
||||
|
||||
const (
|
||||
day = 24 * time.Hour
|
||||
ipv4Bits = 32
|
||||
@@ -512,8 +451,12 @@ var (
|
||||
"is not a whole number above zero, such as 60, or off",
|
||||
)
|
||||
errNotUTF8 = errors.New("is not valid UTF-8")
|
||||
errNotASN = errors.New("is not an AS number such as AS64496")
|
||||
errNotPercentItem = errors.New(
|
||||
"is not a code, : and a percentage, such as AS64496:50 or cn:25")
|
||||
errNotPercent = errors.New(
|
||||
"is not a percentage, a whole number from 0 to 100",
|
||||
)
|
||||
errNotAbuseIPDBThreshold = errors.New(
|
||||
"is not a whole number from 25 to 100, the scores AbuseIPDB's " +
|
||||
"blacklist can be cut at",
|
||||
@@ -615,13 +558,10 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
|
||||
),
|
||||
BytesLimitPerHour: env.size("SWW_WAF_BYTES_LIMIT_PER_HOUR", "50G"),
|
||||
BytesLimitPerDay: env.size("SWW_WAF_BYTES_LIMIT_PER_DAY", "100G"),
|
||||
BytesCounted: env.bytesCounted(
|
||||
"SWW_WAF_BYTES_COUNTED",
|
||||
BytesCountedBoth,
|
||||
),
|
||||
BytesCounted: env.bytesCounted("SWW_WAF_BYTES_COUNTED", "both"),
|
||||
LookupSource: env.lookupSource(
|
||||
"SWW_WAF_LOOKUP_SOURCE",
|
||||
LookupSourceGeoJS,
|
||||
"geojs",
|
||||
),
|
||||
LookupDBPath: env.value("SWW_WAF_LOOKUP_DB_PATH", ""),
|
||||
ASNAndCountryLookupTimeout: env.milliseconds(
|
||||
@@ -629,24 +569,24 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
|
||||
"800",
|
||||
),
|
||||
ExclusivelyAllowedCountries: env.countries(
|
||||
ExclusivelyAllowedCountriesSetting, ""),
|
||||
"SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES", ""),
|
||||
TagLists: env.tagLists("SWW_WAF_TAG_LISTS"),
|
||||
TagDeny: env.tags("SWW_WAF_TAG_DENY"),
|
||||
TagLimitPercent: env.tagPercents(TagLimitPercentSetting),
|
||||
TagLimitPercent: env.tagPercents("SWW_WAF_TAG_LIMIT_PERCENT"),
|
||||
RouteWatchURL: env.routeWatchURL("SWW_WAF_ROUTEWATCH_URL"),
|
||||
ASNBytesLimitPercent: env.percents(
|
||||
ASNBytesLimitPercentSetting,
|
||||
reputation.ParseASN,
|
||||
"SWW_WAF_ASN_BYTES_LIMIT_PERCENT",
|
||||
ParseASN,
|
||||
),
|
||||
CountryBytesLimitPercent: env.percents(
|
||||
CountryBytesLimitPercentSetting,
|
||||
"SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT",
|
||||
parseCountry,
|
||||
),
|
||||
UnknownCountryLimitPercent: env.percent(
|
||||
UnknownCountryLimitPercentSetting,
|
||||
"SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT",
|
||||
"100",
|
||||
),
|
||||
ASNLimitPercentURL: env.listURL(ASNLimitPercentURLSetting),
|
||||
ASNLimitPercentURL: env.listURL("SWW_WAF_ASN_LIMIT_PERCENT_URL"),
|
||||
BlocklistURLs: env.listURLs("SWW_WAF_BLOCKLIST_URLS"),
|
||||
BlocklistRefresh: env.refresh("SWW_WAF_BLOCKLIST_REFRESH", "24h"),
|
||||
DNSBLZones: env.zones("SWW_WAF_DNSBL_ZONES"),
|
||||
@@ -789,8 +729,9 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
|
||||
),
|
||||
AlertNtfyURL: env.webhookURL("SWW_WAF_ALERT_NTFY_URL"),
|
||||
AlertNtfyToken: env.secret("SWW_WAF_ALERT_NTFY_TOKEN"),
|
||||
AlertEvents: env.alertEvents("SWW_WAF_ALERT_EVENTS"),
|
||||
AlertCooldown: env.duration("SWW_WAF_ALERT_COOLDOWN", "15m"),
|
||||
AlertEvents: env.alertEvents("SWW_WAF_ALERT_EVENTS",
|
||||
strings.Join(alerts.Events(), ",")),
|
||||
AlertCooldown: env.duration("SWW_WAF_ALERT_COOLDOWN", "15m"),
|
||||
AlertMaxPerHour: env.numberOrOff(
|
||||
"SWW_WAF_ALERT_MAX_PER_HOUR",
|
||||
"60",
|
||||
@@ -817,11 +758,11 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
|
||||
cfg.InstanceName, cfg.LogRemoteURL != nil)
|
||||
// The app gets the headers by default, unless no client is looked up.
|
||||
cfg.AddLookupHeaders = env.boolean("SWW_WAF_ADD_LOOKUP_HEADERS",
|
||||
strconv.FormatBool(cfg.LookupSource != LookupSourceOff))
|
||||
strconv.FormatBool(cfg.LookupSource != off))
|
||||
cfg.BlocklistAction, cfg.BlocklistLimitPercent = env.action(
|
||||
BlocklistActionSetting, string(ReputationActionDeny))
|
||||
"SWW_WAF_BLOCKLIST_ACTION", "deny")
|
||||
cfg.DNSBLAbuseIPDBAction, cfg.DNSBLAbuseIPDBLimitPercent = env.action(
|
||||
DNSBLAbuseIPDBActionSetting, "limit:25")
|
||||
"SWW_WAF_DNSBL_ABUSEIPDB_ACTION", "limit:25")
|
||||
|
||||
env.checkInstanceNameForNtfy(cfg.InstanceName, cfg.AlertNtfyURL != nil)
|
||||
env.checkLookupDBPath(cfg)
|
||||
@@ -917,12 +858,9 @@ func (e *environment) count(name, defaultValue string) int64 {
|
||||
|
||||
// bytesCounted reads the setting that is which body bytes count toward the
|
||||
// byte limits and the byte anomaly thresholds: response, request or both.
|
||||
func (e *environment) bytesCounted(
|
||||
name string, defaultValue BytesCounted,
|
||||
) BytesCounted {
|
||||
value := BytesCounted(e.value(name, string(defaultValue)))
|
||||
if value != BytesCountedResponse && value != BytesCountedRequest &&
|
||||
value != BytesCountedBoth {
|
||||
func (e *environment) bytesCounted(name, defaultValue string) string {
|
||||
value := e.value(name, defaultValue)
|
||||
if value != "response" && value != "request" && value != "both" {
|
||||
e.check(name, fmt.Errorf("%q %w", value, errNotBytesCounted))
|
||||
}
|
||||
|
||||
@@ -967,10 +905,8 @@ func (e *environment) trapPaths(name string) []string {
|
||||
|
||||
// coreRuleSetMode reads the setting that is what the Core Rule Set does: off,
|
||||
// detect or block.
|
||||
func (e *environment) coreRuleSetMode(
|
||||
name string, defaultValue CoreRuleSetMode,
|
||||
) CoreRuleSetMode {
|
||||
mode := CoreRuleSetMode(e.value(name, string(defaultValue)))
|
||||
func (e *environment) coreRuleSetMode(name, defaultValue string) string {
|
||||
mode := e.value(name, defaultValue)
|
||||
if mode != CoreRuleSetModeOff && mode != CoreRuleSetModeDetect &&
|
||||
mode != CoreRuleSetModeBlock {
|
||||
e.check(name, fmt.Errorf("%q %w", mode, errNotCoreRuleSetMode))
|
||||
@@ -1021,7 +957,7 @@ func (e *environment) percents(
|
||||
}
|
||||
|
||||
// tagLists reads SWW_WAF_TAG_LISTS, which is empty by default.
|
||||
func (e *environment) tagLists(name string) []reputation.TagList {
|
||||
func (e *environment) tagLists(name string) []TagList {
|
||||
lists, err := parseTagLists(e.value(name, ""))
|
||||
e.check(name, err)
|
||||
|
||||
@@ -1047,7 +983,7 @@ func (e *environment) tagPercents(name string) map[string]int64 {
|
||||
|
||||
// percent reads a setting that is a percentage, from 0 to 100.
|
||||
func (e *environment) percent(name, defaultValue string) int64 {
|
||||
percent, err := reputation.ParsePercent(e.value(name, defaultValue))
|
||||
percent, err := ParsePercent(e.value(name, defaultValue))
|
||||
e.check(name, err)
|
||||
|
||||
return percent
|
||||
@@ -1120,29 +1056,25 @@ func (e *environment) refresh(name, defaultValue string) time.Duration {
|
||||
// action reads a setting that is what is done with a client a blocklist
|
||||
// or a DNSBL zone lists: deny, log, or limit:<percent>, which it returns
|
||||
// as limit and the percentage.
|
||||
func (e *environment) action(
|
||||
name, defaultValue string,
|
||||
) (ReputationAction, int64) {
|
||||
value := ReputationAction(e.value(name, defaultValue))
|
||||
if value == ReputationActionDeny || value == ReputationActionLog {
|
||||
func (e *environment) action(name, defaultValue string) (string, int64) {
|
||||
value := e.value(name, defaultValue)
|
||||
if value == "deny" || value == "log" {
|
||||
return value, 0
|
||||
}
|
||||
|
||||
percentText, isLimit := strings.CutPrefix(
|
||||
string(value), string(ReputationActionLimit)+":",
|
||||
)
|
||||
percentText, isLimit := strings.CutPrefix(value, "limit:")
|
||||
|
||||
percent, err := reputation.ParsePercent(percentText)
|
||||
percent, err := ParsePercent(percentText)
|
||||
if !isLimit || err != nil {
|
||||
e.check(name, fmt.Errorf("%q %w", value, errNotAction))
|
||||
}
|
||||
|
||||
return ReputationActionLimit, percent
|
||||
return "limit", percent
|
||||
}
|
||||
|
||||
// zones reads the setting that is the list of DNSBL zones. It is empty by
|
||||
// default. The log shows each zone with its key masked, as
|
||||
// reputation.MaskZoneKey masks it.
|
||||
// default. The log shows each zone with its key masked, as MaskZoneKey
|
||||
// masks it.
|
||||
func (e *environment) zones(name string) []string {
|
||||
value, _ := e.lookup(name)
|
||||
zones, err := parseZones(value)
|
||||
@@ -1150,7 +1082,7 @@ func (e *environment) zones(name string) []string {
|
||||
|
||||
logged := make([]string, len(zones))
|
||||
for i, zone := range zones {
|
||||
logged[i] = reputation.MaskZoneKey(zone)
|
||||
logged[i] = MaskZoneKey(zone)
|
||||
}
|
||||
|
||||
e.settings = append(
|
||||
@@ -1188,12 +1120,9 @@ func (e *environment) routeWatchURL(name string) string {
|
||||
|
||||
// lookupSource reads the setting that is where clients are looked up:
|
||||
// geojs, file, or off.
|
||||
func (e *environment) lookupSource(
|
||||
name string, defaultValue LookupSource,
|
||||
) LookupSource {
|
||||
source := LookupSource(e.value(name, string(defaultValue)))
|
||||
if source != LookupSourceGeoJS && source != LookupSourceFile &&
|
||||
source != LookupSourceOff {
|
||||
func (e *environment) lookupSource(name, defaultValue string) string {
|
||||
source := e.value(name, defaultValue)
|
||||
if source != "geojs" && source != fileSource && source != off {
|
||||
e.check(name, fmt.Errorf("%q %w", source, errNotLookupSource))
|
||||
}
|
||||
|
||||
@@ -1205,12 +1134,12 @@ func (e *environment) lookupSource(
|
||||
// one source at a time.
|
||||
func (e *environment) checkLookupDBPath(cfg *WAF) {
|
||||
switch {
|
||||
case cfg.LookupSource == LookupSourceFile && cfg.LookupDBPath == "":
|
||||
case cfg.LookupSource == fileSource && cfg.LookupDBPath == "":
|
||||
e.check("SWW_WAF_LOOKUP_SOURCE", fmt.Errorf(
|
||||
"is file while SWW_WAF_LOOKUP_DB_PATH is unset; %w",
|
||||
errNeedsDBPath,
|
||||
))
|
||||
case cfg.LookupSource != LookupSourceFile && cfg.LookupDBPath != "":
|
||||
case cfg.LookupSource != fileSource && cfg.LookupDBPath != "":
|
||||
e.check("SWW_WAF_LOOKUP_DB_PATH", fmt.Errorf(
|
||||
"is set while SWW_WAF_LOOKUP_SOURCE is %s; %w",
|
||||
cfg.LookupSource,
|
||||
@@ -1230,12 +1159,12 @@ func (e *environment) checkLookupDBPath(cfg *WAF) {
|
||||
func (e *environment) checkCountriesAndLookups(cfg *WAF) {
|
||||
for _, country := range cfg.ExclusivelyAllowedCountries {
|
||||
if slices.Contains(cfg.TagDeny, CountryTagPrefix+country) {
|
||||
e.check(ExclusivelyAllowedCountriesSetting,
|
||||
e.check("SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES",
|
||||
fmt.Errorf("%q %w", country, errOnBothLists))
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.LookupSource != LookupSourceOff {
|
||||
if cfg.LookupSource != off {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1249,15 +1178,15 @@ func (e *environment) checkCountriesAndLookups(cfg *WAF) {
|
||||
name string
|
||||
set bool
|
||||
}{
|
||||
{ExclusivelyAllowedCountriesSetting, len(cfg.ExclusivelyAllowedCountries) > 0},
|
||||
{"SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES", len(cfg.ExclusivelyAllowedCountries) > 0},
|
||||
{"SWW_WAF_ADD_LOOKUP_HEADERS", cfg.AddLookupHeaders},
|
||||
{"SWW_WAF_TAG_DENY", slices.ContainsFunc(cfg.TagDeny, onlyLookupGives)},
|
||||
{TagLimitPercentSetting, slices.ContainsFunc(
|
||||
{"SWW_WAF_TAG_LIMIT_PERCENT", slices.ContainsFunc(
|
||||
slices.Collect(maps.Keys(cfg.TagLimitPercent)), onlyLookupGives)},
|
||||
{ASNBytesLimitPercentSetting, len(cfg.ASNBytesLimitPercent) > 0},
|
||||
{CountryBytesLimitPercentSetting, len(cfg.CountryBytesLimitPercent) > 0},
|
||||
{UnknownCountryLimitPercentSetting, cfg.UnknownCountryLimitPercent < 100},
|
||||
{ASNLimitPercentURLSetting, cfg.ASNLimitPercentURL != ""},
|
||||
{"SWW_WAF_ASN_BYTES_LIMIT_PERCENT", len(cfg.ASNBytesLimitPercent) > 0},
|
||||
{"SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT", len(cfg.CountryBytesLimitPercent) > 0},
|
||||
{"SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT", cfg.UnknownCountryLimitPercent < 100},
|
||||
{"SWW_WAF_ASN_LIMIT_PERCENT_URL", cfg.ASNLimitPercentURL != ""},
|
||||
{"SWW_WAF_ANOMALY_ASN_REQUESTS_PER_MINUTE", cfg.AnomalyASN.RequestsPerMinute > 0},
|
||||
{"SWW_WAF_ANOMALY_ASN_REQUESTS_PER_HOUR", cfg.AnomalyASN.RequestsPerHour > 0},
|
||||
{"SWW_WAF_ANOMALY_ASN_BYTES_PER_MINUTE", cfg.AnomalyASN.BytesPerMinute > 0},
|
||||
@@ -1277,7 +1206,7 @@ func (e *environment) checkCountriesAndLookups(cfg *WAF) {
|
||||
// blocklist too: the file at a URL is fetched as one list or the other.
|
||||
func (e *environment) checkASNLimitPercentURL(cfg *WAF) {
|
||||
if slices.Contains(cfg.BlocklistURLs, cfg.ASNLimitPercentURL) {
|
||||
e.check(ASNLimitPercentURLSetting,
|
||||
e.check("SWW_WAF_ASN_LIMIT_PERCENT_URL",
|
||||
fmt.Errorf("%q %w", cfg.ASNLimitPercentURL, errInBlocklistURLs))
|
||||
}
|
||||
}
|
||||
@@ -1304,18 +1233,14 @@ func (e *environment) checkTags(cfg *WAF) {
|
||||
}
|
||||
|
||||
for tag := range cfg.TagLimitPercent {
|
||||
e.check(TagLimitPercentSetting, checkTag(cfg, tag))
|
||||
e.check("SWW_WAF_TAG_LIMIT_PERCENT", checkTag(cfg, tag))
|
||||
}
|
||||
}
|
||||
|
||||
// checkTag returns an error when tag would never apply, as checkTags says.
|
||||
func checkTag(cfg *WAF, tag string) error {
|
||||
if !requestCanGetTag(cfg, tag) {
|
||||
return fmt.Errorf(
|
||||
"%q %w",
|
||||
reputation.MaskZoneKey(tag),
|
||||
errNoRequestGetsTag,
|
||||
)
|
||||
return fmt.Errorf("%q %w", MaskZoneKey(tag), errNoRequestGetsTag)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -1323,7 +1248,7 @@ func checkTag(cfg *WAF, tag string) error {
|
||||
|
||||
// requestCanGetTag reports whether anything cfg sets up can give a request tag.
|
||||
func requestCanGetTag(cfg *WAF, tag string) bool {
|
||||
isListName := func(list reputation.TagList) bool { return list.Name == tag }
|
||||
isListName := func(list TagList) bool { return list.Name == tag }
|
||||
if isLookupTag(tag) || slices.ContainsFunc(cfg.TagLists, isListName) {
|
||||
return true
|
||||
}
|
||||
@@ -1347,7 +1272,7 @@ func requestCanGetTag(cfg *WAF, tag string) bool {
|
||||
|
||||
return isZoneTag &&
|
||||
slices.ContainsFunc(cfg.DNSBLZones, func(each string) bool {
|
||||
return strings.ToLower(reputation.MaskZoneKey(each)) == zone
|
||||
return strings.ToLower(MaskZoneKey(each)) == zone
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1685,15 +1610,9 @@ func (e *environment) secret(name string) string {
|
||||
return value
|
||||
}
|
||||
|
||||
// alertEvents reads the setting that is the events alerts are sent for,
|
||||
// every event by default.
|
||||
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, ",")))
|
||||
// alertEvents reads the setting that is the events alerts are sent for.
|
||||
func (e *environment) alertEvents(name, defaultValue string) []string {
|
||||
events, err := parseAlertEvents(e.value(name, defaultValue))
|
||||
e.check(name, err)
|
||||
|
||||
return events
|
||||
@@ -1851,9 +1770,7 @@ func parseNamedNetblocks(value string) ([]anomaly.NamedNetblock, error) {
|
||||
return nil, fmt.Errorf("%q %w", item, errNotNamedNetblock)
|
||||
}
|
||||
|
||||
netblock, err := reputation.ParseNetblock(
|
||||
strings.TrimSpace(netblockText),
|
||||
)
|
||||
netblock, err := ParseNetblock(strings.TrimSpace(netblockText))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -2059,6 +1976,21 @@ func parseCountry(value string) (string, error) {
|
||||
return country, nil
|
||||
}
|
||||
|
||||
// ParseASN reads an AS number such as AS64496, in either case, and
|
||||
// returns it as the lookup gives it: AS and the number, in capitals and
|
||||
// without leading zeros. The file SWW_WAF_ASN_LIMIT_PERCENT_URL names is
|
||||
// read with it too.
|
||||
func ParseASN(value string) (string, error) {
|
||||
digits, hasAS := strings.CutPrefix(strings.ToUpper(value), "AS")
|
||||
|
||||
number, err := strconv.ParseUint(digits, 10, 32)
|
||||
if !hasAS || err != nil {
|
||||
return "", fmt.Errorf("%q %w", value, errNotASN)
|
||||
}
|
||||
|
||||
return "AS" + strconv.FormatUint(number, 10), nil
|
||||
}
|
||||
|
||||
// The tags a request can get that the settings can name, as waf
|
||||
// writes them. The prefixes are followed by a value.
|
||||
const (
|
||||
@@ -2072,17 +2004,24 @@ const (
|
||||
ExploitSignatureTag = "exploitsignature"
|
||||
)
|
||||
|
||||
// TagList is an address list of SWW_WAF_TAG_LISTS: the tag it adds to the
|
||||
// requests of the clients it holds, and the URL it is fetched from.
|
||||
type TagList struct {
|
||||
Name string
|
||||
URL string
|
||||
}
|
||||
|
||||
// parseTagLists reads a comma-separated list of name=URL pairs, such as
|
||||
// torexit=https://example.com/tor.txt, and returns each list with its name
|
||||
// in lower case. A name can hold a =, as vpn=mullvad does, and ends at the
|
||||
// = before http. A name or a URL listed twice is an error.
|
||||
func parseTagLists(value string) ([]reputation.TagList, error) {
|
||||
func parseTagLists(value string) ([]TagList, error) {
|
||||
items, err := parseList(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lists := make([]reputation.TagList, 0, len(items))
|
||||
lists := make([]TagList, 0, len(items))
|
||||
|
||||
for _, item := range items {
|
||||
end := strings.Index(item, "=http")
|
||||
@@ -2090,12 +2029,9 @@ func parseTagLists(value string) ([]reputation.TagList, error) {
|
||||
return nil, fmt.Errorf("%q %w", item, errNotTagList)
|
||||
}
|
||||
|
||||
list := reputation.TagList{
|
||||
Name: strings.ToLower(item[:end]),
|
||||
URL: item[end+1:],
|
||||
}
|
||||
list := TagList{Name: strings.ToLower(item[:end]), URL: item[end+1:]}
|
||||
|
||||
if slices.ContainsFunc(lists, func(earlier reputation.TagList) bool {
|
||||
if slices.ContainsFunc(lists, func(earlier TagList) bool {
|
||||
return earlier.Name == list.Name || earlier.URL == list.URL
|
||||
}) {
|
||||
return nil, fmt.Errorf("%q %w", item, errListedTwice)
|
||||
@@ -2108,8 +2044,7 @@ func parseTagLists(value string) ([]reputation.TagList, error) {
|
||||
}
|
||||
|
||||
// parseTags reads a comma-separated list of tags, which parseTag reads. A
|
||||
// tag listed twice is an error, which shows the tag as
|
||||
// reputation.MaskZoneKey does.
|
||||
// tag listed twice is an error, which shows the tag as MaskZoneKey does.
|
||||
func parseTags(value string) ([]string, error) {
|
||||
items, err := parseList(value)
|
||||
if err != nil {
|
||||
@@ -2129,7 +2064,7 @@ func parseTags(value string) ([]string, error) {
|
||||
if slices.Contains(tags, tag) {
|
||||
return nil, fmt.Errorf(
|
||||
"%q %w",
|
||||
reputation.MaskZoneKey(item),
|
||||
MaskZoneKey(item),
|
||||
errListedTwice,
|
||||
)
|
||||
}
|
||||
@@ -2198,7 +2133,7 @@ func parseTagPercents(value string) (map[string]int64, error) {
|
||||
if colon <= 0 {
|
||||
return nil, fmt.Errorf(
|
||||
"%q %w",
|
||||
reputation.MaskZoneKey(item),
|
||||
MaskZoneKey(item),
|
||||
errNotTagPercentItem,
|
||||
)
|
||||
}
|
||||
@@ -2212,7 +2147,7 @@ func parseTagPercents(value string) (map[string]int64, error) {
|
||||
if err != nil || percent < 1 || percent > 99 {
|
||||
return nil, fmt.Errorf(
|
||||
"%q %w",
|
||||
reputation.MaskZoneKey(item[colon+1:]),
|
||||
MaskZoneKey(item[colon+1:]),
|
||||
errNotTagPercent,
|
||||
)
|
||||
}
|
||||
@@ -2224,11 +2159,7 @@ func parseTagPercents(value string) (map[string]int64, error) {
|
||||
if _, listed := percents[tag]; listed {
|
||||
// tag, since item[:colon] may end in a space, which MaskZoneKey
|
||||
// would not mask.
|
||||
return nil, fmt.Errorf(
|
||||
"%q %w",
|
||||
reputation.MaskZoneKey(tag),
|
||||
errListedTwice,
|
||||
)
|
||||
return nil, fmt.Errorf("%q %w", MaskZoneKey(tag), errListedTwice)
|
||||
}
|
||||
|
||||
percents[tag] = percent
|
||||
@@ -2262,7 +2193,7 @@ func parsePercents(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
percent, err := reputation.ParsePercent(percentText)
|
||||
percent, err := ParsePercent(percentText)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -2277,6 +2208,38 @@ func parsePercents(
|
||||
return percents, nil
|
||||
}
|
||||
|
||||
// ParsePercent reads a percentage, a whole number from 0 to 100. The file
|
||||
// SWW_WAF_ASN_LIMIT_PERCENT_URL names is read with it too.
|
||||
func ParsePercent(value string) (int64, error) {
|
||||
percent, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil || percent < 0 || percent > 100 {
|
||||
return 0, fmt.Errorf("%q %w", value, errNotPercent)
|
||||
}
|
||||
|
||||
return percent, nil
|
||||
}
|
||||
|
||||
// headerNameChars are the characters RFC 9110 allows in a header name:
|
||||
// letters, digits and these marks.
|
||||
const headerNameChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +
|
||||
"0123456789!#$%&'*+-.^_`|~"
|
||||
|
||||
// IsHeaderName reports whether name can be a header name: one or more of
|
||||
// the characters RFC 9110 allows in one.
|
||||
func IsHeaderName(name string) bool {
|
||||
if name == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, char := range name {
|
||||
if !strings.ContainsRune(headerNameChars, char) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// parseHeaderNames reads a comma-separated list of header names in either
|
||||
// case, and returns them in lower case. Host and Transfer-Encoding are
|
||||
// refused: Go's HTTP server takes them out of the request's headers.
|
||||
@@ -2289,7 +2252,7 @@ func parseHeaderNames(value string) ([]string, error) {
|
||||
headers := make([]string, 0, len(items))
|
||||
|
||||
for _, item := range items {
|
||||
if !httpguts.ValidHeaderFieldName(item) {
|
||||
if !IsHeaderName(item) {
|
||||
return nil, fmt.Errorf("%q %w", item, errNotHeaderName)
|
||||
}
|
||||
|
||||
@@ -2445,8 +2408,8 @@ const (
|
||||
// other name for one that does not exist, so that the zone would list no
|
||||
// client. A zone listed twice is an error, whatever the case of its
|
||||
// letters, which DNS names ignore, and whatever its key, since
|
||||
// reputation.MaskZoneKey shows two keys of one zone alike. An error shows a
|
||||
// zone as it does.
|
||||
// MaskZoneKey shows two keys of one zone alike. An error shows a zone as
|
||||
// MaskZoneKey does.
|
||||
func parseZones(value string) ([]string, error) {
|
||||
zones, err := parseList(value)
|
||||
if err != nil {
|
||||
@@ -2456,14 +2419,11 @@ func parseZones(value string) ([]string, error) {
|
||||
}
|
||||
|
||||
for i, zone := range zones {
|
||||
shown := reputation.MaskZoneKey(zone)
|
||||
shown := MaskZoneKey(zone)
|
||||
listedBefore := slices.ContainsFunc(
|
||||
zones[:i],
|
||||
func(earlier string) bool {
|
||||
return strings.EqualFold(
|
||||
reputation.MaskZoneKey(earlier),
|
||||
shown,
|
||||
)
|
||||
return strings.EqualFold(MaskZoneKey(earlier), shown)
|
||||
},
|
||||
)
|
||||
|
||||
@@ -2480,6 +2440,24 @@ func parseZones(value string) ([]string, error) {
|
||||
return zones, nil
|
||||
}
|
||||
|
||||
// MaskZoneKey returns zone with ******** in place of its key, if it is a
|
||||
// zone of Spamhaus's keyed query service, a name under dq.spamhaus.net,
|
||||
// such as <key>.xbl.dq.spamhaus.net, whose first label is the key. Any
|
||||
// other zone it returns as it is. A zone is shown so wherever it leaves
|
||||
// the process: in the log, the alerts and the metrics.
|
||||
func MaskZoneKey(zone string) string {
|
||||
// DNS names ignore case, and a name may be written with a dot at its
|
||||
// end.
|
||||
name := strings.TrimSuffix(strings.ToLower(zone), ".")
|
||||
if !strings.HasSuffix(name, ".dq.spamhaus.net") {
|
||||
return zone
|
||||
}
|
||||
|
||||
_, rest, _ := strings.Cut(zone, ".")
|
||||
|
||||
return masked + "." + rest
|
||||
}
|
||||
|
||||
// isZone reports whether each label of zone is as parseZones takes it. It
|
||||
// also checks the hostnames of SWW_WAF_APPS, which are DNS names too.
|
||||
func isZone(zone string) bool {
|
||||
@@ -2535,7 +2513,7 @@ func parseWebhookHeaders(value string) (http.Header, string, error) {
|
||||
name, headerValue, found := strings.Cut(item, ":")
|
||||
name = strings.TrimSpace(name)
|
||||
|
||||
if !found || !httpguts.ValidHeaderFieldName(name) ||
|
||||
if !found || !IsHeaderName(name) ||
|
||||
strings.ContainsAny(headerValue, "\r\n\x00") {
|
||||
return nil, "", fmt.Errorf(
|
||||
"item %d %w",
|
||||
@@ -2553,20 +2531,16 @@ func parseWebhookHeaders(value string) (http.Header, string, error) {
|
||||
|
||||
// parseAlertEvents reads a comma-separated list of the events alerts can
|
||||
// be sent for.
|
||||
func parseAlertEvents(value string) ([]alerts.Event, error) {
|
||||
names, err := parseList(value)
|
||||
func parseAlertEvents(value string) ([]string, error) {
|
||||
events, err := parseList(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
events := make([]alerts.Event, 0, len(names))
|
||||
for _, name := range names {
|
||||
event := alerts.Event(name)
|
||||
for _, event := range events {
|
||||
if !slices.Contains(alerts.Events(), event) {
|
||||
return nil, fmt.Errorf("%q %w", name, errNotAlertEvent)
|
||||
return nil, fmt.Errorf("%q %w", event, errNotAlertEvent)
|
||||
}
|
||||
|
||||
events = append(events, event)
|
||||
}
|
||||
|
||||
return events, nil
|
||||
|
||||
+27
-51
@@ -18,13 +18,10 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"golang.org/x/net/http/httpguts"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||
"sneak.berlin/go/smallwebwaf/internal/anomaly"
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
"sneak.berlin/go/smallwebwaf/internal/config/configtest"
|
||||
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
||||
)
|
||||
|
||||
// The settings, by name.
|
||||
@@ -189,16 +186,6 @@ const (
|
||||
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.
|
||||
const defaultIPReputationCacheTTL = "24h"
|
||||
|
||||
@@ -627,7 +614,7 @@ func TestCoreRuleSetSettings(t *testing.T) {
|
||||
},
|
||||
{
|
||||
environment{
|
||||
coreRuleSetMode: string(config.CoreRuleSetModeDetect),
|
||||
coreRuleSetMode: config.CoreRuleSetModeDetect,
|
||||
coreRuleSetParanoiaLevel: "4",
|
||||
coreRuleSetScoreThreshold: "10",
|
||||
coreRuleSetDisabledRules: "942100, 920350",
|
||||
@@ -877,32 +864,6 @@ func TestInstanceNameAndLoggedHeadersAsSet(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// config and rules check header names with httpguts.ValidHeaderFieldName.
|
||||
// It takes a name of one or more of the characters RFC 9110 allows in one,
|
||||
// as the check it replaced did: letters, digits and these marks.
|
||||
func TestHeaderNameCheckTakesTheRFC9110Characters(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const marks = "!#$%&'*+-.^_`|~"
|
||||
|
||||
if httpguts.ValidHeaderFieldName("") {
|
||||
t.Error("the empty name is taken")
|
||||
}
|
||||
|
||||
for b := range 256 {
|
||||
char := byte(b)
|
||||
want := 'a' <= char && char <= 'z' || 'A' <= char && char <= 'Z' ||
|
||||
'0' <= char && char <= '9' || strings.IndexByte(marks, char) >= 0
|
||||
|
||||
// string(char) would be the UTF-8 of U+0080 to U+00FF, two bytes,
|
||||
// for a byte above 0x7f.
|
||||
name := string([]byte{char})
|
||||
if got := httpguts.ValidHeaderFieldName(name); got != want {
|
||||
t.Errorf("the name %q is taken: %t, want %t", name, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoteLogSettingsDefaults(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -1121,7 +1082,7 @@ func TestAlertSettingsDefaults(t *testing.T) {
|
||||
if cfg.AlertWebhookURL != nil || len(cfg.AlertWebhookHeaders) != 0 ||
|
||||
cfg.AlertSlackWebhookURL != nil || cfg.AlertNtfyURL != nil ||
|
||||
cfg.AlertNtfyToken != "" ||
|
||||
joinEvents(cfg.AlertEvents) != defaultAlertEvents ||
|
||||
strings.Join(cfg.AlertEvents, ",") != defaultAlertEvents ||
|
||||
cfg.AlertCooldown != 15*time.Minute || cfg.AlertMaxPerHour != 60 {
|
||||
t.Errorf(
|
||||
"alert settings %v, %v, %v, %v, %q, %v, %s and %d, want no URLs, "+
|
||||
@@ -1168,10 +1129,7 @@ func TestAlertSettingsAsSet(t *testing.T) {
|
||||
!reflect.DeepEqual(cfg.AlertWebhookHeaders, headers) ||
|
||||
cfg.AlertSlackWebhookURL.String() != slack || cfg.AlertNtfyURL.String() != ntfy ||
|
||||
cfg.AlertNtfyToken != token ||
|
||||
!slices.Equal(
|
||||
cfg.AlertEvents,
|
||||
[]alerts.Event{"ban", "file_error"},
|
||||
) ||
|
||||
!slices.Equal(cfg.AlertEvents, []string{"ban", "file_error"}) ||
|
||||
cfg.AlertCooldown != time.Hour || cfg.AlertMaxPerHour != 10 {
|
||||
t.Errorf(
|
||||
"alert settings %v, %v, %v, %v, %q, %v, %s and %d",
|
||||
@@ -1736,7 +1694,7 @@ func TestTagSettingsAsSet(t *testing.T) {
|
||||
tagLimitPercent: "vpn=mullvad :50, asn=014061:25, COUNTRY=cn:1, dnsbl=zen.example:99",
|
||||
})
|
||||
|
||||
wantLists := []reputation.TagList{
|
||||
wantLists := []config.TagList{
|
||||
{Name: torExitTag, URL: torURL},
|
||||
{Name: "vpn=mullvad", URL: "https://example.com/mullvad.txt"},
|
||||
}
|
||||
@@ -2075,9 +2033,8 @@ func TestReputationSettingsAsSet(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
value string
|
||||
action config.ReputationAction
|
||||
percent int64
|
||||
value, action string
|
||||
percent int64
|
||||
}{
|
||||
{actionDeny, actionDeny, 0},
|
||||
{actionLog, actionLog, 0},
|
||||
@@ -2210,7 +2167,7 @@ func TestDNSBLSettingsAsSet(t *testing.T) {
|
||||
// want are the zones, resolver, action and percent config.WAF gives.
|
||||
want []string
|
||||
wantResolver string
|
||||
wantAction config.ReputationAction
|
||||
wantAction string
|
||||
wantPercent int64
|
||||
}{
|
||||
{
|
||||
@@ -2330,6 +2287,25 @@ func TestInvalidDNSBLSettingStopsTheStartSayingWhatIsWrong(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaskZoneKeyMasksTheFirstLabelOfAZoneUnderDqSpamhausNet(
|
||||
t *testing.T,
|
||||
) {
|
||||
t.Parallel()
|
||||
|
||||
for zone, want := range map[string]string{
|
||||
spamhaus: spamhausMasked,
|
||||
spamhaus + ".": spamhausMasked + ".",
|
||||
"KEY.ZEN.DQ.SPAMHAUS.NET": "********.ZEN.DQ.SPAMHAUS.NET",
|
||||
dronebl: dronebl,
|
||||
"dq.spamhaus.net": "dq.spamhaus.net",
|
||||
spamhaus + ".example": spamhaus + ".example",
|
||||
} {
|
||||
if got := config.MaskZoneKey(zone); got != want {
|
||||
t.Errorf("MaskZoneKey(%q) is %q, want %q", zone, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNSBLZoneKeyIsLoggedMaskedAndNeverShown(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -3073,7 +3049,7 @@ func TestLogsEachSettingWithItsValue(t *testing.T) {
|
||||
logLevel: "info",
|
||||
ruleFilesDir: "/etc/sww/rules.d",
|
||||
ruleFilesEnabled: "true",
|
||||
coreRuleSetMode: string(config.CoreRuleSetModeBlock),
|
||||
coreRuleSetMode: config.CoreRuleSetModeBlock,
|
||||
coreRuleSetParanoiaLevel: "1",
|
||||
coreRuleSetScoreThreshold: "5",
|
||||
coreRuleSetDisabledRules: defaultCoreRuleSetDisabledRules,
|
||||
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -37,9 +37,10 @@
|
||||
SecRule &TX:app_nextcloud "@eq 0" \
|
||||
"id:900090,phase:1,pass,nolog,skipAfter:END_NEXTCLOUD"
|
||||
|
||||
# The sync client and the Files app use these WebDAV methods besides those
|
||||
# the first change allows, on /remote.php/dav/ and /public.php/dav/ and on the
|
||||
# older /remote.php/webdav/ and /public.php/webdav/. 911100 refused each:
|
||||
# The sync client and the Files app use these WebDAV methods besides PUT,
|
||||
# PATCH and DELETE, which sww allows for every app, on /remote.php/dav/ and
|
||||
# /public.php/dav/ and on the older /remote.php/webdav/ and /public.php/webdav/.
|
||||
# 911100 refused each:
|
||||
# PROPFIND, PROPPATCH: listing a folder, marking a file a favourite
|
||||
# MKCOL: making a folder, and starting a chunked upload
|
||||
# COPY, MOVE: copying, moving and renaming, and ending a chunked upload
|
||||
File renamed without changes.
File renamed without changes.
@@ -12,11 +12,10 @@
|
||||
package coreruleset
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"maps"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
@@ -35,7 +34,7 @@ import (
|
||||
// directives are the Core Rule Set as sww runs it, with the
|
||||
// paranoia level for %d, bodyDirectives for the first %s while
|
||||
// SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE is set, the exclusions file for
|
||||
// the second, and the directives of every app in apps for the third.
|
||||
// the second, and the fixes of every app in appFixes for the third.
|
||||
// Each rule sww adds has an id from 900000 to 900999, the ids the
|
||||
// Core Rule Set keeps for the rules that set it up, which
|
||||
// SWW_WAF_CORE_RULE_SET_DISABLED_RULES and the exclusions file refuse, so that no
|
||||
@@ -54,13 +53,12 @@ Include @crs-setup.conf.example
|
||||
SecAction "id:900000,phase:1,pass,nolog,\
|
||||
setvar:tx.blocking_paranoia_level=%d"
|
||||
|
||||
# The first change: PUT, PATCH and DELETE are allowed besides GET, HEAD,
|
||||
# POST and OPTIONS.
|
||||
# PUT, PATCH and DELETE are allowed besides GET, HEAD, POST and OPTIONS.
|
||||
SecAction "id:900200,phase:1,pass,nolog,\
|
||||
setvar:'tx.allowed_methods=GET HEAD POST OPTIONS PUT PATCH DELETE'"
|
||||
|
||||
# The second: Expect and Content-Encoding are taken off the Core Rule Set's
|
||||
# list of the headers it refuses. Content-Encoding goes back on it for a
|
||||
# Expect and Content-Encoding are taken off the Core Rule Set's list of the
|
||||
# headers it refuses. Content-Encoding goes back on it for a
|
||||
# body the Core Rule Set reads (900260 in bodyDirectives).
|
||||
SecAction "id:900250,phase:1,pass,nolog,\
|
||||
setvar:'tx.restricted_headers_basic=/proxy/ /lock-token/ /content-range/ \
|
||||
@@ -86,18 +84,17 @@ SecRule ARGUMENTS_LIMIT_REACHED "@eq 1" "id:900300,phase:2,pass,\
|
||||
# The apps' fixes come before the rules for the same reason.
|
||||
%s
|
||||
|
||||
# The fifth: only the rules for requests are loaded, and no response is
|
||||
# inspected.
|
||||
# Only the rules for requests are loaded, and no response is inspected.
|
||||
Include @owasp_crs/REQUEST-*.conf
|
||||
|
||||
# The third: redirect_uri is not checked for a URL naming an IP address or
|
||||
# redirect_uri is not checked for a URL naming an IP address or
|
||||
# localhost. Coraza matches a parameter name here without regard to case.
|
||||
# ARGS holds the fields of a form data or multipart body Coraza reads as
|
||||
# well as the query parameters, so a field of this name is left out too.
|
||||
SecRuleUpdateTargetById 931100 "!ARGS:redirect_uri"
|
||||
SecRuleUpdateTargetById 934110 "!ARGS:redirect_uri"
|
||||
|
||||
# The fourth: Referer is not checked for a Unix command without arguments
|
||||
# Referer is not checked for a Unix command without arguments
|
||||
# or for Java starting a process.
|
||||
SecRuleUpdateTargetById 932340 "!REQUEST_HEADERS:Referer"
|
||||
SecRuleUpdateTargetById 944110 "!REQUEST_HEADERS:Referer"
|
||||
@@ -122,9 +119,9 @@ SecRule REQUEST_HEADERS:Content-Type \
|
||||
"@rx ^(?:application|text)/(?:[a-z0-9.-]+[+])?json" \
|
||||
"id:900420,phase:1,pass,nolog,t:none,t:lowercase,ctl:requestBodyProcessor=JSON"
|
||||
|
||||
# The rest of the second change: Content-Encoding is refused again on a
|
||||
# body of a kind the Core Rule Set reads, since a compressed body cannot be
|
||||
# inspected.
|
||||
# Content-Encoding goes back on the list of the headers the Core Rule Set
|
||||
# refuses (900250 in directives) for a body of a kind it reads, since a
|
||||
# compressed body cannot be inspected.
|
||||
SecRule REQBODY_PROCESSOR "@rx ^(?:URLENCODED|MULTIPART|JSON|XML)$" \
|
||||
"id:900260,phase:1,pass,nolog,\
|
||||
setvar:'tx.restricted_headers_basic=%%{tx.restricted_headers_basic} \
|
||||
@@ -145,15 +142,19 @@ SecRule MULTIPART_STRICT_ERROR "!@eq 0" "id:900450,phase:2,pass,\
|
||||
severity:'CRITICAL',setvar:'tx.inbound_anomaly_score_pl1=+5'"
|
||||
`
|
||||
|
||||
// app is an app whose ordinary requests the Core Rule Set would refuse,
|
||||
// with the fixes that let them pass.
|
||||
// appFixes holds the fixes of each app whose ordinary requests the Core
|
||||
// Rule Set would refuse, in a file named for the app by the name
|
||||
// SWW_WAF_APPS gives it, such as apps/gitea.conf. The fixes are Coraza
|
||||
// exclusions, loaded before the Core Rule Set's rules. Their first rule
|
||||
// skips to the marker they end with, unless Inspect sets the transaction
|
||||
// variable named for the app, such as app_gitea. The skip reaches only
|
||||
// phase 1, so a rule of a later phase must check that variable itself.
|
||||
//
|
||||
//go:embed apps/*.conf
|
||||
var appFixes embed.FS
|
||||
|
||||
// app is what an app's file in appFixes cannot say about it.
|
||||
type app struct {
|
||||
// directives are Coraza exclusions, loaded before the Core Rule Set's
|
||||
// rules. Their first rule skips to the marker they end with, unless
|
||||
// Inspect sets the transaction variable named for the app, such as
|
||||
// app_gitea. The skip reaches only phase 1, so a rule of a later phase
|
||||
// must check that variable itself.
|
||||
directives string
|
||||
// cookies are the cookies the Core Rule Set reads a request without.
|
||||
cookies []string
|
||||
// forkOf names the app this one is a fork of, "" for none. A hostname
|
||||
@@ -161,60 +162,34 @@ type app struct {
|
||||
forkOf string
|
||||
}
|
||||
|
||||
//go:embed gitea.conf
|
||||
var giteaDirectives string
|
||||
|
||||
//go:embed wordpress.conf
|
||||
var wordpressDirectives string
|
||||
|
||||
//go:embed drupal.conf
|
||||
var drupalDirectives string
|
||||
|
||||
//go:embed mediawiki.conf
|
||||
var mediawikiDirectives string
|
||||
|
||||
//go:embed mastodon.conf
|
||||
var mastodonDirectives string
|
||||
|
||||
//go:embed vaultwarden.conf
|
||||
var vaultwardenDirectives string
|
||||
|
||||
//go:embed nextcloud.conf
|
||||
var nextcloudDirectives string
|
||||
|
||||
//go:embed forgejo.conf
|
||||
var forgejoDirectives string
|
||||
|
||||
//go:embed ghost.conf
|
||||
var ghostDirectives string
|
||||
|
||||
// apps are the apps whose fixes sww has, by the name SWW_WAF_APPS gives each.
|
||||
// flash is Forgejo's name for gitea_flash.
|
||||
// apps are the apps that need more than their file in appFixes, by the
|
||||
// name SWW_WAF_APPS gives each. flash is Forgejo's name for gitea_flash.
|
||||
//
|
||||
//nolint:gochecknoglobals // a constant cannot be a map
|
||||
var apps = map[string]app{
|
||||
"drupal": {directives: drupalDirectives},
|
||||
"forgejo": {
|
||||
directives: forgejoDirectives,
|
||||
cookies: []string{"flash"},
|
||||
forkOf: "gitea",
|
||||
cookies: []string{"flash"},
|
||||
forkOf: "gitea",
|
||||
},
|
||||
"ghost": {directives: ghostDirectives},
|
||||
"gitea": {
|
||||
directives: giteaDirectives,
|
||||
cookies: []string{"gitea_flash", "redirect_to"},
|
||||
cookies: []string{"gitea_flash", "redirect_to"},
|
||||
},
|
||||
"mastodon": {directives: mastodonDirectives},
|
||||
"mediawiki": {directives: mediawikiDirectives},
|
||||
"nextcloud": {directives: nextcloudDirectives},
|
||||
"vaultwarden": {directives: vaultwardenDirectives},
|
||||
"wordpress": {directives: wordpressDirectives},
|
||||
}
|
||||
|
||||
// AppNames returns the names of the apps whose fixes sww has, in
|
||||
// alphabetical order.
|
||||
func AppNames() []string {
|
||||
return slices.Sorted(maps.Keys(apps))
|
||||
// An embedded directory always reads.
|
||||
files, _ := appFixes.ReadDir("apps")
|
||||
|
||||
names := make([]string, len(files))
|
||||
for i, file := range files {
|
||||
names[i] = strings.TrimSuffix(file.Name(), ".conf")
|
||||
}
|
||||
|
||||
slices.Sort(names)
|
||||
|
||||
return names
|
||||
}
|
||||
|
||||
// Params are what New needs.
|
||||
@@ -373,8 +348,11 @@ func New(params Params) (*CoreRuleSet, error) {
|
||||
}
|
||||
|
||||
var fixes strings.Builder
|
||||
|
||||
for _, name := range AppNames() {
|
||||
fixes.WriteString(apps[name].directives + "\n")
|
||||
// An embedded file always reads.
|
||||
file, _ := appFixes.ReadFile("apps/" + name + ".conf")
|
||||
fixes.WriteString(string(file) + "\n")
|
||||
}
|
||||
|
||||
text := fmt.Sprintf(directives, params.ParanoiaLevel, body, exclusions,
|
||||
|
||||
@@ -17,7 +17,7 @@ const (
|
||||
)
|
||||
|
||||
// giteaSearches are searches of the pages gitea and Forgejo share whose q
|
||||
// skips the lists of system files and command names, in the eighth change.
|
||||
// skips the lists of system files and command names, in gitea's fixes.
|
||||
// gitea serves its admin pages under /-/admin/ and Forgejo under /admin/.
|
||||
//
|
||||
//nolint:gochecknoglobals // a constant cannot be a list
|
||||
@@ -222,8 +222,8 @@ func TestGiteaSearchExclusionsAreNarrow(t *testing.T) {
|
||||
}
|
||||
|
||||
// Under /admin/, where Forgejo serves its admin pages, only the hard
|
||||
// values are left out: the tenth change lets them through in q on every
|
||||
// path there, for Drupal.
|
||||
// values are left out: Drupal's fixes let them through in q on every path
|
||||
// there.
|
||||
for _, target := range []string{
|
||||
"/admin/packages/delete?q=",
|
||||
"/admin/packages?other=",
|
||||
|
||||
+14
-47
@@ -99,13 +99,8 @@ type GeoJS struct {
|
||||
// waiting are the clients without an answer: those to ask GeoJS about,
|
||||
// and those it is being asked about.
|
||||
waiting map[netip.Prefix]*wait
|
||||
// asking is true while a request to GeoJS is under way, and endAsking
|
||||
// ends it. stopped is true once StopAsking has been called, and
|
||||
// askGoroutines waits for the goroutine that makes the requests.
|
||||
asking bool
|
||||
endAsking context.CancelFunc
|
||||
stopped bool
|
||||
askGoroutines sync.WaitGroup
|
||||
// asking is true while a request to GeoJS is under way.
|
||||
asking bool
|
||||
// retryDelay is how long GeoJS is left alone after its last failure,
|
||||
// zero after an answer; retryAt is when it may be asked again.
|
||||
retryDelay time.Duration
|
||||
@@ -215,23 +210,6 @@ func (g *GeoJS) LookUpWithoutWaiting(
|
||||
return answer
|
||||
}
|
||||
|
||||
// StopAsking ends the request to GeoJS under way, and returns once the
|
||||
// goroutine that made it has. A request it ends is no failure: Failed is not
|
||||
// called, and GeoJS is not left alone after it. After StopAsking, GeoJS is
|
||||
// asked nothing.
|
||||
func (g *GeoJS) StopAsking() {
|
||||
g.mu.Lock()
|
||||
|
||||
g.stopped = true
|
||||
if g.asking {
|
||||
g.endAsking()
|
||||
}
|
||||
|
||||
g.mu.Unlock()
|
||||
|
||||
g.askGoroutines.Wait()
|
||||
}
|
||||
|
||||
// Kept returns client's answer, if one is kept, without asking GeoJS.
|
||||
func (g *GeoJS) Kept(client netip.Prefix) (Answer, bool) {
|
||||
g.mu.Lock()
|
||||
@@ -316,8 +294,7 @@ func (g *GeoJS) answerOrWait(
|
||||
}
|
||||
|
||||
if !g.asking {
|
||||
// GeoJS is left alone after a failure, or asked nothing after
|
||||
// StopAsking, so no answer can come.
|
||||
// GeoJS is left alone after a failure, so no answer can come.
|
||||
w.late = true
|
||||
}
|
||||
|
||||
@@ -346,28 +323,22 @@ func (g *GeoJS) kept(client netip.Prefix) (Answer, bool) {
|
||||
}
|
||||
|
||||
// ask starts asking GeoJS about the waiting clients, unless a request to
|
||||
// it is under way, it is left alone after a failure, or StopAsking has
|
||||
// been called. The requests to GeoJS are for every client waiting, so they
|
||||
// go on when the caller whose ctx is given stops waiting, until StopAsking.
|
||||
// it is under way or it is left alone after a failure. The requests to
|
||||
// GeoJS are for every client waiting, so they go on when the caller whose
|
||||
// ctx is given stops waiting.
|
||||
func (g *GeoJS) ask(ctx context.Context) {
|
||||
if g.asking || g.stopped || g.now().Before(g.retryAt) {
|
||||
if g.asking || g.now().Before(g.retryAt) {
|
||||
return
|
||||
}
|
||||
|
||||
askCtx, end := context.WithCancel(context.WithoutCancel(ctx))
|
||||
g.asking, g.endAsking = true, end
|
||||
g.asking = true
|
||||
|
||||
g.askGoroutines.Go(func() {
|
||||
defer end()
|
||||
|
||||
g.askAboutWaiting(askCtx)
|
||||
})
|
||||
go g.askAboutWaiting(context.WithoutCancel(ctx))
|
||||
}
|
||||
|
||||
// askAboutWaiting asks GeoJS about the waiting clients, one request at a
|
||||
// time, until none is left, GeoJS fails, or StopAsking ends ctx. Each
|
||||
// answer kept is given to Answered, outside the lock, since Answered takes
|
||||
// locks of its own.
|
||||
// time, until none is left or GeoJS fails. Each answer kept is given to
|
||||
// Answered, outside the lock, since Answered takes locks of its own.
|
||||
func (g *GeoJS) askAboutWaiting(ctx context.Context) {
|
||||
for {
|
||||
clients := g.nextClients()
|
||||
@@ -376,10 +347,6 @@ func (g *GeoJS) askAboutWaiting(ctx context.Context) {
|
||||
}
|
||||
|
||||
given, err := g.request(ctx, clients)
|
||||
if err != nil && ctx.Err() != nil {
|
||||
continue // ended by StopAsking, which is no failure
|
||||
}
|
||||
|
||||
kept, answered := g.keep(clients, given, err)
|
||||
|
||||
if g.answered != nil {
|
||||
@@ -395,13 +362,13 @@ func (g *GeoJS) askAboutWaiting(ctx context.Context) {
|
||||
}
|
||||
|
||||
// nextClients returns up to maxPerRequest of the waiting clients. When
|
||||
// none is waiting, or StopAsking has been called, it returns none and
|
||||
// notes that no request to GeoJS is under way.
|
||||
// none is waiting, it returns none and notes that no request to GeoJS is
|
||||
// under way.
|
||||
func (g *GeoJS) nextClients() []netip.Prefix {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
|
||||
if len(g.waiting) == 0 || g.stopped {
|
||||
if len(g.waiting) == 0 {
|
||||
g.asking = false
|
||||
|
||||
return nil
|
||||
|
||||
@@ -318,44 +318,6 @@ func TestCallerWhoseContextEndsEarlyDoesNotStopTheRequest(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestStopAskingEndsTheRequestUnderWayAsNoFailureAndAsksNoMore(
|
||||
t *testing.T,
|
||||
) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
service, _, g, reports := startWithReports()
|
||||
clients := newClients()
|
||||
|
||||
service.set(hanging)
|
||||
g.LookUpWithoutWaiting(t.Context(), clients())
|
||||
waitForRequests(t, service, 1)
|
||||
|
||||
// The request is ended, not waited out, and is no failure.
|
||||
began := time.Now()
|
||||
|
||||
g.StopAsking()
|
||||
|
||||
if waited := time.Since(began); waited != 0 {
|
||||
t.Errorf("StopAsking returned after %s, want at once", waited)
|
||||
}
|
||||
|
||||
reports.wantFailures(t)
|
||||
|
||||
// A new client is then not asked about, and its lookup does not
|
||||
// wait.
|
||||
service.set(answering)
|
||||
wantCountry(t, g, clients(), "")
|
||||
|
||||
if waited := time.Since(began); waited != 0 {
|
||||
t.Errorf("the lookup returned after %s, want at once", waited)
|
||||
}
|
||||
|
||||
waitForRequests(t, service, 1)
|
||||
reports.wantFailures(t)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRequestOver10SecondsIsAFailure(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
+12
-14
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||
"sneak.berlin/go/smallwebwaf/internal/remotelog"
|
||||
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
||||
@@ -183,14 +184,14 @@ func New(busiestCount int, instanceName string) *Metrics {
|
||||
func (m *Metrics) AddBansAndClients(
|
||||
ledger *bans.Ledger, limiter *ratelimit.Limiter, now func() time.Time,
|
||||
) {
|
||||
for _, cause := range []bans.Cause{
|
||||
for _, cause := range []string{
|
||||
bans.CauseLimit, bans.CauseAttack, bans.CauseAdmin, bans.CauseAbuseIPDB,
|
||||
} {
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
Name: "sww_bans_made_total",
|
||||
Help: "Bans made, by cause.",
|
||||
ConstLabels: prometheus.Labels{"cause": string(cause)},
|
||||
ConstLabels: prometheus.Labels{"cause": cause},
|
||||
}, func() float64 {
|
||||
return float64(ledger.Made(cause))
|
||||
}),
|
||||
@@ -300,7 +301,7 @@ const sourceLabel = "source"
|
||||
|
||||
// AddReputation adds the metrics of the lists fetched from URLs and of the
|
||||
// DNSBL zones, by source, each list's URL or each zone, its key masked as
|
||||
// reputation.MaskZoneKey masks it: the requests whose client a blocklist, a
|
||||
// config.MaskZoneKey masks it: the requests whose client a blocklist, a
|
||||
// zone's verdict or AbuseIPDB's score lists, which ReputationHit counts,
|
||||
// and, read from lists and dnsbl as the metrics are asked for, for a list,
|
||||
// the fetches that failed and when the copy in use was fetched, and for a
|
||||
@@ -319,9 +320,7 @@ func (m *Metrics) AddReputation(
|
||||
m.registry.MustRegister(m.reputationHits)
|
||||
|
||||
for _, zone := range dnsbl.Zones() {
|
||||
source := prometheus.Labels{
|
||||
sourceLabel: reputation.MaskZoneKey(zone),
|
||||
}
|
||||
source := prometheus.Labels{sourceLabel: config.MaskZoneKey(zone)}
|
||||
|
||||
m.addReputationQueries(
|
||||
source,
|
||||
@@ -391,7 +390,7 @@ func (m *Metrics) ReputationHit(source string) {
|
||||
// dropped. With no destination set, it adds none.
|
||||
func (m *Metrics) AddAlerts(queue *alerts.Queue) {
|
||||
for _, name := range queue.DestinationsSet() {
|
||||
destination := prometheus.Labels{"destination": string(name)}
|
||||
destination := prometheus.Labels{"destination": name}
|
||||
|
||||
m.registry.MustRegister(
|
||||
prometheus.NewCounterFunc(prometheus.CounterOpts{
|
||||
@@ -466,7 +465,7 @@ func (m *Metrics) RequestReported(
|
||||
// LimitHit counts a request that broke a rate limit, a byte limit or the
|
||||
// refusal limit, by the window and the kind of hit.
|
||||
func (m *Metrics) LimitHit(hit ratelimit.Hit) {
|
||||
m.rateLimitHits.WithLabelValues(hit.Window, string(hit.Kind)).Inc()
|
||||
m.rateLimitHits.WithLabelValues(hit.Window, hit.Kind).Inc()
|
||||
}
|
||||
|
||||
// Offences counts the offences of r, a request that has ended, as its
|
||||
@@ -487,8 +486,8 @@ func (m *Metrics) Offences(r ratelimit.Request) {
|
||||
|
||||
// RuleMatched counts a request that matched the rule id, whose action is
|
||||
// action.
|
||||
func (m *Metrics) RuleMatched(id string, action rules.Action) {
|
||||
m.ruleMatches.WithLabelValues(id, string(action)).Inc()
|
||||
func (m *Metrics) RuleMatched(id, action string) {
|
||||
m.ruleMatches.WithLabelValues(id, action).Inc()
|
||||
}
|
||||
|
||||
// CoreRuleSetMatched counts a request that matched the Core Rule Set's rule id,
|
||||
@@ -534,11 +533,10 @@ func (m *Metrics) count(
|
||||
line *requestlog.Line, duration, upstreamDuration time.Duration,
|
||||
) {
|
||||
class := statusClass(line.Status)
|
||||
action := string(line.Action)
|
||||
m.requests.WithLabelValues(class, action).Inc()
|
||||
m.requestBytes.WithLabelValues(class, action).
|
||||
m.requests.WithLabelValues(class, line.Action).Inc()
|
||||
m.requestBytes.WithLabelValues(class, line.Action).
|
||||
Add(float64(line.RequestBytes))
|
||||
m.responseBytes.WithLabelValues(class, action).
|
||||
m.responseBytes.WithLabelValues(class, line.Action).
|
||||
Add(float64(line.ResponseBytes))
|
||||
m.requestDuration.Observe(duration.Seconds())
|
||||
|
||||
|
||||
@@ -19,19 +19,15 @@ import (
|
||||
|
||||
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.
|
||||
const (
|
||||
// KindRequests is a rate limit, on a client's requests.
|
||||
KindRequests LimitKind = "requests"
|
||||
KindRequests = "requests"
|
||||
// KindBytes is a byte limit, on a client's bytes.
|
||||
KindBytes LimitKind = "bytes"
|
||||
KindBytes = "bytes"
|
||||
// KindRefusals is the refusal limit, on a client's requests sww
|
||||
// refused after a rule file match or for a missing or wrong token.
|
||||
KindRefusals LimitKind = "refusals"
|
||||
KindRefusals = "refusals"
|
||||
)
|
||||
|
||||
// Limits are the most requests a client may make in a minute, an hour and
|
||||
@@ -192,7 +188,9 @@ func New(limits Limits, maxClients int) *Limiter {
|
||||
// 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.
|
||||
type Hit struct {
|
||||
Kind LimitKind
|
||||
// Kind is KindRequests for a rate limit, KindBytes for a byte limit,
|
||||
// KindRefusals for the refusal limit.
|
||||
Kind string
|
||||
// Window is "minute", "hour" or "day".
|
||||
Window string
|
||||
// Limit is the window's limit, as the client's percentage of it.
|
||||
|
||||
@@ -628,7 +628,7 @@ func wantBytesCount(
|
||||
client netip.Prefix,
|
||||
now time.Time,
|
||||
bytes int64,
|
||||
want ratelimit.LimitKind,
|
||||
want string,
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
|
||||
@@ -110,12 +110,8 @@ type AbuseIPDB struct {
|
||||
// scores are by client. Each is added as it is fetched and never moved
|
||||
// up, so that the one fetched longest ago is the first dropped.
|
||||
scores *simplelru.LRU[netip.Prefix, Score]
|
||||
// checking are the clients whose check is under way, each with the
|
||||
// function that ends it. stopped is true once StopChecking has been
|
||||
// called, and checkGoroutines waits for the goroutine of each check.
|
||||
checking map[netip.Prefix]context.CancelFunc
|
||||
stopped bool
|
||||
checkGoroutines sync.WaitGroup
|
||||
// checking are the clients whose check is under way.
|
||||
checking map[netip.Prefix]bool
|
||||
// day is the day, in UTC, of the checks spent counts.
|
||||
day civil.Date
|
||||
spent int
|
||||
@@ -138,7 +134,7 @@ func NewAbuseIPDB(params AbuseIPDBParams) *AbuseIPDB {
|
||||
params: params,
|
||||
httpClient: &http.Client{},
|
||||
scores: scores,
|
||||
checking: map[netip.Prefix]context.CancelFunc{},
|
||||
checking: map[netip.Prefix]bool{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,11 +144,11 @@ func NewAbuseIPDB(params AbuseIPDBParams) *AbuseIPDB {
|
||||
// addresses its request comes from. A client without one is checked in
|
||||
// the background, by addr, the address its request came from, if
|
||||
// offender, if it has committed an offence, unless its check is under
|
||||
// way, a check failed less than failureDelay ago, the day's checks have
|
||||
// used up DailyBudget, or StopChecking has been called; Hit never waits
|
||||
// for a check. The check that uses the budget up is logged and raised as a
|
||||
// source_failure alert. ctx is the context of the client's request, and a
|
||||
// check goes on after the request ends, until StopChecking.
|
||||
// way, a check failed less than failureDelay ago, or the day's checks
|
||||
// have used up DailyBudget; Hit never waits for a check. The check that
|
||||
// uses the budget up is logged and raised as a source_failure alert. ctx
|
||||
// is the context of the client's request, and a check goes on after the
|
||||
// request ends.
|
||||
func (a *AbuseIPDB) Hit(
|
||||
ctx context.Context, client netip.Prefix, addr netip.Addr, offender bool,
|
||||
) (int64, bool) {
|
||||
@@ -171,21 +167,14 @@ func (a *AbuseIPDB) Hit(
|
||||
a.day, a.spent = today, 0
|
||||
}
|
||||
|
||||
_, checking := a.checking[client]
|
||||
|
||||
check := offender && !checking && !a.stopped &&
|
||||
!now.Before(a.retryAt) && a.spent < a.params.DailyBudget
|
||||
check := offender && !a.checking[client] && !now.Before(a.retryAt) &&
|
||||
a.spent < a.params.DailyBudget
|
||||
if check {
|
||||
checkCtx, end := context.WithCancel(context.WithoutCancel(ctx))
|
||||
a.checking[client] = end
|
||||
a.checking[client] = true
|
||||
a.checks++
|
||||
a.spent++
|
||||
|
||||
a.checkGoroutines.Go(func() {
|
||||
defer end()
|
||||
|
||||
a.check(checkCtx, client, addr)
|
||||
})
|
||||
go a.check(context.WithoutCancel(ctx), client, addr)
|
||||
}
|
||||
|
||||
usedUp := check && a.spent == a.params.DailyBudget
|
||||
@@ -200,24 +189,6 @@ func (a *AbuseIPDB) Hit(
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// StopChecking ends the checks under way, and returns once their goroutines
|
||||
// have. A check it ends gives no score and is no failure: it is neither
|
||||
// counted, logged nor raised as an alert, and checks are not left alone
|
||||
// after it. The ended check stays spent from the day's budget. After
|
||||
// StopChecking, Hit checks no client.
|
||||
func (a *AbuseIPDB) StopChecking() {
|
||||
a.mu.Lock()
|
||||
|
||||
a.stopped = true
|
||||
for _, end := range a.checking {
|
||||
end()
|
||||
}
|
||||
|
||||
a.mu.Unlock()
|
||||
|
||||
a.checkGoroutines.Wait()
|
||||
}
|
||||
|
||||
// Checked returns how many checks were made.
|
||||
func (a *AbuseIPDB) Checked() int {
|
||||
a.mu.Lock()
|
||||
@@ -344,8 +315,7 @@ func (a *AbuseIPDB) RunBlacklist(ctx context.Context) {
|
||||
// check checks client with AbuseIPDB by addr, one of its addresses, keeps
|
||||
// the score as client's, and notes the check as no longer under way. A
|
||||
// check that fails gives no score: it is counted, logged and raised as a
|
||||
// source_failure alert, and no client is checked for failureDelay. A check
|
||||
// ended as ctx ends, by StopChecking, is no failure.
|
||||
// source_failure alert, and no client is checked for failureDelay.
|
||||
func (a *AbuseIPDB) check(
|
||||
ctx context.Context,
|
||||
client netip.Prefix,
|
||||
@@ -353,26 +323,24 @@ func (a *AbuseIPDB) check(
|
||||
) {
|
||||
score, err := a.ask(ctx, addr)
|
||||
now := a.params.Now()
|
||||
isFailure := err != nil && ctx.Err() == nil
|
||||
|
||||
a.mu.Lock()
|
||||
|
||||
delete(a.checking, client)
|
||||
|
||||
switch {
|
||||
case err == nil:
|
||||
if err == nil {
|
||||
a.scores.Add(
|
||||
client,
|
||||
Score{Client: client, Score: score, Fetched: now},
|
||||
)
|
||||
case isFailure:
|
||||
} else {
|
||||
a.failures++
|
||||
a.retryAt = now.Add(failureDelay)
|
||||
}
|
||||
|
||||
a.mu.Unlock()
|
||||
|
||||
if isFailure {
|
||||
if err != nil {
|
||||
a.alert("checking a client with AbuseIPDB failed", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -394,43 +394,6 @@ func TestCheckNotAnsweredWithinTheTimeoutFailsAndHitNeverWaits(
|
||||
})
|
||||
}
|
||||
|
||||
func TestStopCheckingEndsTheCheckUnderWayAsNoFailureAndChecksNoMore(
|
||||
t *testing.T,
|
||||
) {
|
||||
t.Parallel()
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
queue := newQueue()
|
||||
p := abuseIPDBParams()
|
||||
p.Alerts = queue
|
||||
abuseIPDB := &abuseIPDBStandIn{hanging: true}
|
||||
checker := newAbuseIPDB(abuseIPDB, p)
|
||||
|
||||
wantScore(t, checker, suspect, true, 0, false)
|
||||
synctest.Wait()
|
||||
|
||||
// The check is ended, not waited out, and is no failure.
|
||||
began := time.Now()
|
||||
|
||||
checker.StopChecking()
|
||||
|
||||
if waited := time.Since(began); waited != 0 {
|
||||
t.Errorf("StopChecking returned after %s, want at once", waited)
|
||||
}
|
||||
|
||||
wantFailures(t, checker, 0)
|
||||
|
||||
if got := waiting(queue); len(got) != 0 {
|
||||
t.Errorf("alerts waiting %+v, want none", got)
|
||||
}
|
||||
|
||||
// An offender without a score is then not checked.
|
||||
wantScore(t, checker, other, true, 0, false)
|
||||
synctest.Wait()
|
||||
wantChecked(t, abuseIPDB, suspect)
|
||||
})
|
||||
}
|
||||
|
||||
func TestKeyIsSentInTheKeyHeaderAndNeverShown(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
|
||||
"github.com/hashicorp/golang-lru/v2/simplelru"
|
||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -31,8 +32,6 @@ const (
|
||||
// it fails, and no client is checked with AbuseIPDB after a check
|
||||
// fails, so that a source refusing them is not asked on every request.
|
||||
failureDelay = time.Minute
|
||||
// maskedKey is what MaskZoneKey shows in place of a zone's key.
|
||||
maskedKey = "********"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -83,12 +82,8 @@ type DNSBL struct {
|
||||
// verdicts are by query. Each is added as it is fetched and never moved
|
||||
// up, so that the one fetched longest ago is the first dropped.
|
||||
verdicts *simplelru.LRU[query, Verdict]
|
||||
// asking are the queries under way, each with the function that ends
|
||||
// it. stopped is true once StopAsking has been called, and
|
||||
// queryGoroutines waits for the goroutine of each query.
|
||||
asking map[query]context.CancelFunc
|
||||
stopped bool
|
||||
queryGoroutines sync.WaitGroup
|
||||
// asking are the queries under way.
|
||||
asking map[query]bool
|
||||
// queries and failures count, by zone, the queries made and those that
|
||||
// failed, and retryAt is when a zone whose last query failed may be
|
||||
// asked again.
|
||||
@@ -125,31 +120,13 @@ func NewDNSBL(params DNSBLParams) *DNSBL {
|
||||
params: params,
|
||||
resolver: resolver,
|
||||
verdicts: verdicts,
|
||||
asking: map[query]context.CancelFunc{},
|
||||
asking: map[query]bool{},
|
||||
queries: map[string]int{},
|
||||
failures: map[string]int{},
|
||||
retryAt: map[string]time.Time{},
|
||||
}
|
||||
}
|
||||
|
||||
// MaskZoneKey returns zone with ******** in place of its key, if it is a
|
||||
// zone of Spamhaus's keyed query service, a name under dq.spamhaus.net,
|
||||
// such as <key>.xbl.dq.spamhaus.net, whose first label is the key. Any
|
||||
// other zone it returns as it is. A zone is shown so wherever it leaves
|
||||
// the process: in the log, the alerts and the metrics.
|
||||
func MaskZoneKey(zone string) string {
|
||||
// DNS names ignore case, and a name may be written with a dot at its
|
||||
// end.
|
||||
name := strings.TrimSuffix(strings.ToLower(zone), ".")
|
||||
if !strings.HasSuffix(name, ".dq.spamhaus.net") {
|
||||
return zone
|
||||
}
|
||||
|
||||
_, rest, _ := strings.Cut(zone, ".")
|
||||
|
||||
return maskedKey + "." + rest
|
||||
}
|
||||
|
||||
// Zones returns the zones, in the order SWW_WAF_DNSBL_ZONES names them.
|
||||
func (d *DNSBL) Zones() []string {
|
||||
return slices.Clone(d.params.Zones)
|
||||
@@ -157,14 +134,13 @@ func (d *DNSBL) Zones() []string {
|
||||
|
||||
// ListedBy returns the zones whose verdict on addr, a client's address,
|
||||
// lists it, in the order SWW_WAF_DNSBL_ZONES names them, each with its key
|
||||
// masked, as MaskZoneKey masks it, since they go to the request
|
||||
// masked, as config.MaskZoneKey masks it, since they go to the request
|
||||
// log, the alerts and the metrics. A verdict is used until CacheTTL has
|
||||
// passed since it was fetched. Each zone without one is asked about addr
|
||||
// in the background, unless a query about addr to it is under way, the
|
||||
// zone is left alone after a failure, maxQueries are under way, or
|
||||
// StopAsking has been called; ListedBy never waits for a query. ctx is the
|
||||
// context of the client's request, and a query goes on after the request
|
||||
// ends, until StopAsking.
|
||||
// zone is left alone after a failure, or maxQueries are under way;
|
||||
// ListedBy never waits for a query. ctx is the context of the client's
|
||||
// request, and a query goes on after the request ends.
|
||||
func (d *DNSBL) ListedBy(ctx context.Context, addr netip.Addr) []string {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
@@ -177,47 +153,23 @@ func (d *DNSBL) ListedBy(ctx context.Context, addr netip.Addr) []string {
|
||||
q := query{zone: zone, client: addr}
|
||||
|
||||
kept, found := d.verdicts.Peek(q)
|
||||
_, asking := d.asking[q]
|
||||
|
||||
switch {
|
||||
case found && now.Sub(kept.Fetched) < d.params.CacheTTL:
|
||||
if kept.Listed {
|
||||
listedBy = append(listedBy, MaskZoneKey(zone))
|
||||
listedBy = append(listedBy, config.MaskZoneKey(zone))
|
||||
}
|
||||
case !asking && !d.stopped && !now.Before(d.retryAt[zone]) &&
|
||||
len(d.asking) < maxQueries:
|
||||
queryCtx, end := context.WithCancel(context.WithoutCancel(ctx))
|
||||
d.asking[q] = end
|
||||
case !d.asking[q] && !now.Before(d.retryAt[zone]) && len(d.asking) < maxQueries:
|
||||
d.asking[q] = true
|
||||
d.queries[zone]++
|
||||
|
||||
d.queryGoroutines.Go(func() {
|
||||
defer end()
|
||||
|
||||
d.ask(queryCtx, q)
|
||||
})
|
||||
go d.ask(context.WithoutCancel(ctx), q)
|
||||
}
|
||||
}
|
||||
|
||||
return listedBy
|
||||
}
|
||||
|
||||
// StopAsking ends the queries under way, and returns once their
|
||||
// goroutines have. A query it ends gives no verdict and is no failure: it
|
||||
// is neither counted, logged nor raised as an alert, and the zone is not
|
||||
// left alone after it. After StopAsking, ListedBy asks no zone.
|
||||
func (d *DNSBL) StopAsking() {
|
||||
d.mu.Lock()
|
||||
|
||||
d.stopped = true
|
||||
for _, end := range d.asking {
|
||||
end()
|
||||
}
|
||||
|
||||
d.mu.Unlock()
|
||||
|
||||
d.queryGoroutines.Wait()
|
||||
}
|
||||
|
||||
// Queries returns how many queries were made to zone.
|
||||
func (d *DNSBL) Queries(zone string) int {
|
||||
d.mu.Lock()
|
||||
@@ -286,32 +238,30 @@ func (d *DNSBL) Load(verdicts []Verdict) {
|
||||
// query as no longer under way. A query that fails gives no verdict: it
|
||||
// is counted, logged and raised as a source_failure alert, which show the
|
||||
// zone with its key masked, and the zone is not asked again for
|
||||
// failureDelay. A query ended as ctx ends, by StopAsking, is no failure.
|
||||
// failureDelay.
|
||||
func (d *DNSBL) ask(ctx context.Context, q query) {
|
||||
listed, err := d.lookUp(ctx, q)
|
||||
now := d.params.Now()
|
||||
isFailure := err != nil && ctx.Err() == nil
|
||||
|
||||
d.mu.Lock()
|
||||
|
||||
delete(d.asking, q)
|
||||
|
||||
switch {
|
||||
case err == nil:
|
||||
if err == nil {
|
||||
d.verdicts.Add(q, Verdict{
|
||||
Zone: q.zone, Client: q.client, Listed: listed, Fetched: now,
|
||||
})
|
||||
case isFailure:
|
||||
} else {
|
||||
d.failures[q.zone]++
|
||||
d.retryAt[q.zone] = now.Add(failureDelay)
|
||||
}
|
||||
|
||||
d.mu.Unlock()
|
||||
|
||||
if isFailure {
|
||||
if err != nil {
|
||||
const failed = "asking a DNSBL zone failed"
|
||||
|
||||
shown := MaskZoneKey(q.zone)
|
||||
shown := config.MaskZoneKey(q.zone)
|
||||
|
||||
// Raised before it is logged, so that the alert is there once the
|
||||
// log line is.
|
||||
|
||||
@@ -210,45 +210,6 @@ func TestQueryNotAnsweredWithinTheTimeoutFails(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
//
|
||||
//nolint:paralleltest // one at a time, as the comment at the top of this file says
|
||||
func TestStopAskingEndsTheQueryUnderWayAsNoFailureAndAsksNoMore(
|
||||
t *testing.T,
|
||||
) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
queue := newQueue()
|
||||
p := dnsblParams(zone)
|
||||
p.Alerts = queue
|
||||
resolver := &resolverStandIn{hanging: true}
|
||||
dnsbl := newDNSBL(resolver, p)
|
||||
|
||||
wantZones(t, dnsbl, listed)
|
||||
synctest.Wait()
|
||||
|
||||
// The query is ended, not waited out, and is no failure.
|
||||
began := time.Now()
|
||||
|
||||
dnsbl.StopAsking()
|
||||
|
||||
if waited := time.Since(began); waited != 0 {
|
||||
t.Errorf("StopAsking returned after %s, want at once", waited)
|
||||
}
|
||||
|
||||
wantQueries(t, dnsbl, 1, 0)
|
||||
|
||||
if got := waiting(queue); len(got) != 0 {
|
||||
t.Errorf("alerts waiting %+v, want none", got)
|
||||
}
|
||||
|
||||
// A client without a verdict is then asked about in no zone.
|
||||
wantZones(t, dnsbl, unlisted)
|
||||
synctest.Wait()
|
||||
wantQueries(t, dnsbl, 1, 0)
|
||||
wantAsked(t, resolver, listedName)
|
||||
waitForTheResolver()
|
||||
})
|
||||
}
|
||||
|
||||
//
|
||||
//nolint:paralleltest // one at a time, as the comment at the top of this file says
|
||||
func TestZoneThatFailsOrRefusesGivesNoVerdictAndIsLeftAloneForAMinute(
|
||||
@@ -860,28 +821,3 @@ func wantAsked(t *testing.T, resolver *resolverStandIn, want ...string) {
|
||||
t.Errorf("asked about %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaskZoneKeyMasksTheFirstLabelOfAZoneUnderDqSpamhausNet(
|
||||
t *testing.T,
|
||||
) {
|
||||
t.Parallel()
|
||||
|
||||
const (
|
||||
dronebl = "dnsbl.dronebl.org"
|
||||
spamhaus = "abcdefghijklmnopqrstuvwxyz.xbl.dq.spamhaus.net"
|
||||
spamhausMasked = "********.xbl.dq.spamhaus.net"
|
||||
)
|
||||
|
||||
for zone, want := range map[string]string{
|
||||
spamhaus: spamhausMasked,
|
||||
spamhaus + ".": spamhausMasked + ".",
|
||||
"KEY.ZEN.DQ.SPAMHAUS.NET": "********.ZEN.DQ.SPAMHAUS.NET",
|
||||
dronebl: dronebl,
|
||||
"dq.spamhaus.net": "dq.spamhaus.net",
|
||||
spamhaus + ".example": spamhaus + ".example",
|
||||
} {
|
||||
if got := reputation.MaskZoneKey(zone); got != want {
|
||||
t.Errorf("MaskZoneKey(%q) is %q, want %q", zone, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,8 @@
|
||||
// SWW_WAF_DNSBL_ZONES about clients, and keeps their verdicts, and checks
|
||||
// clients with AbuseIPDB, and keeps their scores and the checks spent
|
||||
// today. The state package writes all of these to files and reads them
|
||||
// from there, so that a restart keeps them too. It fetches AbuseIPDB's
|
||||
// blacklist, which it does not keep. Last, it reads the netblocks, AS
|
||||
// numbers and percentages that both the settings and the lists hold, and
|
||||
// masks the keys of DNSBL zones.
|
||||
// from there, so that a restart keeps them too. Last, it fetches
|
||||
// AbuseIPDB's blacklist, which it does not keep.
|
||||
package reputation
|
||||
|
||||
import (
|
||||
@@ -22,12 +20,12 @@ import (
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -42,28 +40,15 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
errStatus = errors.New("the server answered")
|
||||
errTooLong = errors.New("the list is longer than 16 MiB")
|
||||
errNotNetblockLine = errors.New(
|
||||
errStatus = errors.New("the server answered")
|
||||
errTooLong = errors.New("the list is longer than 16 MiB")
|
||||
errNotNetblock = errors.New(
|
||||
"is not an address or a netblock, such as 192.0.2.0/24",
|
||||
)
|
||||
errNotASNPercent = errors.New(
|
||||
"is not an AS number, : and a percentage, such as AS64496:50")
|
||||
errNotNetblock = errors.New(
|
||||
"is not a netblock such as 10.0.0.0/8, or an address")
|
||||
errNotASN = errors.New("is not an AS number such as AS64496")
|
||||
errNotPercent = errors.New(
|
||||
"is not a percentage, a whole number from 0 to 100",
|
||||
)
|
||||
)
|
||||
|
||||
// TagList is an address list of SWW_WAF_TAG_LISTS: the tag it adds to the
|
||||
// requests of the clients it holds, and the URL it is fetched from.
|
||||
type TagList struct {
|
||||
Name string
|
||||
URL string
|
||||
}
|
||||
|
||||
// List is a list as lists.json holds it: the URL it is fetched from,
|
||||
// when it was last tried, the fetch failed or not, and its last good copy:
|
||||
// when that was fetched, and its lines, as fetched, comment lines
|
||||
@@ -83,7 +68,7 @@ type Params struct {
|
||||
BlocklistURLs []string
|
||||
ASNLimitPercentURL string
|
||||
// TagLists are the address lists that add a tag (SWW_WAF_TAG_LISTS).
|
||||
TagLists []TagList
|
||||
TagLists []config.TagList
|
||||
// Refresh is how long after a list was last fetched or tried it is
|
||||
// fetched again (SWW_WAF_BLOCKLIST_REFRESH).
|
||||
Refresh time.Duration
|
||||
@@ -480,11 +465,7 @@ func parseNetblocks(lines []string) (entries, error) {
|
||||
|
||||
netblock, ok := parseNetblock(text)
|
||||
if !ok {
|
||||
return entries{}, fmt.Errorf(
|
||||
"line %d %w",
|
||||
i+1,
|
||||
errNotNetblockLine,
|
||||
)
|
||||
return entries{}, fmt.Errorf("line %d %w", i+1, errNotNetblock)
|
||||
}
|
||||
|
||||
found.netblocks[netblock] = true
|
||||
@@ -504,7 +485,7 @@ func parseNetblocks(lines []string) (entries, error) {
|
||||
// stands for, 192.0.2.0/24, and a mapped netblock shorter than /96, which
|
||||
// stands for none, is refused.
|
||||
func parseNetblock(text string) (netip.Prefix, bool) {
|
||||
netblock, err := ParseNetblock(text)
|
||||
netblock, err := config.ParseNetblock(text)
|
||||
if err != nil {
|
||||
return netip.Prefix{}, false
|
||||
}
|
||||
@@ -539,9 +520,9 @@ func parsePercents(lines []string) (entries, error) {
|
||||
}
|
||||
|
||||
asnText, percentText, _ := strings.Cut(text, ":")
|
||||
asn, asnErr := ParseASN(asnText)
|
||||
asn, asnErr := config.ParseASN(asnText)
|
||||
|
||||
percent, percentErr := ParsePercent(percentText)
|
||||
percent, percentErr := config.ParsePercent(percentText)
|
||||
if asnErr != nil || percentErr != nil {
|
||||
return entries{}, fmt.Errorf("line %d %w", i+1, errNotASNPercent)
|
||||
}
|
||||
@@ -555,53 +536,6 @@ func parsePercents(lines []string) (entries, error) {
|
||||
return found, nil
|
||||
}
|
||||
|
||||
// ParseNetblock reads a netblock in CIDR form, such as 10.0.0.0/8. A bare
|
||||
// address is a netblock of that address alone, a /32 or a /128. A
|
||||
// blocklist's lines are read with it too.
|
||||
func ParseNetblock(value string) (netip.Prefix, error) {
|
||||
if strings.Contains(value, "/") {
|
||||
netblock, err := netip.ParsePrefix(value)
|
||||
if err != nil {
|
||||
return netip.Prefix{}, fmt.Errorf("%q %w", value, errNotNetblock)
|
||||
}
|
||||
|
||||
return netblock.Masked(), nil
|
||||
}
|
||||
|
||||
addr, err := netip.ParseAddr(value)
|
||||
if err != nil || addr.Zone() != "" {
|
||||
return netip.Prefix{}, fmt.Errorf("%q %w", value, errNotNetblock)
|
||||
}
|
||||
|
||||
return netip.PrefixFrom(addr, addr.BitLen()), nil
|
||||
}
|
||||
|
||||
// ParseASN reads an AS number such as AS64496, in either case, and
|
||||
// returns it as the lookup gives it: AS and the number, in capitals and
|
||||
// without leading zeros. The file SWW_WAF_ASN_LIMIT_PERCENT_URL names is
|
||||
// read with it too.
|
||||
func ParseASN(value string) (string, error) {
|
||||
digits, hasAS := strings.CutPrefix(strings.ToUpper(value), "AS")
|
||||
|
||||
number, err := strconv.ParseUint(digits, 10, 32)
|
||||
if !hasAS || err != nil {
|
||||
return "", fmt.Errorf("%q %w", value, errNotASN)
|
||||
}
|
||||
|
||||
return "AS" + strconv.FormatUint(number, 10), nil
|
||||
}
|
||||
|
||||
// ParsePercent reads a percentage, a whole number from 0 to 100. The file
|
||||
// SWW_WAF_ASN_LIMIT_PERCENT_URL names is read with it too.
|
||||
func ParsePercent(value string) (int64, error) {
|
||||
percent, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil || percent < 0 || percent > 100 {
|
||||
return 0, fmt.Errorf("%q %w", value, errNotPercent)
|
||||
}
|
||||
|
||||
return percent, nil
|
||||
}
|
||||
|
||||
// withoutComment returns line without anything after a ; or a #, and
|
||||
// without the spaces around what is left.
|
||||
func withoutComment(line string) string {
|
||||
|
||||
@@ -15,91 +15,80 @@ import (
|
||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||
)
|
||||
|
||||
// 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.
|
||||
const (
|
||||
// ActionForward is a request waf allowed, which rproxy passes to
|
||||
// the app.
|
||||
ActionForward Action = "forward"
|
||||
ActionForward = "forward"
|
||||
|
||||
// ActionRequestLineTooLong is a request whose request line is too long for
|
||||
// the header limits.
|
||||
ActionRequestLineTooLong Action = "request_line_too_long"
|
||||
ActionRequestLineTooLong = "request_line_too_long"
|
||||
|
||||
// ActionRequestHeaderTooLarge is a request whose headers are over the
|
||||
// header limits.
|
||||
ActionRequestHeaderTooLarge Action = "request_header_too_large"
|
||||
ActionRequestHeaderTooLarge = "request_header_too_large"
|
||||
|
||||
// ActionClientIsRateLimited is a request refused because it took its
|
||||
// client over a rate limit, which bans the client.
|
||||
ActionClientIsRateLimited Action = "client_is_rate_limited"
|
||||
ActionClientIsRateLimited = "client_is_rate_limited"
|
||||
|
||||
// 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
|
||||
// trap path, and so made such a ban.
|
||||
ActionTemporarilyBanned Action = "temporarily_banned"
|
||||
ActionTemporarilyBanned = "temporarily_banned"
|
||||
|
||||
// ActionPermanentlyBanned is a request refused because a ban that never
|
||||
// runs out covers its client, or because the request made such a ban or
|
||||
// made its client's ban permanent.
|
||||
ActionPermanentlyBanned Action = "permanently_banned"
|
||||
ActionPermanentlyBanned = "permanently_banned"
|
||||
|
||||
// ActionBlockedByBlockRule is a request refused because it matched a
|
||||
// block rule.
|
||||
ActionBlockedByBlockRule Action = "blocked_by_block_rule"
|
||||
ActionBlockedByBlockRule = "blocked_by_block_rule"
|
||||
|
||||
// ActionBlockedByCoreRuleSet is a request refused because the Core Rule
|
||||
// Set scored it at or over SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD.
|
||||
ActionBlockedByCoreRuleSet Action = "blocked_by_core_rule_set"
|
||||
ActionBlockedByCoreRuleSet = "blocked_by_core_rule_set"
|
||||
|
||||
// ActionClientDenied is a request refused because its client is in
|
||||
// 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
|
||||
// SWW_WAF_DNSBL_ABUSEIPDB_ACTION is deny, or that carries a tag in
|
||||
// SWW_WAF_TAG_DENY.
|
||||
ActionClientDenied Action = "client_denied"
|
||||
ActionClientDenied = "client_denied"
|
||||
|
||||
// ActionCountryDenied is a request refused because its client's country
|
||||
// is not in SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES.
|
||||
ActionCountryDenied Action = "country_denied"
|
||||
ActionCountryDenied = "country_denied"
|
||||
|
||||
// ActionChecksTimedOut is a request refused because the trap paths, the
|
||||
// rule files and the Core Rule Set had not finished with it within 500
|
||||
// milliseconds.
|
||||
ActionChecksTimedOut Action = "checks_timed_out"
|
||||
ActionChecksTimedOut = "checks_timed_out"
|
||||
|
||||
// ActionOwnEndpoint is a request waf answered at one of its own
|
||||
// endpoints, or a question about a path under /_sww/.
|
||||
ActionOwnEndpoint Action = "own_endpoint"
|
||||
ActionOwnEndpoint = "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
|
||||
// broke a rate limit or the refusal limit, or whose bytes broke a byte
|
||||
// limit.
|
||||
const OffenceLimit Offence = "limit"
|
||||
const OffenceLimit = "limit"
|
||||
|
||||
// LimitHitMinuteRefusals is the limit_hit a request line names for a request
|
||||
// that broke the refusal limit.
|
||||
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
|
||||
// waf's two lines about a question it is.
|
||||
const (
|
||||
// WAFLineDecision is waf's line at its decision.
|
||||
WAFLineDecision WAFLineKind = "decision"
|
||||
WAFLineDecision = "decision"
|
||||
// WAFLineReport is waf's line when rproxy's report
|
||||
// comes, with the decision's fields and the report's together.
|
||||
WAFLineReport WAFLineKind = "report"
|
||||
WAFLineReport = "report"
|
||||
)
|
||||
|
||||
// timeLayout is RFC 3339 with milliseconds.
|
||||
@@ -112,8 +101,9 @@ const timeLayout = "2006-01-02T15:04:05.000Z07:00"
|
||||
//nolint:tagliatelle // docs/request-log.md names the fields in snake_case
|
||||
type Line struct {
|
||||
Type string `json:"type"`
|
||||
// WAFLine is set on waf's two lines about a question.
|
||||
WAFLine WAFLineKind `json:"waf_line,omitempty"`
|
||||
// WAFLine is WAFLineDecision or WAFLineReport
|
||||
// on waf's two lines about a question.
|
||||
WAFLine string `json:"waf_line,omitempty"`
|
||||
|
||||
// The standard web log fields. Scheme is how the client reached rproxy, or
|
||||
// the trusted proxy in front of it.
|
||||
@@ -159,7 +149,7 @@ type Line struct {
|
||||
Aborted bool `json:"aborted,omitempty"`
|
||||
|
||||
// The decision.
|
||||
Action Action `json:"action"`
|
||||
Action string `json:"action"`
|
||||
// WouldAction is, in observe mode, the action enforce mode would have
|
||||
// taken with a request it would have refused: ActionClientDenied,
|
||||
// ActionTemporarilyBanned, ActionPermanentlyBanned, ActionCountryDenied,
|
||||
@@ -167,7 +157,7 @@ type Line struct {
|
||||
// ActionBlockedByCoreRuleSet, or, in waf, ActionRequestLineTooLong or
|
||||
// ActionRequestHeaderTooLarge for the header limits, or
|
||||
// ActionChecksTimedOut.
|
||||
WouldAction Action `json:"would_action,omitempty"`
|
||||
WouldAction string `json:"would_action,omitempty"`
|
||||
// Heuristic is the name of the heuristic that refused the request, in
|
||||
// observe mode too, and HeuristicFigures are the figures it acted on, by
|
||||
// name.
|
||||
@@ -213,8 +203,9 @@ type Line struct {
|
||||
// RouteWatchTagsMissing, while SWW_WAF_ROUTEWATCH_URL is set, says why the
|
||||
// request got no tags from routewatch: no_answer_yet, request_failed or
|
||||
// no_route.
|
||||
RouteWatchTagsMissing string `json:"routewatch_tags_missing,omitempty"`
|
||||
Offence Offence `json:"offence,omitempty"`
|
||||
RouteWatchTagsMissing string `json:"routewatch_tags_missing,omitempty"`
|
||||
// Offence is the offence the request was held as, OffenceLimit.
|
||||
Offence string `json:"offence,omitempty"`
|
||||
// BanExpires is when the ban the request made, or was refused under,
|
||||
// ends: a time, or "permanent".
|
||||
BanExpires string `json:"ban_expires,omitempty"`
|
||||
|
||||
@@ -99,12 +99,8 @@ type Client struct {
|
||||
// answers are by client. Each is added as it comes and never moved up,
|
||||
// so that the one that came longest ago is the first dropped.
|
||||
answers *simplelru.LRU[netip.Prefix, Answer]
|
||||
// asking are the clients whose query is under way, each with the
|
||||
// function that ends it. stopped is true once StopAsking has been
|
||||
// called, and queryGoroutines waits for the goroutine of each query.
|
||||
asking map[netip.Prefix]context.CancelFunc
|
||||
stopped bool
|
||||
queryGoroutines sync.WaitGroup
|
||||
// asking are the clients whose query is under way.
|
||||
asking map[netip.Prefix]bool
|
||||
// retryAt is when routewatch may be asked again after a query failed.
|
||||
retryAt time.Time
|
||||
}
|
||||
@@ -120,7 +116,7 @@ func New(params Params) *Client {
|
||||
params: params,
|
||||
httpClient: &http.Client{},
|
||||
answers: answers,
|
||||
asking: map[netip.Prefix]context.CancelFunc{},
|
||||
asking: map[netip.Prefix]bool{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,9 +124,8 @@ func New(params Params) *Client {
|
||||
// IPv6 group, or why it has no route. It never waits for a query. A client
|
||||
// without an answer is asked about in the background, by addr, the address its
|
||||
// request came from, unless its query is under way, a query failed less than
|
||||
// failureKept ago, maxQueries are under way, or StopAsking has been called. ctx
|
||||
// is the context of the client's request, and a query goes on after the request
|
||||
// ends, until StopAsking.
|
||||
// failureKept ago, or maxQueries are under way. ctx is the context of the
|
||||
// client's request, and a query goes on after the request ends.
|
||||
func (c *Client) Route(
|
||||
ctx context.Context, client netip.Prefix, addr netip.Addr,
|
||||
) (Route, Missing) {
|
||||
@@ -152,38 +147,15 @@ func (c *Client) Route(
|
||||
return Route{}, RequestFailed
|
||||
}
|
||||
|
||||
_, asking := c.asking[client]
|
||||
if !asking && !c.stopped && len(c.asking) < maxQueries {
|
||||
queryCtx, end := context.WithCancel(context.WithoutCancel(ctx))
|
||||
c.asking[client] = end
|
||||
if !c.asking[client] && len(c.asking) < maxQueries {
|
||||
c.asking[client] = true
|
||||
|
||||
c.queryGoroutines.Go(func() {
|
||||
defer end()
|
||||
|
||||
c.ask(queryCtx, client, addr)
|
||||
})
|
||||
go c.ask(context.WithoutCancel(ctx), client, addr)
|
||||
}
|
||||
|
||||
return Route{}, NoAnswerYet
|
||||
}
|
||||
|
||||
// StopAsking ends the queries under way, and returns once their goroutines
|
||||
// have. A query it ends gives no answer and is no failure: it is not logged,
|
||||
// and routewatch is not left alone after it. After StopAsking, Route asks
|
||||
// routewatch nothing.
|
||||
func (c *Client) StopAsking() {
|
||||
c.mu.Lock()
|
||||
|
||||
c.stopped = true
|
||||
for _, end := range c.asking {
|
||||
end()
|
||||
}
|
||||
|
||||
c.mu.Unlock()
|
||||
|
||||
c.queryGoroutines.Wait()
|
||||
}
|
||||
|
||||
// Snapshot returns every answer still in use, sorted by client, as
|
||||
// routewatch.json lists them.
|
||||
func (c *Client) Snapshot() []Answer {
|
||||
@@ -233,8 +205,7 @@ func (c *Client) Load(answers []Answer) {
|
||||
// ask asks routewatch about addr, keeps the answer as client's, and notes the
|
||||
// query as no longer under way. A query that fails gives no answer, and
|
||||
// routewatch is not asked again for failureKept. Only the first failure of
|
||||
// those under way is logged. A query ended as ctx ends, by StopAsking, is no
|
||||
// failure.
|
||||
// those under way is logged.
|
||||
func (c *Client) ask(
|
||||
ctx context.Context,
|
||||
client netip.Prefix,
|
||||
@@ -242,20 +213,18 @@ func (c *Client) ask(
|
||||
) {
|
||||
route, err := c.query(ctx, addr)
|
||||
now := c.params.Now()
|
||||
isFailure := err != nil && ctx.Err() == nil
|
||||
|
||||
c.mu.Lock()
|
||||
|
||||
delete(c.asking, client)
|
||||
|
||||
firstFailure := isFailure && !now.Before(c.retryAt)
|
||||
firstFailure := err != nil && !now.Before(c.retryAt)
|
||||
|
||||
switch {
|
||||
case err == nil:
|
||||
if err == nil {
|
||||
c.answers.Add(client, Answer{
|
||||
Client: client, Netblock: route.Netblock, ASN: route.ASN, Answered: now,
|
||||
})
|
||||
case isFailure:
|
||||
} else {
|
||||
c.retryAt = now.Add(failureKept)
|
||||
}
|
||||
|
||||
|
||||
@@ -361,54 +361,6 @@ func TestHangingRoutewatchNeverDelaysAndFailsTheQuery(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestStopAskingEndsTheQueryUnderWayAsNoFailureAndAsksNoMore(
|
||||
t *testing.T,
|
||||
) {
|
||||
t.Parallel()
|
||||
|
||||
// In a synctest bubble, as TestHangingRoutewatchNeverDelaysAndFailsTheQuery
|
||||
// says.
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
f := &fixture{t: t, log: &syncBuffer{}}
|
||||
f.client = routewatch.New(routewatch.Params{
|
||||
URL: silentURL, Now: time.Now,
|
||||
ProcessLog: slog.New(slog.NewTextHandler(f.log, nil)),
|
||||
})
|
||||
|
||||
f.route()
|
||||
synctest.Wait()
|
||||
|
||||
// The query is ended, not waited out.
|
||||
began := time.Now()
|
||||
|
||||
f.client.StopAsking()
|
||||
|
||||
if waited := time.Since(began); waited != 0 {
|
||||
t.Errorf("StopAsking returned after %s, want at once", waited)
|
||||
}
|
||||
|
||||
// A query started now would fail within waitLimit. None is, and the
|
||||
// one ended is no failure: nothing is logged, and routewatch is not
|
||||
// left alone.
|
||||
for range 2 {
|
||||
if _, missing := f.route(); missing != routewatch.NoAnswerYet {
|
||||
t.Errorf(
|
||||
"Route gave %q, want %q",
|
||||
missing,
|
||||
routewatch.NoAnswerYet,
|
||||
)
|
||||
}
|
||||
|
||||
time.Sleep(waitLimit)
|
||||
synctest.Wait()
|
||||
}
|
||||
|
||||
if f.log.String() != "" {
|
||||
t.Errorf("process log %q, want nothing", f.log.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRepeatedRequestsMakeOneQueryAndTheAnswerExpires(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
@@ -2,12 +2,6 @@ package rproxy
|
||||
|
||||
import "time"
|
||||
|
||||
// StopBreakerClock has the circuit breaker of s read at as the time, so that how
|
||||
// long requests take cannot matter to it. It is called before s serves.
|
||||
func (s *Server) StopBreakerClock(at time.Time) {
|
||||
s.h.breakerClock = func() time.Time { return at }
|
||||
}
|
||||
|
||||
// Breaker is the circuit breaker, for tests that give it the time.
|
||||
type Breaker struct {
|
||||
b breaker
|
||||
|
||||
@@ -67,7 +67,7 @@ type request struct {
|
||||
// disposition is what is done with the request: waf's valid disposition,
|
||||
// or SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION. It is "" when neither
|
||||
// gave one.
|
||||
disposition waf.Disposition
|
||||
disposition string
|
||||
// answered is true when waf gave disposition, which makes a report due,
|
||||
// and wafNoAnswer when SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION did.
|
||||
answered bool
|
||||
@@ -454,7 +454,7 @@ func (rq *request) finish(ctx context.Context) {
|
||||
requestEnd.Sub(rq.start),
|
||||
),
|
||||
DurationResponseMS: requestlog.Milliseconds(end.Sub(requestEnd)),
|
||||
Disposition: string(rq.disposition),
|
||||
Disposition: rq.disposition,
|
||||
WAFBackendNoAnswer: rq.wafNoAnswer,
|
||||
},
|
||||
)
|
||||
|
||||
@@ -94,7 +94,6 @@ func New(params Params) *Server {
|
||||
reportURL: (&url.URL{
|
||||
Scheme: backend.Scheme, Host: backend.Host, Path: waf.ReportPath,
|
||||
}).String(),
|
||||
breakerClock: time.Now,
|
||||
}
|
||||
|
||||
return &Server{
|
||||
@@ -134,10 +133,8 @@ type handler struct {
|
||||
wafTransport http.RoundTripper
|
||||
// reportURL is where the reports go.
|
||||
reportURL string
|
||||
// breaker is the circuit breaker for asks to waf, and breakerClock gives it
|
||||
// the time.
|
||||
breaker breaker
|
||||
breakerClock func() time.Time
|
||||
// breaker is the circuit breaker for asks to waf.
|
||||
breaker breaker
|
||||
// reports waits for the reports being sent. reportsMu guards starting one
|
||||
// against reportsStopped, which is set once nothing is to wait for another.
|
||||
reports sync.WaitGroup
|
||||
@@ -218,7 +215,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// This panic has Go's server close the connection without an answer,
|
||||
// and log nothing; the deferred finish still runs.
|
||||
panic(http.ErrAbortHandler)
|
||||
case waf.DispositionDeny, "": // "" is an error, or no valid disposition
|
||||
default: // a deny, an error, or an answer without a valid disposition
|
||||
rq.answerWith(answer)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,9 +72,9 @@ const (
|
||||
|
||||
// What the stand-in for waf answers.
|
||||
const (
|
||||
dispositionAllow = string(waf.DispositionAllow)
|
||||
dispositionDeny = string(waf.DispositionDeny)
|
||||
dispositionClose = string(waf.DispositionClose)
|
||||
dispositionAllow = waf.DispositionAllow
|
||||
dispositionDeny = waf.DispositionDeny
|
||||
dispositionClose = waf.DispositionClose
|
||||
// deniedBody is the body of a deny.
|
||||
deniedBody = "refused by waf"
|
||||
)
|
||||
@@ -356,19 +356,6 @@ func startServerLogging(
|
||||
) (string, *rproxy.Server) {
|
||||
t.Helper()
|
||||
|
||||
server := newServer(t, backend, appURL, env, requestLog, processLog)
|
||||
|
||||
return serve(t, server), server
|
||||
}
|
||||
|
||||
// newServer returns rproxy, not yet serving, with its request lines going to
|
||||
// requestLog and its process lines to processLog.
|
||||
func newServer(
|
||||
t *testing.T, backend, appURL string, env map[string]string,
|
||||
requestLog, processLog io.Writer,
|
||||
) *rproxy.Server {
|
||||
t.Helper()
|
||||
|
||||
settings := map[string]string{
|
||||
"SWW_RPROXY_WAF_BACKEND": backend, "SWW_RPROXY_UPSTREAM_URL": appURL,
|
||||
wafBackendTimeout: "60000",
|
||||
@@ -380,7 +367,7 @@ func newServer(
|
||||
t.Fatalf("settings %v: %v", settings, err)
|
||||
}
|
||||
|
||||
return rproxy.New(rproxy.Params{
|
||||
server := rproxy.New(rproxy.Params{
|
||||
Config: cfg,
|
||||
RequestLog: requestLog,
|
||||
ProcessLog: requestlog.NewProcessLogger(
|
||||
@@ -389,11 +376,6 @@ func newServer(
|
||||
slog.LevelInfo,
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
// serve has server serve until the test ends, and returns its address.
|
||||
func serve(t *testing.T, server *rproxy.Server) string {
|
||||
t.Helper()
|
||||
|
||||
listener, err := (&net.ListenConfig{}).Listen(
|
||||
t.Context(),
|
||||
@@ -414,7 +396,7 @@ func serve(t *testing.T, server *rproxy.Server) string {
|
||||
server.WaitForReports()
|
||||
})
|
||||
|
||||
return listener.Addr().String()
|
||||
return listener.Addr().String(), server
|
||||
}
|
||||
|
||||
// answered is the visitor's answer.
|
||||
@@ -1550,13 +1532,9 @@ func TestAnswersThatCannotBePassedOnOpenTheBreaker(t *testing.T) {
|
||||
tooLarge(w, r)
|
||||
})
|
||||
app := startApp(t, greet)
|
||||
out := &output{}
|
||||
server := newServer(t, standIn.URL, app.URL, nil, out, out)
|
||||
// The breaker's clock stands still. On a slow host, the five failures could
|
||||
// otherwise take more than the five seconds that open the breaker. The next
|
||||
// request could come more than a second later, when it may ask waf again.
|
||||
server.StopBreakerClock(time.Now())
|
||||
addr := serve(t, server)
|
||||
addr, out := startRProxy(t, standIn.URL, app.URL, map[string]string{
|
||||
wafBackendTimeout: "60000",
|
||||
})
|
||||
|
||||
for range 5 {
|
||||
if got := get(t, addr, "/"); got.status != http.StatusBadGateway {
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"maps"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
||||
)
|
||||
@@ -40,7 +41,7 @@ type wafAnswer struct {
|
||||
// notes nothing, and no report is due for it: waf holds no decision to report
|
||||
// on.
|
||||
func (rq *request) noteAnswer(answer *wafAnswer) {
|
||||
disposition := waf.Disposition(answer.header.Get(waf.DispositionHeader))
|
||||
disposition := answer.header.Get(waf.DispositionHeader)
|
||||
|
||||
switch disposition {
|
||||
case waf.DispositionAllow, waf.DispositionDeny,
|
||||
@@ -76,9 +77,7 @@ func (rq *request) handleNoUsableAnswer(ctx context.Context, err error) {
|
||||
// https://git.eeqj.de/sneak/smallwebwaf/issues/182.
|
||||
rq.h.processLog.Warn("waf gave no answer", "error", err.Error())
|
||||
|
||||
rq.disposition = waf.Disposition(
|
||||
rq.h.config.WAFBackendNoAnswerDisposition,
|
||||
)
|
||||
rq.disposition = rq.h.config.WAFBackendNoAnswerDisposition
|
||||
rq.wafNoAnswer = true
|
||||
|
||||
if rq.disposition == waf.DispositionAllow {
|
||||
@@ -98,7 +97,7 @@ func (rq *request) askUnlessBreakerOpen(
|
||||
ctx context.Context,
|
||||
) (*wafAnswer, error) {
|
||||
b := &rq.h.breaker
|
||||
if !b.mayAsk(rq.h.breakerClock()) {
|
||||
if !b.mayAsk(time.Now()) {
|
||||
return nil, errBreakerOpen
|
||||
}
|
||||
|
||||
@@ -108,7 +107,7 @@ func (rq *request) askUnlessBreakerOpen(
|
||||
case err == nil:
|
||||
b.noteAnswer()
|
||||
case ctx.Err() == nil:
|
||||
b.noteFailure(rq.h.breakerClock())
|
||||
b.noteFailure(time.Now())
|
||||
}
|
||||
|
||||
return answer, err
|
||||
|
||||
+9
-12
@@ -21,24 +21,20 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"golang.org/x/net/http/httpguts"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
)
|
||||
|
||||
// Action is what a rule does when it matches: ActionLog, ActionBlock or
|
||||
// ActionBan.
|
||||
type Action string
|
||||
|
||||
// The actions a rule takes when it matches.
|
||||
const (
|
||||
// ActionLog notes the match in the request log, and does nothing else.
|
||||
ActionLog Action = "log"
|
||||
ActionLog = "log"
|
||||
// ActionBlock refuses the request with 403.
|
||||
ActionBlock Action = "block"
|
||||
ActionBlock = "block"
|
||||
// ActionBan refuses the request and bans the client's netblock: the
|
||||
// request is a clear sign of attack.
|
||||
ActionBan Action = "ban"
|
||||
ActionBan = "ban"
|
||||
)
|
||||
|
||||
// extension ends the name of every rule file.
|
||||
@@ -99,7 +95,8 @@ type Rule struct {
|
||||
// Target is what the regex is matched against, such as path or
|
||||
// header:Accept.
|
||||
Target string
|
||||
Action Action
|
||||
// Action is ActionLog, ActionBlock or ActionBan.
|
||||
Action string
|
||||
|
||||
regex *regexp.Regexp
|
||||
}
|
||||
@@ -369,7 +366,7 @@ func parse(line string) (Rule, bool, error) {
|
||||
return Rule{}, false, errNotRule
|
||||
}
|
||||
|
||||
rule := Rule{ID: fields[1], Target: fields[2], Action: Action(fields[3])}
|
||||
rule := Rule{ID: fields[1], Target: fields[2], Action: fields[3]}
|
||||
headerName, isHeader := strings.CutPrefix(rule.Target, headerTarget)
|
||||
|
||||
switch {
|
||||
@@ -381,7 +378,7 @@ func parse(line string) (Rule, bool, error) {
|
||||
rule.Target,
|
||||
errNotTarget,
|
||||
)
|
||||
case isHeader && !httpguts.ValidHeaderFieldName(headerName):
|
||||
case isHeader && !config.IsHeaderName(headerName):
|
||||
return Rule{}, false, fmt.Errorf(
|
||||
"the target %q %w",
|
||||
rule.Target,
|
||||
@@ -397,7 +394,7 @@ func parse(line string) (Rule, bool, error) {
|
||||
rule.Target,
|
||||
errHeaderTakenOut,
|
||||
)
|
||||
case !slices.Contains([]Action{ActionLog, ActionBlock, ActionBan}, rule.Action):
|
||||
case !slices.Contains([]string{ActionLog, ActionBlock, ActionBan}, rule.Action):
|
||||
return Rule{}, false, fmt.Errorf(
|
||||
"the action %q %w",
|
||||
rule.Action,
|
||||
|
||||
@@ -181,7 +181,7 @@ type BanEntry struct {
|
||||
Netblock netip.Prefix `json:"netblock"`
|
||||
Start time.Time `json:"start"`
|
||||
Expires *time.Time `json:"expires"`
|
||||
Cause bans.Cause `json:"cause"`
|
||||
Cause string `json:"cause"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
Lifted *time.Time `json:"lifted,omitempty"`
|
||||
Notes bans.Notes `json:"notes"`
|
||||
@@ -237,11 +237,11 @@ type routeWatchFile struct {
|
||||
//
|
||||
//nolint:tagliatelle // the state files use snake_case, as the request log does
|
||||
type alertsFile struct {
|
||||
Version int `json:"version"`
|
||||
Cooldowns []alerts.Cooldown `json:"cooldowns"`
|
||||
Hour alerts.Hour `json:"hour"`
|
||||
Waiting map[alerts.DestinationName][]alerts.Alert `json:"waiting"`
|
||||
AnomalyCounters []anomaly.Counter `json:"anomaly_counters"`
|
||||
Version int `json:"version"`
|
||||
Cooldowns []alerts.Cooldown `json:"cooldowns"`
|
||||
Hour alerts.Hour `json:"hour"`
|
||||
Waiting map[string][]alerts.Alert `json:"waiting"`
|
||||
AnomalyCounters []anomaly.Counter `json:"anomaly_counters"`
|
||||
}
|
||||
|
||||
// stateFile is the struct of a state file. Once the file is decoded, its
|
||||
|
||||
@@ -1284,18 +1284,18 @@ func TestBansWrittenOnceBansWriteDelayAfterABan(t *testing.T) {
|
||||
|
||||
// A second ban, made while the first waits to be written, puts the
|
||||
// write off no further, and is written with it.
|
||||
first, _ := params.Ledger.Ban(
|
||||
first, _ := params.Ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.9/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
second, _ := params.Ledger.Ban(
|
||||
second, _ := params.Ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.10/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
|
||||
time.Sleep(5*time.Second - time.Nanosecond)
|
||||
@@ -1358,16 +1358,16 @@ func TestEditJustBeforeAScheduledWriteSurvivesIt(t *testing.T) {
|
||||
run(t, load(t, params).Run)
|
||||
|
||||
// A ban, and bans.json written with it.
|
||||
params.Ledger.Ban(netip.MustParsePrefix("203.0.113.9/32"),
|
||||
midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
params.Ledger.BanForLimit(netip.MustParsePrefix("203.0.113.9/32"),
|
||||
midnight(), bans.Notes{})
|
||||
time.Sleep(params.BansWriteDelay)
|
||||
synctest.Wait()
|
||||
|
||||
// A second ban is to be written BansWriteDelay later. Just before
|
||||
// then, an admin saves bans.json with the first ban lifted and
|
||||
// another added.
|
||||
params.Ledger.Ban(netip.MustParsePrefix("203.0.113.10/32"),
|
||||
midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
params.Ledger.BanForLimit(netip.MustParsePrefix("203.0.113.10/32"),
|
||||
midnight(), bans.Notes{})
|
||||
time.Sleep(params.BansWriteDelay - time.Nanosecond)
|
||||
synctest.Wait()
|
||||
edit(t, dir, bansJSON, permanentBansJSON)
|
||||
@@ -1483,10 +1483,10 @@ func TestFailedWriteLeavesTheFileAsItWas(t *testing.T) {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
|
||||
params.Ledger.Ban(
|
||||
params.Ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.9/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
params.Limiter.Count(
|
||||
netip.MustParsePrefix("203.0.113.9/32"),
|
||||
@@ -1556,10 +1556,10 @@ func TestWritesAreCountedInTheMetrics(t *testing.T) {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
|
||||
params.Ledger.Ban(
|
||||
params.Ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.9/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
|
||||
err = files.WriteAll()
|
||||
@@ -1758,8 +1758,8 @@ func TestEditOfRouteWatchAndAlertsFilesTakenIn(t *testing.T) {
|
||||
Event: alerts.EventBan, Netblock: netip.MustParsePrefix("198.51.100.0/24"),
|
||||
Sent: midnight(),
|
||||
}},
|
||||
Hour: alerts.Hour{HeldBack: map[alerts.Event]int{}},
|
||||
Waiting: map[alerts.DestinationName][]alerts.Alert{
|
||||
Hour: alerts.Hour{HeldBack: map[string]int{}},
|
||||
Waiting: map[string][]alerts.Alert{
|
||||
alerts.DestinationWebhook: {
|
||||
{Event: alerts.EventFileError, Time: midnight()},
|
||||
},
|
||||
@@ -2046,8 +2046,8 @@ func TestBanWithoutACauseTakenInAsAnAdmins(t *testing.T) {
|
||||
|
||||
var written struct {
|
||||
Bans []struct {
|
||||
Cause bans.Cause `json:"cause"`
|
||||
Reason string `json:"reason"`
|
||||
Cause string `json:"cause"`
|
||||
Reason string `json:"reason"`
|
||||
} `json:"bans"`
|
||||
}
|
||||
|
||||
@@ -2135,11 +2135,7 @@ func TestBanLiftedByAnEditWhileRunning(t *testing.T) {
|
||||
|
||||
// The ban that liftedBansJSON lifts, before it is lifted.
|
||||
netblock := netip.MustParsePrefix(liftedClient + "/32")
|
||||
params.Ledger.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
params.Ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
|
||||
_, banned, _ := params.Ledger.Find(netblock.Addr(), afterLifting())
|
||||
if !banned {
|
||||
@@ -2514,15 +2510,11 @@ func fill(params state.Params) {
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
|
||||
params.Ledger.Load([]bans.Ban{permanentBan()})
|
||||
params.Ledger.Ban(client, now, bans.Grounds{
|
||||
Cause: bans.CauseLimit,
|
||||
Notes: bans.Notes{ASN: asn, ASName: asName, Country: "DE", Limit: 1},
|
||||
params.Ledger.BanForLimit(client, now, bans.Notes{
|
||||
ASN: asn, ASName: asName, Country: "DE", Limit: 1,
|
||||
})
|
||||
params.Ledger.Ban(netip.MustParsePrefix("192.0.2.1/32"), now,
|
||||
bans.Grounds{
|
||||
Cause: bans.CauseAttack,
|
||||
Notes: bans.Notes{RuleID: "env-file", Target: "path"},
|
||||
})
|
||||
params.Ledger.BanForAttack(netip.MustParsePrefix("192.0.2.1/32"), now,
|
||||
bans.Notes{RuleID: "env-file", Target: "path"})
|
||||
|
||||
for _, c := range []string{"2001:db8::/64", "203.0.113.9/32", "192.0.2.1/32"} {
|
||||
params.Limiter.Count(netip.MustParsePrefix(c), now, whole)
|
||||
@@ -2708,11 +2700,7 @@ func wantLiftedBanKept(
|
||||
}
|
||||
|
||||
// Were the lifted ban counted, the next would last three hours.
|
||||
ban, _ := ledger.Ban(
|
||||
netblock,
|
||||
afterLifting(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
ban, _ := ledger.BanForLimit(netblock, afterLifting(), bans.Notes{})
|
||||
if ban.Expires.Sub(ban.Start) != time.Hour {
|
||||
t.Errorf(
|
||||
"the next ban lasts %s, want 1h",
|
||||
|
||||
+19
-16
@@ -164,7 +164,7 @@ func TestABanThroughOneSiteRefusesTheClientOnTheOther(t *testing.T) {
|
||||
|
||||
// One request's three lines carry one id, rproxy's with waf's answer.
|
||||
line := firstOut.line(t, "path", bannedProbe)
|
||||
if line["disposition"] != string(waf.DispositionDeny) ||
|
||||
if line["disposition"] != waf.DispositionDeny ||
|
||||
line["client_ip"] != placed {
|
||||
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)
|
||||
|
||||
line := out.line(t, "path", "/after-stop")
|
||||
if line["disposition"] != string(waf.DispositionAllow) ||
|
||||
if line["disposition"] != waf.DispositionAllow ||
|
||||
line["waf_backend_no_answer"] != true {
|
||||
t.Errorf("rproxy's line is %v", line)
|
||||
}
|
||||
@@ -608,8 +608,7 @@ func TestRProxyAsksAWAFThatSpeaksHTTP2OverHTTP1(t *testing.T) {
|
||||
if r.Header.Get(waf.ClientHeader) != "" {
|
||||
protocols <- r.Proto
|
||||
|
||||
w.Header().
|
||||
Set(waf.DispositionHeader, string(waf.DispositionDeny))
|
||||
w.Header().Set(waf.DispositionHeader, waf.DispositionDeny)
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
}
|
||||
}))
|
||||
@@ -692,8 +691,7 @@ func TestRProxyStoppingWaitsForAReportBeingSent(t *testing.T) {
|
||||
backend := httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get(waf.ClientHeader) != "" {
|
||||
w.Header().
|
||||
Set(waf.DispositionHeader, string(waf.DispositionAllow))
|
||||
w.Header().Set(waf.DispositionHeader, waf.DispositionAllow)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -757,7 +755,8 @@ func TestRProxyStoppingWaitsForAReportBeingSent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// sendAndKeepOpen sends request to rproxy at addr, and returns the connection.
|
||||
// sendAndKeepOpen sends request to rproxy at addr, and returns the connection. A
|
||||
// read from it ends with an error once waitLimit is up.
|
||||
func sendAndKeepOpen(t *testing.T, addr, request string) net.Conn {
|
||||
t.Helper()
|
||||
|
||||
@@ -770,6 +769,11 @@ func sendAndKeepOpen(t *testing.T, addr, request string) net.Conn {
|
||||
_ = conn.Close()
|
||||
})
|
||||
|
||||
err = conn.SetDeadline(time.Now().Add(waitLimit))
|
||||
if err != nil {
|
||||
t.Fatalf("set the deadline: %v", err)
|
||||
}
|
||||
|
||||
_, err = io.WriteString(conn, request)
|
||||
if err != nil {
|
||||
t.Fatalf("send: %v", err)
|
||||
@@ -781,7 +785,10 @@ func sendAndKeepOpen(t *testing.T, addr, request string) net.Conn {
|
||||
func TestRProxyStoppingCutsOffARequestAfterFiveSeconds(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const cutOffAfter = 5 * time.Second
|
||||
const (
|
||||
cutOffAfter = 5 * time.Second
|
||||
upstreamSetting = "SWW_RPROXY_UPSTREAM_URL"
|
||||
)
|
||||
|
||||
asked := make(chan struct{})
|
||||
release := make(chan struct{})
|
||||
@@ -795,8 +802,7 @@ func TestRProxyStoppingCutsOffARequestAfterFiveSeconds(t *testing.T) {
|
||||
|
||||
backend := httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().
|
||||
Set(waf.DispositionHeader, string(waf.DispositionAllow))
|
||||
w.Header().Set(waf.DispositionHeader, waf.DispositionAllow)
|
||||
}))
|
||||
t.Cleanup(backend.Close)
|
||||
|
||||
@@ -804,9 +810,9 @@ func TestRProxyStoppingCutsOffARequestAfterFiveSeconds(t *testing.T) {
|
||||
defer stop()
|
||||
|
||||
env := map[string]string{
|
||||
rproxyListenAddr: localhost + ":0",
|
||||
rproxyUpstreamURL: app.URL,
|
||||
wafBackend: backend.URL,
|
||||
rproxyListenAddr: localhost + ":0",
|
||||
upstreamSetting: app.URL,
|
||||
wafBackend: backend.URL,
|
||||
}
|
||||
out := &output{}
|
||||
exited := make(chan int, 1)
|
||||
@@ -832,9 +838,6 @@ func TestRProxyStoppingCutsOffARequestAfterFiveSeconds(t *testing.T) {
|
||||
|
||||
stop()
|
||||
|
||||
// Counted from the stop, so a slow wait for the app cannot shorten it.
|
||||
_ = conn.SetReadDeadline(stopped.Add(cutOffAfter + waitLimit))
|
||||
|
||||
text, err := io.ReadAll(conn)
|
||||
if err != nil || len(text) != 0 {
|
||||
t.Errorf("read %q (%v), want the connection cut off", text, err)
|
||||
|
||||
@@ -660,9 +660,7 @@ func TestCoreRuleSetExclusionsFileThatCannotBeUsedStopsTheStartWhileTheCoreRuleS
|
||||
|
||||
status := run(t.Context(), map[string]string{
|
||||
listenAddr: localhost + ":0", stateDir: t.TempDir(), ruleFilesDir: t.TempDir(),
|
||||
"SWW_WAF_CORE_RULE_SET_MODE": string(
|
||||
config.CoreRuleSetModeOff,
|
||||
),
|
||||
"SWW_WAF_CORE_RULE_SET_MODE": config.CoreRuleSetModeOff,
|
||||
"SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE": path,
|
||||
}, out)
|
||||
if status != 1 {
|
||||
|
||||
+16
-65
@@ -2,7 +2,6 @@ package sww
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
@@ -118,8 +117,7 @@ func RunWAF(ctx context.Context, params Params) int {
|
||||
"settings", cfg)
|
||||
|
||||
// The last write follows the stop of every hook, and so the end of every
|
||||
// loop that changes the state or raises an alert, and of every ask of
|
||||
// GeoJS, the DNSBL zones, AbuseIPDB and routewatch. A hook of the state
|
||||
// loop that changes the state or raises an alert. A hook of the state
|
||||
// files would stop before the loops of the parts built ahead of them.
|
||||
return run(ctx, app, failed, processLog, func() error {
|
||||
err := files.WriteAll()
|
||||
@@ -177,35 +175,11 @@ func newRuleFiles(
|
||||
return ruleFiles, nil
|
||||
}
|
||||
|
||||
// newCoreRuleSet returns the Core Rule Set at SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL,
|
||||
// without the rules SWW_WAF_CORE_RULE_SET_DISABLED_RULES switches off, reading bodies
|
||||
// up to SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE, with the exclusions in
|
||||
// SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE and the fixes of the apps in SWW_WAF_APPS,
|
||||
// or nil while SWW_WAF_CORE_RULE_SET_MODE is off. An exclusions file that
|
||||
// coreruleset.New refuses is an error in every mode. While the mode is off, a
|
||||
// set file is loaded with the Core Rule Set only to check it.
|
||||
// newCoreRuleSet returns the Core Rule Set, as waf.NewCoreRuleSet does.
|
||||
func newCoreRuleSet(cfg *config.WAF) (*coreruleset.CoreRuleSet, error) {
|
||||
if cfg.CoreRuleSetMode == config.CoreRuleSetModeOff &&
|
||||
cfg.CoreRuleSetExclusionsFile == "" {
|
||||
return nil, nil //nolint:nilnil // off is no Core Rule Set, and no error
|
||||
}
|
||||
|
||||
crs, err := coreruleset.New(coreruleset.Params{
|
||||
ParanoiaLevel: cfg.CoreRuleSetParanoiaLevel,
|
||||
DisabledRules: cfg.CoreRuleSetDisabledRules,
|
||||
BodyInspectionSize: cfg.CoreRuleSetBodyInspectionSize,
|
||||
ExclusionsFile: cfg.CoreRuleSetExclusionsFile,
|
||||
Apps: cfg.Apps,
|
||||
})
|
||||
crs, err := waf.NewCoreRuleSet(cfg)
|
||||
if err != nil {
|
||||
return nil, startError{
|
||||
"cannot use the Core Rule Set",
|
||||
fmt.Errorf("SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE: %w", err),
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.CoreRuleSetMode == config.CoreRuleSetModeOff {
|
||||
return nil, nil //nolint:nilnil // off is no Core Rule Set, and no error
|
||||
return nil, startError{"cannot use the Core Rule Set", err}
|
||||
}
|
||||
|
||||
return crs, nil
|
||||
@@ -218,7 +192,7 @@ func newLookupFile(
|
||||
lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger,
|
||||
alertQueue *alerts.Queue,
|
||||
) (*lookup.File, error) {
|
||||
if cfg.LookupSource != config.LookupSourceFile {
|
||||
if cfg.LookupSource != "file" {
|
||||
return nil, nil //nolint:nilnil // no lookup database, and no error
|
||||
}
|
||||
|
||||
@@ -298,13 +272,11 @@ func newLists(
|
||||
return lists
|
||||
}
|
||||
|
||||
// newDNSBL returns what asks the DNSBL zones, none asked yet. At the stop it
|
||||
// ends the queries under way, as reputation.DNSBL.StopAsking says.
|
||||
// newDNSBL returns what asks the DNSBL zones, none asked yet.
|
||||
func newDNSBL(
|
||||
lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger,
|
||||
alertQueue *alerts.Queue,
|
||||
cfg *config.WAF, processLog *slog.Logger, alertQueue *alerts.Queue,
|
||||
) *reputation.DNSBL {
|
||||
dnsbl := reputation.NewDNSBL(reputation.DNSBLParams{
|
||||
return reputation.NewDNSBL(reputation.DNSBLParams{
|
||||
Zones: cfg.DNSBLZones,
|
||||
Resolver: cfg.DNSBLResolver,
|
||||
CacheTTL: cfg.IPReputationCacheTTL,
|
||||
@@ -313,17 +285,12 @@ func newDNSBL(
|
||||
ProcessLog: processLog,
|
||||
Alerts: alertQueue,
|
||||
})
|
||||
|
||||
lc.Append(fx.StopHook(dnsbl.StopAsking))
|
||||
|
||||
return dnsbl
|
||||
}
|
||||
|
||||
// newAbuseIPDB returns what checks clients with AbuseIPDB while
|
||||
// SWW_WAF_ABUSEIPDB_KEY is set, none checked yet. Until waf stops, it
|
||||
// fetches AbuseIPDB's blacklist as it is due, and bans the clients on it in
|
||||
// ledger, as waf.BanAbuseIPDBBlacklist says. At the stop it ends the checks
|
||||
// under way, as reputation.AbuseIPDB.StopChecking says.
|
||||
// ledger, as waf.BanAbuseIPDBBlacklist says.
|
||||
func newAbuseIPDB(
|
||||
lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger,
|
||||
alertQueue *alerts.Queue, ledger *bans.Ledger,
|
||||
@@ -349,30 +316,24 @@ func newAbuseIPDB(
|
||||
})
|
||||
|
||||
lc.Append(background(abuseIPDB.RunBlacklist))
|
||||
lc.Append(fx.StopHook(abuseIPDB.StopChecking))
|
||||
|
||||
return abuseIPDB
|
||||
}
|
||||
|
||||
// newRouteWatch returns what asks routewatch about clients, or nil while
|
||||
// SWW_WAF_ROUTEWATCH_URL is unset. At the stop it ends the queries under
|
||||
// way, as routewatch.Client.StopAsking says.
|
||||
// SWW_WAF_ROUTEWATCH_URL is unset.
|
||||
func newRouteWatch(
|
||||
lc fx.Lifecycle, cfg *config.WAF, processLog *slog.Logger,
|
||||
cfg *config.WAF, processLog *slog.Logger,
|
||||
) *routewatch.Client {
|
||||
if cfg.RouteWatchURL == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
routeWatch := routewatch.New(routewatch.Params{
|
||||
return routewatch.New(routewatch.Params{
|
||||
URL: cfg.RouteWatchURL,
|
||||
Now: utcNow,
|
||||
ProcessLog: processLog,
|
||||
})
|
||||
|
||||
lc.Append(fx.StopHook(routeWatch.StopAsking))
|
||||
|
||||
return routeWatch
|
||||
}
|
||||
|
||||
// newMetrics returns the metrics, with those read from the other parts as
|
||||
@@ -410,18 +371,12 @@ func newMetrics(
|
||||
|
||||
// newGeoJS returns what looks clients up at GeoJS, none looked up yet. Each
|
||||
// answer goes to the limiter and the ledger, as waf.AddLookup says, and each
|
||||
// failure is handled as waf.GeoJSFailed says. At the stop it ends the request
|
||||
// under way, as geojs.GeoJS.StopAsking says.
|
||||
// failure is handled as waf.GeoJSFailed says.
|
||||
func newGeoJS(
|
||||
lc fx.Lifecycle,
|
||||
cfg *config.WAF,
|
||||
m *metrics.Metrics,
|
||||
limiter *ratelimit.Limiter,
|
||||
ledger *bans.Ledger,
|
||||
alertQueue *alerts.Queue,
|
||||
processLog *slog.Logger,
|
||||
cfg *config.WAF, m *metrics.Metrics, limiter *ratelimit.Limiter,
|
||||
ledger *bans.Ledger, alertQueue *alerts.Queue, processLog *slog.Logger,
|
||||
) *geojs.GeoJS {
|
||||
geoJS := geojs.New(geojs.Params{
|
||||
return geojs.New(geojs.Params{
|
||||
URL: geojs.URL,
|
||||
Answered: func(answer geojs.Answer) {
|
||||
waf.AddLookup(cfg, limiter, ledger, answer)
|
||||
@@ -433,10 +388,6 @@ func newGeoJS(
|
||||
},
|
||||
WentWithoutAnswer: m.RequestsWithoutGeoJSAnswer.Inc,
|
||||
})
|
||||
|
||||
lc.Append(fx.StopHook(geoJS.StopAsking))
|
||||
|
||||
return geoJS
|
||||
}
|
||||
|
||||
// newWAFServer returns the server waf runs, with its parts and the time
|
||||
|
||||
@@ -208,9 +208,7 @@ func TestMainWithoutACommandItKnowsPrintsTheCommands(t *testing.T) {
|
||||
|
||||
// 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.
|
||||
func ask(
|
||||
t *testing.T, addr, path string, status int, disposition waf.Disposition,
|
||||
) {
|
||||
func ask(t *testing.T, addr, path string, status int, disposition string) {
|
||||
t.Helper()
|
||||
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet,
|
||||
@@ -234,7 +232,7 @@ func ask(
|
||||
_ = res.Body.Close()
|
||||
|
||||
if res.StatusCode != status ||
|
||||
res.Header.Get(waf.DispositionHeader) != string(disposition) {
|
||||
res.Header.Get(waf.DispositionHeader) != disposition {
|
||||
t.Errorf(
|
||||
"answered %d with disposition %q, want %d with %q",
|
||||
res.StatusCode,
|
||||
|
||||
@@ -54,7 +54,7 @@ func (abuseIPDB) check(
|
||||
"scored by AbuseIPDB at or over SWW_WAF_ABUSEIPDB_MIN_SCORE",
|
||||
)
|
||||
|
||||
if rq.h.config.DNSBLAbuseIPDBAction != config.ReputationActionDeny {
|
||||
if rq.h.config.DNSBLAbuseIPDBAction != deny {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -37,10 +37,10 @@ func TestAdminEndpointsAreOffWhileTheTokenIsUnset(t *testing.T) {
|
||||
"",
|
||||
map[string]string{metricsToken: token},
|
||||
)
|
||||
server.Ledger.Ban(
|
||||
server.Ledger.BanForLimit(
|
||||
netip.MustParsePrefix(otherClient+"/32"),
|
||||
clk.Now(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
before := server.Ledger.Snapshot()
|
||||
|
||||
|
||||
@@ -130,9 +130,11 @@ func TestObserveModeRaisesTheBanAlertsItWouldHave(t *testing.T) {
|
||||
// A ban for a clear sign of attack, which a request under it would make
|
||||
// permanent.
|
||||
group := netip.MustParsePrefix(ipv6Group)
|
||||
attackBan, _ := server.Ledger.Ban(group, start, bans.Grounds{
|
||||
Cause: bans.CauseAttack, Notes: bans.Notes{RuleID: "probe"},
|
||||
})
|
||||
attackBan, _ := server.Ledger.BanForAttack(
|
||||
group,
|
||||
start,
|
||||
bans.Notes{RuleID: "probe"},
|
||||
)
|
||||
|
||||
// The third request breaks the limit, and so does the fourth, within the
|
||||
// cooldown, which raises nothing. The probe is a clear sign of attack.
|
||||
@@ -292,7 +294,7 @@ func startAppWithAlerts(
|
||||
// the time raised, for ban, with its netblock, cause, reason and notes,
|
||||
// which ends at expires, as the log line gives it.
|
||||
func banAlert(
|
||||
event alerts.Event,
|
||||
event string,
|
||||
raised time.Time,
|
||||
client string,
|
||||
ban bans.Ban,
|
||||
@@ -307,9 +309,7 @@ func banAlert(
|
||||
Netblock: ban.Netblock,
|
||||
Reason: ban.Reason,
|
||||
Detail: map[string]any{
|
||||
"cause": string(
|
||||
ban.Cause,
|
||||
), "ban_expires": expires, "notes": ban.Notes,
|
||||
"cause": ban.Cause, "ban_expires": expires, "notes": ban.Notes,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -317,7 +317,7 @@ func banAlert(
|
||||
// attackAlert is banAlert for a ban for the probe rule of testRules, with
|
||||
// the netblock and the notes of ban.
|
||||
func attackAlert(
|
||||
event alerts.Event,
|
||||
event string,
|
||||
raised time.Time,
|
||||
client string,
|
||||
ban bans.Ban,
|
||||
|
||||
@@ -86,10 +86,10 @@ func TestEachScopeAndWindowOverItsThresholdAlertsOncePerCooldown(
|
||||
value int64
|
||||
count float64
|
||||
}{
|
||||
{requestsPerMinute, string(ratelimit.KindRequests), minute, 2, 3},
|
||||
{requestsPerHour, string(ratelimit.KindRequests), hour, 2, 3},
|
||||
{bytesPerMinute, string(ratelimit.KindBytes), minute, 250, 300},
|
||||
{bytesPerHour, string(ratelimit.KindBytes), hour, 250, 300},
|
||||
{requestsPerMinute, ratelimit.KindRequests, minute, 2, 3},
|
||||
{requestsPerHour, ratelimit.KindRequests, hour, 2, 3},
|
||||
{bytesPerMinute, ratelimit.KindBytes, minute, 250, 300},
|
||||
{bytesPerHour, ratelimit.KindBytes, hour, 250, 300},
|
||||
} {
|
||||
setting := scope.prefix + threshold.end
|
||||
value := strconv.FormatInt(threshold.value, 10)
|
||||
@@ -186,7 +186,7 @@ func TestEveryRequestIsCountedWhateverIsDoneWithIt(t *testing.T) {
|
||||
for _, sent := range []struct {
|
||||
from, path string
|
||||
status int
|
||||
action requestlog.Action
|
||||
action string
|
||||
}{
|
||||
{allowed, "/", http.StatusOK, requestlog.ActionForward},
|
||||
{exempt, "/", http.StatusOK, requestlog.ActionForward},
|
||||
|
||||
@@ -134,7 +134,7 @@ func TestARequestLineOverTheLimitIsRefusedWith414BeforeAnyOtherCheck(
|
||||
// cookies is how many cookies the request carries.
|
||||
cookies int
|
||||
status int
|
||||
action requestlog.Action
|
||||
action string
|
||||
}{
|
||||
{
|
||||
"at the limit", lineLimit, 0, http.StatusOK,
|
||||
@@ -686,10 +686,10 @@ func askDirectly(
|
||||
|
||||
// banClient bans client at now.
|
||||
func banClient(_ *testing.T, server *waf.Server, now time.Time) {
|
||||
server.Ledger.Ban(
|
||||
server.Ledger.BanForLimit(
|
||||
netip.MustParsePrefix(client+"/32"),
|
||||
now,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -730,7 +730,7 @@ func TestARefusedRequestDoesNotWaitForASlowGeoJS(t *testing.T) {
|
||||
// cookie is the request's Cookie header.
|
||||
cookie string
|
||||
status int
|
||||
action requestlog.Action
|
||||
action string
|
||||
}{
|
||||
{
|
||||
name: "SWW_WAF_DENY_NETS", env: map[string]string{denyNets: client},
|
||||
@@ -813,7 +813,7 @@ func TestAQuestionWaitsForASlowGeoJSOnlyAsLongAsTheDefaultSays(
|
||||
// prepare gets the server ready, at now, before the question.
|
||||
prepare func(t *testing.T, server *waf.Server, now time.Time)
|
||||
status int
|
||||
action requestlog.Action
|
||||
action string
|
||||
}{
|
||||
{"nothing refuses it", nil, leaveAsItIs, http.StatusOK, requestlog.ActionForward},
|
||||
{
|
||||
|
||||
+63
-88
@@ -17,7 +17,7 @@ import (
|
||||
|
||||
// banResponse is a refusal answered with SWW_WAF_BAN_RESPONSE, and logged
|
||||
// with action.
|
||||
func (rq *request) banResponse(action requestlog.Action) *refusal {
|
||||
func (rq *request) banResponse(action string) *refusal {
|
||||
return &refusal{status: rq.h.config.BanResponse, action: action}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ func (activeBan) check(
|
||||
|
||||
// banAction is the action of a request refused under a ban or for making one:
|
||||
// permanent says the ban never runs out.
|
||||
func banAction(permanent bool) requestlog.Action {
|
||||
func banAction(permanent bool) string {
|
||||
if permanent {
|
||||
return requestlog.ActionPermanentlyBanned
|
||||
}
|
||||
@@ -86,14 +86,13 @@ func (rq *request) countedBytes() int64 {
|
||||
response, request := rq.out.bytes, rq.requestBytes
|
||||
|
||||
switch rq.h.config.BytesCounted {
|
||||
case config.BytesCountedResponse:
|
||||
case "response":
|
||||
return response
|
||||
case config.BytesCountedRequest:
|
||||
case "request":
|
||||
return request
|
||||
case config.BytesCountedBoth:
|
||||
default: // both
|
||||
return response + request
|
||||
}
|
||||
|
||||
return response + request
|
||||
}
|
||||
|
||||
// banForLimit bans the client's netblock at now for a broken limit, the
|
||||
@@ -103,8 +102,9 @@ func (rq *request) countedBytes() int64 {
|
||||
// one whose bytes broke a byte limit, the refusal for one that broke the
|
||||
// refusal limit. The ban's notes give the client's limit percentage for a
|
||||
// rate limit or a byte limit; the refusal limit is not lowered. The ban sets
|
||||
// the client's counters back to zero. In observe mode, banClient says what
|
||||
// happens instead.
|
||||
// the client's counters back to zero. In observe mode it makes no ban and
|
||||
// sets nothing back, and raises the alert for the ban it would have made,
|
||||
// if that alert would be sent.
|
||||
func (rq *request) banForLimit(
|
||||
now time.Time,
|
||||
hit ratelimit.Hit,
|
||||
@@ -115,18 +115,30 @@ func (rq *request) banForLimit(
|
||||
rq.line.LimitHit = hit.Window + "_bytes" // as counts names the byte totals
|
||||
case ratelimit.KindRefusals:
|
||||
rq.line.LimitHit = requestlog.LimitHitMinuteRefusals
|
||||
case ratelimit.KindRequests:
|
||||
default:
|
||||
rq.line.LimitHit = hit.Window
|
||||
}
|
||||
|
||||
rq.line.Offence = requestlog.OffenceLimit
|
||||
rq.h.metrics.LimitHit(hit)
|
||||
|
||||
netblock := banNetblock(rq.h.config, rq.client)
|
||||
if rq.h.config.Observe &&
|
||||
!rq.wouldAlertBan(netblock, now, bans.CauseLimit) {
|
||||
return
|
||||
}
|
||||
|
||||
notes := bans.Notes{
|
||||
Kind: hit.Kind,
|
||||
Limit: hit.Limit,
|
||||
Window: hit.Window,
|
||||
Count: hit.Count,
|
||||
ASN: rq.line.ASN,
|
||||
ASName: rq.line.ASName,
|
||||
Country: rq.line.Country,
|
||||
Kind: hit.Kind,
|
||||
Limit: hit.Limit,
|
||||
Window: hit.Window,
|
||||
Count: hit.Count,
|
||||
Reputation: rq.reputation,
|
||||
Request: rq.noted(now, status),
|
||||
Requests: rq.netblockRequests(netblock),
|
||||
}
|
||||
|
||||
switch hit.Kind {
|
||||
@@ -134,59 +146,49 @@ func (rq *request) banForLimit(
|
||||
notes.LimitPercent, notes.LimitPercentSetting = rq.rateLimitPercent.logged()
|
||||
case ratelimit.KindBytes:
|
||||
notes.LimitPercent, notes.LimitPercentSetting = rq.bytesLimitPercent.logged()
|
||||
case ratelimit.KindRefusals: // the refusal limit is not lowered
|
||||
}
|
||||
|
||||
rq.banClient(
|
||||
now,
|
||||
status,
|
||||
bans.Grounds{Cause: bans.CauseLimit, Notes: notes},
|
||||
)
|
||||
if rq.h.config.Observe {
|
||||
ban, wouldBan := rq.h.ledger.WouldBanForLimit(netblock, now, notes)
|
||||
if wouldBan {
|
||||
rq.alertBan(ban)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ban, made := rq.h.ledger.BanForLimit(netblock, now, notes)
|
||||
rq.h.limiter.Reset(clientGroup(rq.h.config, rq.client))
|
||||
rq.line.BanExpires = banExpires(ban)
|
||||
|
||||
if made {
|
||||
rq.alertBan(ban)
|
||||
}
|
||||
}
|
||||
|
||||
// banForAttack bans the client's netblock at now for a clear sign of
|
||||
// attack, which notes name: the ban rule that matched, or the trap path
|
||||
// asked for. It returns the action for that ban. In observe mode, banClient
|
||||
// says what happens instead.
|
||||
func (rq *request) banForAttack(
|
||||
now time.Time, notes bans.Notes,
|
||||
) requestlog.Action {
|
||||
return rq.banClient(
|
||||
now,
|
||||
rq.h.config.BanResponse,
|
||||
bans.Grounds{Cause: bans.CauseAttack, Notes: notes},
|
||||
)
|
||||
}
|
||||
|
||||
// banClient bans the client's netblock at now on grounds, and returns the
|
||||
// action for that ban. It adds to the notes what the request tells of the
|
||||
// client, with status, what the client was sent. A ban for a broken limit
|
||||
// sets the client's counters back to zero.
|
||||
//
|
||||
// In observe mode it makes no ban and sets nothing back. It raises the
|
||||
// alert for the ban it would have made, if that alert would be sent.
|
||||
func (rq *request) banClient(
|
||||
now time.Time,
|
||||
status int,
|
||||
grounds bans.Grounds,
|
||||
) requestlog.Action {
|
||||
// asked for. It fills in the rest of the notes. In observe mode it makes
|
||||
// no ban, and raises the alert for the ban it would have made, if that
|
||||
// alert would be sent. It returns the action for that ban.
|
||||
func (rq *request) banForAttack(now time.Time, notes bans.Notes) string {
|
||||
netblock := banNetblock(rq.h.config, rq.client)
|
||||
if rq.h.config.Observe &&
|
||||
!rq.wouldAlertBan(netblock, now, bans.CauseAttack) {
|
||||
return banAction(
|
||||
rq.h.ledger.WouldBePermanent(netblock, now, bans.CauseAttack),
|
||||
)
|
||||
}
|
||||
|
||||
notes.ASN = rq.line.ASN
|
||||
notes.ASName = rq.line.ASName
|
||||
notes.Country = rq.line.Country
|
||||
notes.Reputation = rq.reputation
|
||||
notes.Request = rq.noted(now, rq.h.config.BanResponse)
|
||||
notes.Requests = rq.netblockRequests(netblock)
|
||||
|
||||
if rq.h.config.Observe {
|
||||
if !rq.wouldAlertBan(netblock, now, grounds.Cause) {
|
||||
return banAction(
|
||||
rq.h.ledger.WouldBePermanent(netblock, now, grounds.Cause),
|
||||
)
|
||||
}
|
||||
|
||||
grounds.Notes = rq.withClientNotes(
|
||||
grounds.Notes,
|
||||
now,
|
||||
status,
|
||||
netblock,
|
||||
)
|
||||
|
||||
ban, wouldBan := rq.h.ledger.WouldBan(netblock, now, grounds)
|
||||
ban, wouldBan := rq.h.ledger.WouldBanForAttack(netblock, now, notes)
|
||||
if wouldBan {
|
||||
rq.alertBan(ban)
|
||||
}
|
||||
@@ -194,13 +196,7 @@ func (rq *request) banClient(
|
||||
return banAction(ban.Permanent())
|
||||
}
|
||||
|
||||
grounds.Notes = rq.withClientNotes(grounds.Notes, now, status, netblock)
|
||||
|
||||
ban, made := rq.h.ledger.Ban(netblock, now, grounds)
|
||||
if grounds.Cause == bans.CauseLimit {
|
||||
rq.h.limiter.Reset(clientGroup(rq.h.config, rq.client))
|
||||
}
|
||||
|
||||
ban, made := rq.h.ledger.BanForAttack(netblock, now, notes)
|
||||
rq.line.BanExpires = banExpires(ban)
|
||||
|
||||
if made {
|
||||
@@ -210,32 +206,13 @@ func (rq *request) banClient(
|
||||
return banAction(ban.Permanent())
|
||||
}
|
||||
|
||||
// withClientNotes is notes with what the request tells of the client
|
||||
// added: its AS, country and reputation, the request at now with status,
|
||||
// and how many requests netblock, the client's, has sent.
|
||||
func (rq *request) withClientNotes(
|
||||
notes bans.Notes,
|
||||
now time.Time,
|
||||
status int,
|
||||
netblock netip.Prefix,
|
||||
) bans.Notes {
|
||||
notes.ASN = rq.line.ASN
|
||||
notes.ASName = rq.line.ASName
|
||||
notes.Country = rq.line.Country
|
||||
notes.Reputation = rq.reputation
|
||||
notes.Request = rq.noted(now, status)
|
||||
notes.Requests = rq.netblockRequests(netblock)
|
||||
|
||||
return notes
|
||||
}
|
||||
|
||||
// wouldAlertBan reports whether the alert for a ban on netblock for cause
|
||||
// made at now would be sent. In observe mode the ban the request would
|
||||
// have made is worked out only then, at most once per
|
||||
// SWW_WAF_ALERT_COOLDOWN and never with no webhook set: its notes count the
|
||||
// netblock's requests, which can mean going through every client.
|
||||
func (rq *request) wouldAlertBan(
|
||||
netblock netip.Prefix, now time.Time, cause bans.Cause,
|
||||
netblock netip.Prefix, now time.Time, cause string,
|
||||
) bool {
|
||||
event := alerts.EventBan
|
||||
if rq.h.ledger.WouldBePermanent(netblock, now, cause) {
|
||||
@@ -257,9 +234,7 @@ func (rq *request) alertBan(ban bans.Ban) {
|
||||
}
|
||||
|
||||
detail := map[string]any{
|
||||
"cause": string(ban.Cause),
|
||||
"ban_expires": banExpires(ban),
|
||||
"notes": ban.Notes,
|
||||
"cause": ban.Cause, "ban_expires": banExpires(ban), "notes": ban.Notes,
|
||||
}
|
||||
if rq.h.config.Observe {
|
||||
detail["mode"] = "observe"
|
||||
|
||||
@@ -504,9 +504,7 @@ type sender struct {
|
||||
}
|
||||
|
||||
// get sends a GET request for / from the client at from.
|
||||
func (s *sender) get(
|
||||
from string, status int, action requestlog.Action,
|
||||
) logLine {
|
||||
func (s *sender) get(from string, status int, action string) logLine {
|
||||
s.t.Helper()
|
||||
|
||||
return s.request(from, "/", status, action)
|
||||
@@ -519,7 +517,7 @@ func (s *sender) get(
|
||||
func (s *sender) request(
|
||||
from, path string,
|
||||
status int,
|
||||
action requestlog.Action,
|
||||
action string,
|
||||
) logLine {
|
||||
s.t.Helper()
|
||||
|
||||
@@ -529,7 +527,7 @@ func (s *sender) request(
|
||||
// requestWithHeader is request with header, such as "Authorization:
|
||||
// Bearer x", added to the request unless it is "".
|
||||
func (s *sender) requestWithHeader(
|
||||
from, path, header string, status int, action requestlog.Action,
|
||||
from, path, header string, status int, action string,
|
||||
) logLine {
|
||||
s.t.Helper()
|
||||
|
||||
@@ -550,14 +548,8 @@ func (s *sender) requestWithHeader(
|
||||
// body is sent as it is after the headers, header holding its
|
||||
// Content-Length or Transfer-Encoding. header may hold several lines,
|
||||
// separated by "\r\n". It returns the whole answer.
|
||||
//
|
||||
// It waits for the answer as long as it takes, since a busy host can slow
|
||||
// the Core Rule Set past any shorter wait. go test's -timeout ends a test
|
||||
// whose answer never comes.
|
||||
func (s *sender) requestWithBody(
|
||||
method, from, path, header, body string,
|
||||
status int,
|
||||
action requestlog.Action,
|
||||
method, from, path, header, body string, status int, action string,
|
||||
) (logLine, answer) {
|
||||
s.t.Helper()
|
||||
|
||||
@@ -574,6 +566,11 @@ func (s *sender) requestWithBody(
|
||||
send(s.t, conn, method+" "+path+" HTTP/1.1\r\nHost: "+appHost+"\r\n"+
|
||||
forwardedFor+": "+from+"\r\n"+header+"\r\n"+body)
|
||||
|
||||
err := conn.SetReadDeadline(time.Now().Add(waitLimit))
|
||||
if err != nil {
|
||||
s.t.Fatalf("set read deadline: %v", err)
|
||||
}
|
||||
|
||||
var got answer
|
||||
|
||||
res, err := http.ReadResponse(bufio.NewReader(conn), nil)
|
||||
|
||||
@@ -198,7 +198,7 @@ type firstToRefuse struct {
|
||||
env map[string]string
|
||||
inspection time.Duration
|
||||
took time.Duration
|
||||
action requestlog.Action
|
||||
action string
|
||||
}
|
||||
|
||||
// firstToRefuseCases are the cases of
|
||||
|
||||
+9
-10
@@ -53,34 +53,33 @@ func (rq *request) limitPercentages() (percentage, percentage) {
|
||||
if country == "" && !rq.skippedLookupWait {
|
||||
unknown = percentage{
|
||||
cfg.UnknownCountryLimitPercent,
|
||||
config.UnknownCountryLimitPercentSetting,
|
||||
"SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT",
|
||||
}
|
||||
}
|
||||
|
||||
fetched := percentage{percent: whole}
|
||||
if percent, listed := rq.h.lists.ASNLimitPercent(asn); listed {
|
||||
fetched = percentage{percent, config.ASNLimitPercentURLSetting}
|
||||
fetched = percentage{percent, "SWW_WAF_ASN_LIMIT_PERCENT_URL"}
|
||||
}
|
||||
|
||||
blocklisted := percentage{percent: whole}
|
||||
if rq.blocklisted &&
|
||||
cfg.BlocklistAction == config.ReputationActionLimit {
|
||||
if rq.blocklisted && cfg.BlocklistAction == "limit" {
|
||||
blocklisted = percentage{
|
||||
cfg.BlocklistLimitPercent,
|
||||
config.BlocklistActionSetting,
|
||||
"SWW_WAF_BLOCKLIST_ACTION",
|
||||
}
|
||||
}
|
||||
|
||||
dnsblAbuseIPDBListed := percentage{percent: whole}
|
||||
if (rq.dnsblListed || rq.abuseIPDBHit) &&
|
||||
cfg.DNSBLAbuseIPDBAction == config.ReputationActionLimit {
|
||||
cfg.DNSBLAbuseIPDBAction == "limit" {
|
||||
dnsblAbuseIPDBListed = percentage{
|
||||
cfg.DNSBLAbuseIPDBLimitPercent,
|
||||
config.DNSBLAbuseIPDBActionSetting,
|
||||
"SWW_WAF_DNSBL_ABUSEIPDB_ACTION",
|
||||
}
|
||||
}
|
||||
|
||||
const tagSetting = config.TagLimitPercentSetting
|
||||
const tagSetting = "SWW_WAF_TAG_LIMIT_PERCENT"
|
||||
|
||||
asnRequests := lowest(
|
||||
given(cfg.TagLimitPercent, asnTag(asn), tagSetting),
|
||||
@@ -108,7 +107,7 @@ func (rq *request) limitPercentages() (percentage, percentage) {
|
||||
asnBytes = given(
|
||||
cfg.ASNBytesLimitPercent,
|
||||
asn,
|
||||
config.ASNBytesLimitPercentSetting,
|
||||
"SWW_WAF_ASN_BYTES_LIMIT_PERCENT",
|
||||
)
|
||||
}
|
||||
|
||||
@@ -116,7 +115,7 @@ func (rq *request) limitPercentages() (percentage, percentage) {
|
||||
countryBytes = given(
|
||||
cfg.CountryBytesLimitPercent,
|
||||
country,
|
||||
config.CountryBytesLimitPercentSetting,
|
||||
"SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT",
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ func TestEachBiasedThresholdLowersTheRateLimits(t *testing.T) {
|
||||
// Half of 4 requests a minute: the third breaks the limit.
|
||||
for _, sent := range []struct {
|
||||
status int
|
||||
action requestlog.Action
|
||||
action string
|
||||
}{
|
||||
{http.StatusOK, requestlog.ActionForward},
|
||||
{http.StatusOK, requestlog.ActionForward},
|
||||
@@ -452,7 +452,7 @@ func TestBanForALoweredLimitGivesThePercentageInItsNotesAndItsAlert(
|
||||
// limit, which is answered with status and logged with action.
|
||||
before int
|
||||
status int
|
||||
action requestlog.Action
|
||||
action string
|
||||
// reason and want are the ban's reason, and its notes' limit
|
||||
// percentage, as percentText gives it.
|
||||
reason, want string
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
)
|
||||
|
||||
@@ -42,6 +41,5 @@ func (blocklists) check(
|
||||
// client, given listedBy, the blocklists that list it: it does when one lists
|
||||
// it and SWW_WAF_BLOCKLIST_ACTION is deny.
|
||||
func (rq *request) refusedByBlocklists(listedBy []string) bool {
|
||||
return len(listedBy) > 0 && rq.h.config.BlocklistAction ==
|
||||
config.ReputationActionDeny
|
||||
return len(listedBy) > 0 && rq.h.config.BlocklistAction == deny
|
||||
}
|
||||
@@ -47,7 +47,7 @@ func (coreRuleSet) check(
|
||||
rq.line.CoreRuleSetScore = &result.Score
|
||||
|
||||
for _, id := range result.RuleIDs {
|
||||
rq.h.metrics.CoreRuleSetMatched(string(cfg.CoreRuleSetMode), id)
|
||||
rq.h.metrics.CoreRuleSetMatched(cfg.CoreRuleSetMode, id)
|
||||
}
|
||||
|
||||
threshold := cfg.CoreRuleSetScoreThreshold
|
||||
@@ -99,7 +99,7 @@ func (rq *request) alertCoreRuleSetMatch(result coreruleset.Result) {
|
||||
|
||||
switch {
|
||||
case rq.h.config.CoreRuleSetMode == config.CoreRuleSetModeDetect:
|
||||
detail["mode"] = string(config.CoreRuleSetModeDetect)
|
||||
detail["mode"] = config.CoreRuleSetModeDetect
|
||||
case rq.h.config.Observe:
|
||||
detail["mode"] = "observe"
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ func TestCoreRuleSetMatchAddsTheExploitSignatureTag(t *testing.T) {
|
||||
name string
|
||||
mode string
|
||||
status int
|
||||
action requestlog.Action
|
||||
action string
|
||||
}{
|
||||
{block, block, http.StatusForbidden, requestlog.ActionBlockedByCoreRuleSet},
|
||||
{detect, detect, http.StatusOK, requestlog.ActionForward},
|
||||
@@ -158,9 +158,8 @@ func TestCoreRuleSetRefusesAttacksInBlockModeAndOnlyLogsThemInDetectMode(
|
||||
t.Parallel()
|
||||
|
||||
for _, tc := range []struct {
|
||||
mode string
|
||||
action requestlog.Action
|
||||
status int
|
||||
mode, action string
|
||||
status int
|
||||
}{
|
||||
{block, requestlog.ActionBlockedByCoreRuleSet, http.StatusForbidden},
|
||||
{detect, requestlog.ActionForward, http.StatusOK},
|
||||
@@ -653,9 +652,8 @@ func TestCoreRuleSetMatchRaisesTheCoreRuleSetMatchAlert(t *testing.T) {
|
||||
// status and action are what the request is answered and logged
|
||||
// with, and alertMode what the alert's detail gives as mode, if
|
||||
// anything.
|
||||
status int
|
||||
action requestlog.Action
|
||||
alertMode string
|
||||
status int
|
||||
action, alertMode string
|
||||
}{
|
||||
{
|
||||
"block", map[string]string{coreRuleSetMode: block},
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
)
|
||||
|
||||
@@ -34,8 +33,7 @@ func (countries) check(
|
||||
return &verdict{
|
||||
action: requestlog.ActionCountryDenied,
|
||||
figures: map[string]any{
|
||||
"country": country,
|
||||
"setting": config.ExclusivelyAllowedCountriesSetting,
|
||||
"country": country, "setting": "SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES",
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -316,7 +316,7 @@ func TestExclusiveListRefusesAPrivateAddressUnlessAllowed(t *testing.T) {
|
||||
name string
|
||||
allowNets string
|
||||
status int
|
||||
action requestlog.Action
|
||||
action string
|
||||
}{
|
||||
{
|
||||
"not in SWW_WAF_ALLOW_NETS", "",
|
||||
|
||||
@@ -32,8 +32,7 @@ func (dnsbl) check(ctx context.Context, rq *request, _ time.Time) *verdict {
|
||||
rq.addTag(config.DNSBLTagPrefix + strings.ToLower(zone))
|
||||
}
|
||||
|
||||
if !rq.dnsblListed ||
|
||||
rq.h.config.DNSBLAbuseIPDBAction != config.ReputationActionDeny {
|
||||
if !rq.dnsblListed || rq.h.config.DNSBLAbuseIPDBAction != deny {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,6 @@ package waf
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||
)
|
||||
|
||||
// To add a heuristic, write one file with a type that implements heuristic,
|
||||
@@ -22,10 +20,10 @@ type heuristic interface {
|
||||
}
|
||||
|
||||
// verdict is a heuristic refusing a request, or banning its client: the action
|
||||
// the log line names, and the figures the heuristic acted on, by name, such as
|
||||
// the count and the limit.
|
||||
// the log line names, one of requestlog's Action constants, and the figures the
|
||||
// heuristic acted on, by name, such as the count and the limit.
|
||||
type verdict struct {
|
||||
action requestlog.Action
|
||||
action string
|
||||
figures map[string]any
|
||||
}
|
||||
|
||||
@@ -106,6 +104,12 @@ func onReport() []heuristic {
|
||||
}
|
||||
}
|
||||
|
||||
// runAfterAnswer runs the heuristics of afterAnswer on the request, at the time
|
||||
// it is called.
|
||||
func (rq *request) runAfterAnswer(ctx context.Context) {
|
||||
rq.runBanningHeuristics(ctx, afterAnswer())
|
||||
}
|
||||
|
||||
// runHeuristics runs list, one of those checkClient runs, on the request at now,
|
||||
// and returns the first verdict, or nil when none gives one. The log line's
|
||||
// heuristic and heuristic_figures name the heuristic that gave it, and give its
|
||||
|
||||
@@ -49,8 +49,7 @@ func (rq *request) startLookUp(
|
||||
) <-chan lookupResult {
|
||||
result := make(chan lookupResult, 1)
|
||||
|
||||
if rq.h.config.LookupSource == config.LookupSourceOff ||
|
||||
!canBePlaced(rq.client) {
|
||||
if rq.h.config.LookupSource == "off" || !canBePlaced(rq.client) {
|
||||
result <- lookupResult{}
|
||||
|
||||
return result
|
||||
@@ -58,7 +57,7 @@ func (rq *request) startLookUp(
|
||||
|
||||
client := clientGroup(rq.h.config, rq.client)
|
||||
|
||||
if rq.h.config.LookupSource == config.LookupSourceFile {
|
||||
if rq.h.config.LookupSource == "file" {
|
||||
result <- lookupResult{
|
||||
lookedUp: true, answer: rq.h.lookupFile.LookUp(client),
|
||||
}
|
||||
|
||||
@@ -22,12 +22,12 @@ func TestBanActionSaysWhetherTheBanRunsOut(t *testing.T) {
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
cause bans.Cause
|
||||
cause string
|
||||
// endsIn is how long after the request the ban ends: negative for a
|
||||
// ban that has ended, and zero for a permanent ban.
|
||||
endsIn time.Duration
|
||||
path string
|
||||
want requestlog.Action
|
||||
want string
|
||||
}{
|
||||
{
|
||||
"temporary ban", bans.CauseAdmin, time.Hour, "/",
|
||||
@@ -170,10 +170,7 @@ func TestObserveModeForwardsWhatEnforceModeRefuses(t *testing.T) {
|
||||
// and its second breaks it.
|
||||
s.get(fromDE, http.StatusOK, requestlog.ActionForward)
|
||||
|
||||
for _, sent := range []struct {
|
||||
from string
|
||||
refusal requestlog.Action
|
||||
}{
|
||||
for _, sent := range []struct{ from, refusal string }{
|
||||
{denied, requestlog.ActionClientDenied},
|
||||
{banned, requestlog.ActionTemporarilyBanned},
|
||||
{fromKP, requestlog.ActionClientDenied},
|
||||
@@ -256,7 +253,7 @@ func TestObserveModeMakesNoBanAndKeepsTheBansItHas(t *testing.T) {
|
||||
|
||||
// wantWouldAction checks the request log line's would_action, and that a
|
||||
// line that should have none has no such field.
|
||||
func wantWouldAction(t *testing.T, line logLine, want requestlog.Action) {
|
||||
func wantWouldAction(t *testing.T, line logLine, want string) {
|
||||
t.Helper()
|
||||
|
||||
got, present := line.fields["would_action"]
|
||||
@@ -264,7 +261,7 @@ func wantWouldAction(t *testing.T, line logLine, want requestlog.Action) {
|
||||
switch {
|
||||
case want == "" && present:
|
||||
t.Errorf("log line has would_action %v, want none", got)
|
||||
case want != "" && got != string(want):
|
||||
case want != "" && got != want:
|
||||
t.Errorf("log line has would_action %v, want %s", got, want)
|
||||
}
|
||||
}
|
||||
+37
-2
@@ -6,6 +6,7 @@ package waf
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
@@ -82,8 +83,8 @@ type Params struct {
|
||||
// LookupFile is the lookup database they are looked up in while
|
||||
// SWW_WAF_LOOKUP_SOURCE is file, and nil otherwise.
|
||||
LookupFile *lookup.File
|
||||
// CoreRuleSet is what inspects requests while
|
||||
// SWW_WAF_CORE_RULE_SET_MODE is not off, and nil while it is.
|
||||
// CoreRuleSet is what NewCoreRuleSet returns: what inspects requests
|
||||
// while SWW_WAF_CORE_RULE_SET_MODE is not off, and nil while it is.
|
||||
CoreRuleSet CoreRuleSetInspector
|
||||
// Now tells the time by which requests are counted for the rate
|
||||
// limits, bans are made and run out, and GeoJS's answers are kept,
|
||||
@@ -173,6 +174,40 @@ func newHandler(params Params) *handler {
|
||||
}
|
||||
}
|
||||
|
||||
// NewCoreRuleSet returns the Core Rule Set at SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL,
|
||||
// without the rules SWW_WAF_CORE_RULE_SET_DISABLED_RULES switches off, reading bodies
|
||||
// up to SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE, with the exclusions in
|
||||
// SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE and the fixes of the apps in SWW_WAF_APPS,
|
||||
// or nil while SWW_WAF_CORE_RULE_SET_MODE is off. An exclusions file that
|
||||
// coreruleset.New refuses is an error in every mode. While the mode is off, a
|
||||
// set file is loaded with the Core Rule Set only to check it.
|
||||
func NewCoreRuleSet(cfg *config.WAF) (*coreruleset.CoreRuleSet, error) {
|
||||
if cfg.CoreRuleSetMode == config.CoreRuleSetModeOff &&
|
||||
cfg.CoreRuleSetExclusionsFile == "" {
|
||||
return nil, nil //nolint:nilnil // off is no Core Rule Set, and no error
|
||||
}
|
||||
|
||||
coreRuleSet, err := coreruleset.New(coreruleset.Params{
|
||||
ParanoiaLevel: cfg.CoreRuleSetParanoiaLevel,
|
||||
DisabledRules: cfg.CoreRuleSetDisabledRules,
|
||||
BodyInspectionSize: cfg.CoreRuleSetBodyInspectionSize,
|
||||
ExclusionsFile: cfg.CoreRuleSetExclusionsFile,
|
||||
Apps: cfg.Apps,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
if cfg.CoreRuleSetMode == config.CoreRuleSetModeOff {
|
||||
return nil, nil //nolint:nilnil // off is no Core Rule Set, and no error
|
||||
}
|
||||
|
||||
return coreRuleSet, nil
|
||||
}
|
||||
|
||||
// handler is waf's handler. It holds what every request shares; what
|
||||
// belongs to one request is in a request.
|
||||
type handler struct {
|
||||
|
||||
+20
-39
@@ -20,7 +20,6 @@ import (
|
||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||
"sneak.berlin/go/smallwebwaf/internal/config/configtest"
|
||||
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
|
||||
"sneak.berlin/go/smallwebwaf/internal/geojs"
|
||||
"sneak.berlin/go/smallwebwaf/internal/lookup"
|
||||
"sneak.berlin/go/smallwebwaf/internal/metrics"
|
||||
@@ -193,11 +192,11 @@ func (o *output) requestLine(t *testing.T) logLine {
|
||||
// endedLines waits for count requests that went through rproxy, or to waf's
|
||||
// own endpoints, to have ended, and returns the last log line of each, in the
|
||||
// order waf took them. A question's last line is the one its report writes.
|
||||
// It waits as long as it takes, as requestWithBody does.
|
||||
func (o *output) endedLines(t *testing.T, count int) []logLine {
|
||||
t.Helper()
|
||||
|
||||
for {
|
||||
deadline := time.Now().Add(waitLimit)
|
||||
for time.Now().Before(deadline) {
|
||||
found := o.lastLines(t)
|
||||
|
||||
ended := len(found) >= count
|
||||
@@ -213,6 +212,10 @@ func (o *output) endedLines(t *testing.T, count int) []logLine {
|
||||
|
||||
time.Sleep(pollInterval)
|
||||
}
|
||||
|
||||
t.Fatalf("fewer than %d requests have ended after %s", count, waitLimit)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// lastLines returns the last request log line written so far of each request,
|
||||
@@ -501,20 +504,24 @@ func newWAFWith(
|
||||
}
|
||||
}
|
||||
|
||||
coreRuleSet, err := waf.NewCoreRuleSet(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("Core Rule Set: %v", err)
|
||||
}
|
||||
|
||||
params := waf.Params{
|
||||
Config: cfg,
|
||||
RequestLog: out,
|
||||
ProcessLog: processLog,
|
||||
LookupFile: lookupFile,
|
||||
Now: now,
|
||||
Rules: ruleFiles,
|
||||
Alerts: alertQueue,
|
||||
Config: cfg,
|
||||
RequestLog: out,
|
||||
ProcessLog: processLog,
|
||||
LookupFile: lookupFile,
|
||||
CoreRuleSet: coreRuleSet,
|
||||
Now: now,
|
||||
Rules: ruleFiles,
|
||||
Alerts: alertQueue,
|
||||
// The tests of the limit set it back, and hold the Core Rule Set
|
||||
// with a stand-in.
|
||||
BesideLookupTimeLimit: time.Hour,
|
||||
}
|
||||
|
||||
addCoreRuleSet(t, ¶ms)
|
||||
addCounters(¶ms)
|
||||
addOutsideSources(¶ms, geojsURL, abuseIPDBCheckURL)
|
||||
|
||||
@@ -523,30 +530,6 @@ func newWAFWith(
|
||||
return server, out, alertQueue
|
||||
}
|
||||
|
||||
// addCoreRuleSet adds to params the Core Rule Set, with the settings for it,
|
||||
// while SWW_WAF_CORE_RULE_SET_MODE is not off.
|
||||
func addCoreRuleSet(t *testing.T, params *waf.Params) {
|
||||
t.Helper()
|
||||
|
||||
cfg := params.Config
|
||||
if cfg.CoreRuleSetMode == config.CoreRuleSetModeOff {
|
||||
return
|
||||
}
|
||||
|
||||
coreRuleSet, err := coreruleset.New(coreruleset.Params{
|
||||
ParanoiaLevel: cfg.CoreRuleSetParanoiaLevel,
|
||||
DisabledRules: cfg.CoreRuleSetDisabledRules,
|
||||
BodyInspectionSize: cfg.CoreRuleSetBodyInspectionSize,
|
||||
ExclusionsFile: cfg.CoreRuleSetExclusionsFile,
|
||||
Apps: cfg.Apps,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Core Rule Set: %v", err)
|
||||
}
|
||||
|
||||
params.CoreRuleSet = coreRuleSet
|
||||
}
|
||||
|
||||
// addCounters adds to params the metrics, the table of clients, the ledger
|
||||
// and the anomaly counters, as sww builds them from params' settings. The
|
||||
// metrics read the ledger, the table of clients and the rules.
|
||||
@@ -790,9 +773,7 @@ func readResponse(t *testing.T, conn net.Conn) answer {
|
||||
}
|
||||
|
||||
// wantLine checks the request log line's status and action.
|
||||
func wantLine(
|
||||
t *testing.T, line logLine, status int, action requestlog.Action,
|
||||
) {
|
||||
func wantLine(t *testing.T, line logLine, status int, action string) {
|
||||
t.Helper()
|
||||
|
||||
if line.Status != status || line.Action != action {
|
||||
|
||||
@@ -121,7 +121,7 @@ func TestIPv6GroupPrefixSetsTheClientTheLimitsCount(t *testing.T) {
|
||||
// status and action are those of the request that breaks the
|
||||
// limit: a rate limit refuses it, a byte limit passes it on.
|
||||
status int
|
||||
action requestlog.Action
|
||||
action string
|
||||
}{
|
||||
{rateLimitPerMinute, "1", http.StatusForbidden, requestlog.ActionClientIsRateLimited},
|
||||
{bytesLimitPerMinute, byteLimit, http.StatusOK, requestlog.ActionForward},
|
||||
|
||||
@@ -55,14 +55,6 @@ func (refusalLimit) check(
|
||||
case requestlog.ActionTemporarilyBanned,
|
||||
requestlog.ActionPermanentlyBanned:
|
||||
status = cfg.BanResponse
|
||||
case requestlog.ActionForward,
|
||||
requestlog.ActionRequestLineTooLong,
|
||||
requestlog.ActionRequestHeaderTooLarge,
|
||||
requestlog.ActionClientIsRateLimited,
|
||||
requestlog.ActionClientDenied,
|
||||
requestlog.ActionCountryDenied,
|
||||
requestlog.ActionChecksTimedOut,
|
||||
requestlog.ActionOwnEndpoint:
|
||||
}
|
||||
|
||||
rq.banForLimit(now, hit, status)
|
||||
|
||||
@@ -74,7 +74,7 @@ func TestRefusalLimitBreaksAtOneOverTheLimit(t *testing.T) {
|
||||
refusals []refused
|
||||
// nextAction is the action of the client's next request: the next
|
||||
// request after a ban for a ban rule makes it permanent.
|
||||
nextAction requestlog.Action
|
||||
nextAction string
|
||||
}{
|
||||
{
|
||||
"block rule",
|
||||
|
||||
@@ -7,6 +7,10 @@ import (
|
||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||
)
|
||||
|
||||
// deny is the SWW_WAF_BLOCKLIST_ACTION and the SWW_WAF_DNSBL_ABUSEIPDB_ACTION that
|
||||
// refuses the requests of a client a source lists.
|
||||
const deny = "deny"
|
||||
|
||||
// noteListed notes each of sources, the URLs of the blocklists or the
|
||||
// DNSBL zones, their keys masked, that list the client, as noteHit does,
|
||||
// with reason.
|
||||
|
||||
@@ -58,22 +58,22 @@ func TestEachBlocklistActionForAListedAddressAndAListedNetblock(
|
||||
// statuses and actions are those of a listed client's three
|
||||
// requests, and percent their rate_limit_percent, as percentText gives it.
|
||||
statuses []int
|
||||
actions []requestlog.Action
|
||||
actions []string
|
||||
percent string
|
||||
}{
|
||||
{
|
||||
actionDeny, []int{http.StatusForbidden, http.StatusForbidden, http.StatusForbidden},
|
||||
[]requestlog.Action{denied, denied, denied}, none,
|
||||
[]string{denied, denied, denied}, none,
|
||||
},
|
||||
{
|
||||
// Half of 4 requests a minute: the third breaks the limit.
|
||||
limitHalf, []int{http.StatusOK, http.StatusOK, http.StatusForbidden},
|
||||
[]requestlog.Action{forward, forward, requestlog.ActionClientIsRateLimited},
|
||||
[]string{forward, forward, requestlog.ActionClientIsRateLimited},
|
||||
"50 from " + blocklistAction,
|
||||
},
|
||||
{
|
||||
actionLog, []int{http.StatusOK, http.StatusOK, http.StatusOK},
|
||||
[]requestlog.Action{forward, forward, forward}, none,
|
||||
[]string{forward, forward, forward}, none,
|
||||
},
|
||||
} {
|
||||
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
|
||||
// requests, and percent their rate_limit_percent, as percentText gives it.
|
||||
statuses []int
|
||||
actions []requestlog.Action
|
||||
actions []string
|
||||
percent string
|
||||
}{
|
||||
{
|
||||
actionDeny, []int{http.StatusForbidden, http.StatusForbidden, http.StatusForbidden},
|
||||
[]requestlog.Action{denied, denied, denied}, none,
|
||||
[]string{denied, denied, denied}, none,
|
||||
},
|
||||
{
|
||||
// Half of 4 requests a minute: the third breaks the limit.
|
||||
limitHalf, []int{http.StatusOK, http.StatusOK, http.StatusForbidden},
|
||||
[]requestlog.Action{forward, forward, requestlog.ActionClientIsRateLimited},
|
||||
[]string{forward, forward, requestlog.ActionClientIsRateLimited},
|
||||
"50 from " + dnsblAbuseIPDBAction,
|
||||
},
|
||||
{
|
||||
actionLog, []int{http.StatusOK, http.StatusOK, http.StatusOK},
|
||||
[]requestlog.Action{forward, forward, forward}, none,
|
||||
[]string{forward, forward, forward}, none,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.action, func(t *testing.T) {
|
||||
@@ -954,7 +954,7 @@ func TestClientRefusedForAnOffenceIsCheckedWithAbuseIPDBAtItsNextRequest(
|
||||
// history then counts.
|
||||
path string
|
||||
status int
|
||||
action requestlog.Action
|
||||
action string
|
||||
want ratelimit.Offences
|
||||
}{
|
||||
{
|
||||
@@ -1018,22 +1018,22 @@ func TestEachDNSBLAbuseIPDBActionForAClientAbuseIPDBScoresAtOrOverTheMinimum(
|
||||
// statuses and actions are those of fromDE's three requests, and
|
||||
// percent their rate_limit_percent, as percentText gives it.
|
||||
statuses []int
|
||||
actions []requestlog.Action
|
||||
actions []string
|
||||
percent string
|
||||
}{
|
||||
{
|
||||
actionDeny, []int{http.StatusForbidden, http.StatusForbidden, http.StatusForbidden},
|
||||
[]requestlog.Action{denied, denied, denied}, none,
|
||||
[]string{denied, denied, denied}, none,
|
||||
},
|
||||
{
|
||||
// Half of 4 requests a minute: the third breaks the limit.
|
||||
limitHalf, []int{http.StatusOK, http.StatusOK, http.StatusForbidden},
|
||||
[]requestlog.Action{forward, forward, requestlog.ActionClientIsRateLimited},
|
||||
[]string{forward, forward, requestlog.ActionClientIsRateLimited},
|
||||
"50 from " + dnsblAbuseIPDBAction,
|
||||
},
|
||||
{
|
||||
actionLog, []int{http.StatusOK, http.StatusOK, http.StatusOK},
|
||||
[]requestlog.Action{forward, forward, forward}, none,
|
||||
[]string{forward, forward, forward}, none,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.action, func(t *testing.T) {
|
||||
|
||||
+2
-12
@@ -21,7 +21,7 @@ import (
|
||||
// to close its connection without an answer, and the action the log line names.
|
||||
type refusal struct {
|
||||
status int
|
||||
action requestlog.Action
|
||||
action string
|
||||
}
|
||||
|
||||
// decision is what the checks note of a request: its log line, and what
|
||||
@@ -345,16 +345,6 @@ func timing(start, end time.Time) *float64 {
|
||||
return new(requestlog.Milliseconds(end.Sub(start)))
|
||||
}
|
||||
|
||||
// banAndCount runs list, afterAnswer or onReport, whose heuristics only ban.
|
||||
// It then counts the request for the anomaly thresholds and adds it to its
|
||||
// client's history. list runs first, so that a refusal limit it finds broken
|
||||
// is in that history. ctx is the request's own context.
|
||||
func (rq *request) banAndCount(ctx context.Context, list []heuristic) {
|
||||
rq.runBanningHeuristics(ctx, list)
|
||||
rq.countAnomalies()
|
||||
rq.addToHistory()
|
||||
}
|
||||
|
||||
// addToHistory adds the request, which has ended, to its client's
|
||||
// history, and counts its offences in the metrics, and then the lookup's
|
||||
// answer about the client, as answerAtTheEnd gives it, to that history and
|
||||
@@ -431,7 +421,7 @@ func (rq *request) answerAtTheEnd() (geojs.Answer, bool) {
|
||||
return geojs.Answer{}, false
|
||||
}
|
||||
|
||||
if rq.h.config.LookupSource == config.LookupSourceFile {
|
||||
if rq.h.config.LookupSource == "file" {
|
||||
return rq.lookupAnswer, true
|
||||
}
|
||||
|
||||
|
||||
@@ -66,10 +66,9 @@ func (ruleFiles) check(
|
||||
),
|
||||
figures: figures,
|
||||
}
|
||||
case rules.ActionLog:
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// refusedByRule reports whether ruleFiles refuses a request that matched
|
||||
|
||||
@@ -184,7 +184,7 @@ func TestRateLimitExemptNetsAreNeitherCountedNorRefused(t *testing.T) {
|
||||
type sentRequest struct {
|
||||
client string
|
||||
status int
|
||||
action requestlog.Action
|
||||
action string
|
||||
}
|
||||
|
||||
// wantAnswers sends requests to sww at addr one after another and
|
||||
|
||||
+34
-31
@@ -32,22 +32,18 @@ const (
|
||||
DispositionHeader = "X-Sww-Disposition"
|
||||
)
|
||||
|
||||
// Disposition is what waf tells rproxy to do with a request, in
|
||||
// DispositionHeader.
|
||||
type Disposition string
|
||||
|
||||
// The values of DispositionHeader.
|
||||
const (
|
||||
// DispositionAllow tells rproxy to pass the request to the app.
|
||||
DispositionAllow Disposition = "allow"
|
||||
DispositionAllow = "allow"
|
||||
// DispositionDeny tells rproxy to give the visitor the answer's status,
|
||||
// headers and body.
|
||||
DispositionDeny Disposition = "deny"
|
||||
DispositionDeny = "deny"
|
||||
// DispositionClose tells rproxy to close the visitor's connection
|
||||
// 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 being down, and rproxy would then let the request through.
|
||||
DispositionClose Disposition = "close"
|
||||
DispositionClose = "close"
|
||||
)
|
||||
|
||||
// ReportPath is where rproxy sends the report of each request waf
|
||||
@@ -181,13 +177,11 @@ func (h *handler) answerQuestion(w http.ResponseWriter, r *http.Request) {
|
||||
peer.Unmap(),
|
||||
true,
|
||||
)
|
||||
// These run even when a check panics. finish runs first, so that what is
|
||||
// held has the finished log line. Go's server sends the answer only once
|
||||
// this function has returned, so the report cannot come before it is held.
|
||||
defer func() {
|
||||
rq.finish()
|
||||
rq.holdForReport()
|
||||
}()
|
||||
// The last deferred runs first, so that what is held has the finished log
|
||||
// line. Go's server sends the answer only once this function has
|
||||
// returned, so the report cannot come before it is held.
|
||||
defer rq.holdForReport()
|
||||
defer rq.finish()
|
||||
|
||||
rq.site = r.Host
|
||||
rq.line.WAFLine = requestlog.WAFLineDecision
|
||||
@@ -196,28 +190,35 @@ func (h *handler) answerQuestion(w http.ResponseWriter, r *http.Request) {
|
||||
underOwnPrefix := strings.HasPrefix(r.URL.Path, ownEndpointPrefix)
|
||||
|
||||
if refused == nil && !underOwnPrefix {
|
||||
rq.allow()
|
||||
// In observe mode, what enforce mode would have refused counts here.
|
||||
rq.runBanningHeuristics(r.Context(), afterAnswer())
|
||||
defer rq.runAfterAnswer(r.Context())
|
||||
|
||||
rq.allow()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// The last deferred runs first: runAfterAnswer before addToHistory, so that
|
||||
// a broken refusal limit is in the client's history.
|
||||
defer rq.addToHistory()
|
||||
defer rq.countAnomalies()
|
||||
defer rq.runAfterAnswer(r.Context())
|
||||
|
||||
if refused != nil {
|
||||
rq.deny(*refused)
|
||||
} else {
|
||||
// The app never sees this prefix. waf's own endpoints are at its
|
||||
// own address, so this answers as a path with no endpoint does.
|
||||
rq.line.Action = requestlog.ActionOwnEndpoint
|
||||
rq.out.Header().Set(DispositionHeader, string(DispositionDeny))
|
||||
http.Error(
|
||||
rq.out,
|
||||
http.StatusText(http.StatusNotFound),
|
||||
http.StatusNotFound,
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
rq.banAndCount(r.Context(), afterAnswer())
|
||||
// The app never sees this prefix. waf's own endpoints are at its
|
||||
// own address, so this answers as a path with no endpoint does.
|
||||
rq.line.Action = requestlog.ActionOwnEndpoint
|
||||
rq.out.Header().Set(DispositionHeader, DispositionDeny)
|
||||
http.Error(
|
||||
rq.out,
|
||||
http.StatusText(http.StatusNotFound),
|
||||
http.StatusNotFound,
|
||||
)
|
||||
}
|
||||
|
||||
// readQuestion returns the visitor's address and the body a question carries,
|
||||
@@ -324,7 +325,7 @@ func (rq *request) allow() {
|
||||
rq.allowed = true
|
||||
|
||||
header := rq.out.Header()
|
||||
header.Set(DispositionHeader, string(DispositionAllow))
|
||||
header.Set(DispositionHeader, DispositionAllow)
|
||||
|
||||
if rq.h.config.AddLookupHeaders {
|
||||
setLookupHeaders(header, rq.line.ASN, rq.line.Country)
|
||||
@@ -347,13 +348,13 @@ func (rq *request) deny(r refusal) {
|
||||
if r.status == 0 {
|
||||
// SWW_WAF_BAN_RESPONSE is close. Written past rq.out, since the log line's
|
||||
// status stays 0: the visitor is sent nothing.
|
||||
rq.out.Header().Set(DispositionHeader, string(DispositionClose))
|
||||
rq.out.Header().Set(DispositionHeader, DispositionClose)
|
||||
rq.out.ResponseWriter.WriteHeader(http.StatusOK)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
rq.out.Header().Set(DispositionHeader, string(DispositionDeny))
|
||||
rq.out.Header().Set(DispositionHeader, DispositionDeny)
|
||||
http.Error(rq.out, http.StatusText(r.status), r.status)
|
||||
}
|
||||
|
||||
@@ -490,7 +491,9 @@ func (h *handler) finishWith(
|
||||
if rq.allowed {
|
||||
rq.line.DurationUpstreamTotalMS = &report.DurationUpstreamTotalMS
|
||||
|
||||
rq.banAndCount(ctx, onReport())
|
||||
rq.runBanningHeuristics(ctx, onReport())
|
||||
rq.countAnomalies()
|
||||
rq.addToHistory()
|
||||
|
||||
rq.h.metrics.RequestReported(
|
||||
&rq.line,
|
||||
|
||||
@@ -205,13 +205,13 @@ func wantDisposition(
|
||||
t *testing.T,
|
||||
got answer,
|
||||
status int,
|
||||
disposition waf.Disposition,
|
||||
disposition string,
|
||||
) {
|
||||
t.Helper()
|
||||
wantStatus(t, got, status)
|
||||
|
||||
if answered := got.header.Get(waf.DispositionHeader); answered !=
|
||||
string(disposition) {
|
||||
disposition {
|
||||
t.Errorf(
|
||||
"answered with disposition %q, want %q",
|
||||
answered,
|
||||
@@ -1480,7 +1480,7 @@ func TestEveryDeniedRequestsReportGetsItsLastLine(t *testing.T) {
|
||||
env map[string]string
|
||||
path string
|
||||
status int
|
||||
action requestlog.Action
|
||||
action string
|
||||
}{
|
||||
{
|
||||
"deny", map[string]string{denyNets: client}, "/", http.StatusForbidden,
|
||||
|
||||
Reference in new issue
Block a user