check / check (push) Waiting to run
The tags, what observe mode would have done, the broken limit and the three reputation flags are fields of decision, each naming what sets it. The log line is written from them and from the lookup answer, and no heuristic, ban note or alert reads it back. A ban's notes take the request's method, Host and User-Agent from decision, since the report's steps have no request to read them from. Model: opus-5-5
168 lines
5.0 KiB
Go
168 lines
5.0 KiB
Go
package waf
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/netip"
|
|
"strings"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
"sneak.berlin/go/smallwebwaf/internal/geojs"
|
|
"sneak.berlin/go/smallwebwaf/internal/metrics"
|
|
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
|
)
|
|
|
|
// lookupResult is what the lookup gave for a request: whether its client was
|
|
// looked up, the answer, and whether the request skipped the wait for GeoJS.
|
|
type lookupResult struct {
|
|
lookedUp, skippedWait bool
|
|
answer geojs.Answer
|
|
}
|
|
|
|
// startLookUp starts looking up the client's AS number and country, in the
|
|
// lookup database or through GeoJS, unless SWW_WAF_LOOKUP_SOURCE is off or the
|
|
// client is on a private, loopback or link-local address, which no lookup can
|
|
// place. It returns the channel the result comes on. A result that needs no
|
|
// wait is on it when startLookUp returns: the lookup database's, an answer
|
|
// GeoJS gave earlier, or none. With GeoJS, while a setting needs the answer,
|
|
// such as a country list or a biased threshold, a new client's request waits
|
|
// for it up to SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS, unless
|
|
// refusedWhateverTheAnswer reports true. The wait goes on to its end even once
|
|
// the request has been answered, so that it ends as it would have for a
|
|
// request that waited. startLookUp changes nothing of the request. ctx is the
|
|
// request's own context.
|
|
func (rq *request) startLookUp(
|
|
ctx context.Context, now time.Time,
|
|
) <-chan lookupResult {
|
|
result := make(chan lookupResult, 1)
|
|
|
|
if rq.h.config.LookupSource == config.LookupSourceOff ||
|
|
!canBePlaced(rq.client) {
|
|
result <- lookupResult{}
|
|
|
|
return result
|
|
}
|
|
|
|
client := clientGroup(rq.h.config.IPv6GroupPrefix, rq.client)
|
|
|
|
if rq.h.config.LookupSource == config.LookupSourceFile {
|
|
result <- lookupResult{
|
|
lookedUp: true, answer: rq.h.lookupFile.LookUp(client),
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
answer := rq.h.geojs.LookUpWithoutWaiting(ctx, client)
|
|
refused := rq.refusedWhateverTheAnswer(now)
|
|
|
|
if !answer.Answered.IsZero() || refused || !rq.h.waitForGeoJS {
|
|
result <- lookupResult{
|
|
lookedUp: true,
|
|
skippedWait: refused && answer.Answered.IsZero(),
|
|
answer: answer,
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
go func() {
|
|
result <- lookupResult{
|
|
lookedUp: true,
|
|
answer: rq.h.geojs.LookUp(context.WithoutCancel(ctx), client,
|
|
rq.h.config.ASNAndCountryLookupTimeout),
|
|
}
|
|
}()
|
|
|
|
return result
|
|
}
|
|
|
|
// noteLookup notes result for the log line and as the request's asn= and
|
|
// country= tags.
|
|
func (rq *request) noteLookup(result lookupResult) {
|
|
if !result.lookedUp {
|
|
return
|
|
}
|
|
|
|
rq.lookedUp = true
|
|
rq.lookupAnswer = result.answer
|
|
rq.skippedLookupWait = result.skippedWait
|
|
rq.line.ASN = rq.lookupAnswer.ASN
|
|
rq.line.ASName = rq.lookupAnswer.ASName
|
|
rq.line.Country = rq.lookupAnswer.Country
|
|
|
|
if tag := asnTag(rq.lookupAnswer.ASN); tag != "" {
|
|
rq.addTag(tag)
|
|
}
|
|
|
|
if tag := countryTag(rq.lookupAnswer.Country); tag != "" {
|
|
rq.addTag(tag)
|
|
}
|
|
}
|
|
|
|
// asnTag returns the tag of asn, an AS number as the lookup gives it, such as
|
|
// asn=64496 for AS64496, or "" for no AS number.
|
|
func asnTag(asn string) string {
|
|
if asn == "" {
|
|
return ""
|
|
}
|
|
|
|
return config.ASNTagPrefix + strings.TrimPrefix(asn, "AS")
|
|
}
|
|
|
|
// countryTag returns the tag of country, a code as the lookup gives it, such
|
|
// as country=DE for DE, or "" for no country.
|
|
func countryTag(country string) string {
|
|
if country == "" {
|
|
return ""
|
|
}
|
|
|
|
return config.CountryTagPrefix + country
|
|
}
|
|
|
|
// AddLookup adds answer, an answer about a client from the lookup database
|
|
// or GeoJS, to the client's history in limiter, and to the notes of the bans
|
|
// in ledger on its netblock that have no AS number, AS name or country yet.
|
|
// cfg sets the netblock's size.
|
|
func AddLookup(
|
|
cfg *config.WAF, limiter *ratelimit.Limiter, ledger *bans.Ledger,
|
|
answer geojs.Answer,
|
|
) {
|
|
limiter.AddLookup(answer.Client, answer.Answered,
|
|
answer.ASN, answer.ASName, answer.Country)
|
|
netblock := banNetblock(
|
|
cfg.BanScopeV4Prefix, cfg.IPv6GroupPrefix, answer.Client.Addr(),
|
|
)
|
|
ledger.AddLookup(netblock, answer.ASN, answer.ASName, answer.Country)
|
|
}
|
|
|
|
// GeoJSFailed logs a failure of GeoJS to processLog, raises a
|
|
// source_failure alert for it on alertQueue and counts it in m.
|
|
// askingAgainIn is how long GeoJS is left alone after it.
|
|
func GeoJSFailed(
|
|
processLog *slog.Logger, alertQueue *alerts.Queue, m *metrics.Metrics,
|
|
err error, askingAgainIn time.Duration,
|
|
) {
|
|
processLog.Warn("asking GeoJS failed",
|
|
"error", err.Error(), "asking_again_in", askingAgainIn.String())
|
|
alertQueue.Raise(alerts.Alert{
|
|
Event: alerts.EventSourceFailure,
|
|
Reason: "asking GeoJS failed",
|
|
Detail: map[string]any{
|
|
"source": "geojs", "error": err.Error(),
|
|
"asking_again_in": askingAgainIn.String(),
|
|
},
|
|
})
|
|
m.GeoJSFailures.Inc()
|
|
}
|
|
|
|
// canBePlaced reports whether a lookup can place addr: private, loopback
|
|
// and link-local addresses have no AS number or country.
|
|
func canBePlaced(addr netip.Addr) bool {
|
|
return !addr.IsPrivate() && !addr.IsLoopback() &&
|
|
!addr.IsLinkLocalUnicast()
|
|
}
|