pub struct SecurityStack { /* private fields */ }Expand description
Composable security middleware stack for Axum.
Holds an optional configuration for each supported layer. Layers set to None are
skipped in apply. Start from SecurityStack::default()
for safe production defaults, or SecurityStack::new() for the same starting point
and chain builder methods to customise or disable individual layers.
§Layer application order
Layers are applied outermost-first in the following order, so each layer’s rejection response is still wrapped by the layers before it (e.g. security headers appear on rate-limit rejections):
helmet → redirect_https → ipfilter → ratelimit → body_limit → csrf → handler
§Examples
use axum::{routing::get, Router};
use rune_axum_stack::SecurityStack;
// Five-layer default stack
let app: Router = SecurityStack::default().apply(
Router::new().route("/api", get(|| async { "ok" })),
);use axum::{routing::post, Router};
use rune_axum_stack::SecurityStack;
// REST API: no CSRF, no HTTPS redirect (TLS terminated upstream)
let app: Router = SecurityStack::new()
.without_csrf()
.without_redirect_https()
.apply(Router::new().route("/api/data", post(|| async { "ok" })));Implementations§
Source§impl SecurityStack
impl SecurityStack
Sourcepub fn helmet(self, config: Helmet) -> Self
pub fn helmet(self, config: Helmet) -> Self
Replaces the HelmetLayer configuration.
Sourcepub fn without_helmet(self) -> Self
pub fn without_helmet(self) -> Self
Removes the HelmetLayer from the stack.
Sourcepub fn csrf(self, config: CsrfConfig) -> Self
pub fn csrf(self, config: CsrfConfig) -> Self
Replaces the CsrfLayer configuration.
Sourcepub fn without_csrf(self) -> Self
pub fn without_csrf(self) -> Self
Removes the CsrfLayer from the stack.
Useful for stateless APIs that authenticate via bearer tokens or API keys where traditional CSRF protection is not applicable.
Sourcepub fn ratelimit(self, config: RateLimitConfig) -> Self
pub fn ratelimit(self, config: RateLimitConfig) -> Self
Replaces the RateLimitLayer configuration.
Sourcepub fn without_ratelimit(self) -> Self
pub fn without_ratelimit(self) -> Self
Removes the RateLimitLayer from the stack.
Sourcepub fn ipfilter(self, config: IpFilterConfig) -> Self
pub fn ipfilter(self, config: IpFilterConfig) -> Self
Enables IP filtering with the given IpFilterConfig.
IP filtering is disabled by default; call this to opt in.
Sourcepub fn without_ipfilter(self) -> Self
pub fn without_ipfilter(self) -> Self
Removes the IpFilterLayer from the stack (the default state).
Sourcepub fn redirect_https(self, config: RedirectHttps) -> Self
pub fn redirect_https(self, config: RedirectHttps) -> Self
Replaces the RedirectHttpsLayer configuration.
Sourcepub fn without_redirect_https(self) -> Self
pub fn without_redirect_https(self) -> Self
Removes the RedirectHttpsLayer from the stack.
Suitable when TLS is terminated by an upstream proxy that never forwards plain HTTP to the application.
Sourcepub fn body_limit(self, config: BodyLimit) -> Self
pub fn body_limit(self, config: BodyLimit) -> Self
Replaces the BodyLimitLayer configuration.
Sourcepub fn without_body_limit(self) -> Self
pub fn without_body_limit(self) -> Self
Removes the BodyLimitLayer from the stack.
Sourcepub fn apply<S>(self, router: Router<S>) -> Router<S>
pub fn apply<S>(self, router: Router<S>) -> Router<S>
Applies all enabled layers to router and returns the wrapped router.
Layers are composed outermost-first:
helmet → redirect_https → ipfilter → ratelimit → body_limit → csrf → handler.
§Examples
use axum::{routing::get, Router};
use rune_axum_stack::SecurityStack;
let app: Router = SecurityStack::default().apply(
Router::new().route("/", get(|| async { "ok" })),
);Trait Implementations§
Source§impl Clone for SecurityStack
impl Clone for SecurityStack
Source§fn clone(&self) -> SecurityStack
fn clone(&self) -> SecurityStack
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more