hyperlane-plugin-security 22.14.1

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
use super::*;

/// Inherent implementation of [`SecurityViolation`].
impl SecurityViolation {
    /// Maps this violation to its canonical HTTP status code.
    ///
    /// # Returns
    ///
    /// - `usize` - HTTP status code (`400`, `403`, `413`, `414`, or custom).
    pub fn status_code(&self) -> usize {
        match self {
            Self::BodyTooLarge => HttpStatus::PayloadTooLarge.code(),
            Self::UriTooLong => HttpStatus::URITooLong.code(),
            Self::InvalidHost | Self::PathTraversal => HttpStatus::BadRequest.code(),
            Self::CsrfInvalid => HttpStatus::Forbidden.code(),
            Self::Custom { status_code, .. } => *status_code,
        }
    }
}

/// Formatting for [`SecurityViolation`].
impl Display for SecurityViolation {
    /// Formats a human-readable description of the [`SecurityViolation`].
    ///
    /// # Arguments
    ///
    /// - `&mut Formatter<'_>` - Output formatter.
    ///
    /// # Returns
    ///
    /// - `fmt::Result` - Formatting outcome.
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::BodyTooLarge => f.write_str(MSG_BODY_TOO_LARGE),
            Self::UriTooLong => f.write_str(MSG_URI_TOO_LONG),
            Self::InvalidHost => f.write_str(MSG_INVALID_HOST),
            Self::PathTraversal => f.write_str(MSG_PATH_TRAVERSAL),
            Self::CsrfInvalid => f.write_str(MSG_CSRF_INVALID),
            Self::Custom { message, .. } => f.write_str(message),
        }
    }
}

/// Implements [`Error`] for [`SecurityViolation`].
impl Error for SecurityViolation {}

/// Default construction for [`SecurityHeadersConfig`].
impl Default for SecurityHeadersConfig {
    /// Constructs baseline modern HTTP security headers suitable for most services.
    fn default() -> Self {
        Self {
            hsts: Some(DEFAULT_HSTS_VALUE.to_owned()),
            content_security_policy: None,
            x_frame_options: Some(DEFAULT_X_FRAME_OPTIONS.to_owned()),
            x_content_type_options: Some(DEFAULT_X_CONTENT_TYPE_OPTIONS.to_owned()),
            referrer_policy: Some(DEFAULT_REFERRER_POLICY.to_owned()),
            permissions_policy: Some(DEFAULT_PERMISSIONS_POLICY.to_owned()),
            coop: Some(DEFAULT_COOP_VALUE.to_owned()),
            coep: None,
            corp: Some(DEFAULT_CORP_VALUE.to_owned()),
            remove_powered_by: true,
        }
    }
}

/// Inherent presets for [`SecurityHeadersConfig`].
impl SecurityHeadersConfig {
    /// Builds a strict browser-hardened header preset including strict CSP and COEP.
    ///
    /// # Returns
    ///
    /// - `Self` - Strict security headers configuration.
    pub fn strict() -> Self {
        Self {
            hsts: Some(DEFAULT_HSTS_VALUE.to_owned()),
            content_security_policy: Some(DEFAULT_CSP_STRICT.to_owned()),
            x_frame_options: Some(DEFAULT_X_FRAME_OPTIONS.to_owned()),
            x_content_type_options: Some(DEFAULT_X_CONTENT_TYPE_OPTIONS.to_owned()),
            referrer_policy: Some(DEFAULT_REFERRER_POLICY.to_owned()),
            permissions_policy: Some(DEFAULT_PERMISSIONS_POLICY.to_owned()),
            coop: Some(DEFAULT_COOP_VALUE.to_owned()),
            coep: Some(DEFAULT_COEP_VALUE.to_owned()),
            corp: Some(DEFAULT_CORP_VALUE.to_owned()),
            remove_powered_by: true,
        }
    }

    /// Prebuilds a flat `(header_name, value)` vector for zero-allocation hot-path injection.
    ///
    /// # Returns
    ///
    /// - `Vec<(&'static str, String)>` - Compiled static header pairs.
    pub(crate) fn compile(&self) -> Vec<(&'static str, String)> {
        let mut out: Vec<(&'static str, String)> = Vec::with_capacity(9);
        if let Some(val) = self.get_hsts() {
            out.push((STRICT_TRANSPORT_SECURITY, val));
        }
        if let Some(val) = self.get_content_security_policy() {
            out.push((CONTENT_SECURITY_POLICY, val));
        }
        if let Some(val) = self.get_x_frame_options() {
            out.push((X_FRAME_OPTIONS, val));
        }
        if let Some(val) = self.get_x_content_type_options() {
            out.push((X_CONTENT_TYPE_OPTIONS, val));
        }
        if let Some(val) = self.get_referrer_policy() {
            out.push((REFERRER_POLICY, val));
        }
        if let Some(val) = self.get_permissions_policy() {
            out.push((PERMISSIONS_POLICY, val));
        }
        if let Some(val) = self.get_coop() {
            out.push((CROSS_ORIGIN_OPENER_POLICY, val));
        }
        if let Some(val) = self.get_coep() {
            out.push((CROSS_ORIGIN_EMBEDDER_POLICY, val));
        }
        if let Some(val) = self.get_corp() {
            out.push((CROSS_ORIGIN_RESOURCE_POLICY, val));
        }
        out
    }
}

/// Default construction for [`CsrfConfig`].
impl Default for CsrfConfig {
    /// Constructs a disabled [`CsrfConfig`] with standard cookie and header names.
    fn default() -> Self {
        Self {
            enabled: false,
            secret: Vec::new(),
            cookie_name: DEFAULT_CSRF_COOKIE_NAME.to_owned(),
            header_name: DEFAULT_CSRF_HEADER_NAME.to_owned(),
            exempt_paths: Vec::new(),
        }
    }
}

/// Inherent constructors for [`CsrfConfig`].
impl CsrfConfig {
    /// Builds an enabled [`CsrfConfig`] using `secret` for stateless HMAC-SHA256 signing.
    ///
    /// # Arguments
    ///
    /// - `S` - Secret key bytes convertible into `Vec<u8>`.
    ///
    /// # Returns
    ///
    /// - `Self` - Enabled CSRF configuration.
    pub fn enabled<S>(secret: S) -> Self
    where
        S: Into<Vec<u8>>,
    {
        Self {
            enabled: true,
            secret: secret.into(),
            cookie_name: DEFAULT_CSRF_COOKIE_NAME.to_owned(),
            header_name: DEFAULT_CSRF_HEADER_NAME.to_owned(),
            exempt_paths: Vec::new(),
        }
    }
}

/// Default construction for [`RequestGuardConfig`].
impl Default for RequestGuardConfig {
    /// Constructs a [`RequestGuardConfig`] blocking path traversal and oversized URIs.
    fn default() -> Self {
        Self {
            max_body_bytes: DEFAULT_SECURITY_MAX_BODY_BYTES,
            max_uri_bytes: DEFAULT_MAX_URI_BYTES,
            allowed_hosts: Vec::new(),
            block_path_traversal: true,
        }
    }
}

/// Inherent presets and builders for [`SecurityConfig`].
impl SecurityConfig {
    /// Builds a strict browser-hardened [`SecurityConfig`] preset.
    ///
    /// # Returns
    ///
    /// - `Self` - Strict security configuration.
    pub fn strict() -> Self {
        Self {
            headers: SecurityHeadersConfig::strict(),
            csrf: CsrfConfig::default(),
            guard: RequestGuardConfig::default(),
        }
    }

    /// Sets the [`SecurityHeadersConfig`].
    ///
    /// # Arguments
    ///
    /// - `SecurityHeadersConfig` - Header settings.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated configuration.
    pub fn with_headers(mut self, headers: SecurityHeadersConfig) -> Self {
        self.set_headers(headers);
        self
    }

    /// Sets the [`CsrfConfig`].
    ///
    /// # Arguments
    ///
    /// - `CsrfConfig` - CSRF protection settings.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated configuration.
    pub fn with_csrf(mut self, csrf: CsrfConfig) -> Self {
        self.set_csrf(csrf);
        self
    }

    /// Sets the [`RequestGuardConfig`].
    ///
    /// # Arguments
    ///
    /// - `RequestGuardConfig` - Request guard settings.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated configuration.
    pub fn with_guard(mut self, guard: RequestGuardConfig) -> Self {
        self.set_guard(guard);
        self
    }
}

/// Inherent implementation of [`SecurityPlugin`].
impl SecurityPlugin {
    /// Compiles a standalone [`SecurityPlugin`] instance (useful for direct invocation or testing).
    ///
    /// # Arguments
    ///
    /// - `SecurityConfig` - Security headers, CSRF, and guard settings.
    /// - `Vec<SharedSecurityRule>` - Additional custom security inspection rules.
    ///
    /// # Returns
    ///
    /// - `Self` - Compiled security engine.
    pub fn new(config: SecurityConfig, rules: Vec<SharedSecurityRule>) -> Self {
        let compiled_headers: Vec<(&'static str, String)> = config.get_headers().compile();
        let csrf_hmac_key: Option<HmacSha256Key> = if config.get_csrf().get_enabled() {
            Some(precompute_hmac_sha256_key(config.get_csrf().get_secret()))
        } else {
            None
        };
        let csrf_header_lower: String = config.get_csrf().get_header_name().to_ascii_lowercase();
        Self {
            config,
            compiled_headers: Arc::new(compiled_headers),
            rules: Arc::new(rules),
            csrf_hmac_key,
            csrf_header_lower,
        }
    }

    /// Initializes the global [`SecurityPlugin`] once with `config`.
    ///
    /// # Arguments
    ///
    /// - `SecurityConfig` - Security hardening configuration.
    ///
    /// # Returns
    ///
    /// - `&'static Self` - Reference to the initialized global plugin.
    pub fn init(config: SecurityConfig) -> &'static Self {
        Self::init_with_rules(config, Vec::new())
    }

    /// Initializes the global [`SecurityPlugin`] once with `config` and custom [`SecurityRule`] hooks.
    ///
    /// # Arguments
    ///
    /// - `SecurityConfig` - Security hardening configuration.
    /// - `Vec<SharedSecurityRule>` - Custom request security rules.
    ///
    /// # Returns
    ///
    /// - `&'static Self` - Reference to the initialized global plugin.
    pub fn init_with_rules(
        config: SecurityConfig,
        rules: Vec<SharedSecurityRule>,
    ) -> &'static Self {
        SECURITY_PLUGIN.get_or_init(|| Self::new(config, rules))
    }

    /// Returns the initialized global [`SecurityPlugin`], if initialized.
    ///
    /// # Returns
    ///
    /// - `Option<&'static Self>` - Reference to the global plugin, or `None`.
    pub fn get() -> Option<&'static Self> {
        SECURITY_PLUGIN.get()
    }

    /// Returns the active [`SecurityConfig`].
    ///
    /// # Returns
    ///
    /// - `&SecurityConfig` - Active configuration.
    pub fn get_config(&self) -> &SecurityConfig {
        &self.config
    }

    /// Evaluates request guards, double-submit HMAC CSRF protection, and custom rules on `ctx`.
    ///
    /// # Arguments
    ///
    /// - `&Context` - Incoming request context.
    ///
    /// # Returns
    ///
    /// - `Result<(), SecurityViolation>` - `Ok(())` when all checks pass.
    pub fn inspect_request(&self, ctx: &Context) -> Result<(), SecurityViolation> {
        let Self {
            config,
            rules,
            csrf_hmac_key,
            csrf_header_lower,
            ..
        } = self;
        let guard: &RequestGuardConfig = config.get_guard();
        let request: &Request = ctx.get_request();
        let path: &str = request.get_path();
        if guard.get_max_uri_bytes() > 0 && path.len() > guard.get_max_uri_bytes() {
            return Err(SecurityViolation::UriTooLong);
        }
        if guard.get_max_body_bytes() > 0 && request.get_body().len() > guard.get_max_body_bytes() {
            return Err(SecurityViolation::BodyTooLarge);
        }
        if guard.get_block_path_traversal() && has_path_traversal(path) {
            return Err(SecurityViolation::PathTraversal);
        }
        if !guard.get_allowed_hosts().is_empty() {
            let host_ok: bool =
                get_request_header_back_ref(request, HOST).is_some_and(|incoming: &str| {
                    let trimmed: &str = incoming.trim();
                    guard
                        .get_allowed_hosts()
                        .iter()
                        .any(|allowed: &String| allowed.eq_ignore_ascii_case(trimmed))
                });
            if !host_ok {
                return Err(SecurityViolation::InvalidHost);
            }
        }
        let csrf: &CsrfConfig = config.get_csrf();
        if csrf.get_enabled()
            && !is_csrf_safe_method(request.get_method())
            && !csrf
                .get_exempt_paths()
                .iter()
                .any(|exempt: &String| exempt == path)
        {
            let Some(header_token) = get_request_header_back_ref(request, csrf_header_lower) else {
                return Err(SecurityViolation::CsrfInvalid);
            };
            let Some(raw_cookie) = get_request_header_back_ref(request, COOKIE) else {
                return Err(SecurityViolation::CsrfInvalid);
            };
            let Some(cookie_token) = find_cookie_value(raw_cookie, csrf.get_cookie_name()) else {
                return Err(SecurityViolation::CsrfInvalid);
            };
            if !constant_time_eq(header_token.as_bytes(), cookie_token.as_bytes()) {
                return Err(SecurityViolation::CsrfInvalid);
            }
            let sig_ok: bool = if let Some(pre) = csrf_hmac_key {
                verify_csrf_token_with_key(pre, header_token)
            } else {
                verify_csrf_token(csrf.get_secret(), header_token)
            };
            if !sig_ok {
                return Err(SecurityViolation::CsrfInvalid);
            }
        }
        for rule in rules.iter() {
            rule.inspect(ctx)?;
        }
        Ok(())
    }

    /// Applies prebuilt security headers onto `response` and optionally strips `X-Powered-By`.
    ///
    /// # Arguments
    ///
    /// - `&mut Response` - Outgoing HTTP response.
    #[inline]
    pub fn apply_headers(&self, response: &mut Response) {
        let Self {
            config,
            compiled_headers,
            ..
        } = self;
        let headers_slice: &[(&'static str, String)] = compiled_headers.as_slice();
        if response.get_headers().is_empty() {
            for (name, value) in headers_slice {
                response.set_header(*name, value);
            }
            return;
        }
        for (name, value) in headers_slice {
            if !response.has_header(*name) {
                response.set_header(*name, value);
            }
        }
        if config.get_headers().get_remove_powered_by() {
            response.remove_header(X_POWERED_BY);
        }
    }
}

/// Implements [`ServerHook`] for [`SecurityRequestHook`].
impl ServerHook for SecurityRequestHook {
    /// Constructs a per-request [`SecurityRequestHook`].
    ///
    /// # Arguments
    ///
    /// - `&mut Stream` - Connection stream.
    /// - `&mut Context` - Request context.
    ///
    /// # Returns
    ///
    /// - `Self` - Hook instance.
    async fn new(_: &mut Stream, _: &mut Context) -> Self {
        Self
    }

    /// Enforces request security guards, CSRF validation, and custom rules, and seeds response security headers.
    ///
    /// # Arguments
    ///
    /// - `Self` - Hook instance.
    /// - `&mut Stream` - Connection stream.
    /// - `&mut Context` - Request context.
    ///
    /// # Returns
    ///
    /// - `Status` - `Status::Continue` when permitted; `Status::Reject` after sending an error response on violation.
    async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status {
        let Some(plugin) = SecurityPlugin::get() else {
            return Status::Continue;
        };
        plugin.apply_headers(ctx.get_mut_response());
        if let Err(violation) = plugin.inspect_request(ctx) {
            ctx.get_mut_response()
                .set_status_code(violation.status_code())
                .set_body(Vec::new());
            let wire: ResponseData = ctx.get_mut_response().build();
            let _: Result<(), ResponseError> = stream.try_send(&wire).await;
            return Status::Reject;
        }
        Status::Continue
    }
}

/// Implements [`ServerHook`] for [`SecurityResponseHook`].
impl ServerHook for SecurityResponseHook {
    /// Constructs a per-response [`SecurityResponseHook`].
    ///
    /// # Arguments
    ///
    /// - `&mut Stream` - Connection stream.
    /// - `&mut Context` - Request context.
    ///
    /// # Returns
    ///
    /// - `Self` - Hook instance.
    async fn new(_: &mut Stream, _: &mut Context) -> Self {
        Self
    }

    /// Ensures configured security headers remain present and removes `X-Powered-By`.
    ///
    /// # Arguments
    ///
    /// - `Self` - Hook instance.
    /// - `&mut Stream` - Connection stream.
    /// - `&mut Context` - Request context.
    ///
    /// # Returns
    ///
    /// - `Status` - Always `Status::Continue`.
    async fn handle(self, _: &mut Stream, ctx: &mut Context) -> Status {
        let Some(plugin) = SecurityPlugin::get() else {
            return Status::Continue;
        };
        plugin.apply_headers(ctx.get_mut_response());
        Status::Continue
    }
}