Documentation
¶
Overview ¶
Package scripting runs the Lua scripts of the edge. A ---@stage line hooks the global function it precedes, whatever its name, to a stage of the edge handler chain, at http.request or http.response:
---@stage http.request/secrule
function deny_debug(ctx)
if ctx:header("X-Debug") == "1" then ctx:respond(403) end
end
The edge calls it around the stage with a ctx value to read the request, change its headers or answer it. Scripts run in a sandbox without os, io or any way to load code, under a time limit.
A secrule or a rate limiter may also be a Lua source in place of its expression: Program.Invoke calls its global function (match for a secrule, key for a limiter) with a read-only ctx and returns its Value. A match function returns an action constant, as an eBPF program returns XDP_DROP:
function match(ctx)
if ctx:header("X-Debug") == "1" then return SZ_BLOCK end
end
Index ¶
- Constants
- Variables
- func CondSource(match Node) (string, error)
- func IsAction(name string) bool
- func KeySource(match Node) (string, error)
- func MatchSource(r Rule) (string, error)
- func Stages() []string
- type Action
- type Cond
- type Kind
- type Node
- type Op
- type Option
- type Program
- func (p *Program) Call(ctx context.Context, at phase.Phase, stage string, ex exchange.Exchange) (bool, error)
- func (p *Program) Has(at phase.Phase, stage string) bool
- func (p *Program) HasFunc(name string) bool
- func (p *Program) Hooked() bool
- func (p *Program) Invoke(ctx context.Context, fn string, ex exchange.Exchange) (Value, error)
- func (p *Program) Name() string
- func (p *Program) Source() string
- type Rule
- type Source
- type Value
Constants ¶
const ( // MatchFunc the function of a secrule script. MatchFunc = "match" // KeyFunc the function of a rate limiter script. KeyFunc = "key" )
The global functions of the rule scripts.
const MaxConditions = 256
MaxConditions the most conditions of a declarative rule, all its groups together.
Variables ¶
var ( // ErrNotDeclarative a script outside of the declarative form Parse // reads, or a rule the form cannot write. ErrNotDeclarative = errors.New("not declarative") )
Functions ¶
func CondSource ¶
CondSource the Lua condition match matches, the if of the scripts MatchSource and KeySource write:
ctx:path() == "/login" and (ctx:header("X-A") ~= "" or ctx:ip() == "1")
"and" binds tighter than "or" in Lua, as in a Node: an or group in an and group is put in parentheses. A condition matching every request is "true".
func KeySource ¶
KeySource the source of the rate limiter script counting, under the client IP, the requests match matches: ParseKey reads match back from it.
func MatchSource ¶
MatchSource the source of the secrule script of r: ParseMatch reads r back from it.
Types ¶
type Action ¶
type Action struct {
Name string
// Params string, float64, bool or map[string]any fields.
Params map[string]any
}
Action the action of a secrule: its name ("block", which a script returns as SZ_BLOCK or "block"), and its params.
type Cond ¶
Cond a condition: the field Source (of Key, for a keyed source) compared by Op to Value. A numeric Op takes a decimal Value, e.g. "10" or "0.5".
type Node ¶
type Node struct {
// And the group needs all its Nodes to match, not only one. An empty
// And matches every request, an empty Or matches none.
And bool
Nodes []Node
// Cond the condition of a leaf, nil for a group.
Cond *Cond
}
Node a condition of a rule: a Cond, or a group of nodes all of which (And) or one of which (not And) must match. Build one with Leaf, All and Any: they keep the groups in the canonical form the parser reads a script to, so a rule written and read back is equal to itself.
func All ¶
All the node matching when every one of nodes does: the and groups it holds are merged into it, and its conditions come before its or groups, as the edge evaluates them. A single node is returned as is, none match every request.
func Any ¶
Any the node matching when one of nodes does: the or groups it holds are merged into it. A single node is returned as is, none match no request.
func ParseKey ¶
ParseKey reads the condition of a rate limiter script of the form KeySource writes: as ParseMatch, each branch returning true.
type Op ¶
type Op int
Op the comparison of a condition.
const ( // OpEq field == "value". OpEq Op = iota + 1 // OpNe field ~= "value". OpNe // OpContains field:find("value", 1, true). OpContains // OpPrefix field:sub(1, #value) == "value". OpPrefix // OpSuffix field:sub(-#value) == "value". OpSuffix // OpGt tonumber(field) > value. OpGt // OpGte tonumber(field) >= value. OpGte // OpLt tonumber(field) < value. OpLt // OpLte tonumber(field) <= value. OpLte // OpMatches re.match(field, "value"): value a regular expression of // the RE2 syntax of Go. OpMatches // OpWithin ctx:ip_in("value"): SourceIP in the CIDR prefix value. OpWithin // OpNotWithin not ctx:ip_in("value"). OpNotWithin // OpExists ctx:has_header(key) or ctx:has_query(key): the request has // the field Key of a keyed source, Value empty. OpExists // OpAbsent not ctx:has_header(key) or not ctx:has_query(key). OpAbsent )
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures a Program.
func WithTimeout ¶
WithTimeout limits the time of each Call, 50ms by default.
type Program ¶
type Program struct {
// contains filtered or unexported fields
}
Program is a compiled script. It is safe for concurrent use: a call takes a Lua state of its own.
func Compile ¶
Compile parses source, reads the hooks its annotations declare and runs it once. A syntax error, a misplaced annotation or an error of the script body is returned here, not at the first request.
func (*Program) Call ¶
func (p *Program) Call(ctx context.Context, at phase.Phase, stage string, ex exchange.Exchange) (bool, error)
Call runs the functions hooking the stage at phase, in source order, until one answers the request, and reports whether one did. They share the time limit. An error, a timeout included, stops there and discards the Lua state.
func (*Program) Has ¶
Has reports whether a function of the script hooks the stage at the given phase.
func (*Program) HasFunc ¶
HasFunc reports whether the script body defines the global function name: a rule checks its entry point at load, not at the first request.
func (*Program) Invoke ¶
Invoke calls the global function fn, a rule, with a read-only ctx on ex and returns its result. An error, a timeout included, discards the Lua state.
type Rule ¶
Rule the declarative form of a rule script: it matches a request when Match does. A secrule takes Action, a rate limiter counts the matching requests under the client IP.
MatchSource and KeySource write it as Lua, ParseMatch and ParseKey read it back from a script of that form, hand written or not:
function match(ctx)
if ctx:path() == "/admin" and not (ctx:ip() == "10.0.0.1") then
return SZ_BLOCK
end
if ctx:has_header("X-Debug") or re.match(ctx:path(), "^/wp-") then
return SZ_BLOCK
end
end
func ParseMatch ¶
ParseMatch reads the declarative Rule of a secrule script (see Rule): one function match(ctx) whose body is a list of
if <condition> then return <action> end
every branch returning the same action, the last statement possibly an unconditional return. A condition combines with and, or, not and parentheses the comparisons MatchSource writes. Any other script returns an error wrapping ErrNotDeclarative, with the line at fault.
type Source ¶
type Source int
Source a field of the request a condition reads, through the ctx method of the same name.
const ( // SourcePath ctx:path(). SourcePath Source = iota + 1 // SourceMethod ctx:method(). SourceMethod // SourceHost ctx:host(). SourceHost // SourceIP ctx:ip(). SourceIP // SourceHeader ctx:header(key). SourceHeader // SourceQuery ctx:query(key). SourceQuery // SourceBody ctx:body(). SourceBody // SourceJA4 ctx:ja4(). SourceJA4 // SourceTLS ctx:tls(), a boolean: its conditions are OpEq or OpNe to // "true" or "false", written ctx:tls() and not ctx:tls(). SourceTLS )