csrf

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Protect

func Protect() arx.Middleware

Protect returns a Middleware with default configuration. Requires session.Middleware() to run before it in the chain.

func TemplateField

func TemplateField(r *http.Request) string

TemplateField returns an HTML hidden input element for use in server-rendered forms. Output: <input type="hidden" name="csrf_token" value="<token>"> The csrf middleware must run before this is called.

func Token

func Token(r *http.Request) string

Token returns the CSRF token for the current request from the request context. Returns an empty string if the csrf middleware was not applied.

Types

type Config

type Config struct {
	// SessionKey is the session key under which the CSRF token is stored.
	// Default: "_csrf".
	SessionKey string
	// HeaderName is the request header checked on unsafe methods.
	// Default: "X-CSRF-Token".
	HeaderName string
	// FieldName is the HTML form field name checked as a fallback to the header.
	// Default: "csrf_token".
	FieldName string
	// ErrorHandler is called when CSRF validation fails.
	// Default: returns 403 Forbidden with body {"error":"csrf validation failed"}.
	ErrorHandler func(*arx.Context) error
}

Config holds the configuration for a Protector.

type Protector

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

Protector enforces CSRF token validation.

func New

func New(cfg Config) *Protector

New creates a Protector from cfg. Zero-value fields are filled with secure defaults.

func (*Protector) Middleware

func (p *Protector) Middleware() arx.Middleware

Middleware returns an arx.Middleware that enforces CSRF protection. On safe methods: injects the token as X-CSRF-Token response header. On unsafe methods: validates the submitted token; rejects with 403 on mismatch.

Jump to

Keyboard shortcuts

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