waf

package module
v0.36.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 20 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Reference tests codecov GitHub release (latest SemVer) GitHub go.mod Go version

WAF

WAF is an experimental middleware for the Fox router that integrates the Coraza Web Application Firewall (WAF) to enhance the security of your web applications by intercepting and analyzing HTTP requests and responses.

Disclaimer

The current API is not yet stabilized. Breaking changes may occur before v1.0.0 and will be noted on the release note.

Getting Started

Installation

go get -u github.com/fox-toolkit/waf
Usage

Here is an example to load OWASP CRS using coraza-coreruleset.

package main

import (
	"errors"
	"fmt"
	"log"
	"net/http"

	corazaconf "github.com/corazawaf/coraza-coreruleset/coraza/v3"
	crs "github.com/corazawaf/coraza-coreruleset/crs/v4"
	"github.com/corazawaf/coraza/v3"
	"github.com/fox-toolkit/fox"
	"github.com/fox-toolkit/waf"
	"github.com/jcchavezs/mergefs"
)

func main() {

	cfg := coraza.NewWAFConfig().
		WithDirectives("Include @coraza.conf-recommended").
		WithDirectives("Include @crs-setup.conf.example").
		WithDirectives("Include @owasp_crs/*.conf").
		WithDirectives("SecRuleEngine On").
		WithRootFS(mergefs.Merge(crs.FS, corazaconf.FS))

	co, err := coraza.NewWAF(cfg)
	if err != nil {
		panic(err)
	}

	f := fox.MustRouter(
		fox.WithMiddleware(waf.Middleware(co)),
	)

	f.MustAdd(fox.MethodGet, "/hello/{name}", func(c *fox.Context) {
		_ = c.String(http.StatusOK, fmt.Sprintf("Hello, %s", c.Param("name")))
	})

	if err = http.ListenAndServe(":8080", f); err != nil && !errors.Is(err, http.ErrServerClosed) {
		log.Fatalln(err)
	}
}
curl -sS -D - "http://localhost:8080/hello/fox?path=../foo"
# HTTP/1.1 403 Forbidden
# Date: Mon, 15 Jul 2024 14:52:24 GMT
# Content-Length: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(waf coraza.WAF) fox.MiddlewareFunc

Middleware creates a new Fox middleware function using the provided Coraza WAF instance. It intercepts incoming requests and processes them through coraza.WAF before passing them to the next handler.

func WithTransactionHook added in v0.36.0

func WithTransactionHook(hooks ...Hook) fox.RouteOption

WithTransactionHook attaches hooks to a route. For every request matching the route, the middleware calls the hooks in the given order, once the connection, URI and request headers are populated in the transaction and right before the request headers phase (phase 1) is evaluated. Hooks are not called when the rule engine is off. Applying this option more than once to the same route replaces the previously attached hooks. It panics if a hook is nil.

Types

type Context added in v0.36.0

type Context interface {
	Request() *http.Request
	RemoteIP() netip.Addr
	ClientIP() (netip.Addr, error)
	Method() string
	RoutingPath() string
	Host() string
	QueryParams() url.Values
	QueryParam(name string) string
	Header(key string) string
	Route() *fox.Route
	Param(name string) string
	Params() iter.Seq[fox.Param]
	// contains filtered or unexported methods
}

Context is the subset of the fox.Context API passed to a Hook, without access to the response.

type Hook added in v0.36.0

type Hook func(c Context, tx types.Transaction)

Hook customizes a transaction before Coraza evaluates any rule. It must not retain c or tx.

func RemoveRuleRange added in v0.36.0

func RemoveRuleRange(start, end int) Hook

RemoveRuleRange returns a Hook that removes the rules whose ID is between start and end inclusive from the transaction, the per-route equivalent of ctl:ruleRemoveById with a range. The rules of the WAF are not affected. Removing rules requires a transaction created by the Coraza WAF implementation. For any other transaction, the hook is a no-op. It panics if start is greater than end.

func RemoveRules added in v0.36.0

func RemoveRules(ids ...int) Hook

RemoveRules returns a Hook that removes the rules with the given IDs from the transaction, the per-route equivalent of ctl:ruleRemoveById. The rules of the WAF are not affected. Removing rules requires a transaction created by the Coraza WAF implementation. For any other transaction, the hook is a no-op.

func TxVar added in v0.36.0

func TxVar(key, value string) Hook

TxVar returns a Hook that sets the transaction variable TX:key to value, overwriting any previous value. Since rules read TX variables at evaluation time, this is the way to configure per-route behaviors that rule sets expose through TX variables, such as enabling an OWASP CRS plugin or setting the paranoia level. Setting TX variables requires a transaction that implements plugintypes.TransactionState, as the transactions of the Coraza WAF implementation do. For any other transaction, the hook is a no-op.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL