Skip to main content

hyperlane_plugin_security/
struct.rs

1use super::*;
2
3/// Configurable HTTP security response headers.
4#[derive(Clone, Data, Debug, Eq, PartialEq)]
5pub struct SecurityHeadersConfig {
6    /// `Strict-Transport-Security` value.
7    #[get(type(clone))]
8    pub(crate) hsts: Option<String>,
9    /// `Content-Security-Policy` value.
10    #[get(type(clone))]
11    pub(crate) content_security_policy: Option<String>,
12    /// `X-Frame-Options` value.
13    #[get(type(clone))]
14    pub(crate) x_frame_options: Option<String>,
15    /// `X-Content-Type-Options` value.
16    #[get(type(clone))]
17    pub(crate) x_content_type_options: Option<String>,
18    /// `Referrer-Policy` value.
19    #[get(type(clone))]
20    pub(crate) referrer_policy: Option<String>,
21    /// `Permissions-Policy` value.
22    #[get(type(clone))]
23    pub(crate) permissions_policy: Option<String>,
24    /// `Cross-Origin-Opener-Policy` value.
25    #[get(type(clone))]
26    pub(crate) coop: Option<String>,
27    /// `Cross-Origin-Embedder-Policy` value.
28    #[get(type(clone))]
29    pub(crate) coep: Option<String>,
30    /// `Cross-Origin-Resource-Policy` value.
31    #[get(type(clone))]
32    pub(crate) corp: Option<String>,
33    /// Strips `X-Powered-By` from outgoing responses when `true`.
34    #[get(type(copy))]
35    pub(crate) remove_powered_by: bool,
36}
37
38/// Stateless double-submit HMAC-SHA256 CSRF protection settings.
39#[derive(Clone, Data, Debug, Eq, PartialEq)]
40pub struct CsrfConfig {
41    /// Enables CSRF enforcement on state-changing HTTP methods (`POST`, `PUT`, `PATCH`, `DELETE`).
42    #[get(type(copy))]
43    pub(crate) enabled: bool,
44    /// Server-side HMAC-SHA256 signing secret.
45    pub(crate) secret: Vec<u8>,
46    /// Cookie name holding the signed CSRF token.
47    pub(crate) cookie_name: String,
48    /// Request header name carrying the matching CSRF token.
49    pub(crate) header_name: String,
50    /// Exact paths exempted from CSRF checks (for example webhook callbacks).
51    pub(crate) exempt_paths: Vec<String>,
52}
53
54/// Built-in request guard limits protecting routing and handlers from malformed traffic.
55#[derive(Clone, Data, Debug, Eq, PartialEq)]
56pub struct RequestGuardConfig {
57    /// Maximum allowed request body length in bytes (`0` disables this plugin-level cap).
58    #[get(type(copy))]
59    pub(crate) max_body_bytes: usize,
60    /// Maximum allowed request URI path length in bytes (`0` disables cap).
61    #[get(type(copy))]
62    pub(crate) max_uri_bytes: usize,
63    /// Optional case-insensitive `Host` header allowlist (empty allows any host).
64    pub(crate) allowed_hosts: Vec<String>,
65    /// Blocks `..`, `%2e%2e`, backslashes, and NUL bytes in request paths when `true`.
66    #[get(type(copy))]
67    pub(crate) block_path_traversal: bool,
68}
69
70/// Unified security hardening configuration for [`SecurityPlugin`].
71#[derive(Clone, Data, Debug, Default, Eq, PartialEq)]
72pub struct SecurityConfig {
73    /// Outgoing HTTP security headers configuration.
74    pub(crate) headers: SecurityHeadersConfig,
75    /// Double-submit HMAC CSRF protection settings.
76    pub(crate) csrf: CsrfConfig,
77    /// Incoming request guard settings.
78    pub(crate) guard: RequestGuardConfig,
79}
80
81/// Precomputed SHA-256 ipad and opad midstates for zero-allocation HMAC-SHA256 verification.
82#[derive(Clone, Copy, Debug, Eq, PartialEq)]
83pub(crate) struct HmacSha256Key {
84    /// SHA-256 state after compressing the 64-byte `key ^ 0x36` inner pad block.
85    pub(crate) ipad_state: [u32; 8],
86    /// SHA-256 state after compressing the 64-byte `key ^ 0x5c` outer pad block.
87    pub(crate) opad_state: [u32; 8],
88}
89
90/// Process-wide [`SecurityPlugin`] singleton holding prebuilt headers, guards, and custom rules.
91#[derive(Clone)]
92pub struct SecurityPlugin {
93    /// Source security configuration.
94    pub(crate) config: SecurityConfig,
95    /// Prebuilt `(header_name, header_value)` list applied without per-request allocation.
96    pub(crate) compiled_headers: Arc<Vec<(&'static str, String)>>,
97    /// Extensible custom security rules evaluated on each incoming request.
98    pub(crate) rules: Arc<Vec<Arc<dyn SecurityRule>>>,
99    /// Precomputed HMAC-SHA256 key states when CSRF protection is enabled.
100    pub(crate) csrf_hmac_key: Option<HmacSha256Key>,
101    /// Lowercased CSRF request header name for O(1) borrowed map lookup.
102    pub(crate) csrf_header_lower: String,
103    /// Pre-validated RFC 6265 validity of the configured CSRF cookie name.
104    pub(crate) csrf_cookie_name_valid: bool,
105}
106
107/// Opt-in request middleware enforcing guards, CSRF checks, custom rules, and applying security headers.
108#[derive(Clone, Copy, Debug, Default)]
109#[hyperlane::request_middleware(-105)]
110pub struct SecurityRequestHook;
111
112/// Opt-in response middleware guaranteeing security headers and stripping `X-Powered-By`.
113#[derive(Clone, Copy, Debug, Default)]
114#[hyperlane::response_middleware(-75)]
115pub struct SecurityResponseHook;