From 01a8a7aa0819a6a6268faca44f2915d9bb09a254 Mon Sep 17 00:00:00 2001 From: clawbot <35+clawbot@noreply.example.org> Date: Mon, 16 Nov 2026 21:56:30 +0000 Subject: [PATCH] waf: one Cookie line, and a 500 ms GeoJS wait (closes #401) 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 --- README.md | 2 +- SPEC.md | 19 +++++++----- docs/request-log.md | 4 +-- docs/settings.md | 11 ++++--- internal/config/waf.go | 2 +- internal/config/waf_test.go | 6 ++-- internal/coreruleset/inspectiontime_test.go | 34 +++++++++++---------- internal/waf/answertime_test.go | 12 +++++--- internal/waf/headerlimits.go | 25 +++++++++------ 9 files changed, 68 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index eb4ed0b..a493d02 100644 --- a/README.md +++ b/README.md @@ -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_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_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_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`. | diff --git a/SPEC.md b/SPEC.md index 8a2a71f..26f1727 100644 --- a/SPEC.md +++ b/SPEC.md @@ -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 default 2 seconds, and `SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION` then 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. GeoJS is the only outside source a question can wait on. A question waits only 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, 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 of its own. `Host` counts as a line. -- more than 50 cookies. Each part of its `Cookie` headers between semicolons - counts as one, unless it is empty or only spaces and tabs. +- more than one `Cookie` line. A browser sends all its cookies on one line, as + 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 `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 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 -after the default 800 milliseconds of waiting for GeoJS. Enough such requests at -once can still overload `waf` past `SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`. +server to inspect it, even with every core busy. Its share of the default 2 +seconds is 1.3 seconds: the rest is the default 500 milliseconds of waiting for +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 diff --git a/docs/request-log.md b/docs/request-log.md index c06c0b8..10f9de2 100644 --- a/docs/request-log.md +++ b/docs/request-log.md @@ -128,8 +128,8 @@ Every one of these lines has: - `rule_files`: `rule_id` and `rule_action`, of the rule that refused it; - `header_limits`: for a request line, `request_line_bytes` and `request_line_bytes_limit`; for headers, `longest_header_line_bytes`, - `header_line_bytes_limit`, `fields`, `fields_limit`, `cookies` and - `cookies_limit`; + `header_line_bytes_limit`, `fields`, `fields_limit`, `cookie_lines`, + `cookie_lines_limit`, `cookies` and `cookies_limit`; - `core_rule_set`: `score`, and `threshold`, `SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD`; - `refusal_limit`: `window`, `count` and `limit`, of the refusal limit; diff --git a/docs/settings.md b/docs/settings.md index aae3bf5..22edcc4 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -158,7 +158,7 @@ cut off; the client is banned. - an anomaly threshold per AS number. - `SWW_WAF_LOOKUP_DB_PATH` (default empty): the IPinfo Lite database file, in 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 to GeoJS may take. A time in milliseconds; never `off`. - `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; - each alert destination's queue holds at most 1,000 alerts; - `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 - than 50 cookies or more than 100 header fields (see "Answering in time" under - "`waf`" in [`SPEC.md`](../SPEC.md)); +- `waf` refuses a request with any of these (see "Answering in time" under + "`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 kept-open connection idle for 120 seconds; - `rproxy` reads at most 1 MiB of the body of `waf`'s answer, and answers `502` diff --git a/internal/config/waf.go b/internal/config/waf.go index 513d253..a56cf46 100644 --- a/internal/config/waf.go +++ b/internal/config/waf.go @@ -548,7 +548,7 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) { LookupDBPath: env.value("SWW_WAF_LOOKUP_DB_PATH", ""), ASNAndCountryLookupTimeout: env.milliseconds( "SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS", - "800", + "500", ), ExclusivelyAllowedCountries: env.countries( "SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES", ""), diff --git a/internal/config/waf_test.go b/internal/config/waf_test.go index f3ea741..98050fc 100644 --- a/internal/config/waf_test.go +++ b/internal/config/waf_test.go @@ -286,7 +286,7 @@ func TestDefaults(t *testing.T) { RuleFilesEnabled: true, }) wantLookupSettings(t, cfg, config.WAF{ - LookupSource: defaultLookupSource, ASNAndCountryLookupTimeout: 800 * time.Millisecond, + LookupSource: defaultLookupSource, ASNAndCountryLookupTimeout: 500 * time.Millisecond, AddLookupHeaders: true, }) 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. cfg = wafFromEnvironment(t, environment{lookupSource: off}) 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", lookupSource: defaultLookupSource, lookupDBPath: "", - asnAndCountryLookupTimeout: "800", + asnAndCountryLookupTimeout: "500", addLookupHeaders: enabled, allowedCountries: "", tagLists: "", diff --git a/internal/coreruleset/inspectiontime_test.go b/internal/coreruleset/inspectiontime_test.go index f314a53..7ce5c07 100644 --- a/internal/coreruleset/inspectiontime_test.go +++ b/internal/coreruleset/inspectiontime_test.go @@ -35,20 +35,20 @@ const ( mostJSONRead = 1 << 10 ) -// The Core Rule Set has 1 second for one request. That is the default of +// The Core Rule Set has 1.3 seconds for one request. That is the default of // rproxy's SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, 2 seconds, less the default of -// SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS, 800 milliseconds, and less 200 +// SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS, 500 milliseconds, and less 200 // milliseconds for waf's other checks. // // A busy host slows all work, so the test does not compare an inspection with -// 1 second. It compares it with referenceTime, timed on the same thread just -// before and after. inspectionTimeLimitMultiple is how many times +// 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 13 times it is about 1 second. With that host fully busy, -// no try of the slowest request took more than 9.5 times referenceTime. -const inspectionTimeLimitMultiple = 13 +// 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. @@ -221,20 +221,22 @@ const fullLine = lineLimit - 64 // 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 -// holds the most of everything at once: Cookie lines keep it busier than -// other header lines, and the JSON body of the most elements under a key -// keeps it busier than the other bodies. +// holds the most of everything at once. waf refuses a second Cookie line, so +// it has one, and User-Agent and Referer lines, which keep the Core Rule Set +// 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 { - fullCookieLine := cookieLine(1)[:fullLine-len("Cookie: ")] - return append( append(worstCaseHeaders(), worstCaseBodies()...), 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")], - header: http.Header{cookieHeader: { - fullCookieLine, fullCookieLine, fullCookieLine, - }}, + header: http.Header{ + cookieHeader: {cookieLine(1)[:fullLine-len("Cookie: ")]}, + "User-Agent": {headerValue("User-Agent", fullLine)}, + "Referer": {headerValue("Referer", fullLine)}, + }, contentType: jsonType, body: jsonOfMostElementsUnderAKey(), }, ) diff --git a/internal/waf/answertime_test.go b/internal/waf/answertime_test.go index 408e9e6..abbcf5f 100644 --- a/internal/waf/answertime_test.go +++ b/internal/waf/answertime_test.go @@ -33,7 +33,7 @@ const ( // SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, the time waf has to answer. answerTimeLimit = 2 * time.Second // 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 = 2 * time.Second // The header limits SPEC.md gives. @@ -111,7 +111,8 @@ func TestHeaderLimitRefusalNamesItsHeuristicCountsAndLimits(t *testing.T) { s.out.requestLine(t), "header_limits", `{"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) { req.Header.Set("X-Long", strings.Repeat("a", lineLimit+1-len("X-Long: "))) }, 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) { req.Header.Set(cookieHeader, "a="+strings.Repeat("a", lineLimit)) }, true}, @@ -267,8 +271,8 @@ func TestEachHeaderLimitRefusesARequestOverIt(t *testing.T) { {"parts of no-break spaces count", func(req *http.Request) { req.Header.Set(cookieHeader, strings.Repeat(" ;", cookieLimit+1)) }, true}, - {"cookies over the limit across two headers", func(req *http.Request) { - req.Header[cookieHeader] = []string{cookies(cookieLimit), "last=1"} + {"two Cookie lines", func(req *http.Request) { + req.Header[cookieHeader] = []string{"a=1", "b=1"} }, true}, {"cookies without a value count", func(req *http.Request) { req.Header.Set(cookieHeader, strings.Repeat("a;", cookieLimit+1)) diff --git a/internal/waf/headerlimits.go b/internal/waf/headerlimits.go index 19c9b09..facefb7 100644 --- a/internal/waf/headerlimits.go +++ b/internal/waf/headerlimits.go @@ -10,11 +10,15 @@ import ( "sneak.berlin/go/smallwebwaf/internal/requestlog" ) -// The header limits: the most cookies and header fields a request may have, -// and the longest its request line and each header line may be, in bytes, not -// counting the line ending. Real clients rarely reach them. maxLineBytes is -// what nginx allows by default. +// The header limits: the most Cookie lines, cookies and header fields a request +// may have, and the longest its request line and each header line may be, in +// bytes, not counting the line ending. Real clients rarely reach them. +// 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 ( + maxCookieLines = 1 maxCookies = 50 maxHeaderFields = 100 maxLineBytes = 8 << 10 @@ -22,10 +26,11 @@ const ( // headerLimits refuses a request with a request 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 -// 431. Its figures are the request line's length and its limit for 414. For -// 431 they are the longest header line, the header fields and the cookies, -// each with its limit. +// more than maxHeaderFields header fields, more than maxCookieLines Cookie +// lines or more than maxCookies cookies, with 431. Its figures are the request +// line's length and its limit for 414. For 431 they are the longest header +// line, the header fields, the Cookie lines and the cookies, each with its +// limit. type headerLimits struct{} func (headerLimits) name() string { return "header_limits" } @@ -48,9 +53,10 @@ func (headerLimits) check( longest := rq.longestHeaderLineBytes() fields, cookies := rq.headerCounts() + cookieLines := len(rq.in.Header.Values("Cookie")) if longest <= maxLineBytes && fields <= maxHeaderFields && - cookies <= maxCookies { + cookieLines <= maxCookieLines && cookies <= maxCookies { return nil } @@ -60,6 +66,7 @@ func (headerLimits) check( "longest_header_line_bytes": longest, "header_line_bytes_limit": maxLineBytes, "fields": fields, "fields_limit": maxHeaderFields, + "cookie_lines": cookieLines, "cookie_lines_limit": maxCookieLines, "cookies": cookies, "cookies_limit": maxCookies, }, }