forked from mkaring/ConfuserEx
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDiagnosticRedactor.cs
More file actions
52 lines (47 loc) · 1.91 KB
/
Copy pathDiagnosticRedactor.cs
File metadata and controls
52 lines (47 loc) · 1.91 KB
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
using System;
using System.Text;
namespace Confuser.Core.Diagnostics {
/// <summary>
/// Scrubs sensitive information from text destined for a diagnostic report.
/// </summary>
/// <remarks>
/// Diagnostic reports are meant to be pasted into public issue trackers, so any text
/// that flows into one must have the reporter's identity removed. The most common leak
/// is the user-profile path (e.g. <c>C:\Users\alice\...</c>) which appears in absolute
/// paths throughout log output and project configuration.
/// </remarks>
public static class DiagnosticRedactor {
/// <summary>
/// The placeholder substituted for the user-profile directory.
/// </summary>
public const string UserPlaceholder = "%USER%";
/// <summary>
/// Replaces every occurrence of the user-profile directory in <paramref name="text" />
/// with <see cref="UserPlaceholder" />. The match is case-insensitive because Windows
/// paths are.
/// </summary>
/// <param name="text">The text to scrub. Returned unchanged if <c>null</c> or empty.</param>
/// <param name="userProfile">The user-profile directory to redact, or <c>null</c> to skip.</param>
/// <returns>The scrubbed text.</returns>
public static string Redact(string text, string userProfile) {
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(userProfile))
return text;
return ReplaceCaseInsensitive(text, userProfile, UserPlaceholder);
}
static string ReplaceCaseInsensitive(string input, string search, string replacement) {
var sb = new StringBuilder(input.Length);
int index = 0;
while (true) {
int found = input.IndexOf(search, index, StringComparison.OrdinalIgnoreCase);
if (found < 0) {
sb.Append(input, index, input.Length - index);
break;
}
sb.Append(input, index, found - index);
sb.Append(replacement);
index = found + search.Length;
}
return sb.ToString();
}
}
}