using System; using System.Text.RegularExpressions; namespace Cleanuparr.Shared.Helpers; /// /// Helpers for sensitive data masking in API responses and request handling. /// public static partial class SensitiveDataHelper { /// /// The placeholder string used to mask sensitive data in API responses. /// When this value is detected in an update request, the existing DB value is preserved. /// public const string Placeholder = "••••••••"; /// /// Returns true if the given value contains the sensitive data placeholder. /// Uses Contains (not Equals) to handle Apprise URLs like "discord://••••••••". /// public static bool IsPlaceholder(this string? value) => value is not null && value.Contains(Placeholder, StringComparison.Ordinal); /// /// Masks Apprise service URLs by preserving only the scheme. /// Input: "discord://token slack://tokenA/tokenB" /// Output: "discord://•••••••• slack://••••••••" /// public static string? MaskAppriseUrls(string? serviceUrls) { if (string.IsNullOrWhiteSpace(serviceUrls)) { return serviceUrls; } return AppriseUrlPattern().Replace(serviceUrls, match => { var scheme = match.Groups[1].Value; return $"{scheme}://{Placeholder}"; }); } [GeneratedRegex(@"([a-zA-Z][a-zA-Z0-9+.\-]*)://\S+")] private static partial Regex AppriseUrlPattern(); }