Documentation
¶
Overview ¶
Package httpsec holds the Secure cookie rewriting and the Origin allowlist check.
Index ¶
- Constants
- func ApplySecurityHeaders(h http.Header, secure bool)
- func NoStorePath(path, prefix string) bool
- func NormalizeOrigins(in []string) ([]string, error)
- func OriginAllowed(r *http.Request, baseURL string, trusted []string) bool
- func ParseSameSite(s string) (http.SameSite, error)
- func SecurityHeadersMiddleware(isSecure func(*http.Request) bool) func(http.Handler) http.Handler
- type CookiePolicy
- func (p CookiePolicy) Clear(w http.ResponseWriter, name, path string)
- func (p CookiePolicy) ForCrossSiteReturn() CookiePolicy
- func (p CookiePolicy) New(name, value, path string, expires time.Time) (*http.Cookie, error)
- func (p CookiePolicy) NewClear(name, path string) (*http.Cookie, error)
- func (p CookiePolicy) Set(w http.ResponseWriter, name, value, path string, expires time.Time)
- func (p CookiePolicy) Validate(name, path string) error
- func (p CookiePolicy) WithSecure(secure bool) CookiePolicy
- type CookieWriter
Constants ¶
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.
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).
const SecureCookieAttr = "Secure"
SecureCookieAttr is the cookie attribute CookieWriter appends.
Variables ¶
This section is empty.
Functions ¶
func ApplySecurityHeaders ¶ added in v2.12.0
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
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 ¶
NormalizeOrigins canonicalizes Config.TrustedOrigins entries.
func OriginAllowed ¶
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
ParseSameSite maps the Config string form ("", "lax", "strict", "none") to http.SameSite.
func SecurityHeadersMiddleware ¶ added in v2.12.0
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
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) WriteHeader ¶
func (w *CookieWriter) WriteHeader(code int)