security: sanitize Sentry transactions before send (#411)
Heimdall Gitea / Detect CI scope (pull_request) Successful in 7s
Heimdall Gitea / Build and tests (pull_request) Failing after 22m37s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (pull_request) Skipped
Heimdall Gitea / Publish versioned installer release (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Failing after 3s
Heimdall Gitea / Detect CI scope (pull_request) Successful in 7s
Heimdall Gitea / Build and tests (pull_request) Failing after 22m37s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (pull_request) Skipped
Heimdall Gitea / Publish versioned installer release (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Failing after 3s
This commit is contained in:
1 parent
05dbcbdc69
commit
fc36b38c6c
5 files changed
+191
-13
No files matched your search
@@ -11,14 +11,8 @@ internal static class SentryTelemetryPrivacy
|
||||
{
|
||||
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;
|
||||
}
|
||||
SanitizeRequest(sentryEvent.Request);
|
||||
SanitizeContexts(sentryEvent.Contexts);
|
||||
|
||||
if (sentryEvent.Extra is IDictionary<string, object?> extra)
|
||||
RedactObjectValues(extra);
|
||||
@@ -26,6 +20,39 @@ internal static class SentryTelemetryPrivacy
|
||||
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<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;
|
||||
}
|
||||
|
||||
internal static Breadcrumb SanitizeBreadcrumb(Breadcrumb breadcrumb)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(breadcrumb);
|
||||
@@ -59,6 +86,34 @@ internal static class SentryTelemetryPrivacy
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private static void RedactObjectValues(IDictionary<string, object?> values)
|
||||
{
|
||||
foreach (var key in values.Keys.ToArray())
|
||||
@@ -74,6 +129,23 @@ internal static class SentryTelemetryPrivacy
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsSensitiveName(string name)
|
||||
{
|
||||
var normalized = name.Replace("-", string.Empty, StringComparison.Ordinal)
|
||||
|
||||
Reference in new issue
Block a user