using Sentry;
namespace Heimdall.Web.Security;
/// Removes authentication material from request data before error telemetry is sent.
internal static class SentryTelemetryPrivacy
{
private const string Redacted = "[Filtered]";
internal static SentryEvent Sanitize(SentryEvent sentryEvent)
{
ArgumentNullException.ThrowIfNull(sentryEvent);
SanitizeRequest(sentryEvent.Request);
SanitizeContexts(sentryEvent.Contexts);
foreach (var key in sentryEvent.Tags.Keys.ToArray())
{
if (IsSensitiveName(key))
sentryEvent.SetTag(key, Redacted);
}
if (sentryEvent.Extra is IDictionary extra)
RedactObjectValues(extra);
return sentryEvent;
}
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 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;
}
internal static Breadcrumb SanitizeBreadcrumb(Breadcrumb breadcrumb)
{
ArgumentNullException.ThrowIfNull(breadcrumb);
if (breadcrumb.Data is IDictionary 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? values)
{
if (values is null)
return;
foreach (var key in values.Keys.ToArray())
{
if (IsSensitiveName(key))
values[key] = Redacted;
}
}
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 (key is "trace" or "response")
continue;
if (IsSensitiveName(key))
contexts[key] = Redacted;
else
contexts[key] = RedactValue(contexts[key])!;
}
}
private static void RedactObjectValues(IDictionary values)
{
foreach (var key in values.Keys.ToArray())
{
if (IsSensitiveName(key))
{
values[key] = Redacted;
continue;
}
values[key] = RedactValue(values[key]);
}
}
private static void RedactObjectValues(
IReadOnlyDictionary values,
Action setValue)
{
foreach (var key in values.Keys.ToArray())
{
if (IsSensitiveName(key))
{
setValue(key, Redacted);
continue;
}
setValue(key, RedactValue(values[key])!);
}
}
private static object? RedactValue(object? value)
{
if (value is IDictionary mutableDictionary)
{
RedactObjectValues(mutableDictionary);
return value;
}
if (value is IReadOnlyDictionary readOnlyDictionary)
{
var sanitized = new Dictionary(StringComparer.Ordinal);
foreach (var pair in readOnlyDictionary)
sanitized[pair.Key] = IsSensitiveName(pair.Key) ? Redacted : RedactValue(pair.Value);
return sanitized;
}
if (value is System.Collections.IDictionary dictionary)
{
var sanitized = new Dictionary(StringComparer.Ordinal);
foreach (System.Collections.DictionaryEntry entry in dictionary)
{
if (entry.Key is not string key)
return Redacted;
sanitized[key] = IsSensitiveName(key) ? Redacted : RedactValue(entry.Value);
}
return sanitized;
}
if (value is System.Collections.IEnumerable and not string)
return Redacted;
// Arbitrary objects may serialize properties containing credentials under unknown names.
// Keep primitive values; discard unsupported object graphs rather than emit them unsanitized.
return value is null or string or bool or byte or sbyte or short or ushort or int or uint or long or ulong or float or double or decimal
? value
: Redacted;
}
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;
}
}