arbiter-audit 0.0.32

Structured audit logging with argument redaction for the Arbiter proxy
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
//! Argument redaction for sensitive fields.
//!
//! Walks a JSON value tree and replaces any object key matching a configured
//! pattern with `"[REDACTED]"`. Patterns use case-insensitive word-boundary
//! matching (letters only) compiled to regexes, so `key` matches `api_key`
//! but not `monkey` or `keyboard`.

use regex::Regex;
use serde::{Deserialize, Serialize};

/// Placeholder text inserted in place of redacted values.
pub const REDACTED: &str = "[REDACTED]";

/// Configuration for argument redaction.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedactionConfig {
    /// Case-insensitive patterns matched against JSON object keys using
    /// letter-boundary matching. A pattern matches when it is not surrounded
    /// by letters on both sides (underscores, hyphens, digits, and string
    /// boundaries act as separators).
    pub patterns: Vec<String>,
}

impl Default for RedactionConfig {
    fn default() -> Self {
        // Expanded default redaction patterns to cover common variants.
        Self {
            patterns: vec![
                "password".into(),
                "passwd".into(),
                "pwd".into(),
                "token".into(),
                "access_token".into(),
                "refresh_token".into(),
                "secret".into(),
                "client_secret".into(),
                "key".into(),
                "api_key".into(),
                "apikey".into(),
                "api-key".into(),
                "authorization".into(),
                "auth".into(),
                "credential".into(),
                "cred".into(),
                "private".into(),
                "private_key".into(),
                "ssn".into(),
                "social_security".into(),
                "credit_card".into(),
                "card_number".into(),
                "cvv".into(),
                "cvc".into(),
            ],
        }
    }
}

/// Pre-compiled redaction patterns for efficient per-request redaction.
///
/// Compile once at startup via [`RedactionConfig::compile`] and reuse across
/// requests. Previously, regexes were compiled on every call to
/// [`redact_arguments`], adding unnecessary CPU overhead under load.
#[derive(Debug, Clone)]
pub struct CompiledRedaction {
    patterns: Vec<Regex>,
}

impl CompiledRedaction {
    /// Redact sensitive fields using pre-compiled patterns.
    pub fn redact(&self, value: &serde_json::Value) -> serde_json::Value {
        redact_value(value, &self.patterns)
    }
}

impl RedactionConfig {
    /// Pre-compile patterns into regexes for reuse across requests.
    pub fn compile(&self) -> CompiledRedaction {
        let patterns = self
            .patterns
            .iter()
            .filter_map(|p| Regex::new(&format!("(?i){}", regex::escape(p))).ok())
            .collect();
        CompiledRedaction { patterns }
    }
}

/// Redact sensitive fields in a JSON value based on the given configuration.
///
/// Object keys matching any pattern (case-insensitive, letter-boundary) have
/// their values replaced with `"[REDACTED]"`. The walk is recursive through
/// objects and arrays.
///
/// For hot-path usage, prefer [`RedactionConfig::compile`] + [`CompiledRedaction::redact`]
/// to avoid recompiling regexes on every call.
pub fn redact_arguments(value: &serde_json::Value, config: &RedactionConfig) -> serde_json::Value {
    config.compile().redact(value)
}

/// Check whether a pattern match has letter-boundaries: the characters
/// immediately before and after the match must NOT be ASCII letters.
/// This prevents `key` from matching inside `monkey` or `keyboard`,
/// while still matching `api_key`, `api-key`, or standalone `key`.
fn has_letter_boundary_match(key: &str, pattern: &Regex) -> bool {
    for m in pattern.find_iter(key) {
        let before = key[..m.start()].chars().next_back();
        let after = key[m.end()..].chars().next();
        let preceded_by_letter = before.is_some_and(|c| c.is_ascii_alphabetic());
        let followed_by_letter = after.is_some_and(|c| c.is_ascii_alphabetic());
        if !preceded_by_letter && !followed_by_letter {
            return true;
        }
    }
    false
}

fn redact_value(value: &serde_json::Value, patterns: &[Regex]) -> serde_json::Value {
    match value {
        serde_json::Value::Object(map) => {
            let mut redacted = serde_json::Map::new();
            for (k, v) in map {
                if patterns.iter().any(|p| has_letter_boundary_match(k, p)) {
                    redacted.insert(k.clone(), serde_json::Value::String(REDACTED.into()));
                } else {
                    redacted.insert(k.clone(), redact_value(v, patterns));
                }
            }
            serde_json::Value::Object(redacted)
        }
        serde_json::Value::Array(arr) => {
            serde_json::Value::Array(arr.iter().map(|v| redact_value(v, patterns)).collect())
        }
        other => other.clone(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn redacts_sensitive_fields() {
        let config = RedactionConfig::default();
        let input = json!({
            "path": "/etc/hosts",
            "api_key": "sk-12345",
            "password": "hunter2",
            "nested": {
                "access_token": "abc",
                "count": 42
            }
        });

        let redacted = redact_arguments(&input, &config);

        assert_eq!(redacted["path"], "/etc/hosts");
        assert_eq!(redacted["api_key"], REDACTED);
        assert_eq!(redacted["password"], REDACTED);
        assert_eq!(redacted["nested"]["access_token"], REDACTED);
        assert_eq!(redacted["nested"]["count"], 42);
    }

    #[test]
    fn redaction_is_case_insensitive() {
        let config = RedactionConfig {
            patterns: vec!["secret".into()],
        };
        let input = json!({
            "SECRET_VALUE": "classified",
            "my_Secret": "also classified",
            "public": "visible"
        });

        let redacted = redact_arguments(&input, &config);

        assert_eq!(redacted["SECRET_VALUE"], REDACTED);
        assert_eq!(redacted["my_Secret"], REDACTED);
        assert_eq!(redacted["public"], "visible");
    }

    #[test]
    fn redacts_inside_arrays() {
        let config = RedactionConfig {
            patterns: vec!["token".into()],
        };
        let input = json!([
            {"token": "abc", "id": 1},
            {"token": "def", "id": 2}
        ]);

        let redacted = redact_arguments(&input, &config);
        let arr = redacted.as_array().unwrap();

        assert_eq!(arr[0]["token"], REDACTED);
        assert_eq!(arr[0]["id"], 1);
        assert_eq!(arr[1]["token"], REDACTED);
    }

    #[test]
    fn empty_patterns_redact_nothing() {
        let config = RedactionConfig { patterns: vec![] };
        let input = json!({"password": "hunter2", "secret": "x"});
        let redacted = redact_arguments(&input, &config);

        assert_eq!(redacted["password"], "hunter2");
        assert_eq!(redacted["secret"], "x");
    }

    #[test]
    fn scalar_values_pass_through() {
        let config = RedactionConfig::default();
        let input = json!("just a string");
        assert_eq!(redact_arguments(&input, &config), json!("just a string"));

        let input = json!(42);
        assert_eq!(redact_arguments(&input, &config), json!(42));
    }

    // -----------------------------------------------------------------------
    // Redaction over-match (substring matching behavior)
    // -----------------------------------------------------------------------

    /// Redaction uses word-boundary matching (letter-boundary): pattern "key"
    /// matches "api_key" and "key_id" (separated by underscore) but NOT
    /// "monkey" or "keyboard" (embedded in other letters).
    #[test]
    fn redaction_is_word_boundary_match() {
        let config = RedactionConfig {
            patterns: vec!["key".into()],
        };
        let input = json!({
            "api_key": "secret-1",
            "key_id": "secret-2",
            "monkey": "banana",
            "keyboard": "qwerty",
            "unrelated": "visible"
        });

        let redacted = redact_arguments(&input, &config);

        // Fields where "key" appears at a word boundary are redacted.
        assert_eq!(
            redacted["api_key"], REDACTED,
            "api_key has 'key' at boundary"
        );
        assert_eq!(redacted["key_id"], REDACTED, "key_id has 'key' at boundary");

        // Fields where "key" is embedded in other letters are NOT redacted.
        assert_eq!(
            redacted["monkey"], "banana",
            "monkey should not be redacted"
        );
        assert_eq!(
            redacted["keyboard"], "qwerty",
            "keyboard should not be redacted"
        );

        // Fields without "key" are left alone.
        assert_eq!(redacted["unrelated"], "visible");
    }

    /// Pattern "token" matches "tokelau_island" because "token" is NOT a
    /// substring of "tokelau_island" (different letters: "token" vs "tokel").
    /// But it DOES match "tokenizer", "access_token", etc.
    #[test]
    fn redaction_does_not_match_unrelated() {
        let config = RedactionConfig {
            patterns: vec!["token".into()],
        };
        let input = json!({
            "access_token": "secret",
            "token_type": "bearer",
            "tokelau_island": "pacific",
            "notation": "musical"
        });

        let redacted = redact_arguments(&input, &config);

        // "access_token" and "token_type" contain "token" -> redacted.
        assert_eq!(redacted["access_token"], REDACTED);
        assert_eq!(redacted["token_type"], REDACTED);

        // "tokelau_island" does NOT contain "token" -> NOT redacted.
        assert_eq!(redacted["tokelau_island"], "pacific");

        // "notation" does NOT contain "token" -> NOT redacted.
        assert_eq!(redacted["notation"], "musical");
    }

    // -----------------------------------------------------------------------
    // Deeply nested JSON redaction (no stack overflow)
    // -----------------------------------------------------------------------

    #[test]
    fn deeply_nested_json_redaction() {
        let config = RedactionConfig {
            patterns: vec!["secret".into()],
        };

        // Build 10 levels of nesting: {"level": {"level": ... {"secret": "value"}}}
        let mut value = json!({"secret": "deep-secret-value", "visible": "ok"});
        for _ in 0..10 {
            value = json!({"level": value});
        }

        let redacted = redact_arguments(&value, &config);

        // Walk down 10 levels to verify the deeply nested "secret" was redacted.
        let mut current = &redacted;
        for _ in 0..10 {
            current = &current["level"];
        }
        assert_eq!(
            current["secret"], REDACTED,
            "deeply nested 'secret' field must be redacted"
        );
        assert_eq!(
            current["visible"], "ok",
            "non-secret field at depth must be preserved"
        );
    }

    #[test]
    fn does_not_redact_non_sensitive_substrings() {
        let config = RedactionConfig::default();
        let input = json!({
            "keyboard": "mechanical",
            "monkey": "curious george",
            "author": "Jane Doe",
            "authenticate_method": "oauth2"
        });
        let redacted = redact_arguments(&input, &config);
        assert_eq!(
            redacted["keyboard"], "mechanical",
            "keyboard should not be redacted"
        );
        assert_eq!(
            redacted["monkey"], "curious george",
            "monkey should not be redacted"
        );
        assert_eq!(
            redacted["author"], "Jane Doe",
            "author should not be redacted"
        );
        assert_eq!(
            redacted["authenticate_method"], "oauth2",
            "authenticate_method should not be redacted"
        );
    }

    #[test]
    fn still_redacts_sensitive_compound_fields() {
        let config = RedactionConfig::default();
        let input = json!({
            "api_key": "sk-12345",
            "api-key": "sk-67890",
            "x-auth-token": "bearer-abc",
            "user_password": "hunter2"
        });
        let redacted = redact_arguments(&input, &config);
        assert_eq!(redacted["api_key"], "[REDACTED]");
        assert_eq!(redacted["api-key"], "[REDACTED]");
        assert_eq!(redacted["x-auth-token"], "[REDACTED]");
        assert_eq!(redacted["user_password"], "[REDACTED]");
    }

    // ── RT-206: CompiledRedaction direct tests ────────────────────────

    #[test]
    fn compiled_redaction_matches_redact_arguments() {
        let config = RedactionConfig::default();
        let compiled = config.compile();
        let input = json!({
            "path": "/etc/hosts",
            "api_key": "sk-12345",
            "password": "hunter2",
            "nested": {
                "access_token": "abc",
                "count": 42
            }
        });

        let result_compiled = compiled.redact(&input);
        let result_wrapper = redact_arguments(&input, &config);
        assert_eq!(
            result_compiled, result_wrapper,
            "compiled and wrapper should produce identical output"
        );
    }

    #[test]
    fn compiled_redaction_reusable_across_calls() {
        let config = RedactionConfig {
            patterns: vec!["secret".into(), "key".into()],
        };
        let compiled = config.compile();

        let input1 = json!({"secret": "val1", "public": "ok"});
        let input2 = json!({"api_key": "val2", "name": "test"});

        let r1 = compiled.redact(&input1);
        let r2 = compiled.redact(&input2);

        assert_eq!(r1["secret"], REDACTED);
        assert_eq!(r1["public"], "ok");
        assert_eq!(r2["api_key"], REDACTED);
        assert_eq!(r2["name"], "test");
    }

    #[test]
    fn compiled_redaction_empty_patterns() {
        let config = RedactionConfig { patterns: vec![] };
        let compiled = config.compile();
        let input = json!({"password": "hunter2", "secret": "x"});
        let redacted = compiled.redact(&input);
        assert_eq!(redacted["password"], "hunter2");
        assert_eq!(redacted["secret"], "x");
    }
}