praxis-proxy-filter 0.5.6

Filter pipeline engine and built-in filters for Praxis
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024 Praxis Contributors

//! Filter registry: maps filter type names to their factory functions.

use std::collections::HashMap;

use praxis_core::config::InsecureOptions;

use crate::{
    any_filter::AnyFilter,
    binding::{ChainBindingContext, ChainBindingHttpFactory},
    factory::{FilterFactory, HttpFilterFactoryFn, TcpFilterFactoryFn, http_builtin, tcp_builtin},
    filter::FilterError,
};

// -----------------------------------------------------------------------------
// SecurityClass
// -----------------------------------------------------------------------------

/// Classifies whether a filter is security-critical.
///
/// Security-class filters enforce access control, authentication, rate
/// limiting, or other protective policies. This metadata enables future
/// validation (e.g. preventing `SkipTo` from bypassing security filters).
///
/// ```
/// use praxis_filter::SecurityClass;
///
/// assert_eq!(SecurityClass::default(), SecurityClass::Standard);
/// ```
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum SecurityClass {
    /// A security-critical filter (e.g. `cors`, `csrf`, `ip_acl`).
    Security,

    /// A non-security filter (default).
    #[default]
    Standard,
}

// -----------------------------------------------------------------------------
// FilterRegistration
// -----------------------------------------------------------------------------

/// A filter factory paired with its [`SecurityClass`] metadata.
struct FilterRegistration {
    /// The factory function that creates filter instances.
    factory: RegisteredFilterFactory,

    /// Whether this filter is security-critical.
    security_class: SecurityClass,
}

/// A normal public factory or a built-in factory that also needs
/// access to the registry currently resolving the pipeline.
enum RegisteredFilterFactory {
    /// A public factory whose configuration is self-contained.
    Standard(FilterFactory),

    /// A built-in HTTP factory that resolves nested filters.
    HttpWithRegistry(RegistryHttpFilterFactory),

    /// An application HTTP factory that binds an outbound subrequest chain at
    /// construction time via a [`ChainBindingContext`].
    ChainBinding(ChainBindingHttpFactory),
}

/// Factory for a built-in HTTP filter that resolves nested filters
/// against its containing registry.
///
/// The [`InsecureOptions`] argument carries the operator's declared security
/// posture so a factory that builds nested step pipelines gates their inline
/// outbound clusters (SSRF/TLS-verify) by the containing build's real posture
/// rather than an unconditional strict default.
type RegistryHttpFilterFactory = fn(
    &serde_yaml::Value,
    &FilterRegistry,
    &InsecureOptions,
) -> Result<Box<dyn crate::filter::HttpFilter>, FilterError>;

impl RegisteredFilterFactory {
    /// Instantiate the registered filter without an outbound-chain binding
    /// context.
    ///
    /// A [`ChainBinding`](Self::ChainBinding) factory cannot resolve its
    /// outbound chain without a [`ChainBindingContext`], so this path rejects
    /// it and directs callers to [`FilterPipeline::build_with_chains`], which
    /// supplies one.
    ///
    /// [`FilterPipeline::build_with_chains`]: crate::FilterPipeline::build_with_chains
    fn create(&self, config: &serde_yaml::Value, registry: &FilterRegistry) -> Result<AnyFilter, FilterError> {
        match self {
            Self::Standard(factory) => factory.create(config),
            // No binding context here, so no operator posture is available: nested
            // step pipelines are gated by the strict default. The real server path
            // builds through `create_with_binding`, which threads the declared
            // posture from the [`ChainBindingContext`].
            Self::HttpWithRegistry(factory) => {
                Ok(AnyFilter::Http(factory(config, registry, &InsecureOptions::default())?))
            },
            Self::ChainBinding(_) => Err(FilterError::from(
                "this filter binds an outbound subrequest chain and must be built via \
                 FilterPipeline::build_with_chains",
            )),
        }
    }

    /// Instantiate the registered filter, supplying an outbound-chain binding
    /// context to [`ChainBinding`](Self::ChainBinding) factories.
    fn create_with_binding(
        &self,
        config: &serde_yaml::Value,
        registry: &FilterRegistry,
        ctx: &ChainBindingContext<'_>,
    ) -> Result<AnyFilter, FilterError> {
        match self {
            Self::Standard(factory) => factory.create(config),
            Self::HttpWithRegistry(factory) => Ok(AnyFilter::Http(factory(config, registry, ctx.insecure_options())?)),
            Self::ChainBinding(factory) => Ok(AnyFilter::Http(factory(config, ctx)?)),
        }
    }
}

// -----------------------------------------------------------------------------
// FilterRegistry
// -----------------------------------------------------------------------------

/// Registry of available filter types.
///
/// ```
/// use praxis_filter::FilterRegistry;
///
/// let registry = FilterRegistry::with_builtins();
/// let mut names = registry.available_filters();
/// names.sort();
/// assert!(names.contains(&"load_balancer"));
/// assert!(names.contains(&"request_id"));
/// assert!(names.contains(&"router"));
/// ```
pub struct FilterRegistry {
    /// Maps filter names to their registrations (factory + metadata).
    filters: HashMap<String, FilterRegistration>,
}

impl FilterRegistry {
    /// Creates a registry with only the built-in filters.
    #[must_use]
    pub fn with_builtins() -> Self {
        let mut filters = HashMap::new();
        register_http_builtins(&mut filters);
        register_tcp_builtins(&mut filters);
        Self { filters }
    }

    /// Registers a custom filter factory with [`SecurityClass::Standard`].
    ///
    /// Returns an error if a filter with the same name is already registered.
    ///
    /// ```
    /// use praxis_filter::{FilterFactory, FilterRegistry, http_builtin};
    ///
    /// let mut registry = FilterRegistry::with_builtins();
    /// let err = registry
    ///     .register(
    ///         "router",
    ///         FilterFactory::Http(std::sync::Arc::new(|_| Err("unused".into()))),
    ///     )
    ///     .unwrap_err();
    /// assert!(err.to_string().contains("duplicate filter name"));
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`FilterError`] if the name is already registered.
    pub fn register(&mut self, name: &str, factory: FilterFactory) -> Result<(), FilterError> {
        self.register_with_class(name, factory, SecurityClass::Standard)
    }

    /// Registers a custom filter factory with an explicit [`SecurityClass`].
    ///
    /// ```
    /// use praxis_filter::{FilterFactory, FilterRegistry, SecurityClass};
    ///
    /// let mut registry = FilterRegistry::with_builtins();
    /// let factory = FilterFactory::Http(std::sync::Arc::new(|_| Err("unused".into())));
    /// registry
    ///     .register_with_class("my_auth", factory, SecurityClass::Security)
    ///     .unwrap();
    /// assert!(registry.is_security_filter("my_auth"));
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`FilterError`] if the name is already registered.
    pub fn register_with_class(
        &mut self,
        name: &str,
        factory: FilterFactory,
        security_class: SecurityClass,
    ) -> Result<(), FilterError> {
        if self.filters.contains_key(name) {
            return Err(format!("duplicate filter name: '{name}'").into());
        }
        self.filters.insert(
            name.to_owned(),
            FilterRegistration {
                factory: RegisteredFilterFactory::Standard(factory),
                security_class,
            },
        );
        Ok(())
    }

    /// Registers an application HTTP filter that binds an outbound subrequest
    /// chain at construction time, with [`SecurityClass::Standard`].
    ///
    /// The factory receives a [`ChainBindingContext`] and resolves its
    /// configured outbound chain into a prebuilt [`FilterPipeline`]. This is
    /// the mechanism application callout filters (e.g. Praxis AI) use to bind
    /// reusable outbound chains against the active registry.
    ///
    /// # Errors
    ///
    /// Returns [`FilterError`] if the name is already registered.
    ///
    /// # Examples
    ///
    /// Register an application callout that binds its `outbound_chain` once at
    /// construction. Building — or hot-reloading — a pipeline that names the
    /// callout resolves and prebuilds the outbound chain against the active
    /// registry, so a missing filter, a reference cycle, or excessive nesting
    /// fails the build instead of a request:
    ///
    /// ```
    /// use std::{collections::HashMap, sync::Arc};
    ///
    /// use async_trait::async_trait;
    /// use praxis_core::config::{ChainRef, FilterEntry, InsecureOptions};
    /// use praxis_filter::{
    ///     ChainBindingContext, FilterAction, FilterError, FilterPipeline, FilterRegistry, HttpFilter,
    ///     HttpFilterContext,
    /// };
    ///
    /// // The application filter owns the prebuilt outbound pipeline.
    /// struct AiCallout {
    ///     outbound: Arc<FilterPipeline>,
    /// }
    ///
    /// #[async_trait]
    /// impl HttpFilter for AiCallout {
    ///     fn name(&self) -> &'static str {
    ///         "ai_callout"
    ///     }
    ///
    ///     // Delegate the framework's nesting hooks so the bound pipeline joins
    ///     // runtime-resource propagation, hot-reload file discovery, and
    ///     // insecure-option application.
    ///     fn visit_nested_pipelines(&mut self, visitor: &mut dyn FnMut(&mut FilterPipeline)) {
    ///         if let Some(pipeline) = Arc::get_mut(&mut self.outbound) {
    ///             visitor(pipeline);
    ///         }
    ///     }
    ///     fn referenced_files(&self) -> Vec<std::path::PathBuf> {
    ///         self.outbound.referenced_files()
    ///     }
    ///     fn apply_insecure_options(&self, options: &InsecureOptions) {
    ///         self.outbound.apply_insecure_options(options);
    ///     }
    ///
    ///     async fn on_request(
    ///         &self,
    ///         _ctx: &mut HttpFilterContext<'_>,
    ///     ) -> Result<FilterAction, FilterError> {
    ///         Ok(FilterAction::Continue)
    ///     }
    /// }
    ///
    /// let mut registry = FilterRegistry::with_builtins();
    /// registry
    ///     .register_chain_binding(
    ///         "ai_callout",
    ///         Arc::new(|config: &serde_yaml::Value, ctx: &ChainBindingContext<'_>| {
    ///             let raw = config
    ///                 .get("outbound_chain")
    ///                 .cloned()
    ///                 .ok_or_else(|| FilterError::from("ai_callout: missing outbound_chain"))?;
    ///             let chain_ref: ChainRef = serde_yaml::from_value(raw)
    ///                 .map_err(|e| FilterError::from(format!("ai_callout: bad outbound_chain: {e}")))?;
    ///             // Resolve + prebuild the outbound chain against the active registry.
    ///             let outbound = ctx.bind_chain(&chain_ref)?;
    ///             let filter: Box<dyn HttpFilter> = Box::new(AiCallout {
    ///                 outbound: Arc::new(outbound),
    ///             });
    ///             Ok(filter)
    ///         }),
    ///     )
    ///     .unwrap();
    ///
    /// // Building a pipeline that uses the callout binds its outbound chain now,
    /// // before any request is served; a hot reload rebuilds it the same way.
    /// let mut top: Vec<FilterEntry> = serde_yaml::from_str(
    ///     "- filter: ai_callout\n  outbound_chain:\n    name: outbound\n    filters:\n      - filter: request_id\n",
    /// )
    /// .unwrap();
    /// let chains: HashMap<&str, &[FilterEntry]> = HashMap::new();
    /// FilterPipeline::build_with_chains(&mut top, &registry, &chains, &InsecureOptions::default())
    ///     .expect("the outbound chain is validated and bound at build time");
    /// ```
    ///
    /// [`ChainBindingContext`]: crate::ChainBindingContext
    /// [`FilterPipeline`]: crate::FilterPipeline
    pub fn register_chain_binding(&mut self, name: &str, factory: ChainBindingHttpFactory) -> Result<(), FilterError> {
        self.register_chain_binding_with_class(name, factory, SecurityClass::Standard)
    }

    /// Registers a chain-binding filter with an explicit [`SecurityClass`].
    ///
    /// See [`register_chain_binding`](Self::register_chain_binding).
    ///
    /// # Errors
    ///
    /// Returns [`FilterError`] if the name is already registered.
    pub fn register_chain_binding_with_class(
        &mut self,
        name: &str,
        factory: ChainBindingHttpFactory,
        security_class: SecurityClass,
    ) -> Result<(), FilterError> {
        if self.filters.contains_key(name) {
            return Err(format!("duplicate filter name: '{name}'").into());
        }
        self.filters.insert(
            name.to_owned(),
            FilterRegistration {
                factory: RegisteredFilterFactory::ChainBinding(factory),
                security_class,
            },
        );
        Ok(())
    }

    /// Instantiates a filter by type name and config.
    ///
    /// ```
    /// use praxis_filter::FilterRegistry;
    ///
    /// let registry = FilterRegistry::with_builtins();
    /// let filter = registry.create(
    ///     "router",
    ///     &serde_yaml::from_str("routes:\n  - path_prefix: \"/\"\n    cluster: web").unwrap(),
    /// );
    /// assert!(filter.is_ok());
    ///
    /// let err = registry
    ///     .create("nonexistent", &serde_yaml::Value::Null)
    ///     .err()
    ///     .expect("should fail for unknown type");
    /// assert!(err.to_string().contains("unknown filter type"));
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`FilterError`] if the filter type is unknown or instantiation fails.
    pub fn create(&self, name: &str, config: &serde_yaml::Value) -> Result<AnyFilter, FilterError> {
        let registration = self
            .filters
            .get(name)
            .ok_or_else(|| -> FilterError { format!("unknown filter type: '{name}'").into() })?;
        registration.factory.create(config, self)
    }

    /// Instantiates a filter, supplying a [`ChainBindingContext`] so
    /// chain-binding filters can bind their outbound chains.
    ///
    /// Used by the branch-aware pipeline builder. Non-binding filters ignore
    /// the context and behave exactly as under [`create`](Self::create).
    ///
    /// # Errors
    ///
    /// Returns [`FilterError`] if the filter type is unknown or instantiation
    /// fails.
    pub(crate) fn create_with_binding(
        &self,
        name: &str,
        config: &serde_yaml::Value,
        ctx: &ChainBindingContext<'_>,
    ) -> Result<AnyFilter, FilterError> {
        let registration = self
            .filters
            .get(name)
            .ok_or_else(|| -> FilterError { format!("unknown filter type: '{name}'").into() })?;
        registration.factory.create_with_binding(config, self, ctx)
    }

    /// Returns the names of all registered filter types.
    pub fn available_filters(&self) -> Vec<&str> {
        self.filters.keys().map(String::as_str).collect()
    }

    /// Returns `true` if the named filter has [`SecurityClass::Security`].
    ///
    /// Returns `false` for unknown filter names.
    ///
    /// ```
    /// use praxis_filter::FilterRegistry;
    ///
    /// let registry = FilterRegistry::with_builtins();
    /// assert!(registry.is_security_filter("cors"));
    /// assert!(registry.is_security_filter("ip_acl"));
    /// assert!(!registry.is_security_filter("router"));
    /// assert!(!registry.is_security_filter("nonexistent"));
    /// ```
    pub fn is_security_filter(&self, name: &str) -> bool {
        self.filters
            .get(name)
            .is_some_and(|r| r.security_class == SecurityClass::Security)
    }

    /// Returns the names of all filters with [`SecurityClass::Security`].
    ///
    /// ```
    /// use praxis_filter::FilterRegistry;
    ///
    /// let registry = FilterRegistry::with_builtins();
    /// let mut sec = registry.security_filters();
    /// sec.sort();
    /// assert!(sec.contains(&"cors"));
    /// assert!(sec.contains(&"csrf"));
    /// assert!(sec.contains(&"ip_acl"));
    /// assert!(!sec.contains(&"router"));
    /// ```
    pub fn security_filters(&self) -> Vec<&str> {
        self.filters
            .iter()
            .filter(|(_, r)| r.security_class == SecurityClass::Security)
            .map(|(name, _)| name.as_str())
            .collect()
    }
}

// -----------------------------------------------------------------------------
// Filter Factory - Registration
// -----------------------------------------------------------------------------

/// Registers all built-in HTTP filter factories.
#[expect(clippy::too_many_lines, reason = "one line per filter, will grow")]
fn register_http_builtins(filters: &mut HashMap<String, FilterRegistration>) {
    use crate::builtins::{
        AccessLogFilter, CircuitBreakerFilter, CompressionFilter, CorsFilter, CredentialInjectionFilter, CsrfFilter,
        ForwardedHeadersFilter, GrpcDetectionFilter, HeaderFilter, IpAclFilter, JsonBodyFieldFilter, JsonRpcFilter,
        PathRewriteFilter, PeerIdentityTrustFilter, RateLimitFilter, RedirectFilter, RequestIdFilter,
        StaticResponseFilter, TimeoutFilter, TraceContextFilter, UrlRewriteFilter,
    };

    register_http(filters, "access_log", AccessLogFilter::from_config);
    #[cfg(feature = "basic-auth-filter")]
    register_http_security(filters, "basic_auth", crate::BasicAuthFilter::from_config);
    register_http(filters, "circuit_breaker", CircuitBreakerFilter::from_config);
    register_http(filters, "compression", CompressionFilter::from_config);
    register_http_security(filters, "cors", CorsFilter::from_config);
    #[cfg(feature = "policy-engine")]
    register_http_security(filters, "policy", crate::PolicyFilter::from_config);
    register_http_security(filters, "csrf", CsrfFilter::from_config);
    register_http_security(filters, "credential_injection", CredentialInjectionFilter::from_config);
    register_http(
        filters,
        "endpoint_selector",
        crate::builtins::EndpointSelectorFilter::from_config,
    );
    register_http(filters, "headers", HeaderFilter::from_config);
    register_http_security(filters, "forwarded_headers", ForwardedHeadersFilter::from_config);
    register_http(filters, "grpc_detection", GrpcDetectionFilter::from_config);
    register_http_security(filters, "guardrails", crate::GuardrailsFilter::from_config);
    register_http_security(filters, "ip_acl", IpAclFilter::from_config);
    register_http_with_registry(
        filters,
        "iterative_request_router",
        crate::builtins::IterativeRequestRouterFilter::from_config_with_registry_and_insecure,
    );
    register_http(filters, "load_balancer", crate::LoadBalancerFilter::from_config);
    register_http(filters, "path_rewrite", PathRewriteFilter::from_config);
    register_http_security(filters, "rate_limit", RateLimitFilter::from_config);
    register_http(filters, "redirect", RedirectFilter::from_config);
    register_http(filters, "request_id", RequestIdFilter::from_config);
    register_http(filters, "router", crate::RouterFilter::from_config);
    register_http(filters, "static_response", StaticResponseFilter::from_config);
    register_http(filters, "sticky_sessions", crate::StickySessionsFilter::from_config);
    register_http(filters, "timeout", TimeoutFilter::from_config);
    register_http(filters, "trace_context", TraceContextFilter::from_config);
    register_http(filters, "url_rewrite", UrlRewriteFilter::from_config);
    register_http(filters, "json_body_field", JsonBodyFieldFilter::from_config);
    register_http(filters, "json_rpc", JsonRpcFilter::from_config);
    register_http_security(filters, "peer_identity_trust", PeerIdentityTrustFilter::from_config);
}

/// Registers a single HTTP filter factory with [`SecurityClass::Standard`].
fn register_http(filters: &mut HashMap<String, FilterRegistration>, name: &str, factory_fn: HttpFilterFactoryFn) {
    insert_registration(filters, name, http_builtin(factory_fn), SecurityClass::Standard);
}

/// Registers a built-in HTTP filter whose nested configuration must
/// resolve against the same registry as its containing pipeline.
fn register_http_with_registry(
    filters: &mut HashMap<String, FilterRegistration>,
    name: &str,
    factory_fn: RegistryHttpFilterFactory,
) {
    let prev = filters.insert(
        name.to_owned(),
        FilterRegistration {
            factory: RegisteredFilterFactory::HttpWithRegistry(factory_fn),
            security_class: SecurityClass::Standard,
        },
    );
    debug_assert!(prev.is_none(), "duplicate built-in filter name: '{name}'");
}

/// Registers a single HTTP filter factory with [`SecurityClass::Security`].
fn register_http_security(
    filters: &mut HashMap<String, FilterRegistration>,
    name: &str,
    factory_fn: HttpFilterFactoryFn,
) {
    insert_registration(filters, name, http_builtin(factory_fn), SecurityClass::Security);
}

/// Registers all built-in TCP filter factories.
fn register_tcp_builtins(filters: &mut HashMap<String, FilterRegistration>) {
    register_tcp(filters, "sni_router", crate::builtins::SniRouterFilter::from_config);
    register_tcp(
        filters,
        "tcp_access_log",
        crate::builtins::TcpAccessLogFilter::from_config,
    );
    register_tcp(
        filters,
        "tcp_load_balancer",
        crate::builtins::TcpLoadBalancerFilter::from_config,
    );
}

/// Registers a single TCP filter factory with [`SecurityClass::Standard`].
fn register_tcp(filters: &mut HashMap<String, FilterRegistration>, name: &str, factory_fn: TcpFilterFactoryFn) {
    insert_registration(filters, name, tcp_builtin(factory_fn), SecurityClass::Standard);
}

/// Inserts a [`FilterRegistration`] into the map, asserting no duplicates.
fn insert_registration(
    filters: &mut HashMap<String, FilterRegistration>,
    name: &str,
    factory: FilterFactory,
    security_class: SecurityClass,
) {
    let prev = filters.insert(
        name.to_owned(),
        FilterRegistration {
            factory: RegisteredFilterFactory::Standard(factory),
            security_class,
        },
    );
    debug_assert!(prev.is_none(), "duplicate built-in filter name: '{name}'");
}

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

#[cfg(test)]
#[expect(clippy::allow_attributes, reason = "blanket test suppressions")]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::indexing_slicing,
    clippy::panic,
    clippy::cognitive_complexity,
    clippy::too_many_lines,
    clippy::stable_sort_primitive,
    reason = "tests"
)]
mod tests {
    use super::*;

    #[test]
    fn builtins_registered() {
        let registry = FilterRegistry::with_builtins();
        let mut names = registry.available_filters();
        names.sort();

        assert!(names.contains(&"access_log"), "access_log should be registered");
        #[cfg(feature = "basic-auth-filter")]
        assert!(names.contains(&"basic_auth"), "basic_auth should be registered");
        assert!(
            names.contains(&"circuit_breaker"),
            "circuit_breaker should be registered"
        );
        assert!(names.contains(&"compression"), "compression should be registered");
        assert!(names.contains(&"cors"), "cors should be registered");
        assert!(names.contains(&"csrf"), "csrf should be registered");
        assert!(
            names.contains(&"credential_injection"),
            "credential_injection should be registered"
        );
        assert!(
            names.contains(&"endpoint_selector"),
            "endpoint_selector should be registered"
        );
        assert!(
            names.contains(&"forwarded_headers"),
            "forwarded_headers should be registered"
        );
        assert!(names.contains(&"grpc_detection"), "grpc_detection should be registered");
        assert!(names.contains(&"guardrails"), "guardrails should be registered");
        assert!(names.contains(&"headers"), "headers should be registered");
        assert!(names.contains(&"ip_acl"), "ip_acl should be registered");
        assert!(names.contains(&"load_balancer"), "load_balancer should be registered");
        assert!(names.contains(&"path_rewrite"), "path_rewrite should be registered");
        assert!(names.contains(&"rate_limit"), "rate_limit should be registered");
        assert!(names.contains(&"redirect"), "redirect should be registered");
        assert!(names.contains(&"request_id"), "request_id should be registered");
        assert!(names.contains(&"router"), "router should be registered");
        assert!(names.contains(&"sni_router"), "sni_router should be registered");
        assert!(
            names.contains(&"static_response"),
            "static_response should be registered"
        );
        assert!(
            names.contains(&"sticky_sessions"),
            "sticky_sessions should be registered"
        );
        assert!(names.contains(&"tcp_access_log"), "tcp_access_log should be registered");
        assert!(
            names.contains(&"tcp_load_balancer"),
            "tcp_load_balancer should be registered"
        );
        assert!(names.contains(&"timeout"), "timeout should be registered");
        assert!(names.contains(&"trace_context"), "trace_context should be registered");
        assert!(names.contains(&"url_rewrite"), "url_rewrite should be registered");
        assert!(
            names.contains(&"json_body_field"),
            "json_body_field should be registered"
        );
        assert!(
            names.contains(&"iterative_request_router"),
            "iterative_request_router should be registered"
        );
        assert!(names.contains(&"json_rpc"), "json_rpc should be registered");
        assert!(
            names.contains(&"peer_identity_trust"),
            "peer_identity_trust should be registered"
        );
        #[cfg(feature = "policy-engine")]
        assert!(names.contains(&"policy"), "policy should be registered");
    }

    #[test]
    fn unknown_filter_errors() {
        let registry = FilterRegistry::with_builtins();
        match registry.create("nonexistent", &serde_yaml::Value::Null) {
            Err(e) => assert!(
                e.to_string().contains("unknown filter type"),
                "error should mention unknown filter type"
            ),
            Ok(_) => panic!("expected error for unknown filter type"),
        }
    }

    #[test]
    fn register_custom_filter_succeeds() {
        let mut registry = FilterRegistry::with_builtins();
        let factory = FilterFactory::Http(std::sync::Arc::new(|_| Err("unused".into())));
        assert!(
            registry.register("my_custom", factory).is_ok(),
            "registering a unique name should succeed"
        );
        assert!(
            registry.available_filters().contains(&"my_custom"),
            "custom filter should appear in available filters"
        );
    }

    #[test]
    fn register_duplicate_builtin_errors() {
        let mut registry = FilterRegistry::with_builtins();
        let factory = FilterFactory::Http(std::sync::Arc::new(|_| Err("unused".into())));
        let err = registry.register("router", factory).unwrap_err();
        assert!(
            err.to_string().contains("duplicate filter name: 'router'"),
            "error should name the duplicate: {err}"
        );
    }

    #[test]
    fn register_duplicate_custom_errors() {
        let mut registry = FilterRegistry::with_builtins();
        let factory_a = FilterFactory::Http(std::sync::Arc::new(|_| Err("a".into())));
        let factory_b = FilterFactory::Http(std::sync::Arc::new(|_| Err("b".into())));
        registry.register("my_filter", factory_a).unwrap();
        let err = registry.register("my_filter", factory_b).unwrap_err();
        assert!(
            err.to_string().contains("duplicate filter name: 'my_filter'"),
            "error should name the duplicate: {err}"
        );
    }

    #[test]
    fn security_class_default_is_standard() {
        assert_eq!(
            SecurityClass::default(),
            SecurityClass::Standard,
            "default SecurityClass should be Standard"
        );
    }

    #[test]
    fn builtin_security_filters_classified() {
        let registry = FilterRegistry::with_builtins();
        #[allow(unused_mut, reason = "mutated only with basic-auth-filter")]
        let mut expected_security = vec![
            "cors",
            "credential_injection",
            "csrf",
            "forwarded_headers",
            "guardrails",
            "ip_acl",
            "peer_identity_trust",
            "rate_limit",
        ];
        #[cfg(feature = "basic-auth-filter")]
        expected_security.push("basic_auth");

        for name in &expected_security {
            assert!(
                registry.is_security_filter(name),
                "{name} should be classified as Security"
            );
        }
    }

    #[test]
    fn builtin_standard_filters_not_classified_as_security() {
        let registry = FilterRegistry::with_builtins();
        let expected_standard = [
            "access_log",
            "circuit_breaker",
            "compression",
            "headers",
            "load_balancer",
            "router",
            "sticky_sessions",
            "timeout",
        ];

        for name in &expected_standard {
            assert!(
                !registry.is_security_filter(name),
                "{name} should be classified as Standard"
            );
        }
    }

    #[test]
    fn is_security_filter_returns_false_for_unknown() {
        let registry = FilterRegistry::with_builtins();
        assert!(
            !registry.is_security_filter("nonexistent"),
            "unknown filter should not be classified as Security"
        );
    }

    #[test]
    fn security_filters_returns_all_security_names() {
        let registry = FilterRegistry::with_builtins();
        let mut sec = registry.security_filters();
        sec.sort();

        #[cfg(feature = "basic-auth-filter")]
        assert!(sec.contains(&"basic_auth"), "basic_auth should be in security_filters");
        assert!(sec.contains(&"cors"), "cors should be in security_filters");
        assert!(
            sec.contains(&"credential_injection"),
            "credential_injection should be in security_filters"
        );
        assert!(sec.contains(&"csrf"), "csrf should be in security_filters");
        assert!(
            sec.contains(&"forwarded_headers"),
            "forwarded_headers should be in security_filters"
        );
        assert!(sec.contains(&"guardrails"), "guardrails should be in security_filters");
        assert!(sec.contains(&"ip_acl"), "ip_acl should be in security_filters");
        assert!(
            sec.contains(&"peer_identity_trust"),
            "peer_identity_trust should be in security_filters"
        );
        assert!(sec.contains(&"rate_limit"), "rate_limit should be in security_filters");
        assert!(!sec.contains(&"router"), "router should not be in security_filters");
    }

    #[test]
    fn register_with_class_security() {
        let mut registry = FilterRegistry::with_builtins();
        let factory = FilterFactory::Http(std::sync::Arc::new(|_| Err("unused".into())));
        registry
            .register_with_class("my_auth", factory, SecurityClass::Security)
            .unwrap();
        assert!(
            registry.is_security_filter("my_auth"),
            "custom filter registered with Security class should be security"
        );
        assert!(
            registry.security_filters().contains(&"my_auth"),
            "custom Security filter should appear in security_filters()"
        );
    }

    #[test]
    fn register_with_class_standard() {
        let mut registry = FilterRegistry::with_builtins();
        let factory = FilterFactory::Http(std::sync::Arc::new(|_| Err("unused".into())));
        registry
            .register_with_class("my_logger", factory, SecurityClass::Standard)
            .unwrap();
        assert!(
            !registry.is_security_filter("my_logger"),
            "custom filter registered with Standard class should not be security"
        );
    }

    #[test]
    fn register_defaults_to_standard() {
        let mut registry = FilterRegistry::with_builtins();
        let factory = FilterFactory::Http(std::sync::Arc::new(|_| Err("unused".into())));
        registry.register("my_custom", factory).unwrap();
        assert!(
            !registry.is_security_filter("my_custom"),
            "register() should default to Standard security class"
        );
    }

    #[test]
    fn register_with_class_duplicate_errors() {
        let mut registry = FilterRegistry::with_builtins();
        let factory = FilterFactory::Http(std::sync::Arc::new(|_| Err("unused".into())));
        let err = registry
            .register_with_class("router", factory, SecurityClass::Security)
            .unwrap_err();
        assert!(
            err.to_string().contains("duplicate filter name: 'router'"),
            "register_with_class should reject duplicates: {err}"
        );
    }
}