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
122 lines
4.0 KiB
Go
122 lines
4.0 KiB
Go
package waf
|
|
|
|
// The heuristic header_limits. It reads no setting: its limits are fixed.
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
)
|
|
|
|
// 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
|
|
)
|
|
|
|
// 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, 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" }
|
|
|
|
func (headerLimits) check(
|
|
_ context.Context,
|
|
rq *request,
|
|
_ time.Time,
|
|
) *verdict {
|
|
requestLine := rq.requestLineBytes()
|
|
if requestLine > maxLineBytes {
|
|
return &verdict{
|
|
action: requestlog.ActionRequestLineTooLong,
|
|
figures: map[string]any{
|
|
"request_line_bytes": requestLine,
|
|
"request_line_bytes_limit": maxLineBytes,
|
|
},
|
|
}
|
|
}
|
|
|
|
longest := rq.longestHeaderLineBytes()
|
|
fields, cookies := rq.headerCounts()
|
|
cookieLines := len(rq.in.Header.Values("Cookie"))
|
|
|
|
if longest <= maxLineBytes && fields <= maxHeaderFields &&
|
|
cookieLines <= maxCookieLines && cookies <= maxCookies {
|
|
return nil
|
|
}
|
|
|
|
return &verdict{
|
|
action: requestlog.ActionRequestHeaderTooLarge,
|
|
figures: map[string]any{
|
|
"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,
|
|
},
|
|
}
|
|
}
|
|
|
|
// requestLineBytes returns the length of the request's request line, such as
|
|
// "GET /search?q=x HTTP/1.1", not counting its line ending. The target is the
|
|
// one the visitor sent, which rproxy's question carries unchanged.
|
|
func (rq *request) requestLineBytes() int {
|
|
return len(rq.in.Method) + len(" ") + len(rq.in.RequestURI) +
|
|
len(" ") + len(rq.in.Proto)
|
|
}
|
|
|
|
// longestHeaderLineBytes returns the length of the request's longest header
|
|
// line, such as "Cookie: a=1", not counting its line ending. Each value of a
|
|
// header sent twice is a line of its own, and Host is a line too.
|
|
func (rq *request) longestHeaderLineBytes() int {
|
|
longest := len("Host: ") + len(rq.in.Host)
|
|
|
|
for name, values := range rq.in.Header {
|
|
for _, value := range values {
|
|
longest = max(longest, len(name)+len(": ")+len(value))
|
|
}
|
|
}
|
|
|
|
return longest
|
|
}
|
|
|
|
// headerCounts returns the request's header fields, each value of a header sent
|
|
// twice being one, and its cookies, each part of a Cookie header between
|
|
// semicolons being one unless it is empty or only spaces and tabs. The Core
|
|
// Rule Set runs each of its rules on each cookie, so that thousands of small
|
|
// cookies keep it busy for over a second. It trims only spaces and control
|
|
// characters, of which a header value can hold only tabs, so a part of Unicode
|
|
// spaces is a cookie to it.
|
|
func (rq *request) headerCounts() (int, int) {
|
|
fields, cookies := 0, 0
|
|
|
|
for _, values := range rq.in.Header {
|
|
fields += len(values)
|
|
}
|
|
|
|
for _, value := range rq.in.Header.Values("Cookie") {
|
|
for part := range strings.SplitSeq(value, ";") {
|
|
if strings.Trim(part, " \t") != "" {
|
|
cookies++
|
|
}
|
|
}
|
|
}
|
|
|
|
return fields, cookies
|
|
}
|