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

csrf

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// CSRF errors are stable sentinels for custom policy and telemetry.
	ErrTokenMissing      = errors.New("csrf: request token missing")
	ErrCookieMissing     = errors.New("csrf: csrf cookie missing")
	ErrTokenInvalid      = errors.New("csrf: request token invalid")
	ErrFetchSiteRejected = errors.New("csrf: request rejected by fetch metadata")
)

Functions

func Doc added in v0.7.1

func Doc() zinc.MiddlewareDoc

Doc describes the middleware with its default Readers for the OpenAPI spec: requests with an unsafe method send the token in the X-CSRF-Token header, and are refused with 400 when it's missing and 403 when it doesn't match. The token is a security scheme named "csrf", required with each route's own security, so a generated client sets it once rather than on every call. Pass it to Document beside the middleware:

api.Use(csrf.New())
api.Document(csrf.Doc())

With other Readers, describe them in a zinc.MiddlewareDoc of your own.

func New

func New(configs ...Config) zinc.Middleware

New issues tokens on safe methods and requires a constant-time cookie/request-token match on unsafe methods. Cookies alone are never proof.

func Token

func Token(c *zinc.Context) string

Token returns the issued or verified token.

Types

type Config

type Config struct {
	Readers        []Reader
	Generate       Generator
	TokenBytes     int
	Cookie         Cookie
	ExposeHeader   string
	TrustedOrigins []string
	AllowFetchSite FetchSiteDecider
	ErrorHandler   func(*zinc.Context, error) error
}

Config controls double-submit validation and fetch-metadata policy.

type Cookie struct {
	Name     string
	Domain   string
	Path     string
	MaxAge   int
	Secure   bool
	HTTPOnly bool
	SameSite http.SameSite
}

Cookie controls the double-submit token cookie.

type Decision

type Decision struct {
	Site    FetchSite
	Origin  string
	Trusted bool
}

Decision describes fetch-metadata information for policy decisions.

type FetchSite

type FetchSite string

FetchSite represents a normalized Sec-Fetch-Site value.

const (
	FetchSiteSameOrigin FetchSite = "same-origin"
	FetchSiteSameSite   FetchSite = "same-site"
	FetchSiteCrossSite  FetchSite = "cross-site"
	FetchSiteNone       FetchSite = "none"
)

type FetchSiteDecider

type FetchSiteDecider func(*zinc.Context, Decision) (bool, error)

FetchSiteDecider decides whether fetch metadata permits a request.

type Generator

type Generator func(*zinc.Context) (string, error)

Generator creates a new token value.

type Reader

type Reader func(*zinc.Context) ([]string, error)

Reader extracts candidate request tokens.

func FromFirst

func FromFirst(readers ...Reader) Reader

FromFirst tries token sources in order. A source may return multiple candidates; verification succeeds when any candidate matches the cookie.

func FromForm

func FromForm(name string) Reader

FromForm reads a token from URL-encoded or multipart form input. The form is parsed through Zinc, so Config.BodyLimit applies: a larger body is answered with 413.

func FromHeader

func FromHeader(header string) Reader

FromHeader reads token candidates from a request header.

func FromQuery

func FromQuery(name string) Reader

FromQuery reads a token from a query parameter.

type Reason

type Reason string

Reason classifies why a request failed validation.

const (
	ReasonTokenMissing      Reason = "token_missing"
	ReasonCookieMissing     Reason = "cookie_missing"
	ReasonTokenInvalid      Reason = "token_invalid"
	ReasonFetchSiteRejected Reason = "fetch_site_rejected"
)

type State

type State struct {
	Token      string
	CookieName string
	Issued     bool
	Verified   bool
	FetchSite  FetchSite
}

State records token issuance or verification for the current request.

func Get

func Get(c *zinc.Context) (State, bool)

Get returns CSRF state for the current request.

func MustGet

func MustGet(c *zinc.Context) State

MustGet returns CSRF state or panics when middleware did not set it.

type Violation

type Violation struct {
	Reason     Reason
	CookieName string
	FetchSite  FetchSite
	Origin     string
	Cause      error
}

Violation preserves a machine-readable failure reason and context.

func (*Violation) Error

func (e *Violation) Error() string

func (*Violation) Is

func (e *Violation) Is(target error) bool

func (*Violation) Unwrap

func (e *Violation) Unwrap() error

Jump to

Keyboard shortcuts

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