qubit-redact 0.4.0

Rule-driven redaction for fields, diagnostics, HTTP data, and Rust domain objects
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Policy-driven URI redaction facade.

use std::{
    borrow::Cow,
    fmt::{
        self,
        Write,
    },
};

use fluent_uri::Uri;

use crate::policy::OutputCharge;
use crate::{
    RedactedText,
    RedactionPolicy,
    RedactionSession,
    Sensitivity,
    policy::ResolvedField,
};

use super::{
    UriComponent,
    UriFragmentPolicy,
    UriInspection,
    UriPathPolicy,
    UriRedaction,
    UriRedactionReason,
    UriRedactionStatus,
};

use super::internal::{
    BoundedUriWriter,
    UriComponentWriter,
};

const INVALID_URI: &str = "<invalid URI>";

/// Redacts URI strings using one immutable policy snapshot.
#[must_use]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UriRedactor {
    policy: RedactionPolicy,
}

impl UriRedactor {
    /// Creates a URI redactor from an explicit immutable policy.
    #[inline]
    pub const fn new(policy: RedactionPolicy) -> Self {
        Self { policy }
    }

    /// Returns the immutable URI policy snapshot.
    #[must_use = "use the URI policy snapshot"]
    #[inline]
    pub const fn policy(&self) -> &RedactionPolicy {
        &self.policy
    }

    /// Redacts one absolute URI and returns safe text plus metadata.
    ///
    /// Parsing is strict. Invalid syntax, invalid UTF-8 in a percent-encoded
    /// field, and budget violations all return a fixed marker without any
    /// portion of the input URI.
    #[must_use = "use the structured URI redaction result"]
    pub fn redact_uri_str(&self, input: &str) -> UriRedaction {
        if input.len()
            > self.policy.limits().diagnostic_event().max_input_bytes()
        {
            return invalid_result(UriRedactionReason::InputLimitExceeded);
        }

        let parsed = match Uri::<&str>::parse(input) {
            Ok(parsed) => parsed,
            Err(_) => return invalid_result(UriRedactionReason::InvalidUri),
        };
        let mut reasons = Vec::new();
        let mut components = Vec::new();
        let mut rendered = BoundedUriWriter::new(
            self.policy.limits().diagnostic_event().max_output_bytes(),
        );
        let scheme = parsed.scheme().as_str();
        let scheme_end = scheme.len() + 1;
        rendered.write_str(&input[..scheme_end]);

        if let Some(authority) = parsed.authority() {
            rendered.write_str("//");
            if redact_authority(
                authority.as_str(),
                &self.policy,
                &mut reasons,
                &mut components,
                &mut rendered,
            )
            .is_err()
            {
                return invalid_result(UriRedactionReason::InvalidUri);
            }
        }

        let path = parsed.path().as_str();
        if self.policy.path_policy() == UriPathPolicy::Redact
            && !path.is_empty()
            && path != "/"
        {
            mark_component(UriComponent::Path, &mut reasons, &mut components);
            if path.starts_with('/') {
                rendered.write_str("/%3Credacted%3E");
            } else {
                rendered.write_str("%3Credacted%3E");
            }
        } else {
            rendered.write_str(path);
        }

        if let Some(query) = parsed.query() {
            rendered.write_str("?");
            if let Err(reason) = redact_query(
                query.as_str(),
                &self.policy,
                &mut reasons,
                &mut components,
                &mut rendered,
            ) {
                return invalid_result(reason);
            }
        }

        if let Some(fragment) = parsed.fragment() {
            rendered.write_str("#");
            if self.policy.fragment_policy() == UriFragmentPolicy::Redact
                && !fragment.as_str().is_empty()
            {
                mark_component(
                    UriComponent::Fragment,
                    &mut reasons,
                    &mut components,
                );
                write_opaque_mask(
                    &self.policy,
                    Sensitivity::High,
                    &mut rendered,
                );
            } else {
                rendered.write_str(fragment.as_str());
            }
        }

        let status = if components.is_empty() {
            UriRedactionStatus::PassedThrough
        } else {
            UriRedactionStatus::Redacted
        };
        let (safe, truncated) = rendered.finish();
        if truncated {
            reasons.push(UriRedactionReason::OutputTruncated);
        }
        UriRedaction {
            text: safe_text(safe),
            status,
            reasons,
            components,
            truncated,
        }
    }

    /// Inspects one absolute URI and returns metadata without rendering text.
    ///
    /// Parsing, strict percent decoding, and component classification follow
    /// the same URI policy as [`Self::redact_uri_str`]. The input budget still
    /// applies, while the output budget and truncation behavior do not.
    #[must_use = "use the structured URI inspection result"]
    pub fn inspect_uri_str(&self, input: &str) -> UriInspection {
        let budget = self.policy.limits().diagnostic_event();
        if input.len() > budget.max_input_bytes() {
            return invalid_inspection(UriRedactionReason::InputLimitExceeded);
        }

        let parsed = match Uri::<&str>::parse(input) {
            Ok(parsed) => parsed,
            Err(_) => {
                return invalid_inspection(UriRedactionReason::InvalidUri);
            }
        };
        let mut reasons = Vec::new();
        let mut components = Vec::new();

        if let Some(authority) = parsed.authority()
            && inspect_authority(
                authority.as_str(),
                &self.policy,
                &mut reasons,
                &mut components,
            )
            .is_err()
        {
            return invalid_inspection(UriRedactionReason::InvalidUri);
        }

        let path = parsed.path().as_str();
        if self.policy.path_policy() == UriPathPolicy::Redact
            && !path.is_empty()
            && path != "/"
        {
            mark_component(UriComponent::Path, &mut reasons, &mut components);
        }

        if let Some(query) = parsed.query()
            && let Err(reason) = inspect_query(
                query.as_str(),
                &self.policy,
                &mut reasons,
                &mut components,
            )
        {
            return invalid_inspection(reason);
        }

        if let Some(fragment) = parsed.fragment()
            && self.policy.fragment_policy() == UriFragmentPolicy::Redact
            && !fragment.as_str().is_empty()
        {
            mark_component(
                UriComponent::Fragment,
                &mut reasons,
                &mut components,
            );
        }

        let status = if components.is_empty() {
            UriRedactionStatus::PassedThrough
        } else {
            UriRedactionStatus::Redacted
        };
        UriInspection {
            status,
            reasons,
            components,
        }
    }

    /// Redacts one URI while consuming a shared diagnostic session.
    #[must_use = "use the session-bounded URI result"]
    pub fn redact_uri_str_with_session(
        &self,
        input: &str,
        session: &RedactionSession<'_>,
    ) -> UriRedaction {
        if !session.consume_input(input.len()) {
            return session_invalid_result(
                session,
                UriRedactionReason::InputLimitExceeded,
            );
        }
        let result = self.redact_uri_str(input);
        match session.charge_output_or_fallback(
            result.log_safe_text().as_str().len(),
            INVALID_URI.len(),
        ) {
            OutputCharge::Complete => result,
            OutputCharge::Fallback => {
                invalid_result(UriRedactionReason::OutputTruncated)
            }
            OutputCharge::Exhausted => {
                empty_invalid_result(UriRedactionReason::OutputTruncated)
            }
        }
    }

    /// Inspects one URI while consuming a shared diagnostic input budget.
    #[must_use = "use the session-bounded URI inspection"]
    pub fn inspect_uri_str_with_session(
        &self,
        input: &str,
        session: &RedactionSession<'_>,
    ) -> UriInspection {
        if !session.consume_input(input.len()) {
            return invalid_inspection(UriRedactionReason::InputLimitExceeded);
        }
        self.inspect_uri_str(input)
    }
}

impl Default for UriRedactor {
    /// Creates a redactor from the process-wide URI policy snapshot.
    #[inline]
    fn default() -> Self {
        Self::new(RedactionPolicy::default())
    }
}

/// Redacts userinfo while preserving the authority's raw host and port.
fn redact_authority(
    authority: &str,
    policy: &RedactionPolicy,
    reasons: &mut Vec<UriRedactionReason>,
    components: &mut Vec<UriComponent>,
    rendered: &mut BoundedUriWriter,
) -> Result<(), ()> {
    let Some((userinfo, host)) = authority.rsplit_once('@') else {
        rendered.write_str(authority);
        return Ok(());
    };
    let Some((username, password)) = userinfo.split_once(':') else {
        redact_userinfo_value(
            userinfo,
            "username",
            UriComponent::Username,
            policy,
            reasons,
            components,
            rendered,
        )?;
        rendered.write_str("@");
        rendered.write_str(host);
        return Ok(());
    };
    redact_userinfo_value(
        username,
        "username",
        UriComponent::Username,
        policy,
        reasons,
        components,
        rendered,
    )?;
    rendered.write_str(":");
    redact_userinfo_value(
        password,
        "password",
        UriComponent::Password,
        policy,
        reasons,
        components,
        rendered,
    )?;
    rendered.write_str("@");
    rendered.write_str(host);
    Ok(())
}

/// Inspects userinfo without allocating a rendered URI.
fn inspect_authority(
    authority: &str,
    policy: &RedactionPolicy,
    reasons: &mut Vec<UriRedactionReason>,
    components: &mut Vec<UriComponent>,
) -> Result<(), ()> {
    let Some((userinfo, _host)) = authority.rsplit_once('@') else {
        return Ok(());
    };
    let Some((username, password)) = userinfo.split_once(':') else {
        inspect_userinfo_value(
            userinfo,
            "username",
            UriComponent::Username,
            policy,
            reasons,
            components,
        )?;
        return Ok(());
    };
    inspect_userinfo_value(
        username,
        "username",
        UriComponent::Username,
        policy,
        reasons,
        components,
    )?;
    inspect_userinfo_value(
        password,
        "password",
        UriComponent::Password,
        policy,
        reasons,
        components,
    )?;
    Ok(())
}

/// Applies a named field policy to one userinfo component without rendering.
fn inspect_userinfo_value(
    raw: &str,
    field: &str,
    component: UriComponent,
    policy: &RedactionPolicy,
    reasons: &mut Vec<UriRedactionReason>,
    components: &mut Vec<UriComponent>,
) -> Result<(), ()> {
    decode_uri_component(raw).map_err(|_| ())?;
    if matches!(policy.resolve_field(field), ResolvedField::Sensitive { .. }) {
        mark_component(component, reasons, components);
    }
    Ok(())
}

/// Applies a named field policy to one raw userinfo component.
fn redact_userinfo_value(
    raw: &str,
    field: &str,
    component: UriComponent,
    policy: &RedactionPolicy,
    reasons: &mut Vec<UriRedactionReason>,
    components: &mut Vec<UriComponent>,
    rendered: &mut BoundedUriWriter,
) -> Result<(), ()> {
    let decoded = decode_uri_component(raw).map_err(|_| ())?;
    match policy.resolve_field(field) {
        ResolvedField::Sensitive { sensitivity } => {
            mark_component(component, reasons, components);
            write_sensitive_value(policy, sensitivity, &decoded, rendered);
        }
        ResolvedField::PassThrough => {
            rendered.write_str(raw);
        }
    }
    Ok(())
}

/// Redacts query values after strict percent decoding.
fn redact_query(
    query: &str,
    policy: &RedactionPolicy,
    reasons: &mut Vec<UriRedactionReason>,
    components: &mut Vec<UriComponent>,
    rendered: &mut BoundedUriWriter,
) -> Result<(), UriRedactionReason> {
    for (index, pair) in query.split('&').enumerate() {
        if index != 0 {
            rendered.write_str("&");
        }
        let Some((raw_key, raw_value)) = pair.split_once('=') else {
            decode_uri_component(pair)
                .map_err(|_| UriRedactionReason::UndecodableQueryKey)?;
            rendered.write_str(pair);
            continue;
        };
        let key = decode_uri_component(raw_key)
            .map_err(|_| UriRedactionReason::UndecodableQueryKey)?;
        let value = decode_uri_component(raw_value)
            .map_err(|_| UriRedactionReason::UndecodableQueryValue)?;
        match policy.resolve_field(&key) {
            ResolvedField::Sensitive { sensitivity } => {
                mark_component(UriComponent::Query, reasons, components);
                rendered.write_str(raw_key);
                rendered.write_str("=");
                write_sensitive_value(policy, sensitivity, &value, rendered);
            }
            ResolvedField::PassThrough => {
                rendered.write_str(pair);
            }
        }
    }
    Ok(())
}

/// Inspects query fields after strict percent decoding without rendering.
fn inspect_query(
    query: &str,
    policy: &RedactionPolicy,
    reasons: &mut Vec<UriRedactionReason>,
    components: &mut Vec<UriComponent>,
) -> Result<(), UriRedactionReason> {
    for pair in query.split('&') {
        let Some((raw_key, raw_value)) = pair.split_once('=') else {
            decode_uri_component(pair)
                .map_err(|_| UriRedactionReason::UndecodableQueryKey)?;
            continue;
        };
        let key = decode_uri_component(raw_key)
            .map_err(|_| UriRedactionReason::UndecodableQueryKey)?;
        decode_uri_component(raw_value)
            .map_err(|_| UriRedactionReason::UndecodableQueryValue)?;
        if matches!(policy.resolve_field(&key), ResolvedField::Sensitive { .. })
        {
            mark_component(UriComponent::Query, reasons, components);
        }
    }
    Ok(())
}

/// Decodes percent escapes without applying form-urlencoded `+` semantics.
fn decode_uri_component(raw: &str) -> Result<String, ()> {
    let bytes = raw.as_bytes();
    let mut decoded = Vec::with_capacity(bytes.len());
    let mut index = 0;
    while index < bytes.len() {
        if bytes[index] == b'%' {
            if index + 2 >= bytes.len() {
                return Err(());
            }
            let high = hex_value(bytes[index + 1]).ok_or(())?;
            let low = hex_value(bytes[index + 2]).ok_or(())?;
            decoded.push((high << 4) | low);
            index += 3;
        } else {
            decoded.push(bytes[index]);
            index += 1;
        }
    }
    String::from_utf8(decoded).map_err(|_| ())
}

/// Streams one sensitive value through URI encoding and the bounded writer.
fn write_sensitive_value(
    policy: &RedactionPolicy,
    sensitivity: Sensitivity,
    value: &str,
    rendered: &mut BoundedUriWriter,
) {
    if value.is_empty() || rendered.is_full() {
        return;
    }
    let mut writer = UriComponentWriter::new(rendered);
    let _ = policy
        .masking()
        .for_level(sensitivity)
        .write_masked(value, &mut writer);
}

/// Writes an opaque replacement without allocating beyond the output budget.
fn write_opaque_mask(
    policy: &RedactionPolicy,
    sensitivity: Sensitivity,
    rendered: &mut BoundedUriWriter,
) {
    if rendered.is_full() {
        return;
    }
    let mut writer = UriComponentWriter::new(rendered);
    let _ =
        writer.write_str(policy.masking().for_level(sensitivity).opaque_mask());
}

/// Converts one hexadecimal ASCII byte to its numeric value.
const fn hex_value(byte: u8) -> Option<u8> {
    match byte {
        b'0'..=b'9' => Some(byte - b'0'),
        b'a'..=b'f' => Some(byte - b'a' + 10),
        b'A'..=b'F' => Some(byte - b'A' + 10),
        _ => None,
    }
}

/// Records a sensitive component and its corresponding reason once.
fn mark_component(
    component: UriComponent,
    reasons: &mut Vec<UriRedactionReason>,
    components: &mut Vec<UriComponent>,
) {
    if !components.contains(&component) {
        components.push(component);
    }
    let reason = UriRedactionReason::SensitiveComponent(component);
    if !reasons.contains(&reason) {
        reasons.push(reason);
    }
}

/// Builds a fixed-marker result for malformed or over-budget input.
fn invalid_result(reason: UriRedactionReason) -> UriRedaction {
    UriRedaction {
        text: safe_text(INVALID_URI.to_owned()),
        status: UriRedactionStatus::Invalid,
        reasons: vec![reason],
        components: Vec::new(),
        truncated: false,
    }
}

/// Charges the invalid-URI marker once for a shared diagnostic session.
fn session_invalid_result(
    session: &RedactionSession<'_>,
    reason: UriRedactionReason,
) -> UriRedaction {
    match session
        .charge_output_or_fallback(INVALID_URI.len(), INVALID_URI.len())
    {
        OutputCharge::Complete => invalid_result(reason),
        OutputCharge::Fallback | OutputCharge::Exhausted => {
            empty_invalid_result(reason)
        }
    }
}

/// Preserves fail-closed metadata without emitting bytes after exhaustion.
fn empty_invalid_result(reason: UriRedactionReason) -> UriRedaction {
    UriRedaction {
        text: safe_text(String::new()),
        status: UriRedactionStatus::Invalid,
        reasons: vec![reason],
        components: Vec::new(),
        truncated: true,
    }
}

/// Builds metadata for malformed or over-budget input.
fn invalid_inspection(reason: UriRedactionReason) -> UriInspection {
    UriInspection {
        status: UriRedactionStatus::Invalid,
        reasons: vec![reason],
        components: Vec::new(),
    }
}

/// Converts owned output into the library's log-safe text wrapper.
fn safe_text(value: String) -> crate::LogSafeText<'static> {
    RedactedText::new(Cow::Owned(value)).escape_for_log()
}

impl fmt::Display for UriRedactor {
    /// Formats the policy snapshot without exposing URI input.
    #[inline]
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("UriRedactor")
    }
}