check / check (push) Waiting to run
Each heuristic that refuses a request sets the status on its verdict. check copies it into the refusal, or in observe mode notes it, and no longer works the status out from the action. refusalLimit takes the status from that verdict instead of from a switch over the action. Model: opus-5-5
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
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
|
|
}
|