pub struct TrustedHosts(/* private fields */);Expand description
Parsed Host allowlist used by host_validate.
Closes api S-30 (lack of Host validation). The previous implementation
cached the parsed env value in a process-wide OnceLock, which made
tests that wanted to vary the allowlist unable to do so (the first test
to touch the cell froze it for every later test in the same process).
Now the allowlist travels through axum::Extension<TrustedHosts>:
crate::server::build_router_with_audit inserts a from_env() value
at build time; tests can override by inserting a different value into
the router extensions. Tests that bypass the server builder still get
the env-var fallback (cached per-process in a private OnceLock so the
per-request cost stays zero), but an explicit extension always wins.
Precedence: explicit axum::Extension<TrustedHosts> > env-var
fallback (TENSOR_WASM_API_TRUSTED_HOSTS).
Implementations§
Source§impl TrustedHosts
impl TrustedHosts
Sourcepub fn from_env() -> Self
pub fn from_env() -> Self
Parse the allowlist from ENV_TRUSTED_HOSTS. Splits on ,,
trims surrounding whitespace, drops empty entries, and lowercases
each remaining entry for case-insensitive matching. Unset / empty
env var yields an empty list (= Self::allow_all).
Sourcepub fn from_raw(raw: &str) -> Self
pub fn from_raw(raw: &str) -> Self
Helper: parse a comma-separated string as if it were the env variable. Public for the explicit-construction path used by tests.
Sourcepub fn allow_all() -> Self
pub fn allow_all() -> Self
Explicit “no allowlist” constructor — every Host value is admitted (the legacy default when the env var is unset).
Sourcepub fn from_hosts<I, S>(iter: I) -> Self
pub fn from_hosts<I, S>(iter: I) -> Self
Construct from an iterator of allowlist entries. Entries are
lowercased on insertion so case-insensitive matching in
Self::contains is just a byte comparison.
Sourcepub fn contains(&self, host: &str) -> bool
pub fn contains(&self, host: &str) -> bool
true when the supplied host (raw value from Host: or
:authority) matches one of the allowlist entries.
Matching rules:
- Case-insensitive exact match on the supplied host.
- If the supplied host carries a default-port suffix (
:80or:443) and no allowlist entry contains a:, the port is stripped before comparison. This lets operators list bare hostnames (api.example.com) and still admit clients that include the default port in theHostheader. If any allowlist entry contains a port, we do an exact match on the fullhost:portstring — the operator chose to be specific.
Trait Implementations§
Source§impl Clone for TrustedHosts
impl Clone for TrustedHosts
Source§fn clone(&self) -> TrustedHosts
fn clone(&self) -> TrustedHosts
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 TrustedHosts
impl Debug for TrustedHosts
Source§impl Default for TrustedHosts
impl Default for TrustedHosts
Source§fn default() -> TrustedHosts
fn default() -> TrustedHosts
Auto Trait Implementations§
impl Freeze for TrustedHosts
impl RefUnwindSafe for TrustedHosts
impl Send for TrustedHosts
impl Sync for TrustedHosts
impl Unpin for TrustedHosts
impl UnsafeUnpin for TrustedHosts
impl UnwindSafe for TrustedHosts
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> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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 more