praxis-proxy-filter 0.7.2

Filter pipeline engine and built-in filters for Praxis
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024 Praxis Contributors

//! Pipeline construction and ordering diagnostics.
//!
//! Provides [`FilterPipeline::build`] and [`FilterPipeline::build_with_chains`],
//! which instantiate filters from [`FilterEntry`] slices via the
//! [`FilterRegistry`], compute [`BodyCapabilities`], and run ordering
//! validation checks. Branch-aware builds delegate to
//! [`build_branch::resolve_chain_filters`] for recursive resolution.
//!
//! [`FilterEntry`]: praxis_core::config::FilterEntry
//! [`FilterRegistry`]: crate::registry::FilterRegistry
//! [`BodyCapabilities`]: crate::body::BodyCapabilities
//! [`build_branch::resolve_chain_filters`]: super::build_branch::resolve_chain_filters

use std::{collections::HashMap, mem, sync::Arc};

use praxis_core::{
    config::{FilterEntry, InsecureOptions, SkipPipelineChecks},
    id::IdGenerator,
    time::SystemTimeSource,
};
use tracing::debug;

#[cfg(feature = "upstream-binding")]
use super::catalog::ClusterApplicationCatalog;
use super::{
    FilterPipeline,
    body::{body_filter_indices, compute_body_capabilities, selected_upstream_request_body_indices},
    filter::PipelineFilter,
};
use crate::{FilterError, any_filter::AnyFilter, registry::FilterRegistry};

// -----------------------------------------------------------------------------
// FilterPipeline Factory
// -----------------------------------------------------------------------------

impl FilterPipeline {
    /// Build a pipeline by instantiating each filter entry via the registry.
    ///
    /// Moves conditions out of each entry, so after this call every entry's
    /// condition vecs are empty.
    ///
    /// # Errors
    ///
    /// Returns [`FilterError`] if any filter fails to instantiate.
    pub fn build(entries: &mut [FilterEntry], registry: &FilterRegistry) -> Result<Self, FilterError> {
        let mut filters = Vec::with_capacity(entries.len());
        for (filter_id, entry) in entries.iter_mut().enumerate() {
            let filter = registry.create(&entry.filter_type, &entry.config)?;
            reject_tcp_unsupported_fields(&filter, entry)?;
            let has_conditions = !entry.conditions.is_empty() || !entry.response_conditions.is_empty();
            debug!(
                filter = filter.name(),
                conditions = has_conditions,
                "filter added to pipeline"
            );
            let mut pf = PipelineFilter::new(
                filter_id,
                filter,
                mem::take(&mut entry.conditions),
                mem::take(&mut entry.response_conditions),
            );
            pf.failure_mode = entry.failure_mode;
            pf.is_security = registry.is_security_filter(&entry.filter_type);
            pf.name = entry.name.as_ref().map(|n| Arc::from(n.as_str()));
            filters.push(pf);
        }
        Ok(Self::from_filters(filters))
    }

    /// Build a pipeline with branch chain resolution.
    ///
    /// Like [`build`], but also resolves `branch_chains` on each
    /// filter entry into runtime `ResolvedBranch` types using
    /// the provided chain lookup table.
    ///
    /// The `chains` parameter is the **top-level** chain lookup
    /// table (all `filter_chains` from the config), used to
    /// resolve `ChainRef::Named` entries inside branch
    /// configurations. The actual filters for this pipeline come
    /// from `entries`.
    ///
    /// `insecure_options` is the operator's declared security posture. It is
    /// threaded into outbound chain binding so that inline clusters reachable
    /// only through a chain-binding filter's outbound chain are gated by the
    /// same SSRF and insecure-TLS rules as top-level `clusters:`, instead of
    /// silently bypassing them.
    ///
    /// # Errors
    ///
    /// Returns [`FilterError`] if any filter fails to instantiate
    /// or any branch chain reference is unresolvable.
    ///
    /// [`build`]: FilterPipeline::build
    pub fn build_with_chains(
        entries: &mut [FilterEntry],
        registry: &FilterRegistry,
        chains: &HashMap<&str, &[FilterEntry]>,
        insecure_options: &InsecureOptions,
    ) -> Result<Self, FilterError> {
        let filters = super::build_branch::resolve_chain_filters(entries, registry, chains, 0, insecure_options)?;
        Ok(Self::from_filters(filters))
    }

    /// Create a pipeline from an already-resolved filter list.
    #[expect(
        clippy::too_many_lines,
        reason = "single construction choke point: one precompute per body phase plus the full struct literal"
    )]
    pub(crate) fn from_filters(
        #[cfg_attr(
            not(feature = "upstream-binding"),
            expect(unused_mut, reason = "only binding enablement mutates the filters")
        )]
        mut filters: Vec<PipelineFilter>,
    ) -> Self {
        #[cfg(feature = "upstream-binding")]
        if super::checks::uses_bound_upstream(&filters) {
            let catalog = build_cluster_application_catalog(&filters);
            enable_upstream_binding(&mut filters, &catalog);
        }
        let body_capabilities = compute_body_capabilities(&filters);
        let compression = extract_compression_config(&filters);
        let may_select_streaming_subrequest_response = filters_may_select_streaming_subrequest_response(&filters);
        let trace_context_filter_indices = filters
            .iter()
            .enumerate()
            .filter_map(|(idx, pf)| (pf.filter.name() == "trace_context").then_some(idx))
            .collect();
        let (request_body_filter_indices, response_body_filter_indices) = body_filter_indices(&filters);
        let selected_upstream_request_body_filter_indices = selected_upstream_request_body_indices(&filters);
        #[cfg(feature = "bound-upstream-request-body")]
        let bound_upstream_request_body_filter_indices = super::body::bound_upstream_request_body_indices(&filters);
        let response_trailer_filter_indices = super::body::response_trailer_filter_indices(&filters);
        let id_generator = Arc::new(IdGenerator::new());
        let time_source: Arc<dyn praxis_core::time::TimeSource> = Arc::new(SystemTimeSource);
        let mut pipeline = Self {
            body_capabilities,
            compression,
            filters,
            request_body_filter_indices,
            response_body_filter_indices,
            selected_upstream_request_body_filter_indices,
            #[cfg(feature = "bound-upstream-request-body")]
            bound_upstream_request_body_filter_indices,
            allow_private_upstreams: false,
            response_trailer_filter_indices,
            health_registry: None,
            id_generator: Arc::clone(&id_generator),
            kv_stores: None,
            session_stores: None,
            pipeline_extensions: Vec::new(),
            record_filter_duration_metrics: false,
            route_templates: Arc::default(),
            subrequest_client: None,
            may_select_streaming_subrequest_response,
            trace_context_filter_indices,
            time_source: Arc::clone(&time_source),
            request_body_ceiling: None,
            response_body_ceiling: None,
        };
        pipeline.set_id_generator(id_generator);
        pipeline.set_time_source(time_source);
        pipeline
    }

    /// Validate the pipeline for structural misconfigurations that
    /// would cause runtime failures (502s, unreachable filters,
    /// cluster mismatches).
    ///
    /// Individual checks can be skipped via [`SkipPipelineChecks`]
    /// flags. Use [`SkipPipelineChecks::default()`] to run all checks.
    ///
    /// ```
    /// use praxis_core::config::SkipPipelineChecks;
    /// use praxis_filter::{FailureMode, FilterEntry, FilterPipeline, FilterRegistry};
    ///
    /// let registry = FilterRegistry::with_builtins();
    /// let mut entries = vec![FilterEntry {
    ///     branch_chains: None,
    ///     filter_type: "load_balancer".into(),
    ///     config: serde_yaml::from_str("clusters:\n  - name: web\n    endpoints: [\"10.0.0.1:80\"]")
    ///         .unwrap(),
    ///     conditions: vec![],
    ///     name: None,
    ///     response_conditions: vec![],
    ///     failure_mode: FailureMode::default(),
    /// }];
    /// let pipeline = FilterPipeline::build(&mut entries, &registry).unwrap();
    /// let no_skip = SkipPipelineChecks::default();
    /// let errors = pipeline.ordering_errors(&entries, false, &no_skip);
    /// assert!(
    ///     errors
    ///         .iter()
    ///         .any(|e| e.contains("without a preceding router"))
    /// );
    /// ```
    ///
    /// [`build`]: FilterPipeline::build
    /// [`SkipPipelineChecks`]: praxis_core::config::SkipPipelineChecks
    pub fn ordering_errors(
        &self,
        entries: &[FilterEntry],
        allow_open_security: bool,
        skip: &SkipPipelineChecks,
    ) -> Vec<String> {
        // A top-level pipeline starts with nothing bound.
        self.ordering_errors_inner(entries, allow_open_security, skip, false)
    }

    /// The binding checks, run after the ordinary ordering checks.
    ///
    /// `in_irr_step` is `true` for an IRR step continuation, which inherits its
    /// parent's binding and may not publish its own.
    #[cfg(feature = "upstream-binding")]
    fn binding_errors(&self, in_irr_step: bool, names: &[&str], errors: &mut Vec<String>) {
        let uses_bound_upstream = super::checks::uses_bound_upstream(&self.filters);
        if uses_bound_upstream {
            super::checks::check_cluster_metadata_conflicts(&self.filters, errors);
        }
        super::checks::check_bound_upstream_requires_binding(&self.filters, in_irr_step, errors);
        super::checks::check_bound_condition_with_pre_read_body(
            &self.filters,
            self.body_capabilities.request_body_mode,
            errors,
        );
        #[cfg(feature = "bound-upstream-request-body")]
        super::checks::check_bound_upstream_body_participants(&self.filters, in_irr_step, errors);
        if uses_bound_upstream {
            super::checks::check_no_rebind_after_binding(&self.filters, in_irr_step, errors);
        }
        super::checks::check_bound_cluster_coverage(&self.filters, errors);
        super::checks::check_untagged_bound_cluster_fields(&self.filters, errors);
        super::checks::check_irr_coexistence(&self.filters, names, errors);
    }

    /// Validate an `iterative_request_router` step pipeline, which runs as a
    /// continuation of a parent that already guarantees a logical binding before
    /// the IRR (the parent's own bound-upstream reachability check enforces
    /// this). The step's bound consumers are therefore validated with a binding
    /// assumed present on entry.
    #[cfg(feature = "iterative-request-router")]
    pub(crate) fn step_ordering_errors(
        &self,
        entries: &[FilterEntry],
        allow_open_security: bool,
        skip: &SkipPipelineChecks,
    ) -> Vec<String> {
        self.ordering_errors_inner(entries, allow_open_security, skip, true)
    }

    /// Shared body of [`Self::ordering_errors`] and the feature-gated
    /// `step_ordering_errors` (the IRR-step variant).
    ///
    /// `in_irr_step` is `true` for an IRR step continuation, which inherits its
    /// parent's binding and may not publish its own.
    #[expect(
        clippy::too_many_lines,
        reason = "one sequential invocation per ordering check, including the bound-upstream passes"
    )]
    fn ordering_errors_inner(
        &self,
        entries: &[FilterEntry],
        allow_open_security: bool,
        skip: &SkipPipelineChecks,
        in_irr_step: bool,
    ) -> Vec<String> {
        let names: Vec<&str> = self.filters.iter().map(|pf| pf.filter.name()).collect();

        let mut errors = Vec::new();

        if !skip.lb_without_router {
            super::checks::check_lb_without_cluster_selector(&self.filters, &mut errors);
        }
        if !skip.unreachable_filters {
            super::checks::check_unconditional_static_response(&names, &self.filters, &mut errors);
        }
        if !skip.conditional_security {
            super::checks::check_conditional_security(&names, &self.filters, &mut errors);
        }
        super::checks::check_open_security_filters(&names, &self.filters, allow_open_security, &mut errors);
        if !skip.duplicate_routers {
            super::checks::check_duplicate_routers(&names, &mut errors);
        }
        if !skip.duplicate_load_balancers {
            super::checks::check_duplicate_load_balancers(&names, &mut errors);
        }
        if !skip.conflicting_cluster_selectors {
            super::checks::check_conflicting_cluster_selectors(&self.filters, &mut errors);
        }
        if !skip.misaligned_clusters {
            super::checks::check_misaligned_clusters(&self.filters, &mut errors);
        }
        if !skip.duplicate_rewrite_filters {
            super::checks::check_duplicate_rewrite_filters(&names, entries, &mut errors);
        }
        super::checks::check_condition_header_names(&self.filters, &mut errors);
        super::checks::check_trace_context_upstream_conditions(&self.filters, &mut errors);
        super::checks::check_skip_to_bypasses_security(&self.filters, &mut errors);
        super::checks::check_terminal_rejoin_bypasses_security(&self.filters, &mut errors);
        super::checks::check_branch_body_filters(&self.filters, &mut errors);
        super::checks::check_branch_selected_upstream_body_filters(&self.filters, &mut errors);
        super::checks::check_selected_upstream_body_mode(&self.filters, &mut errors);
        #[cfg(feature = "upstream-binding")]
        self.binding_errors(in_irr_step, &names, &mut errors);
        #[cfg(not(feature = "upstream-binding"))]
        let _ = in_irr_step;
        super::checks::check_selected_upstream_condition_ordering(&self.filters, &mut errors);
        super::checks::check_selected_upstream_condition_pre_read(
            &self.filters,
            self.body_capabilities.request_body_mode,
            &mut errors,
        );
        if self.may_select_streaming_subrequest_response
            && matches!(
                self.body_capabilities.response_body_mode,
                crate::BodyMode::StreamBuffer { .. }
            )
        {
            errors.push(
                "pipeline contains a filter that may select a streaming sub-request response, \
                 but its response body mode is StreamBuffer"
                    .to_owned(),
            );
        }

        errors
    }

    /// Check for non-fatal ordering advisories.
    ///
    /// Currently detects: a router with no load balancer, all routers
    /// conditional with no fallback, and security filters reachable only
    /// through a conditional branch.
    ///
    /// ```
    /// use praxis_filter::{FailureMode, FilterEntry, FilterPipeline, FilterRegistry};
    ///
    /// let registry = FilterRegistry::with_builtins();
    /// let mut entries = vec![FilterEntry {
    ///     branch_chains: None,
    ///     filter_type: "router".into(),
    ///     config: serde_yaml::from_str("routes:\n  - path_prefix: \"/\"\n    cluster: web").unwrap(),
    ///     conditions: vec![praxis_core::config::Condition::When(
    ///         praxis_core::config::ConditionMatch {
    ///             grpc: None,
    ///             path: None,
    ///             path_prefix: Some("/api".to_owned()),
    ///             methods: None,
    ///             headers: None,
    ///             bound_upstream: None,
    ///             selected_upstream: None,
    ///         },
    ///     )],
    ///     name: None,
    ///     response_conditions: vec![],
    ///     failure_mode: FailureMode::default(),
    /// }];
    /// let pipeline = FilterPipeline::build(&mut entries, &registry).unwrap();
    /// let warnings = pipeline.ordering_warnings();
    /// assert!(
    ///     warnings
    ///         .iter()
    ///         .any(|w| w.contains("all router filters are conditional"))
    /// );
    /// ```
    pub fn ordering_warnings(&self) -> Vec<String> {
        let names: Vec<&str> = self.filters.iter().map(|pf| pf.filter.name()).collect();

        let mut warnings = Vec::new();

        super::checks::check_router_without_lb(&self.filters, &names, &mut warnings);
        super::checks::check_all_routers_conditional(&names, &self.filters, &mut warnings);
        super::checks::check_security_filter_in_conditional_branch(&self.filters, &mut warnings);

        warnings
    }
}

/// Enable logical binding only on routers in a pipeline that actually uses it,
/// handing each the pipeline's cluster catalog.
#[cfg(feature = "upstream-binding")]
fn enable_upstream_binding(filters: &mut [PipelineFilter], catalog: &Arc<ClusterApplicationCatalog>) {
    for pf in filters {
        if let AnyFilter::Http(filter) = &mut pf.filter {
            filter.enable_upstream_binding(Arc::clone(catalog));
        }
        for branch in &mut pf.branches {
            enable_upstream_binding(&mut branch.filters, catalog);
        }
    }
}

// -----------------------------------------------------------------------------
// Utility Functions
// -----------------------------------------------------------------------------

/// Reject request conditions, response conditions or branch chains on a TCP
/// filter: only HTTP filters evaluate them, so a TCP filter would run
/// unconditionally and never branch.
pub(super) fn reject_tcp_unsupported_fields(filter: &AnyFilter, entry: &FilterEntry) -> Result<(), FilterError> {
    if !matches!(filter, AnyFilter::Tcp(_)) {
        return Ok(());
    }
    if !entry.conditions.is_empty() || !entry.response_conditions.is_empty() {
        return Err(format!(
            "filter '{}': conditions are not supported on TCP filters; they apply to HTTP filters only",
            filter.name()
        )
        .into());
    }
    if entry.branch_chains.is_some() {
        return Err(format!(
            "filter '{}': branch_chains are not supported on TCP filters; they apply to HTTP filters only",
            filter.name()
        )
        .into());
    }
    Ok(())
}

/// Build the metadata-only cluster catalog for a binding-enabled pipeline from
/// every reachable cluster declaration (top-level filters and branch
/// sub-chains).
///
/// Ordinary routing resolves metadata from the load balancer that owns the
/// selected endpoint, so it neither needs this global catalog nor requires
/// declarations in independent dispatch paths to agree. Conflicting
/// declarations are ignored here and surfaced by
/// [`check_cluster_metadata_conflicts`], which blocks a conflicting pipeline
/// from serving traffic.
///
/// [`check_cluster_metadata_conflicts`]: super::checks::check_cluster_metadata_conflicts
#[cfg(feature = "upstream-binding")]
fn build_cluster_application_catalog(filters: &[PipelineFilter]) -> Arc<ClusterApplicationCatalog> {
    let (catalog, _conflicts) = super::catalog::build_catalog(super::collect_cluster_declarations(filters));
    Arc::new(catalog)
}

/// Scan the filter list for a compression filter and extract its config.
fn extract_compression_config(
    filters: &[PipelineFilter],
) -> Option<crate::builtins::http::payload_processing::compression_config::CompressionConfig> {
    filters.iter().find_map(|pf| match &pf.filter {
        AnyFilter::Http(f) => f.compression_config().cloned(),
        AnyFilter::Tcp(_) => None,
    })
}

/// Recursively detect filters that may select streaming sub-request delivery.
fn filters_may_select_streaming_subrequest_response(filters: &[PipelineFilter]) -> bool {
    filters.iter().any(|pf| {
        let selects_streaming = match &pf.filter {
            AnyFilter::Http(filter) => filter.may_select_streaming_subrequest_response(),
            AnyFilter::Tcp(_) => false,
        };
        selects_streaming
            || pf
                .branches
                .iter()
                .any(|branch| filters_may_select_streaming_subrequest_response(&branch.filters))
    })
}