csrf

package
v1.0.0 Latest Latest
Warning

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

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

Documentation

Overview

Package csrf provides a double-submit-cookie CSRF middleware for VULN-008.

Flow:

  1. Middleware issues a signed token cookie (_csrf) on every request.
  2. For mutating methods (POST/PUT/PATCH/DELETE) it validates the token from the X-CSRF-Token header (HTMX) or the _csrf form field.
  3. TokenFromContext exposes the token to templ components via context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(key []byte, secure bool) func(http.Handler) http.Handler

Middleware generates or reuses a signed CSRF token per session, stores it in a SameSite=Strict cookie and in the request context, and validates it on mutating requests. Pass secure=true in production so the cookie is HTTPS-only.

func TokenFromContext

func TokenFromContext(ctx context.Context) string

TokenFromContext returns the CSRF token injected by Middleware. Call this from templ components to populate hidden form inputs.

Types

This section is empty.

Jump to

Keyboard shortcuts

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