2026-11-13 13:12:54 +01:00
|
|
|
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);
|
|
|
|
|
|
2026-11-13 13:41:08 +01:00
|
|
|
SanitizeRequest(sentryEvent.Request);
|
|
|
|
|
SanitizeContexts(sentryEvent.Contexts);
|
2026-11-13 13:12:54 +01:00
|
|
|
|
|
|
|
|
if (sentryEvent.Extra is IDictionary<string, object?> extra)
|
|
|
|
|
RedactObjectValues(extra);
|
|
|
|
|
|
|
|
|
|
return sentryEvent;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-13 13:41:08 +01:00
|
|
|
internal static SentryTransaction Sanitize(SentryTransaction transaction)
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(transaction);
|
|
|
|
|
|
|
|
|
|
SanitizeRequest(transaction.Request);
|
|
|
|
|
SanitizeContexts(transaction.Contexts);
|
|
|
|
|
RedactObjectValues(transaction.Data, transaction.SetData);
|
|
|
|
|
|
|
|
|
|
foreach (var key in transaction.Tags.Keys.ToArray())
|
|
|
|
|
{
|
|
|
|
|
if (IsSensitiveName(key))
|
|
|
|
|
transaction.SetTag(key, Redacted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (transaction.Breadcrumbs is IList<Breadcrumb> breadcrumbs)
|
|
|
|
|
{
|
|
|
|
|
for (var index = 0; index < breadcrumbs.Count; index++)
|
|
|
|
|
breadcrumbs[index] = SanitizeBreadcrumb(breadcrumbs[index]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var span in transaction.Spans)
|
|
|
|
|
{
|
|
|
|
|
RedactObjectValues(span.Data, span.SetData);
|
|
|
|
|
foreach (var key in span.Tags.Keys.ToArray())
|
|
|
|
|
{
|
|
|
|
|
if (IsSensitiveName(key))
|
|
|
|
|
span.SetTag(key, Redacted);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return transaction;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-13 13:12:54 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-13 13:41:08 +01:00
|
|
|
private static void SanitizeRequest(SentryRequest? request)
|
|
|
|
|
{
|
|
|
|
|
if (request is null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
RedactStringValues(request.Headers);
|
|
|
|
|
request.Cookies = null;
|
|
|
|
|
request.Url = StripQueryAndUserInfo(request.Url);
|
|
|
|
|
request.QueryString = null;
|
|
|
|
|
request.Data = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void SanitizeContexts(SentryContexts contexts)
|
|
|
|
|
{
|
|
|
|
|
RedactObjectValues(contexts.Trace.Data, contexts.Trace.SetData);
|
|
|
|
|
RedactStringValues(contexts.Response.Headers);
|
|
|
|
|
contexts.Response.Cookies = null;
|
|
|
|
|
contexts.Response.Data = null;
|
|
|
|
|
|
|
|
|
|
foreach (var key in contexts.Keys.ToArray())
|
|
|
|
|
{
|
|
|
|
|
if (IsSensitiveName(key))
|
|
|
|
|
contexts[key] = Redacted;
|
|
|
|
|
else if (contexts[key] is IDictionary<string, object?> nested)
|
|
|
|
|
RedactObjectValues(nested);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-13 13:12:54 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-13 13:41:08 +01:00
|
|
|
private static void RedactObjectValues(
|
|
|
|
|
IReadOnlyDictionary<string, object?> values,
|
|
|
|
|
Action<string, object?> setValue)
|
|
|
|
|
{
|
|
|
|
|
foreach (var key in values.Keys.ToArray())
|
|
|
|
|
{
|
|
|
|
|
if (IsSensitiveName(key))
|
|
|
|
|
{
|
|
|
|
|
setValue(key, Redacted);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (values[key] is IDictionary<string, object?> nested)
|
|
|
|
|
RedactObjectValues(nested);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-13 13:12:54 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|