security

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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