pub struct SecurityConfig {Show 23 fields
pub csrf: bool,
pub csrf_cookie_secure: bool,
pub signed_csrf: bool,
pub session_bind_cookie: Option<String>,
pub csrf_exempt_paths: Vec<String>,
pub content_type_options: bool,
pub frame_options: Option<String>,
pub referrer_policy: Option<String>,
pub xss_protection: Option<String>,
pub hsts: bool,
pub hsts_max_age: u64,
pub hsts_include_subdomains: bool,
pub hsts_preload: bool,
pub content_security_policy: Option<String>,
pub permissions_policy: Option<String>,
pub cross_origin_opener_policy: Option<String>,
pub cross_origin_resource_policy: Option<String>,
pub cross_origin_embedder_policy: Option<String>,
pub server_header: Option<String>,
pub hide_server_header: bool,
pub request_body_limit: Option<usize>,
pub redact_sensitive_headers: bool,
pub private_cache: bool,
}Expand description
Declarative security configuration. Build from Default (secure,
dev-safe) and override the fields you need — see the crate docs for the
rationale behind each default.
Fields§
§csrf: boolRun the CSRF middleware. Default true.
Force the Secure flag on the CSRF cookie. Default false; Secure is
added automatically under Environment::Prod regardless, so this only
matters for forcing it on in a non-prod HTTPS setup.
signed_csrf: boolSign the CSRF token with the app secret_key (HMAC-SHA256). Default
true — the middleware is the only mint, so every token carries a
signature; stale unsigned cookies rotate automatically on the next
safe request. Set false for plain double-submit.
When signed_csrf is on, also bind the token to this cookie’s value
(typically the session cookie). Default None.
csrf_exempt_paths: Vec<String>Request-path prefixes exempt from CSRF (CSRF-exempt paths).
A token-authenticated REST API carries no session cookie, so a
bearer-auth POST /api/... would otherwise 403; exempt "/api" to
keep it working. Matched as a path prefix. Default empty.
content_type_options: boolX-Content-Type-Options: nosniff. Default true.
frame_options: Option<String>X-Frame-Options. Default Some("DENY").
referrer_policy: Option<String>Referrer-Policy. Default Some("strict-origin-when-cross-origin").
xss_protection: Option<String>X-XSS-Protection. Default Some("0") — disables the buggy legacy
filter rather than enabling it (current OWASP guidance).
hsts: boolEmit Strict-Transport-Security. Default false (dev-safe). Value is
built from the hsts_* fields.
hsts_max_age: u64HSTS max-age in seconds. Default one year.
hsts_include_subdomains: boolAdd ; includeSubDomains to HSTS. Default true.
hsts_preload: boolAdd ; preload to HSTS. Default false.
content_security_policy: Option<String>Content-Security-Policy. Default None — a wrong CSP breaks apps, so
it’s opt-in.
permissions_policy: Option<String>Permissions-Policy. Default None.
cross_origin_opener_policy: Option<String>Cross-Origin-Opener-Policy. Default Some("same-origin").
Set None to omit, e.g. apps relying on cross-origin
popups (some OAuth flows).
cross_origin_resource_policy: Option<String>Cross-Origin-Resource-Policy (e.g. "same-origin"). Default None.
cross_origin_embedder_policy: Option<String>Cross-Origin-Embedder-Policy (e.g. "require-corp"). Default None.
server_header: Option<String>Set the Server response header. Default Some("umbral") — a bare
product name (no version, so no info disclosure), the way many app
servers advertise one. Set None to omit. Prefer no version.
hide_server_header: boolStrip any Server header the stack set. Default false. Ignored when
server_header is Some (the set wins) — to strip, also set
server_header: None.
request_body_limit: Option<usize>Cap request body size in bytes (tower-http RequestBodyLimitLayer).
Default None (axum’s own default applies).
redact_sensitive_headers: boolMark authorization / cookie / set-cookie sensitive so tracing
redacts them. Default true.
private_cache: boolSend Cache-Control: no-store, private on responses to personalised
requests — those carrying a session cookie or an Authorization /
Proxy-Authorization header. Default true.
An authenticated page holds the viewer’s data and their CSRF token
(double-submit, so the token is deliberately readable — see the module
docs). Without this header nothing tells a CDN, a corporate proxy, or
the browser’s back/forward cache that the page belongs to one person.
private stops shared caches; no-store stops the local disk cache
replaying an admin page after logout.
Anonymous requests are untouched, so public pages stay cacheable, and a
handler that sets its own Cache-Control always wins. Turn this off
only if a cache you control already keys on the session cookie.
Implementations§
Source§impl SecurityConfig
impl SecurityConfig
Sourcepub fn production_hardened() -> Self
pub fn production_hardened() -> Self
A production-grade preset (audit_2 plugin-authz S1). The defaults are deliberately dev-safe — HSTS, CSP, and cross-origin isolation are OFF so local HTTP dev works — but that leaves a default deployment without an XSS backstop (no CSP) or SSL-stripping protection (no HSTS). Rather than have every operator hand-assemble a config and risk forgetting one, this turns on the headline prod headers in a single call:
hsts+hsts_preload(long max-age, subdomains, preload-eligible),- a strict
content_security_policybaseline (default-src 'self'withframe-ancestors 'none',base-uri 'self',form-action 'self') — loosen it per app as needed, cross_origin_resource_policy: same-origin(COOP is already same-origin by default),csrf_cookie_secure(prod serves over HTTPS, so the CSRF cookie should carry theSecureattribute).
Everything else keeps the secure defaults (csrf, signed_csrf,
nosniff, X-Frame-Options: DENY, referrer policy). Note the strict CSP
has no 'unsafe-inline', so inline <script>/<style> won’t run —
adjust the policy for your asset strategy.
Trait Implementations§
Source§impl Clone for SecurityConfig
impl Clone for SecurityConfig
Source§fn clone(&self) -> SecurityConfig
fn clone(&self) -> SecurityConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SecurityConfig
impl Debug for SecurityConfig
Auto Trait Implementations§
impl Freeze for SecurityConfig
impl RefUnwindSafe for SecurityConfig
impl Send for SecurityConfig
impl Sync for SecurityConfig
impl Unpin for SecurityConfig
impl UnsafeUnpin for SecurityConfig
impl UnwindSafe for SecurityConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);