Skip to main content

AllowBrowser

Struct AllowBrowser 

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

Tower layer that answers 406 Not Acceptable to browsers older than the versions it allows (Rails’ allow_browser).

modern is Rails’ versions: :modern: browsers with WebP images, web push, badges, import maps, CSS nesting and CSS :has (Safari 17.2, Chrome and Edge 120, Firefox 121, Opera 106; no Internet Explorer). Requests whose User-Agent is missing or names no known browser (bots, curl, API clients, uptime checks) always pass, as in Rails. Outdated browsers get a short HTML page asking to upgrade (replace it with page). Pure CPU, no binding call.

Apply it to the routes of pages (.layer(...) on a Router), not to JSON APIs or webhooks, whose clients are not browsers anyway.

§Examples

use axum::{Router, routing::get};
use ocre::security::{AllowBrowser, Browser};

let app: Router = Router::new().route("/", get(|| async { "home" })).layer(AllowBrowser::modern());

let policy = AllowBrowser::new().minimum(Browser::Firefox, 115, 0).deny(Browser::InternetExplorer);
assert!(policy.allows(Some("Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0")));
assert!(!policy.allows(Some("Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0")));
assert!(policy.allows(None));

Implementations§

Source§

impl AllowBrowser

Source

pub fn new() -> Self

A policy that allows every browser; add limits with minimum and deny.

§Examples
assert!(ocre::security::AllowBrowser::new().allows(Some("Mozilla/4.0 (compatible; MSIE 6.0)")));
Source

pub fn modern() -> Self

Rails’ allow_browser versions: :modern: Safari 17.2, Chrome and Edge 120, Firefox 121, Opera 106, no Internet Explorer.

§Examples
use ocre::security::AllowBrowser;

let old_chrome = "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36";
assert!(!AllowBrowser::modern().allows(Some(old_chrome)));
Source

pub fn minimum(self, browser: Browser, major: u32, minor: u32) -> Self

Allows browser from version major.minor on; replaces an earlier rule for the same browser.

§Examples
use ocre::security::{AllowBrowser, Browser};

let policy = AllowBrowser::modern().minimum(Browser::Safari, 16, 0);
let safari_16 = "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 \
                 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1";
assert!(policy.allows(Some(safari_16)));
Source

pub fn deny(self, browser: Browser) -> Self

Refuses every version of browser (Rails’ ie: false).

§Examples
use ocre::security::{AllowBrowser, Browser};

let policy = AllowBrowser::new().deny(Browser::InternetExplorer);
assert!(!policy.allows(Some("Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko")));
Source

pub fn page(self, html: impl Into<String>) -> Self

Replaces the HTML page sent with the 406 (by default a short “please upgrade your browser” page).

§Examples
let policy = ocre::security::AllowBrowser::modern().page("<h1>Please use a recent browser.</h1>");
Source

pub fn allows(&self, user_agent: Option<&str>) -> bool

Whether a request with this User-Agent passes: a missing header or an unknown client always does.

§Examples
use ocre::security::AllowBrowser;

let policy = AllowBrowser::modern();
assert!(policy.allows(Some("Googlebot/2.1 (+http://www.google.com/bot.html)")));
assert!(policy.allows(None));

Trait Implementations§

Source§

impl Clone for AllowBrowser

Source§

fn clone(&self) -> AllowBrowser

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 AllowBrowser

Source§

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

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

impl Default for AllowBrowser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<S> Layer<S> for AllowBrowser

Source§

type Service = AllowBrowserService<S>

The wrapped service
Source§

fn layer(&self, inner: S) -> Self::Service

Wrap the given service with the middleware, returning a new service that has been decorated with the middleware.

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
§

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

§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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.

§

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

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

§

fn vzip(self) -> V