praxis-proxy-core 0.5.5

Configuration, error types, and server factory 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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024 Praxis Contributors

//! Consolidated security override flags.
//!
//! All options default to `false` (secure by default). Each flag
//! demotes one specific security check from an error to a warning.

use serde::Deserialize;

// -----------------------------------------------------------------------------
// SkipPipelineChecks
// -----------------------------------------------------------------------------

/// Per-check flags for pipeline validation bypass.
///
/// Each flag skips one specific ordering check in the filter pipeline.
/// Prefer these granular flags over the blanket
/// [`InsecureOptions::skip_pipeline_validation`] flag.
///
/// ```
/// use praxis_core::config::SkipPipelineChecks;
///
/// let checks = SkipPipelineChecks::default();
/// assert!(!checks.any());
///
/// let all = SkipPipelineChecks::all();
/// assert!(all.conditional_security);
/// assert!(all.misaligned_clusters);
/// ```
#[expect(clippy::struct_excessive_bools, reason = "per-check skip flags")]
#[derive(Clone, Debug, Default, Deserialize, serde::Serialize)]
#[serde(default, deny_unknown_fields)]
pub struct SkipPipelineChecks {
    /// Skip check: security filters with request conditions (bypass risk).
    pub conditional_security: bool,

    /// Skip check: multiple cluster-selecting filters before load balancer.
    pub conflicting_cluster_selectors: bool,

    /// Skip check: duplicate `load_balancer` filters.
    pub duplicate_load_balancers: bool,

    /// Skip check: multiple path rewriting filters.
    pub duplicate_rewrite_filters: bool,

    /// Skip check: duplicate `router` filters.
    pub duplicate_routers: bool,

    /// Skip check: `load_balancer` without a preceding router.
    pub lb_without_router: bool,

    /// Skip check: cluster references not matching load balancer config.
    pub misaligned_clusters: bool,

    /// Skip check: unconditional `static_response` blocking subsequent
    /// filters.
    pub unreachable_filters: bool,
}

impl SkipPipelineChecks {
    /// Returns a [`SkipPipelineChecks`] with all flags set to `true`.
    ///
    /// ```
    /// use praxis_core::config::SkipPipelineChecks;
    ///
    /// let all = SkipPipelineChecks::all();
    /// assert!(all.any());
    /// assert!(all.lb_without_router);
    /// assert!(all.duplicate_routers);
    /// ```
    pub fn all() -> Self {
        Self {
            conditional_security: true,
            conflicting_cluster_selectors: true,
            duplicate_load_balancers: true,
            duplicate_rewrite_filters: true,
            duplicate_routers: true,
            lb_without_router: true,
            misaligned_clusters: true,
            unreachable_filters: true,
        }
    }

    /// Returns `true` if any check is skipped.
    ///
    /// ```
    /// use praxis_core::config::SkipPipelineChecks;
    ///
    /// assert!(!SkipPipelineChecks::default().any());
    ///
    /// let mut checks = SkipPipelineChecks::default();
    /// checks.duplicate_routers = true;
    /// assert!(checks.any());
    /// ```
    pub fn any(&self) -> bool {
        self.conditional_security
            || self.conflicting_cluster_selectors
            || self.duplicate_load_balancers
            || self.duplicate_rewrite_filters
            || self.duplicate_routers
            || self.lb_without_router
            || self.misaligned_clusters
            || self.unreachable_filters
    }
}

// -----------------------------------------------------------------------------
// InsecureOptions
// -----------------------------------------------------------------------------

/// Consolidated security overrides for Praxis.
///
/// Every field defaults to `false`. Setting a flag to `true`
/// demotes the corresponding security check from an error to a warning.
///
/// Only intended for use in development and testing.
///
/// ```
/// use praxis_core::config::InsecureOptions;
///
/// let opts = InsecureOptions::default();
/// assert!(!opts.allow_open_security_filters);
/// assert!(!opts.allow_private_endpoints);
/// assert!(!opts.allow_private_health_checks);
/// assert!(!opts.allow_private_upstreams);
/// assert!(!opts.allow_public_admin);
/// assert!(!opts.allow_root);
/// assert!(!opts.allow_tls_no_verify);
/// assert!(!opts.allow_tls_without_sni);
/// assert!(!opts.allow_unbounded_body);
/// assert!(!opts.csrf_log_only);
/// assert!(!opts.skip_pipeline_validation);
/// assert!(!opts.skip_pipeline_checks.any());
/// ```
///
/// ```
/// use praxis_core::config::InsecureOptions;
///
/// let opts: InsecureOptions =
///     serde_yaml::from_str("allow_root: true\nallow_public_admin: true\n").unwrap();
/// assert!(opts.allow_root);
/// assert!(opts.allow_public_admin);
/// assert!(!opts.allow_unbounded_body);
/// ```
#[expect(clippy::struct_excessive_bools, reason = "security override flags")]
#[derive(Clone, Debug, Default, Deserialize, serde::Serialize)]
#[serde(default, deny_unknown_fields)]
pub struct InsecureOptions {
    /// Allow security-critical filters to use `failure_mode: open`,
    /// demoting the validation error to a warning.
    pub allow_open_security_filters: bool,

    /// Allow cluster endpoints to resolve to loopback, link-local,
    /// or cloud metadata addresses.
    pub allow_private_endpoints: bool,

    /// Allow health checks to loopback/metadata addresses.
    pub allow_private_health_checks: bool,

    /// Allow upstream connections to resolve to private or reserved IP
    /// addresses at runtime. Without this flag, DNS-resolved upstream
    /// addresses in RFC 1918, loopback, link-local, CGNAT, and IPv6
    /// unique-local ranges are rejected to prevent DNS rebinding and
    /// SSRF attacks, on the TCP and HTTP data planes alike.
    ///
    /// Literal `host:port` upstreams are not affected: they cannot be
    /// substituted by a resolver, and are gated at config time by
    /// [`allow_private_endpoints`].
    ///
    /// [`allow_private_endpoints`]: InsecureOptions::allow_private_endpoints
    pub allow_private_upstreams: bool,

    /// Allow admin endpoint on non-loopback addresses (`0.0.0.0`, LAN IPs, etc.).
    pub allow_public_admin: bool,

    /// Allow running as root (UID 0).
    pub allow_root: bool,

    /// Allow disabling upstream TLS certificate verification (`tls.verify: false`).
    ///
    /// Without this flag, setting `verify: false` on a cluster is a hard
    /// validation error. Enabling it demotes the error to a warning.
    pub allow_tls_no_verify: bool,

    /// Allow TLS without SNI hostname verification.
    pub allow_tls_without_sni: bool,

    /// Allow startup without `body_limits.max_request_bytes` or
    /// `body_limits.max_response_bytes` configured. Without this
    /// flag, missing body limits are a hard validation error.
    pub allow_unbounded_body: bool,

    /// Run the CSRF filter in log-only mode: evaluate all rules
    /// but log violations as warnings instead of rejecting requests.
    pub csrf_log_only: bool,

    /// Granular pipeline validation bypass flags.
    ///
    /// Prefer these over the blanket [`skip_pipeline_validation`]
    /// flag for targeted overrides.
    ///
    /// [`skip_pipeline_validation`]: InsecureOptions::skip_pipeline_validation
    pub skip_pipeline_checks: SkipPipelineChecks,

    /// **Deprecated.** Skip ALL pipeline ordering validation checks.
    ///
    /// Prefer [`skip_pipeline_checks`] for granular control. When this
    /// flag is `true`, [`effective_pipeline_checks`] returns
    /// [`SkipPipelineChecks::all`], overriding individual flags.
    ///
    /// [`skip_pipeline_checks`]: InsecureOptions::skip_pipeline_checks
    /// [`effective_pipeline_checks`]: InsecureOptions::effective_pipeline_checks
    pub skip_pipeline_validation: bool,
}

impl InsecureOptions {
    /// Returns the effective pipeline check skip flags.
    ///
    /// When [`skip_pipeline_validation`] is `true`, all checks are
    /// skipped (backward compatibility). Otherwise, returns the
    /// granular [`skip_pipeline_checks`] flags.
    ///
    /// ```
    /// use praxis_core::config::InsecureOptions;
    ///
    /// let blanket: InsecureOptions = serde_yaml::from_str("skip_pipeline_validation: true").unwrap();
    /// let checks = blanket.effective_pipeline_checks();
    /// assert!(checks.lb_without_router);
    /// assert!(checks.conditional_security);
    ///
    /// let granular: InsecureOptions =
    ///     serde_yaml::from_str("skip_pipeline_checks:\n  duplicate_routers: true").unwrap();
    /// let checks = granular.effective_pipeline_checks();
    /// assert!(checks.duplicate_routers);
    /// assert!(!checks.lb_without_router);
    /// ```
    ///
    /// [`skip_pipeline_validation`]: InsecureOptions::skip_pipeline_validation
    /// [`skip_pipeline_checks`]: InsecureOptions::skip_pipeline_checks
    pub fn effective_pipeline_checks(&self) -> SkipPipelineChecks {
        if self.skip_pipeline_validation {
            SkipPipelineChecks::all()
        } else {
            self.skip_pipeline_checks.clone()
        }
    }
}

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

#[cfg(test)]
#[expect(clippy::allow_attributes, reason = "blanket test suppressions")]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::indexing_slicing,
    clippy::needless_raw_strings,
    clippy::needless_raw_string_hashes,
    clippy::too_many_lines,
    reason = "tests use unwrap/expect/indexing/raw strings for brevity"
)]
mod tests {
    use super::*;

    #[test]
    fn all_flags_default_to_false() {
        let opts = InsecureOptions::default();
        assert!(
            !opts.allow_open_security_filters,
            "allow_open_security_filters should default to false"
        );
        assert!(
            !opts.allow_private_endpoints,
            "allow_private_endpoints should default to false"
        );
        assert!(
            !opts.allow_private_health_checks,
            "allow_private_health_checks should default to false"
        );
        assert!(
            !opts.allow_private_upstreams,
            "allow_private_upstreams should default to false"
        );
        assert!(!opts.allow_public_admin, "allow_public_admin should default to false");
        assert!(!opts.allow_root, "allow_root should default to false");
        assert!(!opts.allow_tls_no_verify, "allow_tls_no_verify should default to false");
        assert!(
            !opts.allow_tls_without_sni,
            "allow_tls_without_sni should default to false"
        );
        assert!(
            !opts.allow_unbounded_body,
            "allow_unbounded_body should default to false"
        );
        assert!(!opts.csrf_log_only, "csrf_log_only should default to false");
        assert!(
            !opts.skip_pipeline_validation,
            "skip_pipeline_validation should default to false"
        );
        assert!(
            !opts.skip_pipeline_checks.any(),
            "skip_pipeline_checks should all default to false"
        );
    }

    #[test]
    fn deserializes_partial_overrides() {
        let yaml = "allow_root: true\nskip_pipeline_validation: true\n";
        let opts: InsecureOptions = serde_yaml::from_str(yaml).unwrap();
        assert!(opts.allow_root, "allow_root should be true");
        assert!(opts.skip_pipeline_validation, "skip_pipeline_validation should be true");
        assert!(!opts.allow_public_admin, "allow_public_admin should still be false");
    }

    #[test]
    fn deserializes_empty_to_defaults() {
        let opts: InsecureOptions = serde_yaml::from_str("{}").unwrap();
        assert!(!opts.allow_root, "empty YAML should produce defaults");
    }

    #[test]
    fn skip_pipeline_checks_all_sets_every_flag() {
        let checks = SkipPipelineChecks::all();
        assert!(checks.conditional_security, "conditional_security should be true");
        assert!(
            checks.conflicting_cluster_selectors,
            "conflicting_cluster_selectors should be true"
        );
        assert!(
            checks.duplicate_load_balancers,
            "duplicate_load_balancers should be true"
        );
        assert!(
            checks.duplicate_rewrite_filters,
            "duplicate_rewrite_filters should be true"
        );
        assert!(checks.duplicate_routers, "duplicate_routers should be true");
        assert!(checks.lb_without_router, "lb_without_router should be true");
        assert!(checks.misaligned_clusters, "misaligned_clusters should be true");
        assert!(checks.unreachable_filters, "unreachable_filters should be true");
    }

    #[test]
    fn skip_pipeline_checks_any_detects_single_flag() {
        let mut checks = SkipPipelineChecks::default();
        assert!(!checks.any(), "default checks should have no flags set");
        checks.duplicate_routers = true;
        assert!(checks.any(), "any() should detect single flag");
    }

    #[test]
    fn effective_pipeline_checks_blanket_overrides_granular() {
        let opts = InsecureOptions {
            skip_pipeline_validation: true,
            ..Default::default()
        };
        let checks = opts.effective_pipeline_checks();
        assert!(checks.lb_without_router, "blanket flag should set all checks");
        assert!(checks.conditional_security, "blanket flag should set all checks");
        assert!(checks.misaligned_clusters, "blanket flag should set all checks");
    }

    #[test]
    fn effective_pipeline_checks_uses_granular_when_blanket_off() {
        let opts = InsecureOptions {
            skip_pipeline_checks: SkipPipelineChecks {
                duplicate_routers: true,
                ..Default::default()
            },
            ..Default::default()
        };
        let checks = opts.effective_pipeline_checks();
        assert!(checks.duplicate_routers, "granular flag should be preserved");
        assert!(!checks.lb_without_router, "other flags should remain false");
    }

    #[test]
    fn deserializes_granular_pipeline_checks() {
        let yaml = "skip_pipeline_checks:\n  duplicate_routers: true\n  misaligned_clusters: true\n";
        let opts: InsecureOptions = serde_yaml::from_str(yaml).unwrap();
        assert!(
            opts.skip_pipeline_checks.duplicate_routers,
            "duplicate_routers should be true"
        );
        assert!(
            opts.skip_pipeline_checks.misaligned_clusters,
            "misaligned_clusters should be true"
        );
        assert!(
            !opts.skip_pipeline_checks.lb_without_router,
            "lb_without_router should remain false"
        );
        assert!(!opts.skip_pipeline_validation, "blanket flag should remain false");
    }

    #[test]
    fn rejects_unknown_skip_pipeline_checks_field() {
        let yaml = "skip_pipeline_checks:\n  nonexistent_check: true\n";
        let err = serde_yaml::from_str::<InsecureOptions>(yaml).unwrap_err();
        assert!(
            err.to_string().contains("nonexistent_check"),
            "unknown skip_pipeline_checks field should be rejected: {err}"
        );
    }
}