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
|
|
|
|
2026-11-13 13:59:27 +01:00
|
|
|
foreach (var key in sentryEvent.Tags.Keys.ToArray())
|
|
|
|
|
{
|
|
|
|
|
if (IsSensitiveName(key))
|
|
|
|
|
sentryEvent.SetTag(key, Redacted);
|
|
|
|
|
}
|
|
|
|
|
|
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())
|
|
|
|
|
{
|
2026-11-13 13:59:27 +01:00
|
|
|
if (key is "trace" or "response")
|
|
|
|
|
continue;
|
|
|
|
|
|
2026-11-13 13:41:08 +01:00
|
|
|
if (IsSensitiveName(key))
|
|
|
|
|
contexts[key] = Redacted;
|
2026-11-13 13:59:27 +01:00
|
|
|
else
|
|
|
|
|
contexts[key] = RedactValue(contexts[key])!;
|
2026-11-13 13:41:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-13 13:59:27 +01:00
|
|
|
values[key] = RedactValue(values[key]);
|
2026-11-13 13:12:54 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-13 13:59:27 +01:00
|
|
|
setValue(key, RedactValue(values[key])!);
|
2026-11-13 13:41:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-13 13:59:27 +01:00
|
|
|
private static object? RedactValue(object? value)
|
|
|
|
|
{
|
|
|
|
|
if (value is IDictionary<string, object?> mutableDictionary)
|
|
|
|
|
{
|
|
|
|
|
RedactObjectValues(mutableDictionary);
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (value is IReadOnlyDictionary<string, object?> readOnlyDictionary)
|
|
|
|
|
{
|
|
|
|
|
var sanitized = new Dictionary<string, object?>(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<string, object?>(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;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|