Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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.
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 ¶
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 FetchSiteDecider ¶
FetchSiteDecider decides whether fetch metadata permits a request.
type Reader ¶
Reader extracts candidate request tokens.
func FromFirst ¶
FromFirst tries token sources in order. A source may return multiple candidates; verification succeeds when any candidate matches the cookie.
func FromForm ¶
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 ¶
FromHeader reads token candidates from a request header.