httpsecurity

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CSRFFormField = "_csrf"
	CSRFHeader    = "X-CSRF-Token"
)

Variables

This section is empty.

Functions

func CSRFTokenFromContext

func CSRFTokenFromContext(ctx context.Context) string

CSRFTokenFromContext returns the token installed by WithCSRFToken, if any.

func EnsureCSRFToken

func EnsureCSRFToken(c fiber.Ctx, store *session.Store) (string, error)

EnsureCSRFToken returns the session token, creating one when the browser session does not have a token yet.

func ExistingCSRFToken

func ExistingCSRFToken(c fiber.Ctx, store *session.Store) (string, error)

ExistingCSRFToken returns the session token only when the request already carries a session lookup value. It never creates or saves a new session.

func HasSessionLookup

func HasSessionLookup(c fiber.Ctx, store *session.Store) bool

HasSessionLookup reports whether the request includes the configured session lookup value. It lets backend middleware avoid creating sessions for generic API clients that did not send browser cookies.

func HashKey

func HashKey(key string) string

HashKey returns a short stable hash for audit logs so raw emails and full throttle keys are never logged.

func NormalizeEmail

func NormalizeEmail(email string) string

NormalizeEmail canonicalizes email input for throttle keys without changing the value passed to downstream validation or auth services.

func SafeMethod

func SafeMethod(method string) bool

SafeMethod reports whether a method is defined as safe for CSRF purposes.

func ValidateCSRFToken

func ValidateCSRFToken(c fiber.Ctx, store *session.Store) (bool, error)

ValidateCSRFToken compares the submitted header or form token with the session token without exposing token values to logs or errors.

func WithCSRFToken

func WithCSRFToken(ctx context.Context, token string) context.Context

WithCSRFToken stores the current request's CSRF token in context so frontend server-to-backend API calls can forward it when they also forward cookies.

Types

type AbuseDecision

type AbuseDecision struct {
	Allowed bool
	ResetAt time.Time
}

AbuseDecision reports whether the operation may proceed and when a denied window resets.

type AbuseLimit

type AbuseLimit struct {
	Scope  string
	Key    string
	Limit  int
	Window time.Duration
}

AbuseLimit describes one shared fixed-window budget. Scope must identify the operation class; Key must identify the client or tenant without containing a credential or other secret.

type AbuseLimiter

type AbuseLimiter interface {
	Attempt(context.Context, AbuseLimit) (AbuseDecision, error)
}

AbuseLimiter is the storage-independent boundary used by HTTP middleware. Production implementations must share state across replicas and restarts.

type MemoryAbuseLimiter

type MemoryAbuseLimiter struct {
	// contains filtered or unexported fields
}

MemoryAbuseLimiter is a deterministic test/local adapter. Production wiring uses the Postgres implementation so replacing a process cannot reset limits.

func NewMemoryAbuseLimiter

func NewMemoryAbuseLimiter() *MemoryAbuseLimiter

NewMemoryAbuseLimiter creates one in-memory limiter.

func (*MemoryAbuseLimiter) Attempt

Attempt atomically records one request against the supplied operation budget.

type Throttle

type Throttle struct {
	// contains filtered or unexported fields
}

func NewThrottle

func NewThrottle(limit int, window time.Duration) *Throttle

NewThrottle creates a process-local fixed-window throttle. It is deliberately in-memory: reverse proxy limits remain the distributed outer layer.

func (*Throttle) Allow

func (t *Throttle) Allow(key string, now time.Time) bool

Allow records one attempt for key and reports whether it remains under the current fixed-window limit.

Jump to

Keyboard shortcuts

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