| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Text.RegularExpressions; |
| | | 4 | | |
| | | 5 | | namespace Intentum.Core.Behavior; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Options for sanitizing a behavior space (actor/action/metadata masking) for GDPR/CCPA or privacy. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <param name="MaskActor">When true, replace actor with a hash or placeholder.</param> |
| | | 11 | | /// <param name="MaskAction">When true, replace action with a hash or placeholder.</param> |
| | | 12 | | /// <param name="MetadataKeysToRedact">Metadata keys to redact (value replaced with "[redacted]"). When null, no metadat |
| | | 13 | | /// <param name="UseHash">When true, use SHA256 hash (truncated) for masked values; when false, use Placeholder.</param> |
| | | 14 | | /// <param name="Placeholder">Placeholder string when UseHash is false (e.g. "***").</param> |
| | | 15 | | /// <param name="MaskEmails">When true, detect and mask email addresses in metadata values.</param> |
| | | 16 | | /// <param name="MaskPhoneNumbers">When true, detect and mask phone numbers in metadata values.</param> |
| | | 17 | | public sealed record SanitizationOptions( |
| | | 18 | | bool MaskActor = true, |
| | | 19 | | bool MaskAction = true, |
| | | 20 | | IReadOnlyList<string>? MetadataKeysToRedact = null, |
| | | 21 | | bool UseHash = true, |
| | | 22 | | string Placeholder = "***", |
| | | 23 | | bool MaskEmails = false, |
| | | 24 | | bool MaskPhoneNumbers = false); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Extension methods for sanitizing (anonymizing/masking) a behavior space before vector production or inference. |
| | | 28 | | /// Use for GDPR/CCPA compliance: mask PII in actor/action/metadata before sending to external models or storage. |
| | | 29 | | /// </summary> |
| | | 30 | | public static class BehaviorSpaceSanitization |
| | | 31 | | { |
| | 1 | 32 | | private static readonly Regex EmailRegex = new(@"[\w.+-]+@[\w-]+\.[\w.]+", RegexOptions.Compiled, TimeSpan.FromSecon |
| | 1 | 33 | | private static readonly Regex PhoneRegex = new(@"[\+]?[\d\s\-\(\)]{10,}", RegexOptions.Compiled, TimeSpan.FromSecond |
| | | 34 | | /// <summary> |
| | | 35 | | /// Returns a new behavior space with actor/action/metadata sanitized according to options. |
| | | 36 | | /// Original space is not modified. |
| | | 37 | | /// </summary> |
| | | 38 | | public static BehaviorSpace Sanitize(this BehaviorSpace space, SanitizationOptions? options = null) |
| | 7 | 39 | | { |
| | 7 | 40 | | ArgumentNullException.ThrowIfNull(space); |
| | 7 | 41 | | options ??= new SanitizationOptions(); |
| | 7 | 42 | | var sanitized = new BehaviorSpace(); |
| | | 43 | | |
| | 37 | 44 | | foreach (var evt in space.Events) |
| | 8 | 45 | | { |
| | 8 | 46 | | var actor = options.MaskActor ? Mask(evt.Actor, options) : evt.Actor; |
| | 8 | 47 | | var action = options.MaskAction ? Mask(evt.Action, options) : evt.Action; |
| | 8 | 48 | | var meta = RedactMetadata(evt.Metadata, options.MetadataKeysToRedact, options); |
| | 8 | 49 | | sanitized.Observe(new BehaviorEvent(actor, action, evt.OccurredAt, meta)); |
| | 8 | 50 | | } |
| | | 51 | | |
| | 7 | 52 | | CopyMetadata(sanitized, space.Metadata, options.MetadataKeysToRedact); |
| | 7 | 53 | | return sanitized; |
| | 7 | 54 | | } |
| | | 55 | | |
| | | 56 | | private static IReadOnlyDictionary<string, object>? RedactMetadata( |
| | | 57 | | IReadOnlyDictionary<string, object>? meta, |
| | | 58 | | IReadOnlyList<string>? keysToRedact, |
| | | 59 | | SanitizationOptions options) |
| | 8 | 60 | | { |
| | 8 | 61 | | if (meta == null) |
| | 4 | 62 | | return meta; |
| | 4 | 63 | | var dict = new Dictionary<string, object>(meta); |
| | 4 | 64 | | if (keysToRedact != null) |
| | 1 | 65 | | { |
| | 5 | 66 | | foreach (var key in keysToRedact.Where(dict.ContainsKey)) |
| | 1 | 67 | | dict[key] = "[redacted]"; |
| | 1 | 68 | | } |
| | 4 | 69 | | if (options.MaskEmails || options.MaskPhoneNumbers) |
| | 2 | 70 | | { |
| | 10 | 71 | | foreach (var key in dict.Keys.ToList()) |
| | 2 | 72 | | { |
| | 2 | 73 | | if (dict[key] is string value) |
| | 2 | 74 | | dict[key] = MaskPii(value, options); |
| | 2 | 75 | | } |
| | 2 | 76 | | } |
| | 4 | 77 | | return dict; |
| | 8 | 78 | | } |
| | | 79 | | |
| | | 80 | | private static string MaskPii(string value, SanitizationOptions options) |
| | 2 | 81 | | { |
| | 2 | 82 | | if (options.MaskEmails) |
| | 2 | 83 | | value = EmailRegex.Replace(value, m => MaskEmail(m.Value)); |
| | 2 | 84 | | if (options.MaskPhoneNumbers) |
| | 2 | 85 | | value = PhoneRegex.Replace(value, m => MaskPhone(m.Value)); |
| | 2 | 86 | | return value; |
| | 2 | 87 | | } |
| | | 88 | | |
| | | 89 | | private static string MaskEmail(string email) |
| | 1 | 90 | | { |
| | 1 | 91 | | var parts = email.Split('@'); |
| | 1 | 92 | | if (parts.Length != 2) return "***"; |
| | 1 | 93 | | var maskedLocal = parts[0].Length > 0 ? parts[0][0].ToString() : "***"; |
| | 1 | 94 | | return $"{maskedLocal}@***.***"; |
| | 1 | 95 | | } |
| | | 96 | | |
| | | 97 | | private static string MaskPhone(string phone) |
| | 1 | 98 | | { |
| | 1 | 99 | | if (phone.Length < 4) return "***"; |
| | 1 | 100 | | return phone[..2] + "***" + phone[^2..]; |
| | 1 | 101 | | } |
| | | 102 | | |
| | | 103 | | private static void CopyMetadata( |
| | | 104 | | BehaviorSpace sanitized, |
| | | 105 | | IReadOnlyDictionary<string, object> metadata, |
| | | 106 | | IReadOnlyList<string>? keysToRedact) |
| | 7 | 107 | | { |
| | 25 | 108 | | foreach (var kv in metadata) |
| | 2 | 109 | | { |
| | 2 | 110 | | var value = keysToRedact != null && keysToRedact.Contains(kv.Key, StringComparer.OrdinalIgnoreCase) |
| | 2 | 111 | | ? "[redacted]" |
| | 2 | 112 | | : kv.Value; |
| | 2 | 113 | | sanitized.SetMetadata(kv.Key, value); |
| | 2 | 114 | | } |
| | 7 | 115 | | } |
| | | 116 | | |
| | | 117 | | private static string Mask(string value, SanitizationOptions options) |
| | 8 | 118 | | { |
| | 8 | 119 | | if (string.IsNullOrEmpty(value)) |
| | 0 | 120 | | return value; |
| | 8 | 121 | | if (options.UseHash) |
| | 6 | 122 | | { |
| | 6 | 123 | | var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(value)); |
| | 6 | 124 | | return Convert.ToHexString(bytes)[..Math.Min(16, bytes.Length * 2)]; |
| | | 125 | | } |
| | 2 | 126 | | return options.Placeholder; |
| | 8 | 127 | | } |
| | | 128 | | } |