Skip to main content

OriginPolicy

Struct OriginPolicy 

Source
pub struct OriginPolicy { /* private fields */ }
Expand description

Which Origins this server answers.

Not a trait and not a port: the check is a pure function over two strings, and a port trait here would be a dynamic dispatch bought for nothing. The type exists so the default can be fail-secure and the comparison can be spelled once.

Implementations§

Source§

impl OriginPolicy

Source

pub const fn strict(own: Option<Origin>) -> Self

A policy that answers only its own origin.

own is None when the server was not told its own origin — behind a proxy that rewrites Host, or bound to 0.0.0.0 and reached by several names. That is not a reason to answer everything: with no known own-origin the policy refuses every Origin that is present, which is the fail-secure direction. A server in that position has to say what it serves, because nothing else can work it out.

Source

pub fn allowing(self, origin: Origin) -> Self

Adds an origin this server answers for.

The list is exactly what the operator wrote: no wildcard, no suffix match, no parent domain. A suffix rule (evil-example.com matching not-evil-example.com) is the sort of thing that gets added once, for a legitimate case, and never looked at again.

Source

pub const fn own(&self) -> Option<&Origin>

The origin this server considers its own, if it was told one.

Source

pub fn permitted(&self) -> impl Iterator<Item = &Origin>

The origins answered for, including this server’s own.

Source

pub fn check(&self, header: Option<&str>) -> Result<(), Refusal>

Decides whether a request carrying header is answered.

None means the header was absent, which is a non-browser client and is allowed — see the module docs for why that asymmetry is the design and not an oversight.

A loopback origin is answered whatever the allow-list says, and that is not a hole in it. Origin is set by the browser and names the page the request came from, so a page served from http://localhost:8080 genuinely sends http://localhost:8080 — and a rebound page at evil.example sends http://evil.example, which is not loopback and is refused here exactly as before. What a loopback allowlist would get wrong is a suffix match, so the host is compared whole.

The comparison is exact and case-insensitive-in-scheme-and-host, done once at parse. A substring or suffix match is the tempting alternative and is the bug this type is shaped to prevent: an allowlist of localhost that also answers localhost.evil.example is a rebinding defence that rebinds.

Source

pub fn check_host(&self, header: Option<&str>) -> Result<(), Refusal>

Decides whether a request whose Host is header is answered.

Host is the header a rebinding attack actually controls, and checking only Origin leaves the attack open to a client that omits it: a browser rebound onto 127.0.0.1 sends Host: evil.example, and a non-browser client can send whatever it likes. A browser cannot forge Host — it is set from the URL it dialled — so a Host that is not loopback and not one this server was told to serve names a request for somewhere else.

Compared on the host rather than the whole authority, because the port is not part of the question: a client reaching this server on the port it was bound to names a host, and a loopback host on a different port is a different server that this policy has no opinion about.

None means the header was absent. HTTP/1.1 requires it and HTTP/2 carries it as :authority, so this is a proxy that stripped it rather than a client that had none; it is refused, because a check that answers “no evidence either way” cannot be the defence.

Trait Implementations§

Source§

impl Clone for OriginPolicy

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OriginPolicy

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for OriginPolicy

Source§

impl PartialEq for OriginPolicy

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for OriginPolicy

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more