dd-sensitive-data-scanner 0.0.0

Core Sensitive Data Scanner library for detecting and redacting sensitive information.
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
#![allow(deprecated)]
// The module level deprecation allow is needed to suppress warnings from `MatchAction::Utf16Hash`
// that I couldn't find a specific line to suppress. It can be removed when the variant is removed.

use std::{borrow::Cow, cmp::min};

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::rule_match::ReplacementType;

#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
#[serde(tag = "type")]
pub enum MatchAction {
    /// Do not modify the input.
    #[default]
    None,
    /// Replace matches with a new string.
    Redact { replacement: String },
    /// Hash the result
    Hash,
    /// Hash the result based on UTF-16 bytes encoded match result
    #[deprecated(
        note = "Support hash from UTF-16 encoded bytes for backward compatibility. Users should use instead hash match action."
    )]
    #[cfg(any(test, feature = "utf16_hash_match_action"))]
    Utf16Hash,
    /// Replace the first or last n characters with asterisks.
    PartialRedact {
        direction: PartialRedactDirection,
        character_count: usize,
    },
}

impl MatchAction {
    pub fn redact(replacement: &str) -> Self {
        Self::Redact {
            replacement: replacement.to_string(),
        }
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum PartialRedactDirection {
    FirstCharacters,
    LastCharacters,
}

const PARTIAL_REDACT_CHARACTER: char = '*';

#[derive(Debug, PartialEq, Eq, Error)]
pub enum MatchActionValidationError {
    #[error("Partial redaction chars must be non-zero")]
    PartialRedactionNumCharsZero,
}

impl MatchAction {
    pub fn validate(&self) -> Result<(), MatchActionValidationError> {
        match self {
            MatchAction::PartialRedact {
                direction: _,
                character_count,
            } => {
                if *character_count == 0 {
                    Err(MatchActionValidationError::PartialRedactionNumCharsZero)
                } else {
                    Ok(())
                }
            }
            MatchAction::None | MatchAction::Redact { replacement: _ } | MatchAction::Hash => {
                Ok(())
            }
            #[cfg(any(test, feature = "utf16_hash_match_action"))]
            #[allow(deprecated)]
            MatchAction::Utf16Hash => Ok(()),
        }
    }

    /// If the match action will modify the content
    pub fn is_mutating(&self) -> bool {
        match self {
            MatchAction::None => false,
            MatchAction::Redact { .. } => true,
            MatchAction::Hash => true,
            #[cfg(any(test, feature = "utf16_hash_match_action"))]
            #[allow(deprecated)]
            MatchAction::Utf16Hash => true,
            MatchAction::PartialRedact { .. } => true,
        }
    }

    pub fn replacement_type(&self) -> ReplacementType {
        match self {
            MatchAction::None => ReplacementType::None,
            MatchAction::Redact { .. } => ReplacementType::Placeholder,
            MatchAction::Hash => ReplacementType::Hash,
            #[cfg(any(test, feature = "utf16_hash_match_action"))]
            #[allow(deprecated)]
            MatchAction::Utf16Hash => ReplacementType::Hash,
            MatchAction::PartialRedact { direction, .. } => match direction {
                PartialRedactDirection::FirstCharacters => ReplacementType::PartialStart,
                PartialRedactDirection::LastCharacters => ReplacementType::PartialEnd,
            },
        }
    }

    pub fn get_replacement(&self, matched_content: &str) -> Option<Replacement<'_>> {
        match self {
            MatchAction::None => None,
            MatchAction::Redact { replacement } => Some(Replacement {
                start: 0,
                end: matched_content.len(),
                replacement: Cow::Borrowed(replacement),
            }),
            MatchAction::Hash => Some(Replacement {
                start: 0,
                end: matched_content.len(),
                replacement: Cow::Owned(Self::hash(matched_content)),
            }),
            #[cfg(any(test, feature = "utf16_hash_match_action"))]
            #[allow(deprecated)]
            MatchAction::Utf16Hash => Some(Replacement {
                start: 0,
                end: matched_content.len(),
                replacement: Cow::Owned(Self::utf16_hash(matched_content)),
            }),
            MatchAction::PartialRedact {
                direction,
                character_count: num_characters,
            } => match direction {
                PartialRedactDirection::FirstCharacters => Some(Self::partial_redaction_first(
                    num_characters,
                    matched_content,
                )),
                PartialRedactDirection::LastCharacters => Some(Self::partial_redaction_last(
                    num_characters,
                    matched_content,
                )),
            },
        }
    }

    fn hash(match_result: &str) -> String {
        let hash = farmhash2::fingerprint64(match_result.as_bytes());
        format!("{hash:x}")
    }

    #[cfg(any(test, feature = "utf16_hash_match_action"))]
    fn utf16_hash(match_result: &str) -> String {
        let utf16_bytes = match_result
            .encode_utf16()
            .flat_map(u16::to_le_bytes)
            .collect::<Vec<_>>();
        let hash = farmhash2::fingerprint64(&utf16_bytes);
        format!("{hash:x}")
    }

    fn partial_redaction_first(
        num_characters: &usize,
        matched_content: &str,
    ) -> Replacement<'static> {
        let match_len = matched_content.chars().count();

        let last_replacement_byte = if match_len > *num_characters {
            matched_content
                .char_indices()
                .nth(*num_characters)
                .unwrap()
                .0
        } else {
            matched_content.len()
        };

        let replacement_length = min(*num_characters, match_len);

        Replacement {
            start: 0,
            end: last_replacement_byte,
            replacement: String::from(PARTIAL_REDACT_CHARACTER)
                .repeat(replacement_length)
                .into(),
        }
    }

    fn partial_redaction_last(num_characters: &usize, match_result: &str) -> Replacement<'static> {
        let match_len = match_result.chars().count();

        let start_replacement_byte = if match_len > *num_characters {
            match_result
                .char_indices()
                .nth_back(*num_characters - 1)
                .unwrap()
                .0
        } else {
            0
        };

        let replacement_length = min(*num_characters, match_len);

        Replacement {
            start: start_replacement_byte,
            end: match_result.len(),
            replacement: String::from(PARTIAL_REDACT_CHARACTER)
                .repeat(replacement_length)
                .into(),
        }
    }
}

#[derive(PartialEq, Debug)]
pub struct Replacement<'a> {
    pub start: usize,
    pub end: usize,
    pub replacement: Cow<'a, str>,
}

#[cfg(test)]
mod test {
    use crate::match_action::PartialRedactDirection::{FirstCharacters, LastCharacters};
    use crate::match_action::{MatchAction, Replacement};

    #[test]
    fn match_with_no_action() {
        let match_action = MatchAction::None;

        assert_eq!(match_action.get_replacement("rene coty"), None);
        assert_eq!(match_action.get_replacement("rene"), None);
    }

    #[test]
    fn match_with_redaction() {
        let match_action = MatchAction::Redact {
            replacement: "[REPLACEMENT]".to_string(),
        };

        assert_eq!(
            match_action.get_replacement("rene coty"),
            Some(Replacement {
                start: 0,
                end: 9,
                replacement: "[REPLACEMENT]".into()
            })
        );

        assert_eq!(
            match_action.get_replacement("coty"),
            Some(Replacement {
                start: 0,
                end: 4,
                replacement: "[REPLACEMENT]".into()
            })
        );
    }

    #[test]
    fn match_with_hash() {
        let match_action = MatchAction::Hash;

        assert_eq!(
            match_action.get_replacement("coty"),
            Some(Replacement {
                start: 0,
                end: 4,
                replacement: "fdf7528ad7f83901".into()
            })
        );

        assert_eq!(
            match_action.get_replacement("rene"),
            Some(Replacement {
                start: 0,
                end: 4,
                replacement: "51a2842f626aaaec".into()
            })
        );

        assert_eq!(
            match_action.get_replacement("😊"),
            Some(Replacement {
                start: 0,
                end: 4,
                replacement: "6ce17744696c2107".into()
            })
        );
    }

    #[test]
    #[cfg(feature = "utf16_hash_match_action")]
    fn match_with_utf16_hash() {
        #[allow(deprecated)]
        let match_action = MatchAction::Utf16Hash;

        assert_eq!(
            match_action.get_replacement("coty"),
            Some(Replacement {
                start: 0,
                end: 4,
                replacement: "d6bf038129a9eb52".into()
            })
        );

        assert_eq!(
            match_action.get_replacement("rene"),
            Some(Replacement {
                start: 0,
                end: 4,
                replacement: "8627c79c79ff4b8b".into()
            })
        );

        assert_eq!(
            match_action.get_replacement("😊"),
            Some(Replacement {
                start: 0,
                end: 4,
                replacement: "268a21f211fdbc0a".into()
            })
        );
    }

    #[test]
    fn match_with_partial_redaction_first_characters_should_always_redact_num_characters_max() {
        let match_action = MatchAction::PartialRedact {
            character_count: 5,
            direction: FirstCharacters,
        };

        assert_eq!(
            match_action.get_replacement("ene coty"),
            Some(Replacement {
                start: 0,
                end: 5,
                replacement: "*****".into()
            })
        );

        assert_eq!(
            match_action.get_replacement("rene"),
            Some(Replacement {
                start: 0,
                end: 4,
                replacement: "****".into()
            })
        );

        assert_eq!(
            match_action.get_replacement("rene "),
            Some(Replacement {
                start: 0,
                end: 5,
                replacement: "*****".into()
            })
        );
    }

    #[test]
    fn match_with_partial_redaction_last_characters_should_always_redact_num_characters_max() {
        let match_action = MatchAction::PartialRedact {
            character_count: 5,
            direction: LastCharacters,
        };

        assert_eq!(
            match_action.get_replacement("rene cot"),
            Some(Replacement {
                start: 3,
                end: 8,
                replacement: "*****".into()
            })
        );

        assert_eq!(
            match_action.get_replacement("rene"),
            Some(Replacement {
                start: 0,
                end: 4,
                replacement: "****".into()
            })
        );

        assert_eq!(
            match_action.get_replacement("rene "),
            Some(Replacement {
                start: 0,
                end: 5,
                replacement: "*****".into()
            })
        );
    }

    #[test]
    fn match_with_partial_redaction_should_redact_match_length_maximum() {
        let match_action = MatchAction::PartialRedact {
            character_count: 350,
            direction: FirstCharacters,
        };

        assert_eq!(
            match_action.get_replacement("rene coty"),
            Some(Replacement {
                start: 0,
                end: 9,
                replacement: "*********".into()
            })
        );

        assert_eq!(
            match_action.get_replacement("👍 rene coty"),
            Some(Replacement {
                start: 0,
                end: 14,
                replacement: "***********".into()
            })
        )
    }

    #[test]
    fn partially_redacts_first_emoji() {
        let match_action = MatchAction::PartialRedact {
            character_count: 1,
            direction: FirstCharacters,
        };

        assert_eq!(
            match_action.get_replacement("😊🤞"),
            Some(Replacement {
                start: 0,
                end: 4,
                replacement: "*".into()
            })
        );
    }

    #[test]
    fn partially_redacts_last_emoji() {
        let match_action = MatchAction::PartialRedact {
            character_count: 2,
            direction: LastCharacters,
        };

        assert_eq!(
            match_action.get_replacement("😊🤞👋"),
            Some(Replacement {
                start: 4,
                end: 12,
                replacement: "**".into()
            })
        );
    }

    #[test]
    fn test_farmhash_bugfix() {
        // Testing the bugfix from https://github.com/seiflotfy/rust-farmhash/pull/16
        assert_eq!(
            MatchAction::Hash.get_replacement(&"x".repeat(128)),
            Some(Replacement {
                start: 0,
                end: 128,
                replacement: "5170af09fd870c17".into()
            })
        );
    }
}