Documentation
¶
Overview ¶
Package coraza implements rules.Evaluator with OWASP Coraza and the OWASP Core Rule Set (CRS), which is embedded in the binary.
Index ¶
Constants ¶
const ( DefaultParanoiaLevel = 1 DefaultInboundAnomalyThreshold = 5 // DefaultMaxArgs matches Coraza's own default argument limit. DefaultMaxArgs = 1000 )
Defaults follow CRS recommendations for a first deployment.
Variables ¶
var CRSRuleIDs = sync.OnceValues(func() ([]int, error) { var ids []int _, err := compile(Config{}, func(r types.RuleMetadata) { if inRange(r.ID(), crsIDMin, crsIDMax) { ids = append(ids, r.ID()) } }) if err != nil { return nil, err } if len(ids) == 0 { return nil, errors.New("this Coraza version doesn't list the rules it compiles") } slices.Sort(ids) return slices.Compact(ids), nil })
CRSRuleIDs returns the IDs of the embedded CRS's rules, sorted. Tools use it to expand ID ranges into exclusions.
var RuleGroups = map[string]int{
"scanner": 913,
"lfi": 930,
"rfi": 931,
"rce": 932,
"php": 933,
"generic": 934,
"xss": 941,
"sqli": 942,
"session-fixation": 943,
"java": 944,
}
RuleGroups maps the CRS attack families that can be disabled to their rule ID prefix. Disabling a family the application cannot be vulnerable to (for example "php" for a Go service) removes its rules from every request.
Functions ¶
func CheckUserRules ¶
CheckUserRules checks SecLang written by policy authors, as the engine loads it: after the Core Rule Set (Config.Directives), so it may refer to CRS rules. Only the directives of UserDirectiveNames are allowed. The error is a *RuleError naming the line of the first problem.
func RuleGroupNames ¶
func RuleGroupNames() []string
RuleGroupNames returns the names in RuleGroups, sorted.
func UserDirectiveNames ¶
func UserDirectiveNames() []string
UserDirectiveNames returns the directives CheckUserRules allows, sorted.
Types ¶
type Config ¶
type Config struct {
// ParanoiaLevel is the CRS blocking paranoia level, 1 (default) to 4.
// Higher levels detect more and produce more false positives.
ParanoiaLevel int
// InboundAnomalyThreshold is the CRS inbound anomaly score at which a
// request is blocked. Defaults to 5 (one critical match).
InboundAnomalyThreshold int
// MaxArgs is the maximum number of arguments per source (query, body,
// path). Requests with more are rejected with 400 by rules 200004
// (phase 1) and 200005 (phase 2) before any CRS argument rule runs, so
// padding a request with arguments is cheap to reject and cannot hide a
// payload past the limit. Zero means DefaultMaxArgs.
MaxArgs int
// DirectivesBeforeCRS are SecLang directives loaded before CRS. CRS
// runtime rule exclusions (rules using ctl:ruleRemoveById and similar)
// must be placed here.
DirectivesBeforeCRS string
// Directives are SecLang directives loaded after CRS, for custom rules
// and configure-time exclusions (SecRuleRemoveById, SecRuleUpdateTargetById).
Directives string
// BodyLimit, if positive, sets Coraza's request body limits
// (SecRequestBodyLimit and SecRequestBodyInMemoryLimit) to the largest
// body the caller will pass, so bodies within the caller's limit are
// neither rejected by Coraza nor buffered to temporary files.
BodyLimit int
// DisabledRuleGroups lists CRS attack families (keys of RuleGroups) to
// remove. Only disable families the protected application cannot be
// vulnerable to.
DisabledRuleGroups []string
// DisableCRS loads only the directives, without the Core Rule Set.
DisableCRS bool
// Exclusions turn rules off, everywhere or for a path or an argument.
// They are applied to each transaction, not compiled into the rules, so
// they are not part of Key: Evaluator.WithExclusions gives configs that
// differ only in exclusions one compiled rule set.
Exclusions []Exclusion
// OnMatch, if set, is called for every rule that matches and logs. It
// runs on the request path and must be fast and safe for concurrent use.
OnMatch func(types.MatchedRule)
}
Config configures the evaluator.
type Directive ¶
type Directive struct {
// Line is the 1-based line the directive starts on.
Line int
// Name is the directive name in lower case, such as "secrule".
Name string
// Text is the directive with its continuation lines joined, as Coraza
// evaluates it.
Text string
}
Directive is one SecLang directive, possibly spanning several lines.
func SplitDirectives ¶
SplitDirectives splits SecLang into directives the way Coraza's parser reads them: lines are trimmed, blank lines and comments skipped, a line ending in a backslash continues on the next, and a backtick at the end of a line opens a block that a line starting with a backtick closes.
type Evaluator ¶
type Evaluator struct {
// contains filtered or unexported fields
}
Evaluator evaluates requests with a compiled Coraza WAF and its exclusions. It is safe for concurrent use.
func (*Evaluator) WithExclusions ¶ added in v0.1.1
WithExclusions returns an evaluator with e's compiled rules and the exclusions list instead of e's. It compiles no rules, so policies that differ only in exclusions share one rule set; except when e's rules negate ARGS targets themselves and list excludes arguments (see argsNegation).
type Exclusion ¶
type Exclusion struct {
// RuleIDs are the rules to turn off.
RuleIDs []int
// Path limits the exclusion to an exact path ("/search") or a prefix
// ("/api/*"), in the router's syntax. Empty: every path.
Path string
// Argument limits it to one query or body argument (ARGS:<argument>),
// in the rule and the rules chained to it. Empty: the whole rule.
Argument string
}
Exclusion turns rules off where they cause false positives.