Skip to main content

CsrfPlugin

Struct CsrfPlugin 

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

CSRF protection plugin.

Validates the Origin or Referer header on state-changing requests (POST, PATCH, DELETE, PUT) against a list of allowed origins. This is complementary to CORS: CORS controls which origins can read responses, while CSRF protection ensures that state-changing requests originate from trusted sources.

Implementations§

Source§

impl CsrfPlugin

Source

pub fn new(allowed_origins: Vec<String>) -> Self

Create a CSRF plugin with explicit allowed origins.

Source

pub fn with_localhost(port: u16) -> Self

Convenience constructor for local development. Allows both localhost and 127.0.0.1 on the given port.

Source

pub fn check( &self, method: &str, origin: Option<&str>, referer: Option<&str>, ) -> Result<(), PluginError>

Validate an incoming request.

For safe methods this always succeeds. For state-changing methods, the Origin header is checked first; if absent the origin is derived from the Referer header.

CSRF defense model. Modern browsers always send Origin on cross-origin state-changing requests — a malicious page can’t suppress it. Browsers also send Origin on same-site POSTs in current spec. So a request with NEITHER Origin nor Referer is by definition not a browser request — it’s a server-to-server caller (Next.js SSR forwarding a session cookie, a curl script with --cookie, an internal admin tool, etc.). Those callers attach the cookie explicitly via the Cookie: header rather than relying on browser auto-attachment, so the cross-site forgery attack surface the CSRF gate exists to protect against doesn’t apply.

Without this allowance every Next.js dashboard route that calls a Pylon mutation server-side (pylon.json("/api/fn/X", {method: "POST"})) would 403 — Next.js SSR has no Origin to send. We learned this the hard way via the dashboard “Members” page returning empty after release 0.3.11.

When a header IS present it must match the allowlist; an attacker can never inject one, so its presence is always trustworthy.

Trait Implementations§

Source§

impl Plugin for CsrfPlugin

Source§

fn name(&self) -> &str

Unique name for this plugin.
Source§

fn on_request( &self, _method: &str, _path: &str, _auth: &AuthContext, ) -> Result<(), PluginError>

Called on every incoming request (middleware).
Source§

fn on_init(&self, _ctx: &PluginContext)

Called once when the plugin is registered.
Source§

fn routes(&self) -> Vec<PluginRoute>

Custom API routes this plugin handles.
Source§

fn before_insert( &self, _entity: &str, _data: &mut Value, _auth: &AuthContext, ) -> Result<(), PluginError>

Called before an entity insert. Return Err to reject.
Source§

fn after_insert( &self, _entity: &str, _id: &str, _data: &Value, _auth: &AuthContext, )

Called after a successful insert.
Source§

fn before_update( &self, _entity: &str, _id: &str, _data: &mut Value, _auth: &AuthContext, ) -> Result<(), PluginError>

Called before an entity update. Return Err to reject.
Source§

fn after_update( &self, _entity: &str, _id: &str, _data: &Value, _auth: &AuthContext, )

Called after a successful update.
Source§

fn before_delete( &self, _entity: &str, _id: &str, _auth: &AuthContext, ) -> Result<(), PluginError>

Called before an entity delete. Return Err to reject.
Source§

fn after_delete(&self, _entity: &str, _id: &str, _auth: &AuthContext)

Called after a successful delete.
Source§

fn on_request_with_meta( &self, method: &str, path: &str, auth: &AuthContext, _meta: &RequestMeta<'_>, ) -> Result<(), PluginError>

Richer variant of [on_request] that also receives per-request metadata (peer IP today; more fields may be added later). The default implementation delegates to on_request so existing plugins keep working without changes. Plugins that care about IP — notably rate limiting — override this hook.
Source§

fn on_session_create(&self, _user_id: &str, _token: &str)

Called when a new session is created.
Source§

fn entities(&self) -> Vec<ManifestEntity>

Additional manifest entities this plugin contributes.

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V