diff --git a/src/Heimdall.Web/Security/SentryTelemetryPrivacy.cs b/src/Heimdall.Web/Security/SentryTelemetryPrivacy.cs index 3467df6..7870dd2 100644 --- a/src/Heimdall.Web/Security/SentryTelemetryPrivacy.cs +++ b/src/Heimdall.Web/Security/SentryTelemetryPrivacy.cs @@ -14,6 +14,12 @@ internal static class SentryTelemetryPrivacy 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); @@ -107,10 +113,13 @@ internal static class SentryTelemetryPrivacy foreach (var key in contexts.Keys.ToArray()) { + if (key is "trace" or "response") + continue; + if (IsSensitiveName(key)) contexts[key] = Redacted; - else if (contexts[key] is IDictionary nested) - RedactObjectValues(nested); + else + contexts[key] = RedactValue(contexts[key])!; } } @@ -124,8 +133,7 @@ internal static class SentryTelemetryPrivacy continue; } - if (values[key] is IDictionary nested) - RedactObjectValues(nested); + values[key] = RedactValue(values[key]); } } @@ -141,11 +149,48 @@ internal static class SentryTelemetryPrivacy continue; } - if (values[key] is IDictionary nested) - RedactObjectValues(nested); + 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) diff --git a/tests/Heimdall.Web.Tests/Security/SentryTelemetryPrivacyTests.cs b/tests/Heimdall.Web.Tests/Security/SentryTelemetryPrivacyTests.cs index cdd397f..b680f74 100644 --- a/tests/Heimdall.Web.Tests/Security/SentryTelemetryPrivacyTests.cs +++ b/tests/Heimdall.Web.Tests/Security/SentryTelemetryPrivacyTests.cs @@ -24,7 +24,9 @@ public sealed class SentryTelemetryPrivacyTests }, "extra": { "safeValue": "preserved", - "nested": { "clientSecret": "SENTINEL_EXTRA_SECRET_411" } + "nested": { "clientSecret": "SENTINEL_EXTRA_SECRET_411" }, + "array": [ { "accessToken": "SENTINEL_ARRAY_TOKEN_411", "safe": "preserved" } ], + "opaque": { "credential": "SENTINEL_OPAQUE_CREDENTIAL_411" } } } """); @@ -41,6 +43,8 @@ public sealed class SentryTelemetryPrivacyTests Assert.Null(sanitized.Request.Data); Assert.Equal("preserved", sanitized.Extra!["safeValue"]); Assert.Equal("[Filtered]", ((IDictionary)sanitized.Extra["nested"]!)["clientSecret"]); + Assert.Equal("[Filtered]", sanitized.Extra["array"]); + Assert.Equal("[Filtered]", ((IDictionary)sanitized.Extra["opaque"]!)["credential"]); var serialized = SerializeEvent(sanitized); Assert.DoesNotContain(apiKey, serialized, StringComparison.Ordinal); @@ -132,6 +136,22 @@ public sealed class SentryTelemetryPrivacyTests Assert.DoesNotContain("SENTINEL_", serialized, StringComparison.Ordinal); } + [Fact] + public void Sanitize_RedactsSensitiveEventTagsInSdkPayload() + { + using var document = System.Text.Json.JsonDocument.Parse(""" + { "message": "synthetic", "tags": { "api_key": "SENTINEL_EVENT_TAG_411", "method": "GET" } } + """); + var sentryEvent = SentryEvent.FromJson(document.RootElement); + + var sanitized = SentryTelemetryPrivacy.Sanitize(sentryEvent); + + Assert.Equal("[Filtered]", sanitized.Tags["api_key"]); + Assert.Equal("GET", sanitized.Tags["method"]); + var serialized = SerializeEvent(sanitized); + Assert.DoesNotContain("SENTINEL_EVENT_TAG_411", serialized, StringComparison.Ordinal); + } + private static string SerializeTransaction(SentryTransaction transaction) { using var stream = new MemoryStream();