Help us shape the future of Go! Take the 2026 Go Developer Survey

security

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientKey added in v0.3.0

func ClientKey(trustedProxies []*net.IPNet) func(*http.Request) string

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

func DeriveCSRFToken(secret string) string

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

func IsHTTPS(r *http.Request) bool

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

func IsLoopbackHost(host string) bool

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

func IsSafeRedirect(raw string, allowedPrefix string) bool

IsSafeRedirect reports whether raw is a same-application redirect target under allowedPrefix, rejecting absolute URLs and protocol-relative URLs.

func SafeRedirect

func SafeRedirect(raw string, allowedPrefix string, fallback string) string

SafeRedirect returns raw when it is safe for allowedPrefix, otherwise fallback.

func VerifyCSRFToken

func VerifyCSRFToken(submitted string, secret string) bool

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.

Jump to

Keyboard shortcuts

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