package waf // The heuristic trap_paths. It reads SWW_WAF_TRAP_PATHS and // SWW_WAF_BAN_RESPONSE. import ( "context" "slices" "time" "sneak.berlin/go/smallwebwaf/internal/bans" "sneak.berlin/go/smallwebwaf/internal/rules" ) // trapPaths refuses a request for a path in SWW_WAF_TRAP_PATHS, as trapPath // finds it, with SWW_WAF_BAN_RESPONSE. Such a request is a clear sign of // attack, as a ban rule's match is: it bans the client's netblock, or in // observe mode raises the alert for the ban it would have made. The ban's // notes give the path as sent, and so does its figure. type trapPaths struct { paths []string // SWW_WAF_TRAP_PATHS banMaker banMaker } func (trapPaths) name() string { return "trap_paths" } func (t trapPaths) check( _ context.Context, rq *request, now time.Time, ) *verdict { path := rq.findings.trapPath if path == "" { return nil } rq.attack = true return &verdict{ action: t.banMaker.banForAttack( rq, now, bans.Notes{TrapPath: path}, ), status: t.banMaker.banResponse, figures: map[string]any{"path": path}, } } // trapPath returns rq's path as sent, when its path as a path rule sees it, // without the query, as sent or once percent-decoded, is in // SWW_WAF_TRAP_PATHS, and "" otherwise. func (t trapPaths) trapPath(rq *request) string { path := rules.Path(rq.in) if !slices.Contains(t.paths, path) && !slices.Contains(t.paths, rules.DecodeOnce(path)) { return "" } return path }