pub struct CsrfLayer<T = DefaultResponseForProtectionError> { /* private fields */ }csrf only.Expand description
Layer that applies the Csrf middleware.
See the module docs for an example.
Implementations§
Source§impl<T> CsrfLayer<T>
impl<T> CsrfLayer<T>
Sourcepub fn add_trusted_origin<S: AsRef<str>>(
self,
origin: S,
) -> Result<Self, ConfigError>
pub fn add_trusted_origin<S: AsRef<str>>( self, origin: S, ) -> Result<Self, ConfigError>
Adds a trusted origin that allows all requests whose Origin header
matches the given value.
The value is matched byte-for-byte against the request’s Origin
header — there is no normalization (this mirrors the Go reference). It
must therefore be written exactly as a browser sends it:
- form
scheme://host[:port], whereschemeishttporhttps; - the host lowercased (browsers lowercase it; IDN hosts must be given in
punycode, e.g.
xn--exmple-cua.com); - default ports omitted — browsers drop
:80/:443, so an explicit default port (e.g.https://example.com:443) will never match; - no trailing slash, path, query, or fragment.
Inputs that can’t represent a browser Origin are rejected with a
ConfigError; inputs that parse but aren’t in the canonical browser
form above are accepted but will silently never match.
// Matches `Origin: https://example.com`:
let layer = CsrfLayer::new().add_trusted_origin("https://example.com")?;
// Accepted, but never matches a browser Origin (explicit default port):
let layer = CsrfLayer::new().add_trusted_origin("https://example.com:443")?;Sourcepub fn with_insecure_bypass<F>(self, predicate: F) -> Self
pub fn with_insecure_bypass<F>(self, predicate: F) -> Self
Adds a bypass predicate that returns true for requests which should
skip CSRF protection.
This is an escape hatch for endpoints that legitimately need to accept cross-origin POSTs (e.g. webhook receivers). Bypassed endpoints must have their own protection (signed payloads, authentication tokens, etc.) — otherwise they are CSRF-vulnerable.
Sourcepub fn with_rejection_response<R>(self, rejection_response: R) -> CsrfLayer<R>where
R: Clone,
pub fn with_rejection_response<R>(self, rejection_response: R) -> CsrfLayer<R>where
R: Clone,
Replaces the response builder used when a request is rejected.
Accepts any type that implements ResponseForProtectionError,
including a FnMut(ProtectionError) -> Response<B> + Clone closure.
The default builder returns a 403 Forbidden with an empty body.
Regardless of the builder, Csrf attaches the
ProtectionError to the response’s extensions,
so a custom builder need not re-attach it.
Trait Implementations§
Auto Trait Implementations§
impl<T = DefaultResponseForProtectionError> !RefUnwindSafe for CsrfLayer<T>
impl<T = DefaultResponseForProtectionError> !UnwindSafe for CsrfLayer<T>
impl<T> Freeze for CsrfLayer<T>where
T: Freeze,
impl<T> Send for CsrfLayer<T>where
T: Send,
impl<T> Sync for CsrfLayer<T>where
T: Sync,
impl<T> Unpin for CsrfLayer<T>where
T: Unpin,
impl<T> UnsafeUnpin for CsrfLayer<T>where
T: UnsafeUnpin,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ServiceExt for T
impl<T> ServiceExt for T
Source§fn propagate_header(self, header: HeaderName) -> PropagateHeader<Self>where
Self: Sized,
fn propagate_header(self, header: HeaderName) -> PropagateHeader<Self>where
Self: Sized,
propagate-header and util only.Source§fn add_extension<T>(self, value: T) -> AddExtension<Self, T>where
Self: Sized,
fn add_extension<T>(self, value: T) -> AddExtension<Self, T>where
Self: Sized,
add-extension and util only.Source§fn map_request_body<F>(self, f: F) -> MapRequestBody<Self, F>where
Self: Sized,
fn map_request_body<F>(self, f: F) -> MapRequestBody<Self, F>where
Self: Sized,
map-request-body and util only.Source§fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>where
Self: Sized,
fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>where
Self: Sized,
map-response-body and util only.Source§fn compression(self) -> Compression<Self>where
Self: Sized,
fn compression(self) -> Compression<Self>where
Self: Sized,
util and (crate features compression-br or compression-deflate or compression-gzip or compression-zstd) only.Source§fn decompression(self) -> Decompression<Self>where
Self: Sized,
fn decompression(self) -> Decompression<Self>where
Self: Sized,
util and (crate features decompression-br or decompression-deflate or decompression-gzip or decompression-zstd) only.Source§fn trace_for_http(self) -> Trace<Self, HttpMakeClassifier>where
Self: Sized,
fn trace_for_http(self) -> Trace<Self, HttpMakeClassifier>where
Self: Sized,
trace and util only.Source§fn trace_for_grpc(self) -> Trace<Self, GrpcMakeClassifier>where
Self: Sized,
fn trace_for_grpc(self) -> Trace<Self, GrpcMakeClassifier>where
Self: Sized,
trace and util only.Source§fn follow_redirects(self) -> FollowRedirect<Self, Standard>where
Self: Sized,
fn follow_redirects(self) -> FollowRedirect<Self, Standard>where
Self: Sized,
follow-redirect and util only.Source§fn sensitive_headers(
self,
headers: impl IntoIterator<Item = HeaderName>,
) -> SetSensitiveHeaders<Self>where
Self: Sized,
fn sensitive_headers(
self,
headers: impl IntoIterator<Item = HeaderName>,
) -> SetSensitiveHeaders<Self>where
Self: Sized,
sensitive-headers and util only.Source§fn sensitive_request_headers(
self,
headers: impl IntoIterator<Item = HeaderName>,
) -> SetSensitiveRequestHeaders<Self>where
Self: Sized,
fn sensitive_request_headers(
self,
headers: impl IntoIterator<Item = HeaderName>,
) -> SetSensitiveRequestHeaders<Self>where
Self: Sized,
sensitive-headers and util only.Source§fn sensitive_response_headers(
self,
headers: impl IntoIterator<Item = HeaderName>,
) -> SetSensitiveResponseHeaders<Self>where
Self: Sized,
fn sensitive_response_headers(
self,
headers: impl IntoIterator<Item = HeaderName>,
) -> SetSensitiveResponseHeaders<Self>where
Self: Sized,
sensitive-headers and util only.Source§fn override_request_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetRequestHeader<Self, M>where
Self: Sized,
fn override_request_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetRequestHeader<Self, M>where
Self: Sized,
set-header and util only.Source§fn append_request_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetRequestHeader<Self, M>where
Self: Sized,
fn append_request_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetRequestHeader<Self, M>where
Self: Sized,
set-header and util only.Source§fn insert_request_header_if_not_present<M>(
self,
header_name: HeaderName,
make: M,
) -> SetRequestHeader<Self, M>where
Self: Sized,
fn insert_request_header_if_not_present<M>(
self,
header_name: HeaderName,
make: M,
) -> SetRequestHeader<Self, M>where
Self: Sized,
set-header and util only.Source§fn override_response_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetResponseHeader<Self, M>where
Self: Sized,
fn override_response_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetResponseHeader<Self, M>where
Self: Sized,
set-header and util only.Source§fn append_response_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetResponseHeader<Self, M>where
Self: Sized,
fn append_response_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetResponseHeader<Self, M>where
Self: Sized,
set-header and util only.Source§fn insert_response_header_if_not_present<M>(
self,
header_name: HeaderName,
make: M,
) -> SetResponseHeader<Self, M>where
Self: Sized,
fn insert_response_header_if_not_present<M>(
self,
header_name: HeaderName,
make: M,
) -> SetResponseHeader<Self, M>where
Self: Sized,
set-header and util only.Source§fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
request-id and util only.Source§fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
request-id and util only.x-request-id as the header name. Read moreSource§fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
request-id and util only.Source§fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
request-id and util only.x-request-id as the header name. Read moreSource§fn catch_panic(self) -> CatchPanic<Self, DefaultResponseForPanic>where
Self: Sized,
fn catch_panic(self) -> CatchPanic<Self, DefaultResponseForPanic>where
Self: Sized,
catch-panic and util only.500 Internal Server responses. Read moreSource§fn request_body_limit(self, limit: usize) -> RequestBodyLimit<Self>where
Self: Sized,
fn request_body_limit(self, limit: usize) -> RequestBodyLimit<Self>where
Self: Sized,
limit and util only.413 Payload Too Large responses. Read moreSource§fn trim_trailing_slash(self) -> NormalizePath<Self>where
Self: Sized,
fn trim_trailing_slash(self) -> NormalizePath<Self>where
Self: Sized,
normalize-path and util only.Source§fn append_trailing_slash(self) -> NormalizePath<Self>where
Self: Sized,
fn append_trailing_slash(self) -> NormalizePath<Self>where
Self: Sized,
normalize-path and util only.