Package com.jsgalactic.axiom.security
Class Csrf.Builder
java.lang.Object
com.jsgalactic.axiom.security.Csrf.Builder
- Enclosing class:
Csrf
Builds
Csrf; used during configuration, not thread-safe.-
Method Summary
Modifier and TypeMethodDescriptionallowedOrigins(String... origins) Sets the origins allowed to send unsafe requests, as exactscheme://host[:port]values in lower case.allowSameSite(boolean allow) AcceptsSec-Fetch-Site: same-site(other origins of the same registrable domain); off by default.Sets what a double-submit token is bound to (default: the principal, or empty when anonymous).build()Builds the middleware.Sets the time source of double-submit tokens, for deterministic tests.cookieName(String cookieName) Sets the double-submit cookie name (default__Host-csrf, orcsrfwithoutSecure).exemptWhen(Predicate<Request> exempt) Skips all checks for requests the predicate accepts, for example those with anAuthorizationheader, which a browser never adds by itself.fetchMetadata(boolean enabled) Turns theSec-Fetch-Sitecheck on (default) or off.headerName(String headerName) Sets the request header carrying the token (defaultX-CSRF-Token).httpOnly(boolean httpOnly) Makes the double-submit cookie invisible to scripts (default off).Sets how long a double-submit token is accepted (default 12 hours).random(SecureRandom random) Sets the source of randomness, for tests.sameSite(SetCookie.SameSite sameSite) SetsSameSiteon the double-submit cookie (default Lax).secure(boolean secure) SetsSecureon the double-submit cookie (default on; off only for local development).
-
Method Details
-
headerName
Sets the request header carrying the token (defaultX-CSRF-Token). Browsers only send custom headers from scripts, which cross-origin pages cannot do without CORS approval.- Parameters:
headerName- an HTTP header token- Returns:
- this builder
-
allowedOrigins
Sets the origins allowed to send unsafe requests, as exactscheme://host[:port]values in lower case. Without any, anOriginmust match theHostheader.- Parameters:
origins- one or more origins- Returns:
- this builder
-
allowSameSite
AcceptsSec-Fetch-Site: same-site(other origins of the same registrable domain); off by default.- Parameters:
allow- whether to accept it- Returns:
- this builder
-
fetchMetadata
Turns theSec-Fetch-Sitecheck on (default) or off.- Parameters:
enabled- whether to check it- Returns:
- this builder
-
exemptWhen
Skips all checks for requests the predicate accepts, for example those with anAuthorizationheader, which a browser never adds by itself. The predicate decides on request facts only; anything an attacker's page can cause (cookies, form posts) must not make it return true.- Parameters:
exempt- the exemption rule- Returns:
- this builder
-
binding
Sets what a double-submit token is bound to (default: the principal, or empty when anonymous). Ignored by the synchronizer variant.- Parameters:
binding- a pure function of the request that does not use client-controlled data- Returns:
- this builder
-
maxAge
Sets how long a double-submit token is accepted (default 12 hours).- Parameters:
maxAge- 1 minute to 30 days- Returns:
- this builder
-
clock
Sets the time source of double-submit tokens, for deterministic tests.- Parameters:
clock- the clock- Returns:
- this builder
-
random
Sets the source of randomness, for tests.- Parameters:
random- a cryptographically strong generator- Returns:
- this builder
-
cookieName
Sets the double-submit cookie name (default__Host-csrf, orcsrfwithoutSecure).- Parameters:
cookieName- a cookie token- Returns:
- this builder
-
secure
SetsSecureon the double-submit cookie (default on; off only for local development).- Parameters:
secure- whether the cookie is Secure- Returns:
- this builder
-
sameSite
SetsSameSiteon the double-submit cookie (default Lax).- Parameters:
sameSite- the attribute- Returns:
- this builder
-
httpOnly
Makes the double-submit cookie invisible to scripts (default off). The page then needs the token fromCsrf.token(Context)instead of reading the cookie.- Parameters:
httpOnly- whether the cookie is HttpOnly- Returns:
- this builder
-
build
Builds the middleware.- Returns:
- the CSRF protection
- Throws:
IllegalArgumentException- if the cookie settings are inconsistent
-