Documentation
¶
Index ¶
- func ClientKey(trustedProxies []*net.IPNet) func(*http.Request) string
- func DeriveCSRFToken(secret string) string
- func IsHTTPS(r *http.Request) bool
- func IsLoopbackHost(host string) bool
- func IsSafeRedirect(raw string, allowedPrefix string) bool
- func SafeRedirect(raw string, allowedPrefix string, fallback string) string
- func VerifyCSRFToken(submitted string, secret string) bool
- type RateLimiter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClientKey ¶ added in v0.3.0
ClientKey returns the function a failed-attempt limiter uses to name the client behind a request. With no trustedProxies it is the connection's remote host and no header is read, which is what these limiters have always done; with them, the rules are internal/clientip's, the same ones ratelimit.RemoteIPKey uses.
func DeriveCSRFToken ¶
DeriveCSRFToken derives a form-safe CSRF token from a secret value such as a session token. The secret itself is never rendered into the form.
func IsHTTPS ¶ added in v0.1.0
IsHTTPS reports whether the client reached the app over HTTPS: either the connection itself is TLS, or a proxy that terminated TLS in front of the app says so in X-Forwarded-Proto or Forwarded (the first hop wins).
It decides cookies' Secure flag, where trusting the headers is safe: a client that forges "https" only gets a cookie its own browser won't send back over plain HTTP. Don't use it for security decisions that a forged header could weaken.
func IsLoopbackHost ¶ added in v0.2.0
IsLoopbackHost reports whether host is "localhost" or a loopback IP literal. It never resolves a name, so a DNS answer can't make a remote host count as local.
func IsSafeRedirect ¶
IsSafeRedirect reports whether raw is a same-application redirect target under allowedPrefix, rejecting absolute URLs and protocol-relative URLs.
func SafeRedirect ¶
SafeRedirect returns raw when it is safe for allowedPrefix, otherwise fallback.
func VerifyCSRFToken ¶
VerifyCSRFToken reports whether submitted matches the token derived from secret, using a constant-time comparison.
Types ¶
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter tracks recent failed attempts per caller-provided key, usually a source IP address. It is intentionally in-memory and single-process: callers that need cross-process throttling should provide their own middleware or service-level protection.
func NewRateLimiter ¶
func NewRateLimiter(maxAttempts int, window time.Duration) *RateLimiter
NewRateLimiter constructs a per-key sliding-window limiter. A key is allowed while it has fewer than maxAttempts recorded failures within window.
func (*RateLimiter) Allow ¶
func (l *RateLimiter) Allow(key string) bool
Allow reports whether key is currently under the failed-attempt threshold, pruning older attempts as it goes.
func (*RateLimiter) RecordFailure ¶
func (l *RateLimiter) RecordFailure(key string)
RecordFailure records a failed attempt for key.