gaze-proxy 0.15.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
use std::fmt;
use std::path::PathBuf;

use http::StatusCode;

use http::Method;
use thiserror::Error;
use url::Url;

use crate::codec::OpaqueCarrierLocation;

#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ProxyError {
    #[error("upstream unreachable: {url}")]
    UpstreamUnreachable {
        url: Url,
        #[source]
        source: reqwest::Error,
    },
    #[error("adapter not found for {method} {path}")]
    AdapterNotFound { path: String, method: Method },
    #[error("body too large: limit {limit_bytes} bytes")]
    BodyTooLarge { limit_bytes: u64 },
    #[error("invalid json body")]
    InvalidJson {
        #[source]
        source: serde_json::Error,
    },
    #[error("sse partial frame: {reason}")]
    SsePartialFrame { reason: String },
    /// A legacy-path request carried PII in a position no adapter surface covered.
    ///
    /// Raised by the outbound request residual re-scan, which fails closed rather than
    /// silently rewriting provider-owned fields. The variant carries the JSON field path
    /// ONLY -- never the detected value, nor any substring of it -- so neither `Display`,
    /// `Debug`, nor any log line derived from them can egress protected bytes.
    #[error("unsurfaced pii at {field_path}")]
    UnsurfacedPii { field_path: String },
    /// A request reached the legacy path under a contract that claims codec-proved coverage.
    ///
    /// The contract and the router disagree, so no coverage mechanism is known to have run.
    /// Proxying under an unproven coverage claim is exactly the failure this policy exists to
    /// prevent, so the request is refused.
    #[error("adapter coverage contract is unproven on the legacy path")]
    UnprovenCoverage,
    #[error("pipeline failed")]
    Pipeline {
        #[source]
        source: gaze::Error,
    },
    #[error("http server failed")]
    Server {
        #[source]
        source: std::io::Error,
    },
    #[error("daemon already running: pid {pid} ({pidfile})", pidfile = pidfile.display())]
    DaemonAlreadyRunning { pid: u32, pidfile: PathBuf },
    #[error("daemon is not running")]
    DaemonNotRunning,
    #[error("daemon pidfile stale: {pidfile}", pidfile = pidfile.display())]
    DaemonPidfileStale { pidfile: PathBuf },
    #[error("daemon io failed: {path}", path = path.display())]
    DaemonIo {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("daemon config failed: {detail}")]
    DaemonConfig { detail: String },
    /// The detached daemon child exited before it could serve.
    ///
    /// The child resolves the configured policy, so its early exit is normally a
    /// fail-closed policy load. Reporting that as a successful start is what let
    /// an unloadable policy look like a running chokepoint.
    #[error(
        "daemon exited before serving ({code}); see {stderr_file}",
        code = code.map_or_else(|| "terminated by signal".to_string(), |code| format!("exit code {code}")),
        stderr_file = stderr_file.display()
    )]
    DaemonExitedEarly {
        code: Option<i32>,
        stderr_file: PathBuf,
    },
}

/// Closed failure codes used by the direct proxy profile.
///
/// Unlike [`ProxyError`], this surface never stores provider, request, URL, parser, transport, or
/// operating-system error text.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProxyErrorCode {
    RouteRejected,
    HeaderRejected,
    InvalidUpstreamHeader,
    SessionIdentityRequired,
    UnexpectedSessionHeader,
    SessionExpired,
    SessionGenerationConflict,
    SessionCommitFailure,
    PrincipalRequired,
    PrincipalRejected,
    RegistryCapacity,
    InvalidUpstreamUrl,
    ProxyConfiguration,
    UnsupportedContentEncoding,
    InvalidFraming,
    RequestBodyLimitExceeded,
    ResponseBodyLimitExceeded,
    HeaderLimitExceeded,
    InvalidRequestFormat,
    InvalidUpstreamResponseFormat,
    DuplicateObjectKey,
    InternalCoverageFailure,
    ControlWouldMutate,
    OpaqueMediaUninspected,
    InvalidProvenance,
    InvalidToken,
    SignedMutationRequired,
    SignedSurfaceMalformed,
    InvalidSseLifecycle,
    InvalidContentBlockIndex,
    ConnectTimeout,
    RequestTimeout,
    TotalTimeout,
    UpstreamRedirect,
    UpstreamBadRequest,
    UpstreamUnauthorized,
    UpstreamForbidden,
    UpstreamNotFound,
    UpstreamConflict,
    UpstreamPayloadTooLarge,
    UpstreamRateLimited,
    UpstreamClientFailure,
    UpstreamUnavailable,
    UpstreamServerFailure,
    UpstreamUnreachable,
    UpstreamProtocol,
    InspectionInternal,
    InvalidStateTransition,
}

impl ProxyErrorCode {
    /// Constructs a sanitized direct-profile error at a closed phase.
    #[must_use]
    pub const fn error(self, phase: ProxyErrorPhase) -> DirectProxyError {
        DirectProxyError::new(self, phase)
    }

    /// Returns the single reviewed downstream status for this code.
    #[must_use]
    pub const fn http_status(self) -> StatusCode {
        match self {
            Self::RouteRejected => StatusCode::NOT_FOUND,
            Self::HeaderRejected
            | Self::SessionIdentityRequired
            | Self::UnexpectedSessionHeader
            | Self::InvalidRequestFormat
            | Self::DuplicateObjectKey => StatusCode::BAD_REQUEST,
            Self::PrincipalRequired | Self::UpstreamUnauthorized => StatusCode::UNAUTHORIZED,
            Self::PrincipalRejected | Self::UpstreamForbidden => StatusCode::FORBIDDEN,
            Self::SessionExpired => StatusCode::GONE,
            Self::SessionGenerationConflict | Self::UpstreamConflict => StatusCode::CONFLICT,
            Self::RegistryCapacity
            | Self::InvalidUpstreamUrl
            | Self::ProxyConfiguration
            | Self::UpstreamUnavailable => StatusCode::SERVICE_UNAVAILABLE,
            Self::RequestBodyLimitExceeded | Self::UpstreamPayloadTooLarge => {
                StatusCode::PAYLOAD_TOO_LARGE
            }
            Self::ControlWouldMutate | Self::OpaqueMediaUninspected => {
                StatusCode::UNPROCESSABLE_ENTITY
            }
            Self::ConnectTimeout | Self::RequestTimeout | Self::TotalTimeout => {
                StatusCode::GATEWAY_TIMEOUT
            }
            Self::UpstreamBadRequest => StatusCode::BAD_REQUEST,
            Self::UpstreamNotFound => StatusCode::NOT_FOUND,
            Self::UpstreamRateLimited => StatusCode::TOO_MANY_REQUESTS,
            Self::SessionCommitFailure
            | Self::InspectionInternal
            | Self::InvalidStateTransition => StatusCode::INTERNAL_SERVER_ERROR,
            Self::InvalidUpstreamHeader
            | Self::UnsupportedContentEncoding
            | Self::InvalidFraming
            | Self::ResponseBodyLimitExceeded
            | Self::HeaderLimitExceeded
            | Self::InvalidUpstreamResponseFormat
            | Self::InternalCoverageFailure
            | Self::InvalidProvenance
            | Self::InvalidToken
            | Self::SignedMutationRequired
            | Self::SignedSurfaceMalformed
            | Self::InvalidSseLifecycle
            | Self::InvalidContentBlockIndex
            | Self::UpstreamRedirect
            | Self::UpstreamClientFailure
            | Self::UpstreamServerFailure
            | Self::UpstreamUnreachable
            | Self::UpstreamProtocol => StatusCode::BAD_GATEWAY,
        }
    }
}

/// Closed processing phase carried by a direct-profile failure.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProxyErrorPhase {
    Ingress,
    RequestValidation,
    Session,
    RequestTransform,
    UpstreamConfiguration,
    UpstreamConnect,
    UpstreamHeaders,
    UpstreamBody,
    ResponseValidation,
    ResponseTransform,
    ResponseReplay,
    Inspection,
    Framework,
}

/// Structural reason a request header was rejected.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum HeaderRejectionReason {
    NameNotAllowlisted,
    EmptyValue,
    DuplicateSingleton,
}

impl HeaderRejectionReason {
    /// Returns the stable downstream identifier for this rejection cause.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::NameNotAllowlisted => "name_not_allowlisted",
            Self::EmptyValue => "empty_value",
            Self::DuplicateSingleton => "duplicate_singleton",
        }
    }
}

const MAX_STRUCTURAL_HEADER_NAME_BYTES: usize = 128;
const PACKED_STRUCTURAL_HEADER_NAME_BYTES: usize = MAX_STRUCTURAL_HEADER_NAME_BYTES * 6 / 8;

// `HeaderName::as_str()` uses the 51 lowercase HTTP token characters. Six-bit packing retains
// every reviewed 128-byte header name exactly while keeping the closed error small and `Copy`.
#[derive(Clone, Copy, Eq, PartialEq)]
struct HeaderRejectionIdentifier {
    packed_name: [u8; PACKED_STRUCTURAL_HEADER_NAME_BYTES],
    name_length: u8,
    reason: HeaderRejectionReason,
}

impl HeaderRejectionIdentifier {
    fn new(name: &str, reason: HeaderRejectionReason) -> Option<Self> {
        let name_length = u8::try_from(name.len()).ok()?;
        if name.len() > MAX_STRUCTURAL_HEADER_NAME_BYTES {
            return None;
        }
        let mut packed_name = [0; PACKED_STRUCTURAL_HEADER_NAME_BYTES];
        for (index, byte) in name.bytes().enumerate() {
            let code = encode_header_name_byte(byte)?;
            let bit_offset = index * 6;
            let byte_offset = bit_offset / 8;
            let shift = bit_offset % 8;
            packed_name[byte_offset] |= code << shift;
            if shift > 2 {
                packed_name[byte_offset + 1] |= code >> (8 - shift);
            }
        }
        Some(Self {
            packed_name,
            name_length,
            reason,
        })
    }

    fn name(self) -> String {
        let mut name = String::with_capacity(usize::from(self.name_length));
        for index in 0..usize::from(self.name_length) {
            let bit_offset = index * 6;
            let byte_offset = bit_offset / 8;
            let shift = bit_offset % 8;
            let mut code = self.packed_name[byte_offset] >> shift;
            if shift > 2 {
                code |= self.packed_name[byte_offset + 1] << (8 - shift);
            }
            name.push(char::from(
                decode_header_name_byte(code & 0x3f)
                    .expect("packed header names contain only validated token bytes"),
            ));
        }
        name
    }
}

fn encode_header_name_byte(byte: u8) -> Option<u8> {
    match byte {
        b'a'..=b'z' => Some(byte - b'a'),
        b'0'..=b'9' => Some(26 + byte - b'0'),
        b'!' => Some(36),
        b'#' => Some(37),
        b'$' => Some(38),
        b'%' => Some(39),
        b'&' => Some(40),
        b'\'' => Some(41),
        b'*' => Some(42),
        b'+' => Some(43),
        b'-' => Some(44),
        b'.' => Some(45),
        b'^' => Some(46),
        b'_' => Some(47),
        b'`' => Some(48),
        b'|' => Some(49),
        b'~' => Some(50),
        _ => None,
    }
}

fn decode_header_name_byte(code: u8) -> Option<u8> {
    match code {
        0..=25 => Some(b'a' + code),
        26..=35 => Some(b'0' + code - 26),
        36 => Some(b'!'),
        37 => Some(b'#'),
        38 => Some(b'$'),
        39 => Some(b'%'),
        40 => Some(b'&'),
        41 => Some(b'\''),
        42 => Some(b'*'),
        43 => Some(b'+'),
        44 => Some(b'-'),
        45 => Some(b'.'),
        46 => Some(b'^'),
        47 => Some(b'_'),
        48 => Some(b'`'),
        49 => Some(b'|'),
        50 => Some(b'~'),
        _ => None,
    }
}

impl fmt::Debug for HeaderRejectionIdentifier {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Header")
            .field("name", &self.name())
            .field("reason", &self.reason)
            .finish()
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum DirectProxyErrorIdentifier {
    Header(HeaderRejectionIdentifier),
    OpaqueCarrier(OpaqueCarrierLocation),
}

/// Sanitized direct-profile failure.
///
/// `Display`, `Debug`, and the source chain expose only its closed code, phase, and reviewed
/// structural identifiers. Header values and carrier bytes are never retained.
///
/// This follows the field-path-only precedent established for [`ProxyError::UnsurfacedPii`] in
/// #400: report the structural location that makes a rejection actionable, never the value.
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct DirectProxyError {
    code: ProxyErrorCode,
    phase: ProxyErrorPhase,
    retry_after_seconds: Option<u32>,
    identifier: Option<DirectProxyErrorIdentifier>,
}

impl DirectProxyError {
    #[must_use]
    pub const fn new(code: ProxyErrorCode, phase: ProxyErrorPhase) -> Self {
        Self {
            code,
            phase,
            retry_after_seconds: None,
            identifier: None,
        }
    }

    #[must_use]
    pub(crate) const fn with_retry_after_seconds(mut self, seconds: Option<u32>) -> Self {
        self.retry_after_seconds = seconds;
        self
    }

    pub(crate) fn with_header_rejection(
        mut self,
        name: &str,
        reason: HeaderRejectionReason,
    ) -> Self {
        self.identifier =
            HeaderRejectionIdentifier::new(name, reason).map(DirectProxyErrorIdentifier::Header);
        self
    }

    pub(crate) fn with_opaque_carrier(mut self, opaque_carrier: OpaqueCarrierLocation) -> Self {
        self.identifier = Some(DirectProxyErrorIdentifier::OpaqueCarrier(opaque_carrier));
        self
    }

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

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

    #[must_use]
    pub const fn http_status(&self) -> StatusCode {
        self.code.http_status()
    }

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

    /// Returns the rejected header name without retaining or exposing its value.
    #[must_use]
    pub fn header_name(&self) -> Option<String> {
        match &self.identifier {
            Some(DirectProxyErrorIdentifier::Header(identifier)) => Some(identifier.name()),
            _ => None,
        }
    }

    /// Returns the structural header-rejection cause.
    #[must_use]
    pub const fn header_rejection_reason(&self) -> Option<HeaderRejectionReason> {
        match &self.identifier {
            Some(DirectProxyErrorIdentifier::Header(identifier)) => Some(identifier.reason),
            _ => None,
        }
    }

    /// Returns only the opaque carrier's scheme and structural location.
    #[must_use]
    pub const fn opaque_carrier(&self) -> Option<OpaqueCarrierLocation> {
        match &self.identifier {
            Some(DirectProxyErrorIdentifier::OpaqueCarrier(location)) => Some(*location),
            _ => None,
        }
    }

    #[must_use]
    pub const fn sse_error_frame(&self) -> &'static [u8] {
        crate::server::DIRECT_PROXY_ERROR_FRAME
    }
}

impl fmt::Debug for DirectProxyError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut debug = formatter.debug_struct("DirectProxyError");
        debug.field("code", &self.code).field("phase", &self.phase);
        if let Some(identifier) = &self.identifier {
            debug.field("identifier", identifier);
        }
        debug.finish()
    }
}

impl fmt::Display for DirectProxyError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "proxy_{:?}_{:?}", self.phase, self.code)
    }
}

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