httpsec

package
v2.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 7 Imported by: 0 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 httpsec holds the Secure cookie rewriting and the Origin allowlist check.

Index

Constants

View Source
const (
	// HostPrefix marks a cookie browsers accept only when Secure, Path=/ and without Domain.
	HostPrefix = "__Host-"
	// SecurePrefix marks a cookie browsers accept only when Secure.
	SecurePrefix = "__Secure-"
)

Cookie name prefixes defined by RFC 6265bis.

View Source
const HSTSValue = "max-age=31536000; includeSubDomains"

HSTSValue is the Strict-Transport-Security value ApplySecurityHeaders sends: one year, subdomains included, no preload (preload is an irreversible opt in the operator makes on their own).

View Source
const SecureCookieAttr = "Secure"

SecureCookieAttr is the cookie attribute CookieWriter appends.

Variables

This section is empty.

Functions

func ApplySecurityHeaders added in v2.12.0

func ApplySecurityHeaders(h http.Header, secure bool)

ApplySecurityHeaders sets the opt-in response hardening headers on h: X-Content-Type-Options, Referrer-Policy (no-referrer, so magic-link and reset tokens in URLs never leak), X-Frame-Options and, when secure is true, Strict-Transport-Security. Headers a handler sets later win.

func NoStorePath added in v2.12.0

func NoStorePath(path, prefix string) bool

NoStorePath reports whether path is under prefix, the route tree whose session and credential responses must never sit in a shared or browser cache.

func NormalizeOrigins

func NormalizeOrigins(in []string) ([]string, error)

NormalizeOrigins canonicalizes Config.TrustedOrigins entries.

func OriginAllowed

func OriginAllowed(r *http.Request, baseURL string, trusted []string) bool

OriginAllowed is the CSRF check for cookie-authenticated unsafe requests. Safe methods and bearer requests are exempt. Otherwise Origin or Referer must be trusted, and Sec-Fetch-Site decides when neither is sent. SameSite=Lax alone does not stop sibling subdomains.

func ParseSameSite added in v2.12.0

func ParseSameSite(s string) (http.SameSite, error)

ParseSameSite maps the Config string form ("", "lax", "strict", "none") to http.SameSite.

func SecurityHeadersMiddleware added in v2.12.0

func SecurityHeadersMiddleware(isSecure func(*http.Request) bool) func(http.Handler) http.Handler

SecurityHeadersMiddleware applies ApplySecurityHeaders to every response. isSecure decides per request whether HSTS is sent (it must be ignored over plain http).

Types

type CookiePolicy added in v2.12.0

type CookiePolicy struct {
	// Secure is the static Secure flag. The per-request upgrade in CookieWriter can still add
	// Secure on a TLS request, but prefix, SameSite=None and Partitioned rules look at this value.
	Secure bool
	// Domain, when set, shares the cookie with every subdomain. Empty keeps it host only.
	Domain string
	// SameSite is Lax, Strict or None. The zero value means Lax.
	SameSite http.SameSite
	// Partitioned adds the CHIPS Partitioned attribute. It requires Secure.
	Partitioned bool
}

CookiePolicy is the one attribute set every Set-Cookie the library writes goes through. HttpOnly is always on. The zero value reproduces the historic behavior: SameSite=Lax, no Domain, not Partitioned, and Secure only as far as Secure says.

func (CookiePolicy) Clear added in v2.12.0

func (p CookiePolicy) Clear(w http.ResponseWriter, name, path string)

Clear writes the expiring counterpart of a cookie, see NewClear.

func (CookiePolicy) ForCrossSiteReturn added in v2.12.0

func (p CookiePolicy) ForCrossSiteReturn() CookiePolicy

ForCrossSiteReturn returns p with SameSite=Strict relaxed to Lax. Cookies read on a top-level cross-site redirect back (the OAuth state cookie) are not sent under Strict.

func (CookiePolicy) New added in v2.12.0

func (p CookiePolicy) New(name, value, path string, expires time.Time) (*http.Cookie, error)

New builds a cookie with value, valid until expires (zero means a session cookie), after validating the policy against name and path. It sets both Expires and Max-Age.

func (CookiePolicy) NewClear added in v2.12.0

func (p CookiePolicy) NewClear(name, path string) (*http.Cookie, error)

NewClear builds the expiring counterpart of a cookie. It repeats Domain, Path, Secure and Partitioned, which browsers require to match before they delete the stored cookie.

func (CookiePolicy) Set added in v2.12.0

func (p CookiePolicy) Set(w http.ResponseWriter, name, value, path string, expires time.Time)

Set writes the cookie. An invalid policy is rejected at startup, so a failure here is a bug: it is logged and the cookie is not written, never written with weaker attributes.

func (CookiePolicy) Validate added in v2.12.0

func (p CookiePolicy) Validate(name, path string) error

Validate rejects attribute combinations browsers drop or that weaken a prefix guarantee. name and path are the cookie being written. It is cheap, so Config validation runs it at startup for the session cookie and the builder runs it again on every write.

func (CookiePolicy) WithSecure added in v2.12.0

func (p CookiePolicy) WithSecure(secure bool) CookiePolicy

WithSecure returns p with Secure turned on when secure is true. It never turns Secure off.

type CookieWriter

type CookieWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

CookieWriter adds the Secure attribute to every Set-Cookie header at write time, so handlers in internal packages need no per-request flag.

func NewCookieWriter

func NewCookieWriter(w http.ResponseWriter) *CookieWriter

NewCookieWriter wraps w so every Set-Cookie carries Secure.

func (*CookieWriter) Flush

func (w *CookieWriter) Flush()

func (*CookieWriter) Unwrap

func (w *CookieWriter) Unwrap() http.ResponseWriter

Unwrap lets http.ResponseController reach Hijack and deadlines.

func (*CookieWriter) Write

func (w *CookieWriter) Write(b []byte) (int, error)

func (*CookieWriter) WriteHeader

func (w *CookieWriter) WriteHeader(code int)

Jump to

Keyboard shortcuts

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