security: redact Sentry request credentials (#411)
Heimdall Gitea / Detect CI scope (pull_request) Successful in 5s
Heimdall Gitea / Build and tests (pull_request) Successful in 19m42s
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (pull_request) Skipped
Heimdall Gitea / Build and scan server image (pull_request) Successful in 15m43s
Heimdall Gitea / Publish versioned installer release (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Successful in 2s

This commit is contained in:
arnaud.dartois committed 2026-11-13 13:12:54 +01:00
1 parent 04b3279d1f
commit 05dbcbdc69
5 files changed
+289 -8

No files matched your search

@@ -0,0 +1,111 @@
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;
}
}