gaze-proxy 0.14.0

Feature-gated multi-provider HTTP proxy for Gaze PII pseudonymization
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
use std::fmt;
use std::time::Duration;

use http::Method;
use serde_json::Value;
use url::Url;

use crate::codec::{BodyCodec, CodecLimits, WireFormat};

/// Transport protocol selected by a reviewed adapter contract.
#[derive(Clone, Copy)]
pub enum ProtocolContract<'a> {
    /// Preserve the pre-codec JSON/SSE surface traversal path.
    LegacySurfaces,
    /// Transform complete bodies through the borrowed codec.
    Codec(&'a dyn BodyCodec),
}

impl ProtocolContract<'_> {
    /// Returns the selected codec, if this is a codec-backed contract.
    #[must_use]
    pub const fn codec(&self) -> Option<&dyn BodyCodec> {
        match self {
            Self::LegacySurfaces => None,
            Self::Codec(codec) => Some(*codec),
        }
    }

    /// Reports whether this contract preserves the legacy traversal path.
    #[must_use]
    pub const fn is_legacy(&self) -> bool {
        matches!(self, Self::LegacySurfaces)
    }
}

impl fmt::Debug for ProtocolContract<'_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::LegacySurfaces => formatter.write_str("LegacySurfaces"),
            Self::Codec(_) => formatter.debug_tuple("Codec").field(&"BodyCodec").finish(),
        }
    }
}

/// Session lifecycle required by an adapter protocol.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SessionPolicy {
    /// A request owns an isolated session that is discarded after its response.
    EphemeralSingleRequest,
    /// An opaque header selects a bounded continuity registry.
    OpaqueHeaderContinuity(SessionRegistryConfig),
}

/// Closed validation failure for [`SessionRegistryConfig`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SessionRegistryConfigError {
    InvalidMaxActiveSessions,
    InvalidSessionTtl,
    InvalidMaxTombstones,
    InvalidMaxTombstoneBytes,
    InvalidTombstoneTtl,
    NotALowering,
}

impl fmt::Display for SessionRegistryConfigError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(match self {
            Self::InvalidMaxActiveSessions => "invalid maximum active session count",
            Self::InvalidSessionTtl => "invalid session TTL",
            Self::InvalidMaxTombstones => "invalid maximum tombstone count",
            Self::InvalidMaxTombstoneBytes => "invalid maximum tombstone byte count",
            Self::InvalidTombstoneTtl => "invalid tombstone TTL",
            Self::NotALowering => "registry limit may only be lowered",
        })
    }
}

impl std::error::Error for SessionRegistryConfigError {}

/// Finite, validated bounds for an opaque-session continuity registry.
///
/// Fields are deliberately private so callers cannot bypass validation.
///
/// ```compile_fail
/// use gaze_proxy::SessionRegistryConfig;
///
/// let _ = SessionRegistryConfig { max_active_sessions: 1 };
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SessionRegistryConfig {
    max_active_sessions: usize,
    session_ttl: Duration,
    max_tombstones: usize,
    max_tombstone_bytes: usize,
    tombstone_ttl: Duration,
}

impl Default for SessionRegistryConfig {
    fn default() -> Self {
        Self {
            max_active_sessions: 4_096,
            session_ttl: Duration::from_secs(30 * 60),
            max_tombstones: 4_096,
            max_tombstone_bytes: 1024 * 1024,
            tombstone_ttl: Duration::from_secs(30 * 60),
        }
    }
}

impl SessionRegistryConfig {
    pub const HARD_MAX_ACTIVE_SESSIONS: usize = 65_536;
    pub const HARD_MAX_SESSION_TTL: Duration = Duration::from_secs(24 * 60 * 60);
    pub const HARD_MAX_TOMBSTONES: usize = 65_536;
    pub const HARD_MAX_TOMBSTONE_BYTES: usize = 64 * 1024 * 1024;
    pub const HARD_MAX_TOMBSTONE_TTL: Duration = Duration::from_secs(24 * 60 * 60);

    /// Constructs a registry configuration after validating every finite bound.
    pub fn try_new(
        max_active_sessions: usize,
        session_ttl: Duration,
        max_tombstones: usize,
        max_tombstone_bytes: usize,
        tombstone_ttl: Duration,
    ) -> Result<Self, SessionRegistryConfigError> {
        if max_active_sessions == 0 || max_active_sessions > Self::HARD_MAX_ACTIVE_SESSIONS {
            return Err(SessionRegistryConfigError::InvalidMaxActiveSessions);
        }
        if session_ttl.is_zero() || session_ttl > Self::HARD_MAX_SESSION_TTL {
            return Err(SessionRegistryConfigError::InvalidSessionTtl);
        }
        if max_tombstones == 0 || max_tombstones > Self::HARD_MAX_TOMBSTONES {
            return Err(SessionRegistryConfigError::InvalidMaxTombstones);
        }
        if max_tombstone_bytes == 0 || max_tombstone_bytes > Self::HARD_MAX_TOMBSTONE_BYTES {
            return Err(SessionRegistryConfigError::InvalidMaxTombstoneBytes);
        }
        if tombstone_ttl.is_zero() || tombstone_ttl > Self::HARD_MAX_TOMBSTONE_TTL {
            return Err(SessionRegistryConfigError::InvalidTombstoneTtl);
        }
        Ok(Self {
            max_active_sessions,
            session_ttl,
            max_tombstones,
            max_tombstone_bytes,
            tombstone_ttl,
        })
    }

    #[must_use]
    pub const fn max_active_sessions(&self) -> usize {
        self.max_active_sessions
    }

    #[must_use]
    pub const fn session_ttl(&self) -> Duration {
        self.session_ttl
    }

    #[must_use]
    pub const fn max_tombstones(&self) -> usize {
        self.max_tombstones
    }

    #[must_use]
    pub const fn max_tombstone_bytes(&self) -> usize {
        self.max_tombstone_bytes
    }

    #[must_use]
    pub const fn tombstone_ttl(&self) -> Duration {
        self.tombstone_ttl
    }

    pub fn try_with_max_active_sessions(
        mut self,
        value: usize,
    ) -> Result<Self, SessionRegistryConfigError> {
        if value == 0 || value > Self::HARD_MAX_ACTIVE_SESSIONS {
            return Err(SessionRegistryConfigError::InvalidMaxActiveSessions);
        }
        if value > self.max_active_sessions {
            return Err(SessionRegistryConfigError::NotALowering);
        }
        self.max_active_sessions = value;
        Ok(self)
    }

    pub fn try_with_session_ttl(
        mut self,
        value: Duration,
    ) -> Result<Self, SessionRegistryConfigError> {
        if value.is_zero() || value > Self::HARD_MAX_SESSION_TTL {
            return Err(SessionRegistryConfigError::InvalidSessionTtl);
        }
        if value > self.session_ttl {
            return Err(SessionRegistryConfigError::NotALowering);
        }
        self.session_ttl = value;
        Ok(self)
    }

    pub fn try_with_max_tombstones(
        mut self,
        value: usize,
    ) -> Result<Self, SessionRegistryConfigError> {
        if value == 0 || value > Self::HARD_MAX_TOMBSTONES {
            return Err(SessionRegistryConfigError::InvalidMaxTombstones);
        }
        if value > self.max_tombstones {
            return Err(SessionRegistryConfigError::NotALowering);
        }
        self.max_tombstones = value;
        Ok(self)
    }

    pub fn try_with_max_tombstone_bytes(
        mut self,
        value: usize,
    ) -> Result<Self, SessionRegistryConfigError> {
        if value == 0 || value > Self::HARD_MAX_TOMBSTONE_BYTES {
            return Err(SessionRegistryConfigError::InvalidMaxTombstoneBytes);
        }
        if value > self.max_tombstone_bytes {
            return Err(SessionRegistryConfigError::NotALowering);
        }
        self.max_tombstone_bytes = value;
        Ok(self)
    }

    pub fn try_with_tombstone_ttl(
        mut self,
        value: Duration,
    ) -> Result<Self, SessionRegistryConfigError> {
        if value.is_zero() || value > Self::HARD_MAX_TOMBSTONE_TTL {
            return Err(SessionRegistryConfigError::InvalidTombstoneTtl);
        }
        if value > self.tombstone_ttl {
            return Err(SessionRegistryConfigError::NotALowering);
        }
        self.tombstone_ttl = value;
        Ok(self)
    }
}

/// Route matching policy declared by an adapter.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RoutePolicy {
    LegacyAdapterMatch,
    ExactMethodAndPath,
}

/// Forwarded-header policy declared by an adapter.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum HeaderPolicy {
    LegacyForwarded,
    StrictAllowlist,
}

/// Redirect policy declared by an adapter.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RedirectPolicy {
    LegacyClient,
    Deny,
}

/// PII coverage proof policy declared by an adapter.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CoveragePolicy {
    LegacySurfaces,
    CodecProved,
}

/// Request, response, and streaming wire formats accepted by an adapter.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct FormatPolicy {
    request: WireFormat,
    response: WireFormat,
    streaming_response: WireFormat,
}

impl FormatPolicy {
    /// Characterizes the pre-codec JSON request/response and SSE stream behavior.
    #[must_use]
    pub const fn legacy() -> Self {
        Self {
            request: WireFormat::Json,
            response: WireFormat::Json,
            streaming_response: WireFormat::Sse,
        }
    }

    #[must_use]
    pub const fn request(&self) -> WireFormat {
        self.request
    }

    #[must_use]
    pub const fn response(&self) -> WireFormat {
        self.response
    }

    #[must_use]
    pub const fn streaming_response(&self) -> WireFormat {
        self.streaming_response
    }
}

/// Reviewed protocol, session, routing, and coverage declarations for an adapter.
///
/// Fields are private so downstream code cannot assemble an unreviewed contract.
/// Existing adapters obtain [`Self::legacy`] through the default trait method.
///
/// ```compile_fail
/// use gaze_proxy::{AdapterContract, ProtocolContract};
///
/// let _ = AdapterContract { protocol: ProtocolContract::LegacySurfaces };
/// ```
#[derive(Clone, Copy, Debug)]
pub struct AdapterContract<'a> {
    protocol: ProtocolContract<'a>,
    session_policy: Option<SessionPolicy>,
    route_policy: RoutePolicy,
    header_policy: HeaderPolicy,
    redirect_policy: RedirectPolicy,
    coverage_policy: CoveragePolicy,
    formats: FormatPolicy,
    codec_limits: Option<CodecLimits>,
}

impl AdapterContract<'static> {
    /// Characterizes the existing adapter behavior without changing it.
    #[must_use]
    pub const fn legacy() -> Self {
        Self {
            protocol: ProtocolContract::LegacySurfaces,
            session_policy: None,
            route_policy: RoutePolicy::LegacyAdapterMatch,
            header_policy: HeaderPolicy::LegacyForwarded,
            redirect_policy: RedirectPolicy::LegacyClient,
            coverage_policy: CoveragePolicy::LegacySurfaces,
            formats: FormatPolicy::legacy(),
            codec_limits: None,
        }
    }
}

impl<'a> AdapterContract<'a> {
    /// Constructs the only non-legacy policy bundle accepted by later direct adapters.
    #[allow(
        dead_code,
        reason = "consumed by the direct adapter in the next serialized checkpoint"
    )]
    pub(crate) const fn codec(
        codec: &'a dyn BodyCodec,
        session_policy: SessionPolicy,
        codec_limits: CodecLimits,
    ) -> Self {
        Self {
            protocol: ProtocolContract::Codec(codec),
            session_policy: Some(session_policy),
            route_policy: RoutePolicy::ExactMethodAndPath,
            header_policy: HeaderPolicy::StrictAllowlist,
            redirect_policy: RedirectPolicy::Deny,
            coverage_policy: CoveragePolicy::CodecProved,
            formats: FormatPolicy::legacy(),
            codec_limits: Some(codec_limits),
        }
    }

    #[must_use]
    pub const fn protocol(&self) -> ProtocolContract<'a> {
        self.protocol
    }

    /// Legacy adapters preserve the server's pre-contract auto-create behavior.
    #[must_use]
    pub const fn uses_legacy_session_auto_create(&self) -> bool {
        self.session_policy.is_none()
    }

    #[must_use]
    pub const fn session_policy(&self) -> Option<SessionPolicy> {
        self.session_policy
    }

    #[must_use]
    pub const fn route_policy(&self) -> RoutePolicy {
        self.route_policy
    }

    #[must_use]
    pub const fn header_policy(&self) -> HeaderPolicy {
        self.header_policy
    }

    #[must_use]
    pub const fn redirect_policy(&self) -> RedirectPolicy {
        self.redirect_policy
    }

    #[must_use]
    pub const fn coverage_policy(&self) -> CoveragePolicy {
        self.coverage_policy
    }

    #[must_use]
    pub const fn formats(&self) -> FormatPolicy {
        self.formats
    }

    #[must_use]
    pub const fn codec_limits(&self) -> Option<CodecLimits> {
        self.codec_limits
    }
}

#[async_trait::async_trait]
pub trait ProviderAdapter: Send + Sync + 'static {
    /// Declares protocol and safety behavior while preserving old implementations.
    fn contract(&self) -> AdapterContract<'_> {
        AdapterContract::legacy()
    }

    fn name(&self) -> &'static str;
    fn matches_path(&self, method: &Method, path: &str) -> bool;
    fn upstream_base(&self) -> &Url;
    fn request_pii_surfaces<'a>(&self, body: &'a mut Value) -> Vec<PiiSurface<'a>>;
    fn response_pii_surfaces<'a>(&self, body: &'a mut Value) -> Vec<PiiSurface<'a>>;
    fn sse_event_pii_surfaces<'a>(&self, event: &'a mut SseEvent) -> Vec<PiiSurface<'a>>;
}

pub struct PiiSurface<'a> {
    pub field_path: String,
    pub text: &'a mut String,
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct SseEvent {
    pub event: Option<String>,
    pub data: Value,
}

impl SseEvent {
    pub fn new(event: Option<String>, data: Value) -> Self {
        Self { event, data }
    }
}

pub(crate) fn push_string<'a>(
    surfaces: &mut Vec<PiiSurface<'a>>,
    field_path: impl Into<String>,
    value: &'a mut Value,
) {
    if let Value::String(text) = value {
        surfaces.push(PiiSurface {
            field_path: field_path.into(),
            text,
        });
    }
}

pub(crate) fn walk_all_strings<'a>(
    surfaces: &mut Vec<PiiSurface<'a>>,
    prefix: String,
    value: &'a mut Value,
) {
    match value {
        Value::String(text) => surfaces.push(PiiSurface {
            field_path: prefix,
            text,
        }),
        Value::Array(items) => {
            for (index, item) in items.iter_mut().enumerate() {
                walk_all_strings(surfaces, format!("{prefix}[{index}]"), item);
            }
        }
        Value::Object(map) => {
            for (key, child) in map {
                let path = if prefix.is_empty() {
                    key.clone()
                } else {
                    format!("{prefix}.{key}")
                };
                walk_all_strings(surfaces, path, child);
            }
        }
        Value::Null | Value::Bool(_) | Value::Number(_) => {}
    }
}

pub(crate) fn push_text_blocks<'a>(
    surfaces: &mut Vec<PiiSurface<'a>>,
    prefix: &str,
    value: &'a mut Value,
) {
    match value {
        Value::String(text) => surfaces.push(PiiSurface {
            field_path: prefix.to_string(),
            text,
        }),
        Value::Array(items) => {
            for (index, item) in items.iter_mut().enumerate() {
                match item {
                    Value::String(text) => surfaces.push(PiiSurface {
                        field_path: format!("{prefix}[{index}]"),
                        text,
                    }),
                    Value::Object(map) => {
                        if matches!(map.get("type"), Some(Value::String(kind)) if kind == "text") {
                            if let Some(Value::String(text)) = map.get_mut("text") {
                                surfaces.push(PiiSurface {
                                    field_path: format!("{prefix}[{index}].text"),
                                    text,
                                });
                            }
                        }
                    }
                    _ => {}
                }
            }
        }
        _ => {}
    }
}