relay-core-api 0.8.3

[Internal] Shared data contracts for relay-core. Use `relay-core-runtime` instead.
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
use secrecy::{ExposeSecret, SecretString};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::path::PathBuf;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RedactionPolicy {
    #[serde(default = "default_false")]
    pub enabled: bool,
    #[serde(default = "default_sensitive_header_names")]
    pub sensitive_header_names: Vec<String>,
    #[serde(default = "default_sensitive_query_keys")]
    pub sensitive_query_keys: Vec<String>,
    #[serde(default = "default_false")]
    pub redact_bodies: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct RedactionPolicyPatch {
    #[serde(default)]
    pub enabled: Option<bool>,
    #[serde(default)]
    pub sensitive_header_names: Option<Vec<String>>,
    #[serde(default)]
    pub sensitive_query_keys: Option<Vec<String>>,
    #[serde(default)]
    pub redact_bodies: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct ProxyPolicyPatch {
    #[serde(default)]
    pub redaction: Option<RedactionPolicyPatch>,
    #[serde(default)]
    pub upstream: Option<UpstreamProxyConfig>,
}

// ── Upstream Proxy ──────────────────────────────────────

/// Upstream proxy configuration.
#[derive(Clone, Serialize, Deserialize)]
pub struct UpstreamProxyConfig {
    /// Upstream proxy URL, e.g. "http://corp-proxy:8080" or "https://secure-proxy:8443"
    pub proxy_url: String,
    /// Optional HTTP Basic authentication
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub auth: Option<UpstreamAuth>,
    /// Hosts to bypass upstream (CIDR with `cidr:` prefix, IP literals, or glob hostnames)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub bypass_hosts: Vec<String>,
    /// When true, unreachable upstream falls back to direct connection (default false = fail-closed)
    #[serde(default)]
    pub fail_open: bool,
}

/// HTTP Basic credentials for upstream proxy authentication.
#[derive(Clone, Serialize, Deserialize)]
pub struct UpstreamAuth {
    pub username: String,
    #[serde(
        serialize_with = "serialize_secret",
        deserialize_with = "deserialize_secret"
    )]
    pub password: SecretString,
}

fn serialize_secret<S: serde::Serializer>(_v: &SecretString, s: S) -> Result<S::Ok, S::Error> {
    s.serialize_str("***")
}
fn deserialize_secret<'de, D: serde::Deserializer<'de>>(d: D) -> Result<SecretString, D::Error> {
    let s = String::deserialize(d)?;
    Ok(SecretString::new(s.into()))
}

impl fmt::Debug for UpstreamProxyConfig {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("UpstreamProxyConfig")
            .field("proxy_url", &self.proxy_url)
            .field("auth", &self.auth.as_ref().map(|_| "***"))
            .field("bypass_hosts", &self.bypass_hosts)
            .field("fail_open", &self.fail_open)
            .finish()
    }
}

impl UpstreamAuth {
    pub fn new(username: String, password: String) -> Self {
        Self {
            username,
            password: SecretString::new(password.into()),
        }
    }
}

impl fmt::Debug for UpstreamAuth {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("UpstreamAuth")
            .field("username", &self.username)
            .field("password", &"***")
            .finish()
    }
}

impl PartialEq for UpstreamProxyConfig {
    fn eq(&self, other: &Self) -> bool {
        self.proxy_url == other.proxy_url
            && self.bypass_hosts == other.bypass_hosts
            && self.fail_open == other.fail_open
            && match (&self.auth, &other.auth) {
                (Some(a), Some(b)) => {
                    a.username == b.username
                        && a.password.expose_secret() == b.password.expose_secret()
                }
                (None, None) => true,
                _ => false,
            }
    }
}

impl Eq for UpstreamProxyConfig {}

impl Default for RedactionPolicy {
    fn default() -> Self {
        Self {
            enabled: false,
            sensitive_header_names: default_sensitive_header_names(),
            sensitive_query_keys: default_sensitive_query_keys(),
            redact_bodies: false,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProxyPolicy {
    /// In strict mode, invalid method/status does not silently rewrite to GET/200.
    #[serde(default = "default_true")]
    pub strict_http_semantics: bool,

    /// Allow fallback to GET for invalid methods (only if strict_http_semantics is false)
    #[serde(default = "default_false")]
    pub allow_fallback_method: bool,

    /// Allow fallback to 200 OK for invalid status (only if strict_http_semantics is false)
    #[serde(default = "default_false")]
    pub allow_fallback_status: bool,

    /// Enable automatic retries for idempotent requests
    #[serde(default = "default_false")]
    pub enable_retry: bool,

    /// Only retry idempotent methods (GET, HEAD, OPTIONS)
    #[serde(default = "default_true")]
    pub retry_idempotent_only: bool,

    /// Maximum number of retries
    #[serde(default = "default_max_retries")]
    pub max_retries: u8,

    /// Root directory for local file access (sandbox)
    pub sandbox_root: Option<PathBuf>,

    /// Maximum allowed size for local file read
    #[serde(default = "default_max_file_bytes")]
    pub max_local_file_bytes: usize,

    /// Maximum allowed request/response body size for proxy inspection
    #[serde(default = "default_max_body_bytes")]
    pub max_body_size: usize,

    /// P1: Maximum bytes to buffer for rule body inspection.
    /// When exceeded, body-stage rules are skipped with budget_exceeded tag.
    /// Default: 1 MB. Set to 0 to disable rule body inspection entirely.
    #[serde(default = "default_rule_body_inspect_budget")]
    pub rule_body_inspect_budget: usize,

    /// Request timeout in milliseconds (connect + send request + receive response headers)
    #[serde(default = "default_request_timeout_ms")]
    pub request_timeout_ms: u64,

    /// Enable transparent proxy mode
    #[serde(default = "default_false")]
    pub transparent_enabled: bool,

    /// Require original destination to be present (strict mode)
    #[serde(default = "default_true")]
    pub transparent_require_original_dst: bool,

    /// Allow fallback to Host header when original destination is missing
    #[serde(default = "default_false")]
    pub transparent_allow_host_fallback: bool,

    /// Reject connections that would create a loop
    #[serde(default = "default_true")]
    pub transparent_reject_loopback_target: bool,

    /// Log level for transparent proxy events
    #[serde(default = "default_transparent_log_level")]
    pub transparent_log_level: TransparentLogLevel,

    /// QUIC handling mode
    #[serde(default = "default_quic_mode")]
    pub quic_mode: QuicMode,

    /// Optionally emit Clear-Site-Data: "cache" to invalidate client Alt-Svc cache
    #[serde(default = "default_false")]
    pub quic_downgrade_clear_cache: bool,

    #[serde(default)]
    pub redaction: RedactionPolicy,

    /// Upstream (parent) proxy configuration. None = direct connection mode.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub upstream: Option<UpstreamProxyConfig>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum TransparentLogLevel {
    Silent, // No logging
    Info,   // Log connections only
    Debug,  // Log with destination details
    Trace,  // Full packet-level logging (expensive)
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum QuicMode {
    /// Force clients to use HTTP/1.1 or HTTP/2
    Downgrade,

    /// Pass through QUIC traffic without inspection
    Passthrough,

    /// [EXPERIMENTAL] Full HTTP/3 MITM
    #[cfg(feature = "quic_mitm_experimental")]
    ExperimentalMitm,
}

fn default_quic_mode() -> QuicMode {
    QuicMode::Downgrade
}

impl Default for ProxyPolicy {
    fn default() -> Self {
        Self {
            strict_http_semantics: true,
            allow_fallback_method: false,
            allow_fallback_status: false,
            enable_retry: false,
            retry_idempotent_only: true,
            max_retries: 3,
            sandbox_root: None,
            max_local_file_bytes: 10 * 1024 * 1024, // 10MB
            max_body_size: 10 * 1024 * 1024,        // 10MB
            rule_body_inspect_budget: 1024 * 1024,  // 1MB
            request_timeout_ms: 30_000,             // 30 seconds
            transparent_enabled: false,
            transparent_require_original_dst: true,
            transparent_allow_host_fallback: false,
            transparent_reject_loopback_target: true,
            transparent_log_level: TransparentLogLevel::Info,
            quic_mode: QuicMode::Downgrade,
            quic_downgrade_clear_cache: false,
            redaction: RedactionPolicy::default(),
            upstream: None,
        }
    }
}

impl RedactionPolicy {
    pub fn apply_patch(&mut self, patch: RedactionPolicyPatch) {
        if let Some(enabled) = patch.enabled {
            self.enabled = enabled;
        }
        if let Some(names) = patch.sensitive_header_names {
            self.sensitive_header_names = names;
        }
        if let Some(keys) = patch.sensitive_query_keys {
            self.sensitive_query_keys = keys;
        }
        if let Some(redact_bodies) = patch.redact_bodies {
            self.redact_bodies = redact_bodies;
        }
    }
}

impl ProxyPolicy {
    pub fn apply_patch(&mut self, patch: ProxyPolicyPatch) {
        if let Some(redaction_patch) = patch.redaction {
            self.redaction.apply_patch(redaction_patch);
        }
        if let Some(upstream) = patch.upstream {
            self.upstream = Some(upstream);
        }
    }
}

fn default_true() -> bool {
    true
}
fn default_false() -> bool {
    false
}
fn default_max_retries() -> u8 {
    3
}
fn default_max_file_bytes() -> usize {
    10 * 1024 * 1024
}
fn default_max_body_bytes() -> usize {
    10 * 1024 * 1024
}
fn default_rule_body_inspect_budget() -> usize {
    1024 * 1024 // 1MB
}
fn default_request_timeout_ms() -> u64 {
    30_000
}
fn default_transparent_log_level() -> TransparentLogLevel {
    TransparentLogLevel::Info
}
fn default_sensitive_header_names() -> Vec<String> {
    vec![
        "authorization".to_string(),
        "proxy-authorization".to_string(),
        "cookie".to_string(),
        "set-cookie".to_string(),
        "x-api-key".to_string(),
        "x-auth-token".to_string(),
    ]
}
fn default_sensitive_query_keys() -> Vec<String> {
    vec![
        "token".to_string(),
        "access_token".to_string(),
        "refresh_token".to_string(),
        "api_key".to_string(),
        "apikey".to_string(),
        "password".to_string(),
        "secret".to_string(),
    ]
}

#[cfg(test)]
mod tests {
    use super::*;
    use secrecy::SecretString;

    #[test]
    fn upstream_proxy_config_debug_masks_password() {
        let cfg = UpstreamProxyConfig {
            proxy_url: "http://proxy:8080".to_string(),
            auth: Some(UpstreamAuth {
                username: "user".to_string(),
                password: SecretString::new("s3cret".to_string().into()),
            }),
            bypass_hosts: vec!["*.local".to_string()],
            fail_open: false,
        };
        let dbg = format!("{:?}", cfg);
        assert!(dbg.contains("http://proxy:8080"));
        assert!(dbg.contains("***"));
        assert!(!dbg.contains("s3cret"));
    }

    #[test]
    fn upstream_auth_debug_masks_password() {
        let auth = UpstreamAuth {
            username: "user".to_string(),
            password: SecretString::new("s3cret".to_string().into()),
        };
        let dbg = format!("{:?}", auth);
        assert!(dbg.contains("user"));
        assert!(dbg.contains("***"));
        assert!(!dbg.contains("s3cret"));
    }

    #[test]
    fn proxy_policy_default_has_no_upstream() {
        let policy = ProxyPolicy::default();
        assert!(policy.upstream.is_none());
    }

    #[test]
    fn proxy_policy_patch_applies_upstream() {
        let mut policy = ProxyPolicy::default();
        let upstream = UpstreamProxyConfig {
            proxy_url: "http://corp:8080".to_string(),
            auth: None,
            bypass_hosts: vec![],
            fail_open: false,
        };
        policy.apply_patch(ProxyPolicyPatch {
            redaction: None,
            upstream: Some(upstream.clone()),
        });
        assert_eq!(policy.upstream, Some(upstream));
    }

    #[test]
    fn upstream_proxy_config_serde_masks_password() {
        let cfg = UpstreamProxyConfig {
            proxy_url: "https://secure-proxy:8443".to_string(),
            auth: Some(UpstreamAuth {
                username: "admin".to_string(),
                password: SecretString::new("p@ss".to_string().into()),
            }),
            bypass_hosts: vec!["cidr:10.0.0.0/8".to_string(), "*.internal".to_string()],
            fail_open: true,
        };
        let json = serde_json::to_string(&cfg).unwrap();
        // Serialized JSON must NOT contain the real password.
        assert!(!json.contains("p@ss"));
        assert!(json.contains("***"));
        // Non-password fields round-trip correctly.
        let decoded: UpstreamProxyConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.proxy_url, cfg.proxy_url);
        assert_eq!(decoded.bypass_hosts, cfg.bypass_hosts);
        assert_eq!(decoded.fail_open, cfg.fail_open);
    }

    #[test]
    fn proxy_policy_serialization_hides_upstream_when_none() {
        let policy = ProxyPolicy::default();
        let json = serde_json::to_string(&policy).unwrap();
        assert!(!json.contains("upstream"));
    }

    #[test]
    fn upstream_partial_eq_compares_password() {
        let a = UpstreamProxyConfig {
            proxy_url: "http://p:8080".to_string(),
            auth: Some(UpstreamAuth {
                username: "u".to_string(),
                password: SecretString::new("a".to_string().into()),
            }),
            bypass_hosts: vec![],
            fail_open: false,
        };
        let b = UpstreamProxyConfig {
            proxy_url: "http://p:8080".to_string(),
            auth: Some(UpstreamAuth {
                username: "u".to_string(),
                password: SecretString::new("b".to_string().into()),
            }),
            bypass_hosts: vec![],
            fail_open: false,
        };
        assert_ne!(a, b);
    }
}