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
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Shared construction kernel for application rules and floors.
use std::collections::{
BTreeMap,
BTreeSet,
};
use super::{
FieldNameMatching,
PolicyError,
PolicyLocation,
SensitiveFieldPreset,
Sensitivity,
UnknownFieldPolicy,
internal::{
RedactionPolicyInner,
canonicalize_field_name,
},
};
#[derive(Debug, Clone)]
pub(crate) struct RedactionRulesBuilder {
sensitive: BTreeMap<String, Sensitivity>,
allow_exact: BTreeSet<String>,
allow_suffix: BTreeSet<String>,
matching: FieldNameMatching,
unknown_field_policy: UnknownFieldPolicy,
location: PolicyLocation,
}
impl RedactionRulesBuilder {
/// Creates an empty rules builder for one policy location.
///
/// # Parameters
///
/// * `location` - Policy location used when reporting validation errors.
pub(crate) fn empty(location: PolicyLocation) -> Self {
Self {
sensitive: BTreeMap::new(),
allow_exact: BTreeSet::new(),
allow_suffix: BTreeSet::new(),
matching: FieldNameMatching::ExactOrTokenSuffix,
unknown_field_policy: UnknownFieldPolicy::PassThrough,
location,
}
}
/// Copies rule configuration from an immutable policy snapshot.
///
/// # Parameters
///
/// * `inner` - Immutable rule state to copy.
/// * `location` - Policy location used for later validation errors.
pub(crate) fn from_inner(
inner: &RedactionPolicyInner,
location: PolicyLocation,
) -> Self {
Self {
sensitive: inner.sensitive.clone(),
allow_exact: inner.allow_exact.clone(),
allow_suffix: inner.allow_suffix.clone(),
matching: inner.matching,
unknown_field_policy: inner.unknown_field_policy,
location,
}
}
/// Sets the field-name matching mode.
///
/// # Parameters
///
/// * `matching` - Matching mode used for subsequent field lookups.
pub(crate) fn matching(&mut self, matching: FieldNameMatching) {
self.matching = matching;
}
/// Sets the fallback behavior for fields without an explicit rule.
///
/// # Parameters
///
/// * `policy` - Fallback behavior for unknown fields.
pub(crate) fn unknown_field_policy(&mut self, policy: UnknownFieldPolicy) {
self.unknown_field_policy = policy;
}
/// Adds every field rule supplied by a built-in sensitive-field preset.
///
/// # Parameters
///
/// * `preset` - Preset whose rules are added.
///
/// # Panics
///
/// Panics if a built-in preset contains an invalid field name.
pub(crate) fn include_preset(&mut self, preset: SensitiveFieldPreset) {
for &(field, level) in preset.fields() {
self.raise(field, level)
.expect("built-in sensitive field presets must be valid");
}
}
/// Raises a field's configured sensitivity without lowering an existing
/// one.
///
/// # Parameters
///
/// * `field` - Field name to canonicalize and update.
/// * `level` - Minimum sensitivity to apply.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when `field` has no canonical
/// field name.
pub(crate) fn raise(
&mut self,
field: &str,
level: Sensitivity,
) -> Result<(), PolicyError> {
let field = self.canonical_field(field)?;
self.sensitive
.entry(field)
.and_modify(|old| *old = (*old).max(level))
.or_insert(level);
Ok(())
}
/// Replaces a field's configured sensitivity.
///
/// # Parameters
///
/// * `field` - Field name to canonicalize and update.
/// * `level` - Sensitivity to apply.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when `field` has no canonical
/// field name.
pub(crate) fn override_level(
&mut self,
field: &str,
level: Sensitivity,
) -> Result<(), PolicyError> {
let field = self.canonical_field(field)?;
self.sensitive.insert(field, level);
Ok(())
}
/// Adds an exact allow rule for a field.
///
/// # Parameters
///
/// * `field` - Field name to allow after canonicalization.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when `field` has no canonical
/// field name.
pub(crate) fn allow_canonical_exact(
&mut self,
field: &str,
) -> Result<(), PolicyError> {
let field = self.canonical_field(field)?;
self.allow_exact.insert(field);
Ok(())
}
/// Adds a token-suffix allow rule for a field.
///
/// # Parameters
///
/// * `field` - Field-name suffix to allow after canonicalization.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when `field` has no canonical
/// field name.
pub(crate) fn allow_suffix(
&mut self,
field: &str,
) -> Result<(), PolicyError> {
let field = self.canonical_field(field)?;
self.allow_suffix.insert(field);
Ok(())
}
/// Removes an exact allow rule for a field.
///
/// # Parameters
///
/// * `field` - Field name to remove after canonicalization.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when `field` has no canonical
/// field name.
pub(crate) fn remove_allow_canonical_exact(
&mut self,
field: &str,
) -> Result<(), PolicyError> {
let field = self.canonical_field(field)?;
self.allow_exact.remove(&field);
Ok(())
}
/// Removes a token-suffix allow rule for a field.
///
/// # Parameters
///
/// * `field` - Field-name suffix to remove after canonicalization.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when `field` has no canonical
/// field name.
pub(crate) fn remove_allow_suffix(
&mut self,
field: &str,
) -> Result<(), PolicyError> {
let field = self.canonical_field(field)?;
self.allow_suffix.remove(&field);
Ok(())
}
/// Removes all exact and suffix allow rules.
pub(crate) fn clear_allow_rules(&mut self) {
self.allow_exact.clear();
self.allow_suffix.clear();
}
/// Validates a field name at a specific policy location.
///
/// # Parameters
///
/// * `field` - Field name to canonicalize and validate.
/// * `location` - Location attached to any validation error.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when `field` has no canonical
/// field name.
pub(crate) fn validate_field_name(
field: &str,
location: PolicyLocation,
) -> Result<(), PolicyError> {
Self::checked_canonical_field(field, location).map(|_| ())
}
/// Builds immutable rule state from the accumulated configuration.
///
/// # Returns
///
/// The immutable rule state used by policy snapshots.
pub(crate) fn build_inner(
self,
) -> Result<RedactionPolicyInner, PolicyError> {
Ok(RedactionPolicyInner {
sensitive: self.sensitive,
allow_exact: self.allow_exact,
allow_suffix: self.allow_suffix,
matching: self.matching,
unknown_field_policy: self.unknown_field_policy,
})
}
/// Canonicalizes and validates a field using this builder's location.
///
/// # Parameters
///
/// * `field` - Field name to canonicalize and validate.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when `field` has no canonical
/// field name.
fn canonical_field(&self, field: &str) -> Result<String, PolicyError> {
Self::checked_canonical_field(field, self.location)
}
/// Canonicalizes a field and attaches `location` to validation errors.
///
/// # Parameters
///
/// * `field` - Field name to canonicalize.
/// * `location` - Location attached to any validation error.
///
/// # Errors
///
/// Returns [`PolicyError::EmptyFieldName`] when canonicalization produces
/// an empty field name.
fn checked_canonical_field(
field: &str,
location: PolicyLocation,
) -> Result<String, PolicyError> {
let canonical = canonicalize_field_name(field);
if canonical.is_empty() {
Err(PolicyError::EmptyFieldName { location })
} else {
Ok(canonical.into_owned())
}
}
}