hyperlane-plugin-security 23.2.0

A zero-allocation security hardening plugin for the Hyperlane framework providing prebuilt HSTS/CSP/COOP/COEP/CORP/Referrer headers, stateless HMAC-SHA256 CSRF protection, host/URI/body/path-traversal guards, and extensible SecurityRule hooks.
Documentation
use super::*;

/// Configurable HTTP security response headers.
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct SecurityHeadersConfig {
    /// `Strict-Transport-Security` value.
    #[get(type(clone))]
    pub(crate) hsts: Option<String>,
    /// `Content-Security-Policy` value.
    #[get(type(clone))]
    pub(crate) content_security_policy: Option<String>,
    /// `X-Frame-Options` value.
    #[get(type(clone))]
    pub(crate) x_frame_options: Option<String>,
    /// `X-Content-Type-Options` value.
    #[get(type(clone))]
    pub(crate) x_content_type_options: Option<String>,
    /// `Referrer-Policy` value.
    #[get(type(clone))]
    pub(crate) referrer_policy: Option<String>,
    /// `Permissions-Policy` value.
    #[get(type(clone))]
    pub(crate) permissions_policy: Option<String>,
    /// `Cross-Origin-Opener-Policy` value.
    #[get(type(clone))]
    pub(crate) coop: Option<String>,
    /// `Cross-Origin-Embedder-Policy` value.
    #[get(type(clone))]
    pub(crate) coep: Option<String>,
    /// `Cross-Origin-Resource-Policy` value.
    #[get(type(clone))]
    pub(crate) corp: Option<String>,
    /// Strips `X-Powered-By` from outgoing responses when `true`.
    #[get(type(copy))]
    pub(crate) remove_powered_by: bool,
}

/// Stateless double-submit HMAC-SHA256 CSRF protection settings.
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct CsrfConfig {
    /// Enables CSRF enforcement on state-changing HTTP methods (`POST`, `PUT`, `PATCH`, `DELETE`).
    #[get(type(copy))]
    pub(crate) enabled: bool,
    /// Server-side HMAC-SHA256 signing secret.
    pub(crate) secret: Vec<u8>,
    /// Cookie name holding the signed CSRF token.
    pub(crate) cookie_name: String,
    /// Request header name carrying the matching CSRF token.
    pub(crate) header_name: String,
    /// Exact paths exempted from CSRF checks (for example webhook callbacks).
    pub(crate) exempt_paths: Vec<String>,
}

/// Built-in request guard limits protecting routing and handlers from malformed traffic.
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct RequestGuardConfig {
    /// Maximum allowed request body length in bytes (`0` disables this plugin-level cap).
    #[get(type(copy))]
    pub(crate) max_body_bytes: usize,
    /// Maximum allowed request URI path length in bytes (`0` disables cap).
    #[get(type(copy))]
    pub(crate) max_uri_bytes: usize,
    /// Optional case-insensitive `Host` header allowlist (empty allows any host).
    pub(crate) allowed_hosts: Vec<String>,
    /// Blocks `..`, `%2e%2e`, backslashes, and NUL bytes in request paths when `true`.
    #[get(type(copy))]
    pub(crate) block_path_traversal: bool,
}

/// Unified security hardening configuration for [`SecurityPlugin`].
#[derive(Clone, Data, Debug, Default, Eq, PartialEq)]
pub struct SecurityConfig {
    /// Outgoing HTTP security headers configuration.
    pub(crate) headers: SecurityHeadersConfig,
    /// Double-submit HMAC CSRF protection settings.
    pub(crate) csrf: CsrfConfig,
    /// Incoming request guard settings.
    pub(crate) guard: RequestGuardConfig,
}

/// Precomputed SHA-256 ipad and opad midstates for zero-allocation HMAC-SHA256 verification.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct HmacSha256Key {
    /// SHA-256 state after compressing the 64-byte `key ^ 0x36` inner pad block.
    pub(crate) ipad_state: [u32; 8],
    /// SHA-256 state after compressing the 64-byte `key ^ 0x5c` outer pad block.
    pub(crate) opad_state: [u32; 8],
}

/// Process-wide [`SecurityPlugin`] singleton holding prebuilt headers, guards, and custom rules.
#[derive(Clone)]
pub struct SecurityPlugin {
    /// Source security configuration.
    pub(crate) config: SecurityConfig,
    /// Prebuilt `(header_name, header_value)` list applied without per-request allocation.
    pub(crate) compiled_headers: Arc<Vec<(&'static str, String)>>,
    /// Extensible custom security rules evaluated on each incoming request.
    pub(crate) rules: Arc<Vec<Arc<dyn SecurityRule>>>,
    /// Precomputed HMAC-SHA256 key states when CSRF protection is enabled.
    pub(crate) csrf_hmac_key: Option<HmacSha256Key>,
    /// Lowercased CSRF request header name for O(1) borrowed map lookup.
    pub(crate) csrf_header_lower: String,
    /// Pre-validated RFC 6265 validity of the configured CSRF cookie name.
    pub(crate) csrf_cookie_name_valid: bool,
}

/// Opt-in request middleware enforcing guards, CSRF checks, custom rules, and applying security headers.
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::request_middleware(-105)]
pub struct SecurityRequestHook;

/// Opt-in response middleware guaranteeing security headers and stripping `X-Powered-By`.
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::response_middleware(-75)]
pub struct SecurityResponseHook;