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
This commit is contained in:
clawbot authored and sneak committed 2026-11-16 22:15:13 +00:00
1 parent 988f117ef3
commit 01a8a7aa08
9 files changed
+68 -47

No files matched your search

+8 -4
View File
@@ -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))
+16 -9
View File
@@ -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,
},
}