111 lines
3.6 KiB
C#
111 lines
3.6 KiB
C#
using Sentry;
|
|||
|
|
|
||
|
|
namespace Heimdall.Web.Security;
|
||
|
|
|
||
|
|
/// <summary>Removes authentication material from request data before error telemetry is sent.</summary>
|
||
|
|
internal static class SentryTelemetryPrivacy
|
||
|
|
{
|
||
|
|
private const string Redacted = "[Filtered]";
|
||
|
|
|
||
|
|
internal static SentryEvent Sanitize(SentryEvent sentryEvent)
|
||
|
|
{
|
||
|
|
ArgumentNullException.ThrowIfNull(sentryEvent);
|
||
|
|
|
||
|
|
if (sentryEvent.Request is { } request)
|
||
|
|
{
|
||
|
|
RedactStringValues(request.Headers);
|
||
|
|
request.Cookies = null;
|
||
|
|
request.Url = StripQueryAndUserInfo(request.Url);
|
||
|
|
request.QueryString = null;
|
||
|
|
request.Data = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (sentryEvent.Extra is IDictionary<string, object?> extra)
|
||
|
|
RedactObjectValues(extra);
|
||
|
|
|
||
|
|
return sentryEvent;
|
||
|
|
}
|
||
|
|
|
||
|
|
internal static Breadcrumb SanitizeBreadcrumb(Breadcrumb breadcrumb)
|
||
|
|
{
|
||
|
|
ArgumentNullException.ThrowIfNull(breadcrumb);
|
||
|
|
if (breadcrumb.Data is IDictionary<string, string> mutableData)
|
||
|
|
{
|
||
|
|
RedactStringValues(mutableData);
|
||
|
|
return breadcrumb;
|
||
|
|
}
|
||
|
|
|
||
|
|
var data = breadcrumb.Data?.ToDictionary(
|
||
|
|
pair => pair.Key,
|
||
|
|
pair => IsSensitiveName(pair.Key) ? Redacted : pair.Value,
|
||
|
|
StringComparer.Ordinal);
|
||
|
|
return new Breadcrumb(
|
||
|
|
breadcrumb.Message ?? string.Empty,
|
||
|
|
breadcrumb.Type ?? string.Empty,
|
||
|
|
data,
|
||
|
|
breadcrumb.Category,
|
||
|
|
breadcrumb.Level);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void RedactStringValues(IDictionary<string, string>? values)
|
||
|
|
{
|
||
|
|
if (values is null)
|
||
|
|
return;
|
||
|
|
|
||
|
|
foreach (var key in values.Keys.ToArray())
|
||
|
|
{
|
||
|
|
if (IsSensitiveName(key))
|
||
|
|
values[key] = Redacted;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void RedactObjectValues(IDictionary<string, object?> values)
|
||
|
|
{
|
||
|
|
foreach (var key in values.Keys.ToArray())
|
||
|
|
{
|
||
|
|
if (IsSensitiveName(key))
|
||
|
|
{
|
||
|
|
values[key] = Redacted;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (values[key] is IDictionary<string, object?> nested)
|
||
|
|
RedactObjectValues(nested);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static bool IsSensitiveName(string name)
|
||
|
|
{
|
||
|
|
var normalized = name.Replace("-", string.Empty, StringComparison.Ordinal)
|
||
|
|
.Replace("_", string.Empty, StringComparison.Ordinal);
|
||
|
|
|
||
|
|
return normalized.Contains("authorization", StringComparison.OrdinalIgnoreCase) ||
|
||
|
|
normalized.Contains("apikey", StringComparison.OrdinalIgnoreCase) ||
|
||
|
|
normalized.Contains("token", StringComparison.OrdinalIgnoreCase) ||
|
||
|
|
normalized.Contains("secret", StringComparison.OrdinalIgnoreCase) ||
|
||
|
|
normalized.Contains("password", StringComparison.OrdinalIgnoreCase) ||
|
||
|
|
normalized.Contains("credential", StringComparison.OrdinalIgnoreCase) ||
|
||
|
|
normalized.Contains("cookie", StringComparison.OrdinalIgnoreCase);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static string? StripQueryAndUserInfo(string? url)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(url))
|
||
|
|
return url;
|
||
|
|
|
||
|
|
if (Uri.TryCreate(url, UriKind.Absolute, out var uri))
|
||
|
|
{
|
||
|
|
var builder = new UriBuilder(uri)
|
||
|
|
{
|
||
|
|
UserName = string.Empty,
|
||
|
|
Password = string.Empty,
|
||
|
|
Query = string.Empty,
|
||
|
|
Fragment = string.Empty,
|
||
|
|
};
|
||
|
|
return builder.Uri.GetLeftPart(UriPartial.Path);
|
||
|
|
}
|
||
|
|
|
||
|
|
var queryStart = url.IndexOfAny(['?', '#']);
|
||
|
|
return queryStart >= 0 ? url[..queryStart] : url;
|
||
|
|
}
|
||
|
|
}
|