1 Commits
Author SHA1 Message Date
clawbot 5fd4c961a2 smallwebwaf starts with SWWAF_STATE_DIR given as a file on a new volume (closes #137)
check / check (push) Canceled after 0s
The run script gave ${SWWAF_STATE_DIR:-/var/lib/smallwebwaf} to the
smallwebwaf user and never read SWWAF_STATE_DIR_FILE, so a new volume
at the directory that file names stayed root's. The run script now
takes the setting from the variable or the file, and changes the owner
only when it is the absolute path of a directory that exists;
otherwise it starts smallwebwaf, which stops with its message naming
the setting. A chown that fails, as on a read-only volume, no longer
ends the run script. script/example-app, and its entry in README.md,
gain two steps: a container on such a volume becomes healthy, and a
missing state directory reaches smallwebwaf's message.

Model: opus-5-5
2026-10-20 14:27:11 +00:00
7 changed files with 52 additions and 171 deletions
+33 -38
View File
@@ -113,16 +113,16 @@ in `bin/state` unless `SWWAF_STATE_DIR` is set, and the default rule file of
`X-Forwarded-Proto`, and `X-Forwarded-For` with the peer added at the end. It `X-Forwarded-Proto`, and `X-Forwarded-For` with the peer added at the end. It
also gets the request's id in `X-Request-ID`, the same id as in the request's also gets the request's id in `X-Request-ID`, the same id as in the request's
log line (see `request_id` in "Request log" below). log line (see `request_id` in "Request log" below).
- Enforces the timeouts and the size limits below. Once a request's line and - Enforces the timeouts and the size limits below. A limit passed before the
headers have been read, a limit passed before the response has started gets response has started gets `smallwebwaf`'s own answer: `408` for a client too
`smallwebwaf`'s own answer: `408` for a client too slow to send its body, slow to send its request, `413` for a request body that is too large, `504`
`413` for a request body that is too large, `504` for an app too slow to for an app too slow to answer, and `502` for a response that is too large or
answer, and `502` for a response that is too large or an app that cannot be an app that cannot be reached. A request that announces a body over the limit
reached. A request that announces a body over the limit is refused before is refused before anything reaches the app. While a request body is still on
anything reaches the app. While a request body is still on its way, a request its way, a request timeout that runs out answers `408` if `smallwebwaf` was
timeout that runs out answers `408` if `smallwebwaf` was waiting for the waiting for the client to send more, and `504` if it was waiting for the app
client to send more, and `504` if it was waiting for the app to take what it to take what it had. Once the response has started, a limit can only cut the
had. Once the response has started, a limit can only cut the connection. connection.
- Counts each client's requests over a minute, an hour and a day. A request that - Counts each client's requests over a minute, an hour and a day. A request that
takes the client over one of the rate limits below is refused with takes the client over one of the rate limits below is refused with
`SWWAF_BAN_RESPONSE`, `403` by default, before anything reaches the app, and `SWWAF_BAN_RESPONSE`, `403` by default, before anything reaches the app, and
@@ -1045,11 +1045,9 @@ less severe than `SWWAF_LOG_LEVEL` are not written.
Go's HTTP server, on which `smallwebwaf` is built, reads a request's line and Go's HTTP server, on which `smallwebwaf` is built, reads a request's line and
headers before `smallwebwaf` sees the request, and some requests end there, headers before `smallwebwaf` sees the request, and some requests end there,
without a line in the log: headers over `SWWAF_CLIENT_REQUEST_HEADER_MAX_BYTES`, without a line in the log: headers over `SWWAF_CLIENT_REQUEST_HEADER_MAX_BYTES`,
which it answers `431`, and requests it cannot read at all, which it answers which it answers `431`, headers slower than `SWWAF_CLIENT_REQUEST_TIMEOUT`,
itself, mostly with `400`. A client too slow to send its request line and whose connection it closes without an answer, and requests it cannot read at
headers within `SWWAF_CLIENT_REQUEST_TIMEOUT` gets either no answer or an error all, which it answers itself, mostly with `400`.
answer from the server, mostly `400 Bad Request`; either way the server closes
its connection, nothing reaches the app, and no line is written in the log.
### Sending the log to a syslog server ### Sending the log to a syslog server
@@ -1449,12 +1447,11 @@ whole service, its `asn`, for an AS number, its `name`, for a named netblock, or
the `start` of a window in which it has requests or bytes; a list's `url`, the `start` of a window in which it has requests or bytes; a list's `url`,
`fetched` or `lines`, which is `[]` for an empty list; a verdict's `zone`, `fetched` or `lines`, which is `[]` for an empty list; a verdict's `zone`,
`client`, `listed`, which is `false` for a client the zone does not list, or `client`, `listed`, which is `false` for a client the zone does not list, or
`fetched`. So does a ban whose `expires` is not after its `start` or whose `fetched`. So does a ban whose `cause` is not `limit`, `attack`, `admin` or
`cause` is not `limit`, `attack`, `admin` or `crowdsec`, alerts waiting for a `crowdsec`, alerts waiting for a destination that is not `webhook`, `slack` or
destination that is not `webhook`, `slack` or `ntfy`, an anomaly counter whose `ntfy`, an anomaly counter whose `scope` is not `client`, `net`, `asn`, `total`
`scope` is not `client`, `net`, `asn`, `total` or `watch`, and a copy of a list or `watch`, and a copy of a list with a line that would make its fetch fail. An
with a line that would make its fetch fail. An answer's `asn` or `as_name` left answer's `asn` or `as_name` left out reads as empty.
out reads as empty.
While it runs, `smallwebwaf` watches `SWWAF_STATE_DIR` and takes in your edit of While it runs, `smallwebwaf` watches `SWWAF_STATE_DIR` and takes in your edit of
a state file as soon as you save it: what the file then holds replaces what a state file as soon as you save it: what the file then holds replaces what
@@ -1464,15 +1461,14 @@ writes a file it takes in any edit made since, so your edit is not overwritten;
a change `smallwebwaf` made after you opened the file, such as a new ban, is a change `smallwebwaf` made after you opened the file, such as a new ban, is
lost when you save over it. An edit that would stop the start, because it does lost when you save over it. An edit that would stop the start, because it does
not parse, has another `version`, leaves out a field an entry needs, gives a ban not parse, has another `version`, leaves out a field an entry needs, gives a ban
an `expires` not after its `start` or another `cause`, names another another `cause`, names another destination, gives an anomaly counter another
destination, gives an anomaly counter another `scope` or gives a list's copy a `scope` or gives a list's copy a line that would make its fetch fail, does not
line that would make its fetch fail, does not stop the running `smallwebwaf`: it stop the running `smallwebwaf`: it keeps what it holds, and at the file's next
keeps what it holds, and at the file's next write renames your file to write renames your file to `<name>.bad`, such as `bans.json.bad`, writes the
`<name>.bad`, such as `bans.json.bad`, writes the file again from memory, logs file again from memory, logs the file and where the error is, and raises a
the file and where the error is, and raises a `file_error` alert for it. It `file_error` alert for it. It waits for that write because an editor's file can
waits for that write because an editor's file can be read before the editor has be read before the editor has finished writing it. Mend the `.bad` file and move
finished writing it. Mend the `.bad` file and move it back. A file you remove is it back. A file you remove is written again at its next write.
written again at its next write.
To ban a netblock, add an entry to `bans.json` with its `netblock`, its `start` To ban a netblock, add an entry to `bans.json` with its `netblock`, its `start`
and its `expires`, `null` for a ban that never ends; its `reason` and its and its `expires`, `null` for a ban that never ends; its `reason` and its
@@ -2160,14 +2156,13 @@ addresses sends, so that one client costs at most one check every
`SWWAF_REPUTATION_CACHE_TTL`. `SWWAF_REPUTATION_CACHE_TTL`.
Only a client whose history counts an offence is checked, so that the checks are Only a client whose history counts an offence is checked, so that the checks are
spent on suspects: so far, one that has broken a rate limit, a byte limit or the spent on suspects: so far, one that has broken a rate limit or a byte limit,
error burst, matched a ban rule, asked for a trap path, or had a request refused matched a ban rule, or had a request refused by a block rule or the Core Rule
by a block rule, by the Core Rule Set or for a missing or wrong token. A client Set. A client dropped from the table of clients loses its history, and with it
dropped from the table of clients loses its history, and with it its offences. A its offences. A client is checked in the background, at its first request after
client is checked in the background, at its first request after its offence that its offence that reaches the check: no request waits, a request refused under
reaches the check: no request waits, a request refused under its ban is not its ban is not checked, and the request that has it checked, and any other from
checked, and the request that has it checked, and any other from it before the it before the answer comes, goes on as from a client without a score.
answer comes, goes on as from a client without a score.
A score at or over `SWWAF_ABUSEIPDB_MIN_SCORE`, 75 by default, is a hit, and A score at or over `SWWAF_ABUSEIPDB_MIN_SCORE`, 75 by default, is a hit, and
`SWWAF_REPUTATION_ACTION` says what is done with its client, as for a DNSBL `SWWAF_REPUTATION_ACTION` says what is done with its client, as for a DNSBL
+17 -19
View File
@@ -225,9 +225,9 @@ address or one /64, so a permanent ban does not reach neighbours who did
nothing. nothing.
An offence is a request `smallwebwaf` holds against the client: one that carries An offence is a request `smallwebwaf` holds against the client: one that carries
a clear sign of attack, breaks a limit, or is refused by a rule file, by the a clear sign of attack, breaks a limit, or is refused by a rule file or the Core
Core Rule Set or for a missing or wrong token. Offences are counted by kind in Rule Set. Offences are counted by kind in the client's history. Two kinds lead
the client's history. Two kinds lead to a ban, each by its own rule: to a ban, each by its own rule:
- A clear sign of attack: a request that matches a rule file rule whose action - A clear sign of attack: a request that matches a rule file rule whose action
is `ban`, or asks for a path in `SWWAF_TRAP_PATHS`. Examples are a probe for a is `ban`, or asks for a path in `SWWAF_TRAP_PATHS`. Examples are a probe for a
@@ -414,10 +414,10 @@ The settings, by group:
to send its request line and headers, and then, from the end of the to send its request line and headers, and then, from the end of the
headers, its body. headers, its body.
- `SWWAF_CLIENT_REQUEST_HEADER_MAX_BYTES` (default `32K`): the largest - `SWWAF_CLIENT_REQUEST_HEADER_MAX_BYTES` (default `32K`): the largest
request line and headers a client may send. Over it, Go's HTTP server request line and headers a client may send. Over it, `smallwebwaf` answers
answers `431` and closes the connection, and nothing reaches the app. It `431` and closes the connection, and nothing reaches the app. It must be
must be more than `4K`, and cannot be `off`: Go's HTTP server always has more than `4K`, and cannot be `off`: Go's HTTP server always has such a
such a limit, and reads 4 KiB past the one it is given before it refuses. limit, and reads 4 KiB past the one it is given before it refuses.
- `SWWAF_CLIENT_IDLE_TIMEOUT` (default `120s`): how long a kept-open - `SWWAF_CLIENT_IDLE_TIMEOUT` (default `120s`): how long a kept-open
connection may wait for its next request before `smallwebwaf` closes it. connection may wait for its next request before `smallwebwaf` closes it.
It is longer than the 90 seconds after which traefik, by default, closes a It is longer than the 90 seconds after which traefik, by default, closes a
@@ -435,13 +435,12 @@ The settings, by group:
the client sends it and the app receives it. the client sends it and the app receives it.
- `SWWAF_RESPONSE_MAX_BYTES` (default `5G`): the largest response body, as - `SWWAF_RESPONSE_MAX_BYTES` (default `5G`): the largest response body, as
the app sends it and the client receives it. the app sends it and the client receives it.
- When a limit is passed after a request's line and headers have been read, - When a limit is passed before the response has started, `smallwebwaf`
but before the response has started, `smallwebwaf` answers itself: `413` answers itself: `413` for a request body that is too large, `408` for a
for a request body that is too large, `408` for a client too slow to send client that is too slow, `502` for a response that is too large, `504` for
its body, `502` for a response that is too large, `504` for an app that is an app that is too slow. A request that announces a body larger than its
too slow. A request that announces a body larger than its limit is refused limit is refused before anything reaches the app. Once the response has
before anything reaches the app. Once the response has started it can only started it can only be cut off, and the connection is closed.
be cut off, and the connection is closed.
- Since a request body streams through, each side can hold up the other: a - Since a request body streams through, each side can hold up the other: a
slow client slows the send to the app, and an app slow to take the body slow client slows the send to the app, and an app slow to take the body
slows the client's send. So while a request body is still on its way, a slows the client's send. So while a request body is still on its way, a
@@ -451,11 +450,10 @@ The settings, by group:
waiting for the app to take what it had. waiting for the app to take what it had.
- Go's HTTP server, on which `smallwebwaf` is built, reads a request's line - Go's HTTP server, on which `smallwebwaf` is built, reads a request's line
and headers before `smallwebwaf` sees the request. A client that takes and headers before `smallwebwaf` sees the request. A client that takes
longer than `SWWAF_CLIENT_REQUEST_TIMEOUT` to send them gets either no longer than `SWWAF_CLIENT_REQUEST_TIMEOUT` to send them gets no answer:
answer or an error answer from the server, mostly `400 Bad Request`; the server closes its connection. Headers over
either way the server closes its connection and nothing reaches the app. `SWWAF_CLIENT_REQUEST_HEADER_MAX_BYTES` are answered `431` by the server
Headers over `SWWAF_CLIENT_REQUEST_HEADER_MAX_BYTES` are answered `431` by itself. Neither request gets a line in the request log.
the server itself. Neither request gets a line in the request log.
- A WebSocket connection leaves these limits behind once it is upgraded: it - A WebSocket connection leaves these limits behind once it is upgraded: it
stays open until either side closes it. stays open until either side closes it.
- Lookup of AS number and country (R7). On by default through GeoJS, which needs - Lookup of AS number and country (R7). On by default through GeoJS, which needs
-30
View File
@@ -192,36 +192,6 @@ func wantRequestFields(t *testing.T, line logLine, host string, sent, received i
} }
} }
func TestAddsNoContentTypeTheAppDidNotSend(t *testing.T) {
t.Parallel()
const typed = "text/plain"
app := startApp(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/typed" {
w.Header().Set("Content-Type", typed)
} else {
// The key with no value keeps Go's server from adding one.
w.Header()["Content-Type"] = nil
}
_, _ = io.WriteString(w, "<!doctype html><title>page</title>")
})
addr, _ := startProxy(t, app.URL, nil)
got := get(t, addr, "/")
if got.header.Values("Content-Type") != nil {
t.Errorf("client got Content-Type %q, want none",
got.header.Get("Content-Type"))
}
got = get(t, addr, "/typed")
if got.header.Get("Content-Type") != typed {
t.Errorf("client got Content-Type %q, want %q",
got.header.Get("Content-Type"), typed)
}
}
func TestStreamsTheRequestBody(t *testing.T) { func TestStreamsTheRequestBody(t *testing.T) {
t.Parallel() t.Parallel()
-7
View File
@@ -431,13 +431,6 @@ func (rq *request) modifyResponse(res *http.Response) error {
return errResponseTooLarge return errResponseTooLarge
} }
// Go's server adds a Content-Type it guesses from the body to a
// response whose headers have no Content-Type key. The key with no
// value sends none, as the app sent none.
if res.Header.Get("Content-Type") == "" {
rq.out.Header()["Content-Type"] = nil
}
res.Body = &responseBody{body: limitBody(res.Body, maxBytes), rq: rq} res.Body = &responseBody{body: limitBody(res.Body, maxBytes), rq: rq}
rq.startClientResponseTimeout() rq.startClientResponseTimeout()
-55
View File
@@ -1,55 +0,0 @@
package proxy
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"sneak.berlin/go/smallwebwaf/internal/config"
)
func TestAnAnswerWithoutAContentTypeGetsNone(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, _ *http.Request) {
rq := &request{
h: &handler{config: &config.Config{}},
out: &responseWriter{ResponseWriter: w},
}
app := &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{},
Body: http.NoBody,
}
err := rq.modifyResponse(app)
if err != nil {
t.Errorf("modifyResponse: %v", err)
}
// The headers go out with this, the first part of the body,
// which Go's server would guess a Content-Type from.
_, _ = io.WriteString(w, "<!doctype html><title>page</title>")
}))
t.Cleanup(srv.Close)
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, srv.URL,
http.NoBody)
if err != nil {
t.Fatalf("new request: %v", err)
}
res, err := srv.Client().Do(req)
if err != nil {
t.Fatalf("get: %v", err)
}
_ = res.Body.Close()
if res.Header.Values("Content-Type") != nil {
t.Errorf("client got Content-Type %q, want none",
res.Header.Get("Content-Type"))
}
}
+2 -7
View File
@@ -61,7 +61,6 @@ var (
// errMissing is for an entry without a field it needs. // errMissing is for an entry without a field it needs.
errMissing = errors.New("has no") errMissing = errors.New("has no")
errCause = errors.New("is not limit, attack, admin or crowdsec") errCause = errors.New("is not limit, attack, admin or crowdsec")
errExpires = errors.New("is not after its start")
errDestination = errors.New("is not webhook, slack or ntfy") errDestination = errors.New("is not webhook, slack or ntfy")
errScope = errors.New("is not client, net, asn, total or watch") errScope = errors.New("is not client, net, asn, total or watch")
errWaitingList = errors.New(`waiting is a list, but now lists the alerts by ` + errWaitingList = errors.New(`waiting is a list, but now lists the alerts by ` +
@@ -647,10 +646,8 @@ func (e BanEntry) ban() bans.Ban {
// client, a start, from which the length of the netblock's next ban is // client, a start, from which the length of the netblock's next ban is
// worked out, or an expires, which would make it permanent. A permanent // worked out, or an expires, which would make it permanent. A permanent
// ban's expires is null, which Bans cannot tell from a missing one, so // ban's expires is null, which Bans cannot tell from a missing one, so
// each expires is read again as written. An expires not after the start // each expires is read again as written. A cause other than limit,
// is refused too, since the netblock's next ban for a broken limit, three // attack, admin or crowdsec, most likely misspelt, is refused too.
// times as long as this one, would then refuse nothing; and so is a cause
// other than limit, attack, admin or crowdsec, most likely misspelt.
func (f *bansFile) check(data []byte) error { func (f *bansFile) check(data []byte) error {
var written struct { var written struct {
Bans []struct { Bans []struct {
@@ -671,8 +668,6 @@ func (f *bansFile) check(data []byte) error {
return missing(i, "start") return missing(i, "start")
case written.Bans[i].Expires == nil: case written.Bans[i].Expires == nil:
return missing(i, "expires") return missing(i, "expires")
case entry.Expires != nil && !entry.Expires.After(entry.Start):
return fmt.Errorf("entry %d's expires %w", i+1, errExpires)
case entry.Cause != "" && entry.Cause != bans.CauseLimit && case entry.Cause != "" && entry.Cause != bans.CauseLimit &&
entry.Cause != bans.CauseAttack && entry.Cause != bans.CauseAdmin && entry.Cause != bans.CauseAttack && entry.Cause != bans.CauseAdmin &&
entry.Cause != bans.CauseCrowdSec: entry.Cause != bans.CauseCrowdSec:
-15
View File
@@ -865,21 +865,6 @@ func TestBanWithAnotherCauseStopsTheStart(t *testing.T) {
`: entry 4's cause "atack" is not limit, attack, admin or crowdsec`) `: entry 4's cause "atack" is not limit, attack, admin or crowdsec`)
} }
func TestBanThatDoesNotEndAfterItsStartStopsTheStart(t *testing.T) {
t.Parallel()
// The first ban ends a second after its start; the second ends as it
// starts, or an hour before.
for _, expires := range []string{`"2026-10-06T00:00:00Z"`, `"2026-10-05T23:00:00Z"`} {
wantRefused(t, bansJSON, `{"version": 1, "bans": [`+
`{"netblock": "203.0.113.9/32", "start": "2026-10-06T00:00:00Z", `+
`"expires": "2026-10-06T00:00:01Z"}, `+
`{"netblock": "203.0.113.10/32", "start": "2026-10-06T00:00:00Z", `+
`"expires": `+expires+`}]}`,
`: entry 2's expires is not after its start`)
}
}
func TestUnknownVersionStopsTheStart(t *testing.T) { func TestUnknownVersionStopsTheStart(t *testing.T) {
t.Parallel() t.Parallel()