Documentation
¶
Overview ¶
Package abuse provides behavioral analysis middleware for Echo v5. It tracks client reputation via a scoring system to mitigate scrapers and scanners.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultConfig = Config{ Skipper: middleware.DefaultSkipper, KeyFunc: func(c *echo.Context) string { return c.RealIP() }, Threshold: 100, Cooldown: 30 * time.Minute, MaxDelay: 2 * time.Second, DelayFunc: func(score int) time.Duration { return time.Duration(score) * 10 * time.Millisecond }, ErrorHandler: func(c *echo.Context, score int) error { return echo.NewHTTPError(http.StatusForbidden, "Access denied due to suspicious activity") }, }
DefaultConfig provides the standard settings for abuse detection middleware.
Functions ¶
func New ¶
func New(config Config) echo.MiddlewareFunc
New returns an Echo middleware that monitors and limits abusive behavior. It should be placed early in the middleware stack to protect downstream resources.
Example ¶
package main
import (
"github.com/its-ernest/http-keystone/abuse"
"github.com/labstack/echo/v5"
)
func main() {
e := echo.New()
// Instant-ban on sensitive files, warn on API spam
e.Use(abuse.New(abuse.Config{
Threshold: 100,
Rules: []abuse.Rule{
// immediate ban for ANY attempt on .env
{Path: "/.env", Score: 100},
// heavy score only for POST requests to login (brute force protection)
{Path: "/api/v1/login", Method: "POST", Score: 20},
// light score for general API exploration
{Path: "/api/v1/*", Method: "GET", Score: 2},
},
}))
}
Output:
func NewForwardedIPKeyFunc ¶ added in v0.2.0
func NewForwardedIPKeyFunc(options ...echo.TrustOption) func(c *echo.Context) string
NewForwardedIPKeyFunc returns a KeyFunc that resolves the client IP from X-Forwarded-For, trusting only the direct peer when that peer is a trusted proxy. Pass echo.TrustIPRange (and the other echo.Trust* options) to describe your proxies; by default loopback, link-local and private ranges are trusted.
Use it when the middleware runs behind a reverse proxy and you do not want to set echo.Echo.IPExtractor globally:
e.Use(abuse.New(abuse.Config{
Store: counter,
KeyFunc: abuse.NewForwardedIPKeyFunc(echo.TrustIPRange(proxyCIDR)),
}))
Without trusted proxies (for example when the app faces the internet directly), use echo.ExtractIPDirect with NewIPKeyFunc so headers are ignored completely.
func NewIPKeyFunc ¶ added in v0.2.0
func NewIPKeyFunc(extractor echo.IPExtractor) func(c *echo.Context) string
NewIPKeyFunc adapts an echo.IPExtractor into a KeyFunc. A nil extractor falls back to c.RealIP().
Types ¶
type Config ¶
type Config struct {
// Skipper defines a function to skip middleware. Defaults to
// middleware.DefaultSkipper.
Skipper middleware.Skipper
// KeyFunc derives the identity used to track heat. The returned string
// is appended to "abuse:heat:" as the store key.
//
// The default is c.RealIP(). Note that c.RealIP() trusts
// X-Forwarded-For / X-Real-IP when Echo has an IPExtractor configured,
// and Echo's default extracts the header values without verification.
// Behind a proxy, configure e.IPExtractor (see
// echo.ExtractIPFromXFFHeader) or set KeyFunc to something the client
// cannot choose. See NewForwardedIPKeyFunc for a ready-made helper.
KeyFunc func(c *echo.Context) string
Store store.Counter
// Threshold is the heat score at which requests are rejected.
// Values <= 0 fall back to DefaultConfig.Threshold (100) so a zero
// Config can never block every request.
Threshold int
Rules []Rule
// Cooldown is how long heat is remembered. Values <= 0 fall back to
// DefaultConfig.Cooldown (30 minutes).
Cooldown time.Duration
// EnableDelay adds latency to suspicious requests instead of a hard block.
EnableDelay bool
// MaxDelay caps the tarpit delay applied when EnableDelay is set.
// Values <= 0 fall back to DefaultConfig.MaxDelay (2s).
MaxDelay time.Duration
// DelayFunc computes the tarpit delay for a heat score. The result is
// clamped to MaxDelay and never exceeds it.
// Values <= 0 mean no delay.
// Default is a linear curve of score*10ms (e.g. 75 heat -> 750ms).
DelayFunc func(score int) time.Duration
// Jitter adds +/-20% randomness to the computed delay so delayed
// clients do not retry in lockstep. The clamped MaxDelay is still
// honoured.
Jitter bool
ErrorHandler func(c *echo.Context, score int) error
}
Config defines the settings for the abuse detection middleware.
type Rule ¶
type Rule struct {
// Path is the URL path to match. It supports exact matches and a
// trailing "*" wildcard on a path-segment boundary; Echo's router is
// not involved. Matching is case-sensitive and ignores query strings.
Path string
// Method is the HTTP method to match (GET, POST, etc.). Empty means match all.
Method string
// Score is the amount of *heat* to add when this rule matches
Score int
}
Rule defines the "heat" points added for specific path patterns. Path supports exact matches (e.g., "/.env") and a trailing "*" wildcard on a path-segment boundary: "/admin*" matches "/admin" and "/admin/config" but not "/administrator".