csrfx

package
v0.19.4 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Code generated by apic; DO NOT EDIT.

Package csrfx implements stateless, HMAC-signed, session-bound CSRF tokens using the signed double-submit pattern (OWASP). A token embeds a random nonce, an expiry, and an HMAC binding it to a session identifier; the generated middleware additionally requires the cookie value to equal the header value (double-submit) before verifying the signature. Code generated by apic; DO NOT EDIT.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCSRFMalformed indicates the token is not valid base64url or has the
	// wrong length/version.
	ErrCSRFMalformed = errors.New("csrfx: malformed token")
	// ErrCSRFExpired indicates the token's embedded expiry is in the past.
	ErrCSRFExpired = errors.New("csrfx: token expired")
	// ErrCSRFMismatch indicates the HMAC did not verify (tampered token or
	// wrong key) or the double-submit cookie/header pair did not match.
	ErrCSRFMismatch = errors.New("csrfx: token signature mismatch")
	// ErrCSRFSessionMismatch indicates the token was issued for a different
	// session than the one presenting it.
	ErrCSRFSessionMismatch = errors.New("csrfx: token session mismatch")
	// ErrCSRFUnbound indicates Verify was called with an empty sessionID.
	// Issue("") still mints a token bound to "" (used for the best-effort
	// pre-login /csrf mint, see api.go.tmpl) but Verify refuses to accept
	// ANY token — even one whose signature is otherwise valid — against an
	// empty session: every caller lacking a session would collapse onto the
	// same "" binding, making a pre-login-minted token replayable by anyone
	// who never authenticates (SEC-0061). Callers must resolve a real
	// session id (typically the verified JWT "sub") before Verify.
	ErrCSRFUnbound = errors.New("csrfx: token not bound to a session")
	// ErrKeyTooShort indicates the signing key is shorter than the 32-byte
	// minimum.
	ErrKeyTooShort = errors.New("csrfx: signing key must be at least 32 bytes")
)

Functions

This section is empty.

Types

type Signer

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

Signer issues and verifies signed, session-bound CSRF tokens.

func New

func New(key []byte, ttl time.Duration) (*Signer, error)

New returns a Signer. key must be at least 32 bytes; it is copied.

func (*Signer) Issue

func (s *Signer) Issue(sessionID string) (string, error)

Issue mints a token bound to sessionID, expiring after the Signer's ttl.

func (*Signer) Verify

func (s *Signer) Verify(token, sessionID string) error

Verify checks the token's signature, session binding, and expiry. A token minted for a different session fails the HMAC check and returns ErrCSRFMismatch (the binding is cryptographic, so wrong-session and tamper are indistinguishable at this layer).

SEC-0061: an empty sessionID is refused unconditionally with ErrCSRFUnbound, before the token is even decoded. Issue("") remains legal (the pre-login /csrf mint binds to "" and re-mints once the caller authenticates), but Verify treating "" as a valid binding would let every unauthenticated caller share and replay the same token.

Jump to

Keyboard shortcuts

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