csrf

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package csrf implements the double-submit cookie pattern for plain HTML forms on cacheable pages.

Issue wraps the page handler so every GET/HEAD leaves a random token in a cookie (sliding expiry). The token is never embedded in the HTML, so pages stay cacheable; a small script copies the cookie into a hidden form field at submit time (uikit ships one: js/public/csrf.js). Verify then checks that the posted field equals the cookie. A cross-site attacker can make the browser send the cookie but cannot read it to fill the field, and the cookie is SameSite=Strict on top.

guard := csrf.New(csrf.Options{}) // Secure cookie unless HB_ENV=local
mux.Handle("/", guard.Issue(pages))
// in the POST handler:
if err := guard.Verify(r); err != nil { ... reject ... }

Index

Constants

View Source
const (
	DefaultCookie = "hbl_csrf"
	DefaultField  = "_csrf"
	DefaultMaxAge = 30 * 24 * time.Hour
)

Defaults applied by New.

Variables

View Source
var (
	ErrCookieMissing = errors.New("csrf: cookie missing")
	ErrFieldEmpty    = errors.New("csrf: form field empty")
	ErrMismatch      = errors.New("csrf: cookie and form field differ")
)

Verification errors.

Functions

This section is empty.

Types

type Guard

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

Guard issues and verifies double-submit tokens. Safe for concurrent use.

func New

func New(opts Options) *Guard

New applies defaults to opts. Whether the cookie is Secure is decided here, from opts.Insecure and HB_ENV.

func (*Guard) Cookie

func (g *Guard) Cookie() string

Cookie returns the cookie name.

func (*Guard) Field

func (g *Guard) Field() string

Field returns the form field name.

func (*Guard) Issue

func (g *Guard) Issue(next http.Handler) http.Handler

Issue wraps next so every GET/HEAD response carries the token cookie, reusing a well-formed existing token and refreshing its expiry. If no random token can be made the cookie is skipped, so the next POST fails closed.

func (*Guard) Verify

func (g *Guard) Verify(r *http.Request) error

Verify checks the posted field against the cookie in constant time. It parses the form if needed.

type Options

type Options struct {
	// Cookie is the token cookie name. It must match what the
	// client-side copy script reads.
	Cookie string
	// Field is the hidden form field name.
	Field string
	// Insecure drops the cookie's Secure flag. The flag is set by
	// default and dropped automatically in local dev
	// (siteurl.IsDev(), HB_ENV=local), where pages run on plain
	// http://localhost; set Insecure only for another non-TLS setup.
	Insecure bool
	// MaxAge is the sliding cookie lifetime.
	MaxAge time.Duration
}

Options configures a Guard. Zero values take the defaults above.

Jump to

Keyboard shortcuts

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