From 277ef90d418288bbdbe270dd72f2f3a5039fd3c6 Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 6 Sep 2026 10:30:00 +0900 Subject: [PATCH] =?UTF-8?q?=E5=AE=9F=E8=A1=8C=E6=99=82=E3=81=AE=E3=83=91?= =?UTF-8?q?=E3=82=B9=E3=81=A8=E5=88=A9=E7=94=A8=E8=80=85=E5=90=8D=E3=82=92?= =?UTF-8?q?=E8=90=BD=E3=81=A8=E3=81=99=E9=99=A4=E5=8E=BB=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Telemetry/TextScrubber.cs | 109 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 Telemetry/TextScrubber.cs diff --git a/Telemetry/TextScrubber.cs b/Telemetry/TextScrubber.cs new file mode 100644 index 0000000..a09bcc4 --- /dev/null +++ b/Telemetry/TextScrubber.cs @@ -0,0 +1,109 @@ +using System.Text.RegularExpressions; + +namespace Telemetry; + +internal static partial class TextScrubber +{ + private const int MinimumReplaceableLength = 3; + + private const string UserPlaceholder = "[user]"; + private const string UserProfilePlaceholder = "[userprofile]"; + private const string AppDataPlaceholder = "[appdata]"; + private const string LocalAppDataPlaceholder = "[localappdata]"; + private const string DomainPlaceholder = "[domain]"; + private const string MachinePlaceholder = "[machine]"; + private const string MailPlaceholder = "[mail]"; + private const string AddressPlaceholder = "[address]"; + + private static readonly Lazy>> LiteralReplacements = + new(BuildLiteralReplacements, true); + + public static string? Scrub(string? value) + { + if (string.IsNullOrEmpty(value)) + { + return value; + } + + var result = value; + + foreach (var (literal, placeholder) in LiteralReplacements.Value) + { + if (result.Contains(literal, StringComparison.OrdinalIgnoreCase)) + { + result = result.Replace(literal, placeholder, StringComparison.OrdinalIgnoreCase); + } + } + + result = WindowsUserDirectory().Replace(result, "$1:\\Users\\" + UserPlaceholder); + result = UnixHomeDirectory().Replace(result, "/home/" + UserPlaceholder); + result = MailAddress().Replace(result, MailPlaceholder); + result = InternetAddress().Replace(result, AddressPlaceholder); + + return result; + } + + private static IReadOnlyList> BuildLiteralReplacements() => SelectReplacements( + [ + new KeyValuePair(ReadFolder(Environment.SpecialFolder.UserProfile), UserProfilePlaceholder), + new KeyValuePair(ReadVariable("USERPROFILE"), UserProfilePlaceholder), + new KeyValuePair(ReadFolder(Environment.SpecialFolder.ApplicationData), AppDataPlaceholder), + new KeyValuePair(ReadFolder(Environment.SpecialFolder.LocalApplicationData), LocalAppDataPlaceholder), + new KeyValuePair(ReadVariable("USERDOMAIN"), DomainPlaceholder), + new KeyValuePair(Read(static () => Environment.UserName), UserPlaceholder), + new KeyValuePair(Read(static () => Environment.MachineName), MachinePlaceholder), + ]); + + internal static IReadOnlyList> SelectReplacements( + IReadOnlyList> candidates) + { + ArgumentNullException.ThrowIfNull(candidates); + + var replacements = new List>(candidates.Count); + + foreach (var (literal, placeholder) in candidates) + { + if (literal is null + || literal.Length < MinimumReplaceableLength + || replacements.Exists(existing => string.Equals(existing.Key, literal, StringComparison.OrdinalIgnoreCase))) + { + continue; + } + + replacements.Add(new KeyValuePair(literal, placeholder)); + } + + replacements.Sort(static (left, right) => right.Key.Length.CompareTo(left.Key.Length)); + return replacements; + } + + private static string? ReadFolder(Environment.SpecialFolder folder) => + Read(() => Environment.GetFolderPath(folder, Environment.SpecialFolderOption.DoNotVerify)); + + private static string? ReadVariable(string name) => Read(() => Environment.GetEnvironmentVariable(name)); + + private static string? Read(Func reader) + { + try + { + var value = reader(); + return string.IsNullOrWhiteSpace(value) ? null : value; + } + catch (Exception) + { + return null; + } + } + + [GeneratedRegex(@"([A-Za-z]):\\Users\\[^\\/:*?""<>|\r\n]+", RegexOptions.CultureInvariant)] + private static partial Regex WindowsUserDirectory(); + + [GeneratedRegex(@"/home/[^/\s]+", RegexOptions.CultureInvariant)] + private static partial Regex UnixHomeDirectory(); + + [GeneratedRegex(@"[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}", RegexOptions.CultureInvariant)] + private static partial Regex MailAddress(); + + [GeneratedRegex(@"\b(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\b", RegexOptions.CultureInvariant)] + private static partial Regex InternetAddress(); +}