Compare commits

..
Author SHA1 Message Date
clawbot 01a8a7aa08 waf: one Cookie line, and a 500 ms GeoJS wait (closes #401)
check / check (push) Failing after 14m7s
A browser sends all its cookies on one Cookie line (RFC 6265), and Go's
server joins HTTP/2 Cookie lines into one, so waf now refuses a second
line with 431. Further lines only gave a visitor more bytes for the Core
Rule Set to read. The refusal logs cookie_lines and cookie_lines_limit.

The default of SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS drops from 800
to 500 milliseconds, so the Core Rule Set's share of rproxy's 2 seconds
grows from 1 to 1.3 seconds. The slowest worst-case request takes about
0.84 seconds of processor time with every core busy.

Model: opus-5-5
2026-11-16 22:15:13 +00:00
clawbot 988f117ef3 Check builds use Docker's normal cache
check / check (push) Failing after 1m53s
Drop --no-cache from every docker build in script/: an unchanged tree
needs no rerun.

Model: opus-5-5
2026-11-16 21:48:28 +00:00
clawbot a2fcc00ded coreruleset: time worst-case inspections against reference work (closes #400)
check / check (push) Failing after 2m14s
The worst-case inspection test compared each inspection with a fixed 1
second, so a busy host failed it at random. It now times, on the same
thread and in processor time, a fixed regular-expression job just
before and after each inspection. An inspection fails when it takes
more than 13 times that job, about 1 second on an idle host.

Model: opus-5-5
2026-11-16 10:11:56 +01:00
clawbot 3c6b0d519f rproxy: breaker test no longer needs each failed ask to reach waf (closes #398)
check / check (push) Failing after 2m12s
TestFiveFailedAsksKeepTheNextRequestFromAskingWAF waited for a question
from each of the five failing asks. Under load an ask could time out
before the stand-in for waf saw it, and the test failed with "no
question". It now waits only for each visitor's answer.

Before the sixth request, it checks that rproxy has not written that the
circuit breaker is open, so none of the first five was kept from asking
waf. It checks that the sixth request did not ask by its request ID,
which the app records, so late questions from the failed asks cannot be
mistaken for it.

Model: opus-5-5
2026-11-16 08:58:18 +01:00
clawbot f33d0a4fce Remove every reference to our own servers (closes #396)
check / check (push) Successful in 4m48s
script/publish no longer defaults DOCKER_HOST to an SSH target on one of
our servers or PUBLISH_IMAGE to the registry. Both are required, and an
unset one stops it with a message naming it. The README says so.

The instance name in docs and tests was our server's name; it is now
host1/waf in the docs and host1/blog in the tests.

Model: opus-5-5
2026-11-16 07:29:16 +01:00
17 changed files with 183 additions and 82 deletions

No files matched your search

+1 -1
View File
@@ -329,7 +329,7 @@ Every setting the code reads, with its default:
| `SWW_WAF_BAN_SCOPE_V4_PREFIX` | `32` | The prefix length of the netblock a ban on an IPv4 client covers, from 0 to 32. | | `SWW_WAF_BAN_SCOPE_V4_PREFIX` | `32` | The prefix length of the netblock a ban on an IPv4 client covers, from 0 to 32. |
| `SWW_WAF_LOOKUP_SOURCE` | `geojs` | Where each client's AS number and country come from: `geojs` (a web service), `file` (a local database) or `off`. | | `SWW_WAF_LOOKUP_SOURCE` | `geojs` | Where each client's AS number and country come from: `geojs` (a web service), `file` (a local database) or `off`. |
| `SWW_WAF_LOOKUP_DB_PATH` | empty | The IPinfo Lite database file, in its `.mmdb` form, read while the source is `file`. | | `SWW_WAF_LOOKUP_DB_PATH` | empty | The IPinfo Lite database file, in its `.mmdb` form, read while the source is `file`. |
| `SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS` | `800` | How long a request waits for its client's first GeoJS answer when a setting needs it, and how long a request to GeoJS may take, in milliseconds. | | `SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS` | `500` | How long a request waits for its client's first GeoJS answer when a setting needs it, and how long a request to GeoJS may take, in milliseconds. |
| `SWW_WAF_ADD_LOOKUP_HEADERS` | `true`, or `false` while the lookup source is `off` | `true` passes the app the client's AS number and country in `X-Client-ASN` and `X-Client-Country`. | | `SWW_WAF_ADD_LOOKUP_HEADERS` | `true`, or `false` while the lookup source is `off` | `true` passes the app the client's AS number and country in `X-Client-ASN` and `X-Client-Country`. |
| `SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES` | empty | When set, the only countries whose clients get through; a client the lookup cannot place is refused too. | | `SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES` | empty | When set, the only countries whose clients get through; a client the lookup cannot place is refused too. |
| `SWW_WAF_TAG_LISTS` | empty | Address lists that each add a tag to their clients' requests, as `name=URL` pairs, such as `torexit=https://example.com/tor.txt`. | | `SWW_WAF_TAG_LISTS` | empty | Address lists that each add a tag to their clients' requests, as `name=URL` pairs, such as `torexit=https://example.com/tor.txt`. |
+12 -7
View File
@@ -2405,12 +2405,12 @@ The Core Rule Set inspects the body a question carries only while
`rproxy` stops waiting for `waf` after `SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`, by `rproxy` stops waiting for `waf` after `SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`, by
default 2 seconds, and `SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION` then default 2 seconds, and `SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION` then
decides. The longest a question waits on an outside source is decides. The longest a question waits on an outside source is
`SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS`, by default 800 milliseconds, well `SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS`, by default 500 milliseconds, well
within that time. within that time.
GeoJS is the only outside source a question can wait on. A question waits only GeoJS is the only outside source a question can wait on. A question waits only
when step 6 says so, and for at most when step 6 says so, and for at most
`SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS`, by default 800 milliseconds. A `SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS`, by default 500 milliseconds. A
client without an answer by then counts as unknown. No question waits on these, client without an answer by then counts as unknown. No question waits on these,
which run in the background: which run in the background:
@@ -2451,8 +2451,11 @@ It refuses a request with `431` for any of:
- a header line longer than 8 KiB. Each value of a header sent twice is a line - a header line longer than 8 KiB. Each value of a header sent twice is a line
of its own. `Host` counts as a line. of its own. `Host` counts as a line.
- more than 50 cookies. Each part of its `Cookie` headers between semicolons - more than one `Cookie` line. A browser sends all its cookies on one line, as
counts as one, unless it is empty or only spaces and tabs. RFC 6265 requires. Go's server joins the `Cookie` lines of an HTTP/2 request
into one before `rproxy` sees them.
- more than 50 cookies. Each part of its `Cookie` line between semicolons counts
as one, unless it is empty or only spaces and tabs.
- more than 100 header fields. A header sent twice counts twice. `Host` and - more than 100 header fields. A header sent twice counts twice. `Host` and
`Transfer-Encoding` do not count. `X-Request-ID` and `X-Forwarded-Proto` do. `Transfer-Encoding` do not count. `X-Request-ID` and `X-Forwarded-Proto` do.
@@ -2490,9 +2493,11 @@ body can add up to more than the body. From 1 KiB of JSON, Coraza makes at most
A request can reach all of these at once. At paranoia level 4, with no rule A request can reach all of these at once. At paranoia level 4, with no rule
switched off, the Core Rule Set takes under a second on one core of a current switched off, the Core Rule Set takes under a second on one core of a current
server to inspect it. So `waf` answers it within the default 2 seconds, even server to inspect it, even with every core busy. Its share of the default 2
after the default 800 milliseconds of waiting for GeoJS. Enough such requests at seconds is 1.3 seconds: the rest is the default 500 milliseconds of waiting for
once can still overload `waf` past `SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`. GeoJS and 200 milliseconds for the other checks. So `waf` answers it in time.
Enough such requests at once can still overload `waf` past
`SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`.
### The report ### The report
+2 -2
View File
@@ -128,8 +128,8 @@ Every one of these lines has:
- `rule_files`: `rule_id` and `rule_action`, of the rule that refused it; - `rule_files`: `rule_id` and `rule_action`, of the rule that refused it;
- `header_limits`: for a request line, `request_line_bytes` and - `header_limits`: for a request line, `request_line_bytes` and
`request_line_bytes_limit`; for headers, `longest_header_line_bytes`, `request_line_bytes_limit`; for headers, `longest_header_line_bytes`,
`header_line_bytes_limit`, `fields`, `fields_limit`, `cookies` and `header_line_bytes_limit`, `fields`, `fields_limit`, `cookie_lines`,
`cookies_limit`; `cookie_lines_limit`, `cookies` and `cookies_limit`;
- `core_rule_set`: `score`, and `threshold`, - `core_rule_set`: `score`, and `threshold`,
`SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD`; `SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD`;
- `refusal_limit`: `window`, `count` and `limit`, of the refusal limit; - `refusal_limit`: `window`, `count` and `limit`, of the refusal limit;
+7 -4
View File
@@ -158,7 +158,7 @@ cut off; the client is banned.
- an anomaly threshold per AS number. - an anomaly threshold per AS number.
- `SWW_WAF_LOOKUP_DB_PATH` (default empty): the IPinfo Lite database file, in - `SWW_WAF_LOOKUP_DB_PATH` (default empty): the IPinfo Lite database file, in
its `.mmdb` form, for `SWW_WAF_LOOKUP_SOURCE=file`. its `.mmdb` form, for `SWW_WAF_LOOKUP_SOURCE=file`.
- `SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS` (default `800`): how long a - `SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS` (default `500`): how long a
request waits for its client's first answer from GeoJS, and how long a request request waits for its client's first answer from GeoJS, and how long a request
to GeoJS may take. A time in milliseconds; never `off`. to GeoJS may take. A time in milliseconds; never `off`.
- `SWW_WAF_ADD_LOOKUP_HEADERS` (default `true`): `true` has `rproxy` pass the - `SWW_WAF_ADD_LOOKUP_HEADERS` (default `true`): `true` has `rproxy` pass the
@@ -422,9 +422,12 @@ A few limits are fixed rather than settings:
- at most 20,000 anomaly counters are kept; - at most 20,000 anomaly counters are kept;
- each alert destination's queue holds at most 1,000 alerts; - each alert destination's queue holds at most 1,000 alerts;
- `waf` holds at most 10,000 answered requests awaiting their reports; - `waf` holds at most 10,000 answered requests awaiting their reports;
- `waf` refuses a request with a request line or header line over 8 KiB, more - `waf` refuses a request with any of these (see "Answering in time" under
than 50 cookies or more than 100 header fields (see "Answering in time" under "`waf`" in [`SPEC.md`](../SPEC.md)):
"`waf`" in [`SPEC.md`](../SPEC.md)); - a request line or header line over 8 KiB;
- more than one `Cookie` line;
- more than 50 cookies;
- more than 100 header fields;
- `rproxy` takes a request line and headers of at most 32 KiB, and closes a - `rproxy` takes a request line and headers of at most 32 KiB, and closes a
kept-open connection idle for 120 seconds; kept-open connection idle for 120 seconds;
- `rproxy` reads at most 1 MiB of the body of `waf`'s answer, and answers `502` - `rproxy` reads at most 1 MiB of the body of `waf`'s answer, and answers `502`
+1 -1
View File
@@ -12,6 +12,7 @@ require (
github.com/oschwald/maxminddb-golang/v2 v2.7.0 github.com/oschwald/maxminddb-golang/v2 v2.7.0
github.com/prometheus/client_golang v1.24.1 github.com/prometheus/client_golang v1.24.1
github.com/spf13/viper v1.21.0 github.com/spf13/viper v1.21.0
golang.org/x/sys v0.48.0
) )
require ( require (
@@ -47,7 +48,6 @@ require (
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
golang.org/x/net v0.60.0 // indirect golang.org/x/net v0.60.0 // indirect
golang.org/x/sync v0.23.0 // indirect golang.org/x/sync v0.23.0 // indirect
golang.org/x/sys v0.48.0 // indirect
golang.org/x/text v0.42.0 // indirect golang.org/x/text v0.42.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect google.golang.org/protobuf v1.36.11 // indirect
rsc.io/binaryregexp v0.2.0 // indirect rsc.io/binaryregexp v0.2.0 // indirect
+1 -1
View File
@@ -548,7 +548,7 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
LookupDBPath: env.value("SWW_WAF_LOOKUP_DB_PATH", ""), LookupDBPath: env.value("SWW_WAF_LOOKUP_DB_PATH", ""),
ASNAndCountryLookupTimeout: env.milliseconds( ASNAndCountryLookupTimeout: env.milliseconds(
"SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS", "SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS",
"800", "500",
), ),
ExclusivelyAllowedCountries: env.countries( ExclusivelyAllowedCountries: env.countries(
"SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES", ""), "SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES", ""),
+3 -3
View File
@@ -286,7 +286,7 @@ func TestDefaults(t *testing.T) {
RuleFilesEnabled: true, RuleFilesEnabled: true,
}) })
wantLookupSettings(t, cfg, config.WAF{ wantLookupSettings(t, cfg, config.WAF{
LookupSource: defaultLookupSource, ASNAndCountryLookupTimeout: 800 * time.Millisecond, LookupSource: defaultLookupSource, ASNAndCountryLookupTimeout: 500 * time.Millisecond,
AddLookupHeaders: true, AddLookupHeaders: true,
}) })
wantByteLimitSettings(t, cfg, config.WAF{ wantByteLimitSettings(t, cfg, config.WAF{
@@ -1407,7 +1407,7 @@ func TestLookupSettingsAsSet(t *testing.T) {
// With no client looked up, the headers are off unless set. // With no client looked up, the headers are off unless set.
cfg = wafFromEnvironment(t, environment{lookupSource: off}) cfg = wafFromEnvironment(t, environment{lookupSource: off})
wantLookupSettings(t, cfg, config.WAF{ wantLookupSettings(t, cfg, config.WAF{
LookupSource: off, ASNAndCountryLookupTimeout: 800 * time.Millisecond, LookupSource: off, ASNAndCountryLookupTimeout: 500 * time.Millisecond,
}) })
} }
@@ -2933,7 +2933,7 @@ func TestLogsEachSettingWithItsValue(t *testing.T) {
bytesCounted: "both", bytesCounted: "both",
lookupSource: defaultLookupSource, lookupSource: defaultLookupSource,
lookupDBPath: "", lookupDBPath: "",
asnAndCountryLookupTimeout: "800", asnAndCountryLookupTimeout: "500",
addLookupHeaders: enabled, addLookupHeaders: enabled,
allowedCountries: "", allowedCountries: "",
tagLists: "", tagLists: "",
+94 -27
View File
@@ -5,12 +5,16 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/netip" "net/netip"
"regexp"
"runtime"
"slices" "slices"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
"time" "time"
"golang.org/x/sys/unix"
"sneak.berlin/go/smallwebwaf/internal/coreruleset" "sneak.berlin/go/smallwebwaf/internal/coreruleset"
) )
@@ -31,12 +35,45 @@ const (
mostJSONRead = 1 << 10 mostJSONRead = 1 << 10
) )
// inspectionTimeLimit is the most time the Core Rule Set may take on one // The Core Rule Set has 1.3 seconds for one request. That is the default of
// request. It is the default of rproxy's SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, 2 // rproxy's SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, 2 seconds, less the default of
// seconds, less the default of SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS, 800 // SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS, 500 milliseconds, and less 200
// milliseconds, which a question can also wait for GeoJS, and less 200
// milliseconds for waf's other checks. // milliseconds for waf's other checks.
const inspectionTimeLimit = time.Second //
// A busy host slows all work, so the test does not compare an inspection with
// 1.3 seconds. It compares it with referenceTime, timed on the same thread
// just before and after. inspectionTimeLimitMultiple is how many times
// referenceTime an inspection may take.
//
// On the host that runs our checks, referenceTime took at least 78
// milliseconds, so 16 times it is about 1.25 seconds. With that host fully
// busy, no try of the slowest request took more than 6.7 times referenceTime.
const inspectionTimeLimitMultiple = 16
// referencePattern is what referenceTime matches. Like the Core Rule Set's
// rules, it is a regular expression with alternatives and repeats.
var referencePattern = regexp.MustCompile(
`(?i)(union|select|insert|delete)\s+\w+\s+(from|into)\s+\w+[0-9]+;`,
)
// referenceTime returns the processor time the calling thread takes to do a
// fixed amount of work unrelated to the Core Rule Set, so that a change to the
// rules does not change it. referencePattern matches nowhere in the text, so
// each match reads all of it.
func referenceTime(t *testing.T) time.Duration {
t.Helper()
referenceText := text(4 << 10)
began := threadCPUTime(t)
for range 200 {
if referencePattern.MatchString(referenceText) {
t.Fatal("the reference pattern matched")
}
}
return threadCPUTime(t) - began
}
// text returns n bytes of letters, digits and spaces, which the Core Rule Set // text returns n bytes of letters, digits and spaces, which the Core Rule Set
// takes longer over than one letter repeated. // takes longer over than one letter repeated.
@@ -184,20 +221,22 @@ const fullLine = lineLimit - 64
// worstCaseRequests are, for each kind of thing a visitor sends, the requests // worstCaseRequests are, for each kind of thing a visitor sends, the requests
// that keep the Core Rule Set busiest, within the bounds above. The last // that keep the Core Rule Set busiest, within the bounds above. The last
// holds the most of everything at once: Cookie lines keep it busier than // holds the most of everything at once. waf refuses a second Cookie line, so
// other header lines, and the JSON body of the most elements under a key // it has one, and User-Agent and Referer lines, which keep the Core Rule Set
// keeps it busier than the other bodies. // busier than other header lines. The JSON body of the most elements under a
// key keeps it busier than the other bodies.
func worstCaseRequests() []visitorRequest { func worstCaseRequests() []visitorRequest {
fullCookieLine := cookieLine(1)[:fullLine-len("Cookie: ")]
return append( return append(
append(worstCaseHeaders(), worstCaseBodies()...), append(worstCaseHeaders(), worstCaseBodies()...),
visitorRequest{ visitorRequest{
name: "the longest request line, three Cookie lines and body", name: "the longest request line, Cookie, User-Agent and " +
"Referer lines and body",
target: longestTarget()[:fullLine-len("POST HTTP/1.1")], target: longestTarget()[:fullLine-len("POST HTTP/1.1")],
header: http.Header{cookieHeader: { header: http.Header{
fullCookieLine, fullCookieLine, fullCookieLine, cookieHeader: {cookieLine(1)[:fullLine-len("Cookie: ")]},
}}, "User-Agent": {headerValue("User-Agent", fullLine)},
"Referer": {headerValue("Referer", fullLine)},
},
contentType: jsonType, body: jsonOfMostElementsUnderAKey(), contentType: jsonType, body: jsonOfMostElementsUnderAKey(),
}, },
) )
@@ -302,11 +341,30 @@ func worstCaseBodies() []visitorRequest {
} }
} }
// inspectionTime returns how long crs takes to inspect tc, and fails t unless // threadCPUTime returns the processor time the calling thread has used.
// it reads all of tc's body and Coraza parses it. func threadCPUTime(t *testing.T) time.Duration {
t.Helper()
var now unix.Timespec
err := unix.ClockGettime(unix.CLOCK_THREAD_CPUTIME_ID, &now)
if err != nil {
t.Fatalf("read the thread's processor time: %v", err)
}
return time.Duration(now.Nano())
}
// inspectionTime returns the processor time crs takes to inspect tc, and the
// mean of referenceTime just before and after. It fails t unless crs reads all
// of tc's body and Coraza parses it.
//
// All of it runs on this goroutine, kept on one thread, so the thread's
// processor time measures it. Time other work holds the processor does not
// count.
func inspectionTime( func inspectionTime(
t *testing.T, crs *coreruleset.CoreRuleSet, tc visitorRequest, t *testing.T, crs *coreruleset.CoreRuleSet, tc visitorRequest,
) time.Duration { ) (time.Duration, time.Duration) {
t.Helper() t.Helper()
req := httptest.NewRequestWithContext( req := httptest.NewRequestWithContext(
@@ -318,11 +376,19 @@ func inspectionTime(
req.Header.Set("Content-Type", tc.contentType) req.Header.Set("Content-Type", tc.contentType)
} }
began := time.Now() runtime.LockOSThread()
defer runtime.UnlockOSThread()
before := referenceTime(t)
began := threadCPUTime(t)
result, read, _ := crs.Inspect( result, read, _ := crs.Inspect(
req, netip.MustParseAddr("192.0.2.1"), strings.NewReader(tc.body), req, netip.MustParseAddr("192.0.2.1"), strings.NewReader(tc.body),
) )
took := time.Since(began) took := threadCPUTime(t) - began
reference := (before + referenceTime(t)) / 2
if string(read) != tc.body { if string(read) != tc.body {
t.Errorf("read %d bytes of %d", len(read), len(tc.body)) t.Errorf("read %d bytes of %d", len(read), len(tc.body))
@@ -333,13 +399,13 @@ func inspectionTime(
t.Errorf("Coraza could not parse the body") t.Errorf("Coraza could not parse the body")
} }
return took return took, reference
} }
// TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime runs one at a time, so // TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime runs one at a time, so
// that the tests running in parallel do not slow it. Other work on the host // that the tests running in parallel do not slow it. Other work on the host
// can still slow one try, so a request fails only when three tries in a row // can still slow one try more than the reference work around it, so a request
// take too long. // fails only when three tries in a row take too long.
// //
//nolint:paralleltest // one at a time, as above //nolint:paralleltest // one at a time, as above
func TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime(t *testing.T) { func TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime(t *testing.T) {
@@ -353,18 +419,19 @@ func TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime(t *testing.T) {
for _, tc := range worstCaseRequests() { for _, tc := range worstCaseRequests() {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
var took time.Duration var took, reference time.Duration
for range tries { for range tries {
took = inspectionTime(t, crs, tc) took, reference = inspectionTime(t, crs, tc)
if took <= inspectionTimeLimit { if took <= inspectionTimeLimitMultiple*reference {
return return
} }
} }
t.Errorf( t.Errorf(
"took %s on the last of %d tries, over %s", "took %s on the last of %d tries, over %d times the %s "+
took, tries, inspectionTimeLimit, "of the reference work",
took, tries, inspectionTimeLimitMultiple, reference,
) )
}) })
} }
+29 -5
View File
@@ -1424,7 +1424,14 @@ func TestFiveFailedAsksKeepTheNextRequestFromAskingWAF(t *testing.T) {
failure(w, r) failure(w, r)
}, },
) )
app := startApp(t, greet) nextID := make(chan string, 1)
app := startApp(t, func(w http.ResponseWriter, r *http.Request) {
if answering.Load() {
nextID <- r.Header.Get(requestIDHeader)
}
greet(w, r)
})
addr, out := startRProxy( addr, out := startRProxy(
t, t,
standIn.URL, standIn.URL,
@@ -1434,19 +1441,36 @@ func TestFiveFailedAsksKeepTheNextRequestFromAskingWAF(t *testing.T) {
}, },
) )
// An ask can time out before it reaches the stand-in, so these
// may leave fewer than five questions, or leave some late.
for range 5 { for range 5 {
wantGreeting(t, get(t, addr, "/")) wantGreeting(t, get(t, addr, "/"))
standIn.question(t) }
// rproxy writes this line before it answers a request it kept
// from asking waf, so no wait is needed.
if out.has("the circuit breaker is open") {
t.Error("the circuit breaker opened before five asks failed")
} }
answering.Store(true) answering.Store(true)
wantGreeting(t, get(t, addr, "/")) wantGreeting(t, get(t, addr, "/"))
out.waitFor(t, "the circuit breaker is open") out.waitFor(t, "the circuit breaker is open")
// Had the next request asked waf, its question would have reached
// the stand-in before the request reached the app.
var id string
select { select {
case <-standIn.questions: case id = <-nextID:
t.Error("waf was asked, want it not to be") default:
case <-time.After(10 * pollInterval): t.Fatal("the app did not get the next request")
}
for len(standIn.questions) > 0 {
asked := <-standIn.questions
if asked.header.Get(requestIDHeader) == id {
t.Error("waf was asked, want it not to be")
}
} }
}) })
} }
+8 -4
View File
@@ -33,7 +33,7 @@ const (
// SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, the time waf has to answer. // SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, the time waf has to answer.
answerTimeLimit = 2 * time.Second answerTimeLimit = 2 * time.Second
// defaultLookupWait is the default of SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS. // defaultLookupWait is the default of SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS.
defaultLookupWait = 800 * time.Millisecond defaultLookupWait = 500 * time.Millisecond
// defaultDNSBLAbuseIPDBWait is the default of SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS. // defaultDNSBLAbuseIPDBWait is the default of SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS.
defaultDNSBLAbuseIPDBWait = 2 * time.Second defaultDNSBLAbuseIPDBWait = 2 * time.Second
// The header limits SPEC.md gives. // The header limits SPEC.md gives.
@@ -111,7 +111,8 @@ func TestHeaderLimitRefusalNamesItsHeuristicCountsAndLimits(t *testing.T) {
s.out.requestLine(t), s.out.requestLine(t),
"header_limits", "header_limits",
`{"longest_header_line_bytes": 40, "header_line_bytes_limit": 8192, `{"longest_header_line_bytes": 40, "header_line_bytes_limit": 8192,
"fields": 101, "fields_limit": 100, "cookies": 0, "cookies_limit": 50}`, "fields": 101, "fields_limit": 100, "cookie_lines": 0,
"cookie_lines_limit": 1, "cookies": 0, "cookies_limit": 50}`,
) )
} }
@@ -198,6 +199,9 @@ func TestAHeaderLineOverTheLimitIsRefusedWith431(t *testing.T) {
{"a header line over the limit", func(req *http.Request) { {"a header line over the limit", func(req *http.Request) {
req.Header.Set("X-Long", strings.Repeat("a", lineLimit+1-len("X-Long: "))) req.Header.Set("X-Long", strings.Repeat("a", lineLimit+1-len("X-Long: ")))
}, true}, }, true},
{"a Cookie line at the limit", func(req *http.Request) {
req.Header.Set(cookieHeader, "a="+strings.Repeat("a", lineLimit-len("Cookie: a=")))
}, false},
{"one cookie over the limit", func(req *http.Request) { {"one cookie over the limit", func(req *http.Request) {
req.Header.Set(cookieHeader, "a="+strings.Repeat("a", lineLimit)) req.Header.Set(cookieHeader, "a="+strings.Repeat("a", lineLimit))
}, true}, }, true},
@@ -267,8 +271,8 @@ func TestEachHeaderLimitRefusesARequestOverIt(t *testing.T) {
{"parts of no-break spaces count", func(req *http.Request) { {"parts of no-break spaces count", func(req *http.Request) {
req.Header.Set(cookieHeader, strings.Repeat(" ;", cookieLimit+1)) req.Header.Set(cookieHeader, strings.Repeat(" ;", cookieLimit+1))
}, true}, }, true},
{"cookies over the limit across two headers", func(req *http.Request) { {"two Cookie lines", func(req *http.Request) {
req.Header[cookieHeader] = []string{cookies(cookieLimit), "last=1"} req.Header[cookieHeader] = []string{"a=1", "b=1"}
}, true}, }, true},
{"cookies without a value count", func(req *http.Request) { {"cookies without a value count", func(req *http.Request) {
req.Header.Set(cookieHeader, strings.Repeat("a;", cookieLimit+1)) req.Header.Set(cookieHeader, strings.Repeat("a;", cookieLimit+1))
+16 -9
View File
@@ -10,11 +10,15 @@ import (
"sneak.berlin/go/smallwebwaf/internal/requestlog" "sneak.berlin/go/smallwebwaf/internal/requestlog"
) )
// The header limits: the most cookies and header fields a request may have, // The header limits: the most Cookie lines, cookies and header fields a request
// and the longest its request line and each header line may be, in bytes, not // may have, and the longest its request line and each header line may be, in
// counting the line ending. Real clients rarely reach them. maxLineBytes is // bytes, not counting the line ending. Real clients rarely reach them.
// what nginx allows by default. // maxLineBytes is what nginx allows by default. A browser sends all its cookies
// on one Cookie line, as RFC 6265 requires, and Go's server joins the Cookie
// lines of an HTTP/2 request into one. A second line would only give a visitor
// another 8 KiB for the Core Rule Set to read.
const ( const (
maxCookieLines = 1
maxCookies = 50 maxCookies = 50
maxHeaderFields = 100 maxHeaderFields = 100
maxLineBytes = 8 << 10 maxLineBytes = 8 << 10
@@ -22,10 +26,11 @@ const (
// headerLimits refuses a request with a request line longer than maxLineBytes, // headerLimits refuses a request with a request line longer than maxLineBytes,
// with 414. It refuses a request with a header line longer than maxLineBytes, // with 414. It refuses a request with a header line longer than maxLineBytes,
// more than maxHeaderFields header fields or more than maxCookies cookies, with // more than maxHeaderFields header fields, more than maxCookieLines Cookie
// 431. Its figures are the request line's length and its limit for 414. For // lines or more than maxCookies cookies, with 431. Its figures are the request
// 431 they are the longest header line, the header fields and the cookies, // line's length and its limit for 414. For 431 they are the longest header
// each with its limit. // line, the header fields, the Cookie lines and the cookies, each with its
// limit.
type headerLimits struct{} type headerLimits struct{}
func (headerLimits) name() string { return "header_limits" } func (headerLimits) name() string { return "header_limits" }
@@ -48,9 +53,10 @@ func (headerLimits) check(
longest := rq.longestHeaderLineBytes() longest := rq.longestHeaderLineBytes()
fields, cookies := rq.headerCounts() fields, cookies := rq.headerCounts()
cookieLines := len(rq.in.Header.Values("Cookie"))
if longest <= maxLineBytes && fields <= maxHeaderFields && if longest <= maxLineBytes && fields <= maxHeaderFields &&
cookies <= maxCookies { cookieLines <= maxCookieLines && cookies <= maxCookies {
return nil return nil
} }
@@ -60,6 +66,7 @@ func (headerLimits) check(
"longest_header_line_bytes": longest, "longest_header_line_bytes": longest,
"header_line_bytes_limit": maxLineBytes, "header_line_bytes_limit": maxLineBytes,
"fields": fields, "fields_limit": maxHeaderFields, "fields": fields, "fields_limit": maxHeaderFields,
"cookie_lines": cookieLines, "cookie_lines_limit": maxCookieLines,
"cookies": cookies, "cookies_limit": maxCookies, "cookies": cookies, "cookies_limit": maxCookies,
}, },
} }
+1 -4
View File
@@ -2,9 +2,6 @@
# script/cibuild: run the CI build. It bootstraps first: a CI runner # script/cibuild: run the CI build. It bootstraps first: a CI runner
# checks out and runs this and nothing else, and script/fmt-check runs # checks out and runs this and nothing else, and script/fmt-check runs
# the formatter on the host, which a pristine checkout cannot do. # the formatter on the host, which a pristine checkout cannot do.
# --no-cache for the same reason as script/docker: the gate phases the
# final stage depends on are RUN steps, and a cached one is a check that
# did not run.
set -eu set -eu
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
@@ -20,7 +17,7 @@ main() {
# the version a build stage derives from the .git in the context. # the version a build stage derives from the .git in the context.
version="$(git describe --tags --always --dirty 2>/dev/null || true)" version="$(git describe --tags --always --dirty 2>/dev/null || true)"
[ -n "$version" ] || version="unknown" [ -n "$version" ] || version="unknown"
docker build --no-cache \ docker build \
--build-arg VERSION="$version" \ --build-arg VERSION="$version" \
-t "$("$SCRIPT_DIR/projectname")" . -t "$("$SCRIPT_DIR/projectname")" .
} }
+1 -3
View File
@@ -1,8 +1,6 @@
#!/bin/sh #!/bin/sh
# script/docker: build the Docker image tagged with the project name. # script/docker: build the Docker image tagged with the project name.
# Identical in all repos; the tag comes from script/projectname. # Identical in all repos; the tag comes from script/projectname.
# --no-cache because the gate phases the final stage depends on are RUN
# steps, and a cached one is a check that did not run.
set -eu set -eu
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
@@ -16,7 +14,7 @@ main() {
# the version a build stage derives from the .git in the context. # the version a build stage derives from the .git in the context.
version="$(git describe --tags --always --dirty 2>/dev/null || true)" version="$(git describe --tags --always --dirty 2>/dev/null || true)"
[ -n "$version" ] || version="unknown" [ -n "$version" ] || version="unknown"
docker build --no-cache \ docker build \
--build-arg VERSION="$version" \ --build-arg VERSION="$version" \
-t "$("$SCRIPT_DIR/projectname")" . -t "$("$SCRIPT_DIR/projectname")" .
} }
+2 -2
View File
@@ -170,8 +170,8 @@ main() {
trap cleanup EXIT trap cleanup EXIT
trap 'exit 1' HUP INT TERM trap 'exit 1' HUP INT TERM
docker build --no-cache -t "$IMAGE" . docker build -t "$IMAGE" .
docker build --no-cache --build-arg SWW_IMAGE="$IMAGE" \ docker build --build-arg SWW_IMAGE="$IMAGE" \
-t "$APP_IMAGE" deploy/example-app -t "$APP_IMAGE" deploy/example-app
docker network create "$NETWORK" >/dev/null docker network create "$NETWORK" >/dev/null
+2 -3
View File
@@ -5,8 +5,7 @@
# make its answer untrustworthy. # make its answer untrustworthy.
# #
# The phase is not the last stage in the file, so it is built only when # The phase is not the last stage in the file, so it is built only when
# --target names it. --no-cache because a cached lint layer is a lint # --target names it. The tag makes each build replace the previous image
# that did not run. The tag makes each build replace the previous image
# instead of leaving a dangling one behind. # instead of leaving a dangling one behind.
set -eu set -eu
@@ -15,7 +14,7 @@ ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)"
main() { main() {
cd "$ROOT" cd "$ROOT"
docker build --no-cache \ docker build \
--target lint \ --target lint \
-t "$("$SCRIPT_DIR/projectname")-lint" . -t "$("$SCRIPT_DIR/projectname")-lint" .
} }
+2 -3
View File
@@ -2,8 +2,7 @@
# script/test: run the test suite. Testing is a phase of the Dockerfile # script/test: run the test suite. Testing is a phase of the Dockerfile
# and this builds that phase alone, on the same terms as script/lint: # and this builds that phase alone, on the same terms as script/lint:
# --target because a phase that is not the last stage is built only when # --target because a phase that is not the last stage is built only when
# named, --no-cache because a cached test layer is a test that did not # named, and a tag so each build replaces the previous image.
# run, and a tag so each build replaces the previous image.
set -eu set -eu
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
@@ -11,7 +10,7 @@ ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)"
main() { main() {
cd "$ROOT" cd "$ROOT"
docker build --no-cache \ docker build \
--target test \ --target test \
-t "$("$SCRIPT_DIR/projectname")-test" . -t "$("$SCRIPT_DIR/projectname")-test" .
} }
+1 -3
View File
@@ -3,15 +3,13 @@
# the test phase of the Dockerfile checks. This builds the Dockerfile's # the test phase of the Dockerfile checks. This builds the Dockerfile's
# tidy-files stage, which holds the two files alone, and --output writes # tidy-files stage, which holds the two files alone, and --output writes
# them into the working tree. The build makes no image, so it has no tag. # them into the working tree. The build makes no image, so it has no tag.
# --no-cache because `go mod tidy` asks the module proxy, whose answers a
# cached layer would repeat.
set -eu set -eu
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)" ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
main() { main() {
cd "$ROOT" cd "$ROOT"
docker build --no-cache \ docker build \
--target tidy-files \ --target tidy-files \
--output "type=local,dest=$ROOT" . --output "type=local,dest=$ROOT" .
} }