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
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Builder for immutable HTTP redaction policy snapshots.
use crate::{
DiagnosticBudget,
PolicyError,
RedactionPolicy,
RedactionPolicyBuilder,
Sensitivity,
};
use super::{
BodyBudget,
HttpRedactionPolicy,
TextBodyPolicy,
UnkeyedJsonValuePolicy,
UrlPathPolicy,
};
/// Mutable construction state for an [`HttpRedactionPolicy`].
#[must_use]
#[derive(Debug, Clone)]
pub struct HttpRedactionPolicyBuilder {
/// Header-field policy construction state.
header: RedactionPolicyBuilder,
/// Query and form field-policy construction state.
query: RedactionPolicyBuilder,
/// Structured-body field-policy construction state.
body: RedactionPolicyBuilder,
/// Visibility choice for non-root URL paths.
url_path_policy: UrlPathPolicy,
/// Visibility choice for opaque UTF-8 text bodies.
text_body_policy: TextBodyPolicy,
/// Visibility choice for unkeyed JSON scalars.
unkeyed_json_value_policy: UnkeyedJsonValuePolicy,
/// Finite body parser-input and log-output limits.
body_budget: BodyBudget,
/// Finite limits for HTTP diagnostics outside captured bodies.
diagnostic_budget: DiagnosticBudget,
}
impl HttpRedactionPolicyBuilder {
/// Creates a builder with empty field policies and default HTTP behavior.
///
/// # Returns
///
/// A builder with fail-closed behavior choices and finite default limits.
#[inline]
pub fn new() -> Self {
Self::empty()
}
/// Creates a builder with empty field policies and default HTTP behavior.
///
/// # Returns
///
/// A builder with fail-closed behavior choices and finite default limits.
#[inline]
pub(super) fn empty() -> Self {
Self {
header: RedactionPolicyBuilder::empty(),
query: RedactionPolicyBuilder::empty(),
body: RedactionPolicyBuilder::empty(),
url_path_policy: UrlPathPolicy::default(),
text_body_policy: TextBodyPolicy::default(),
unkeyed_json_value_policy: UnkeyedJsonValuePolicy::default(),
body_budget: BodyBudget::default(),
diagnostic_budget: DiagnosticBudget::default(),
}
}
/// Replaces this builder with the current default HTTP policy snapshot.
///
/// # Returns
///
/// A mutable copy of `HttpRedactionPolicy::default`.
///
/// # Warning
///
/// This replaces every builder component, including the header, query, and
/// body policies, behavior choices, and budgets. Call this method before
/// adding application-specific configuration.
#[inline]
pub fn load_default(self) -> Self {
Self::from_policy(&HttpRedactionPolicy::default())
}
/// Creates a builder by copying a complete immutable HTTP policy.
///
/// # Parameters
///
/// * `policy` - HTTP policy whose fields, behaviors, and budgets are
/// copied.
///
/// # Returns
///
/// Mutable construction state equivalent to `policy`.
#[inline]
pub fn from_policy(policy: &HttpRedactionPolicy) -> Self {
Self {
header: RedactionPolicy::builder_from(policy.header_policy()),
query: RedactionPolicy::builder_from(policy.query_policy()),
body: RedactionPolicy::builder_from(policy.body_policy()),
url_path_policy: policy.url_path_policy(),
text_body_policy: policy.text_body_policy(),
unkeyed_json_value_policy: policy.unkeyed_json_value_policy(),
body_budget: policy.body_budget(),
diagnostic_budget: policy.diagnostic_budget(),
}
}
/// Creates a builder with three mutable copies of `base`.
///
/// # Parameters
///
/// * `base` - Field policy copied for all three HTTP contexts.
///
/// # Returns
///
/// A builder with fail-closed behavior choices and finite default limits.
#[inline]
pub(super) fn from_base_policy(base: RedactionPolicy) -> Self {
Self {
header: RedactionPolicy::builder_from(&base),
query: RedactionPolicy::builder_from(&base),
body: RedactionPolicy::builder_from(&base),
url_path_policy: UrlPathPolicy::default(),
text_body_policy: TextBodyPolicy::default(),
unkeyed_json_value_policy: UnkeyedJsonValuePolicy::default(),
body_budget: BodyBudget::default(),
diagnostic_budget: base.diagnostic_budget(),
}
}
/// Replaces the header-field policy snapshot.
///
/// # Parameters
///
/// * `policy` - Immutable policy used for HTTP headers.
///
/// # Returns
///
/// The updated builder.
#[inline(always)]
pub fn header_policy(mut self, policy: RedactionPolicy) -> Self {
self.header = RedactionPolicy::builder_from(&policy);
self
}
/// Replaces the query and form field-policy snapshot.
///
/// # Parameters
///
/// * `policy` - Immutable policy used for query and form fields.
///
/// # Returns
///
/// The updated builder.
#[inline(always)]
pub fn query_policy(mut self, policy: RedactionPolicy) -> Self {
self.query = RedactionPolicy::builder_from(&policy);
self
}
/// Replaces the structured-body field-policy snapshot.
///
/// # Parameters
///
/// * `policy` - Immutable policy used for fields inside HTTP bodies.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn body_policy(mut self, policy: RedactionPolicy) -> Self {
self.body = RedactionPolicy::builder_from(&policy);
self
}
/// Raises one header field to at least `level`.
///
/// # Parameters
///
/// * `name` - Header name to canonicalize.
/// * `level` - Minimum sensitivity level.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn raise_header(mut self, name: &str, level: Sensitivity) -> Self {
self.header = self.header.raise(name, level);
self
}
/// Replaces one header field's sensitivity with `level`.
///
/// # Parameters
///
/// * `name` - Header name to canonicalize.
/// * `level` - Explicit replacement sensitivity.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn override_header(mut self, name: &str, level: Sensitivity) -> Self {
self.header = self.header.override_level(name, level);
self
}
/// Allows one exact header name to remain visible.
///
/// # Parameters
///
/// * `name` - Exact header name to allow after canonicalization.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn allow_header_exact(mut self, name: &str) -> Self {
self.header = self.header.allow_exact(name);
self
}
/// Allows one header name at token-suffix boundaries.
///
/// # Parameters
///
/// * `name` - Header suffix to allow after canonicalization.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn allow_header_suffix(mut self, name: &str) -> Self {
self.header = self.header.allow_suffix(name);
self
}
/// Removes one exact header allow rule.
///
/// # Parameters
///
/// * `name` - Header name whose exact allow rule is removed.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn remove_header_allow_exact(mut self, name: &str) -> Self {
self.header = self.header.remove_allow_exact(name);
self
}
/// Removes one token-suffix header allow rule.
///
/// # Parameters
///
/// * `name` - Header suffix whose allow rule is removed.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn remove_header_allow_suffix(mut self, name: &str) -> Self {
self.header = self.header.remove_allow_suffix(name);
self
}
/// Removes every header allow rule.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn clear_header_allow_rules(mut self) -> Self {
self.header = self.header.clear_allow_rules();
self
}
/// Raises one query or form field to at least `level`.
///
/// # Parameters
///
/// * `name` - Query field name to canonicalize.
/// * `level` - Minimum sensitivity level.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn raise_query(mut self, name: &str, level: Sensitivity) -> Self {
self.query = self.query.raise(name, level);
self
}
/// Replaces one query or form field's sensitivity with `level`.
///
/// # Parameters
///
/// * `name` - Query field name to canonicalize.
/// * `level` - Explicit replacement sensitivity.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn override_query(mut self, name: &str, level: Sensitivity) -> Self {
self.query = self.query.override_level(name, level);
self
}
/// Allows one exact query or form field name to remain visible.
///
/// # Parameters
///
/// * `name` - Exact query field name to allow after canonicalization.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn allow_query_exact(mut self, name: &str) -> Self {
self.query = self.query.allow_exact(name);
self
}
/// Allows one query or form field at token-suffix boundaries.
///
/// # Parameters
///
/// * `name` - Query field suffix to allow after canonicalization.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn allow_query_suffix(mut self, name: &str) -> Self {
self.query = self.query.allow_suffix(name);
self
}
/// Removes one exact query or form-field allow rule.
///
/// # Parameters
///
/// * `name` - Query or form-field name whose exact allow rule is removed.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn remove_query_allow_exact(mut self, name: &str) -> Self {
self.query = self.query.remove_allow_exact(name);
self
}
/// Removes one token-suffix query or form-field allow rule.
///
/// # Parameters
///
/// * `name` - Query or form-field suffix whose allow rule is removed.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn remove_query_allow_suffix(mut self, name: &str) -> Self {
self.query = self.query.remove_allow_suffix(name);
self
}
/// Removes every query and form-field allow rule.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn clear_query_allow_rules(mut self) -> Self {
self.query = self.query.clear_allow_rules();
self
}
/// Raises one structured-body field to at least `level`.
///
/// # Parameters
///
/// * `name` - Body field name to canonicalize.
/// * `level` - Minimum sensitivity level.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn raise_body(mut self, name: &str, level: Sensitivity) -> Self {
self.body = self.body.raise(name, level);
self
}
/// Replaces one structured-body field's sensitivity with `level`.
///
/// # Parameters
///
/// * `name` - Body field name to canonicalize.
/// * `level` - Explicit replacement sensitivity.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn override_body(mut self, name: &str, level: Sensitivity) -> Self {
self.body = self.body.override_level(name, level);
self
}
/// Allows one exact structured-body field name to remain visible.
///
/// # Parameters
///
/// * `name` - Exact body field name to allow after canonicalization.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn allow_body_exact(mut self, name: &str) -> Self {
self.body = self.body.allow_exact(name);
self
}
/// Allows one structured-body field at token-suffix boundaries.
///
/// # Parameters
///
/// * `name` - Body field suffix to allow after canonicalization.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn allow_body_suffix(mut self, name: &str) -> Self {
self.body = self.body.allow_suffix(name);
self
}
/// Removes one exact structured-body allow rule.
///
/// # Parameters
///
/// * `name` - Body-field name whose exact allow rule is removed.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn remove_body_allow_exact(mut self, name: &str) -> Self {
self.body = self.body.remove_allow_exact(name);
self
}
/// Removes one token-suffix structured-body allow rule.
///
/// # Parameters
///
/// * `name` - Body-field suffix whose allow rule is removed.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn remove_body_allow_suffix(mut self, name: &str) -> Self {
self.body = self.body.remove_allow_suffix(name);
self
}
/// Removes every structured-body allow rule.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub fn clear_body_allow_rules(mut self) -> Self {
self.body = self.body.clear_allow_rules();
self
}
/// Replaces the URL path visibility choice.
///
/// # Parameters
///
/// * `policy` - Visibility behavior for non-root URL paths.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub const fn url_path_policy(mut self, policy: UrlPathPolicy) -> Self {
self.url_path_policy = policy;
self
}
/// Replaces the opaque text-body visibility choice.
///
/// # Parameters
///
/// * `policy` - Visibility behavior for opaque UTF-8 body text.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub const fn text_body_policy(mut self, policy: TextBodyPolicy) -> Self {
self.text_body_policy = policy;
self
}
/// Replaces the unkeyed JSON scalar visibility choice.
///
/// # Parameters
///
/// * `policy` - Visibility behavior for JSON values without field names.
///
/// # Returns
///
/// The updated builder.
#[inline]
pub const fn unkeyed_json_value_policy(
mut self,
policy: UnkeyedJsonValuePolicy,
) -> Self {
self.unkeyed_json_value_policy = policy;
self
}
/// Replaces the finite hard body limits.
///
/// # Parameters
///
/// * `budget` - Previously checked parser-input and output byte limits.
///
/// # Returns
///
/// The updated builder.
#[inline(always)]
pub const fn body_budget(mut self, budget: BodyBudget) -> Self {
self.body_budget = budget;
self
}
/// Replaces the finite hard HTTP diagnostic limits.
///
/// # Parameters
///
/// * `budget` - Previously checked diagnostic input and output byte limits.
///
/// # Returns
///
/// The updated builder.
#[inline(always)]
pub fn diagnostic_budget(mut self, budget: DiagnosticBudget) -> Self {
self.diagnostic_budget = budget;
self
}
/// Validates all field rules and builds the complete HTTP policy.
///
/// # Returns
///
/// A complete immutable HTTP policy snapshot.
///
/// # Errors
///
/// Returns the first [`PolicyError`] found while validating the header,
/// query, and body policy builders in that order.
pub fn build(self) -> Result<HttpRedactionPolicy, PolicyError> {
let header = self.header.build()?;
let query = self.query.build()?;
let body = self.body.build()?;
Ok(HttpRedactionPolicy::from_parts(
header,
query,
body,
self.url_path_policy,
self.text_body_policy,
self.unkeyed_json_value_policy,
self.body_budget,
)
.with_diagnostic_budget(self.diagnostic_budget))
}
}
impl Default for HttpRedactionPolicyBuilder {
/// Creates the same empty construction state as [`Self::new`].
///
/// # Returns
///
/// A builder with empty field policies and default HTTP behavior.
fn default() -> Self {
Self::new()
}
}