From bc2bff96fb30e15646f0ea012e658e291faee0aa Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Fri, 13 Nov 2026 14:09:24 +0100 Subject: [PATCH] security: sanitize Sentry request environment metadata (#411) --- skills/sentry-telemetry-privacy/SKILL.md | 5 ++++- src/Heimdall.Web/Security/SentryTelemetryPrivacy.cs | 2 ++ .../Security/SentryTelemetryPrivacyTests.cs | 9 ++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/skills/sentry-telemetry-privacy/SKILL.md b/skills/sentry-telemetry-privacy/SKILL.md index 8e8de9d..61d6f0c 100644 --- a/skills/sentry-telemetry-privacy/SKILL.md +++ b/skills/sentry-telemetry-privacy/SKILL.md @@ -5,7 +5,7 @@ description: Keep Heimdall Sentry events free of authentication material while p # Sentry telemetry privacy -Sentry event and transaction processing are configured in `src/Heimdall.Web/Program.cs`. `SetBeforeSend` and `SetBeforeSendTransaction` are separate SDK callbacks, so apply `SentryTelemetryPrivacy` to both. Before either payload is sent, remove or redact request/response authentication headers, cookies, query strings, bodies, URL user information, and sensitive values in event extras, transaction contexts, tags, spans, and breadcrumbs. Keep the helper in the Web boundary; do not move request telemetry into Domain or Application. +Sentry event and transaction processing are configured in `src/Heimdall.Web/Program.cs`. `SetBeforeSend` and `SetBeforeSendTransaction` are separate SDK callbacks, so apply `SentryTelemetryPrivacy` to both. Before either payload is sent, filter request `Headers`, `Env`, and `Other` maps; remove request/response cookies, query strings, and bodies; strip URL user information; and filter sensitive values in event extras, transaction contexts, tags, spans, and breadcrumbs. Keep the helper in the Web boundary; do not move request telemetry into Domain or Application. When changing this behavior: @@ -13,6 +13,9 @@ When changing this behavior: - Treat header names case-insensitively and normalize common separators before matching names that indicate authorization, API keys, tokens, secrets, passwords, credentials, or cookies. - Clear entire fields such as cookie strings, query strings, and request bodies when selective redaction could miss nested or encoded credentials. Strip URL user information, query, and fragment while retaining the safe path. - Apply the same sensitive-key filtering to event extras and breadcrumbs. Preserve the existing rate-limit breadcrumb filter. +- Recursively inspect string-keyed maps in extras and custom contexts. Replace arrays, other collections, and opaque object graphs with `[Filtered]` instead of allowing uninspected JSON serialization; retain safe primitive values and safe map entries. +- Filter sensitive-name tags on both `SentryEvent` and `SentryTransaction`; sanitize trace custom data, response headers, and span data/tags. +- Include synthetic request `env`/`other`, sensitive event tags, nested maps, arrays, and opaque values in serializer-level tests. - Never copy actual event headers, API keys, cookies, tokens, or request bodies into issue comments, test fixtures, logs, or acceptance evidence. Record only field names and synthetic test outcomes. ## Acceptance diff --git a/src/Heimdall.Web/Security/SentryTelemetryPrivacy.cs b/src/Heimdall.Web/Security/SentryTelemetryPrivacy.cs index 7870dd2..a273f84 100644 --- a/src/Heimdall.Web/Security/SentryTelemetryPrivacy.cs +++ b/src/Heimdall.Web/Security/SentryTelemetryPrivacy.cs @@ -98,6 +98,8 @@ internal static class SentryTelemetryPrivacy return; RedactStringValues(request.Headers); + RedactStringValues(request.Env); + RedactStringValues(request.Other); request.Cookies = null; request.Url = StripQueryAndUserInfo(request.Url); request.QueryString = null; diff --git a/tests/Heimdall.Web.Tests/Security/SentryTelemetryPrivacyTests.cs b/tests/Heimdall.Web.Tests/Security/SentryTelemetryPrivacyTests.cs index b680f74..4e12ff5 100644 --- a/tests/Heimdall.Web.Tests/Security/SentryTelemetryPrivacyTests.cs +++ b/tests/Heimdall.Web.Tests/Security/SentryTelemetryPrivacyTests.cs @@ -19,6 +19,8 @@ public sealed class SentryTelemetryPrivacyTests "x-api-key": "{{apiKey}}", "Content-Type": "application/json" }, + "env": { "AUTHORIZATION": "SENTINEL_ENV_AUTH_411", "REMOTE_ADDR": "192.0.2.1" }, + "other": { "access_token": "SENTINEL_OTHER_TOKEN_411", "safe": "preserved" }, "cookies": "session={{cookie}}", "data": { "access_token": "SENTINEL_BODY_TOKEN_411" } }, @@ -37,6 +39,10 @@ public sealed class SentryTelemetryPrivacyTests Assert.Same(sentryEvent, sanitized); Assert.Equal("[Filtered]", sanitized.Request!.Headers!["x-api-key"]); Assert.Equal("application/json", sanitized.Request.Headers["Content-Type"]); + Assert.Equal("[Filtered]", sanitized.Request.Env!["AUTHORIZATION"]); + Assert.Equal("192.0.2.1", sanitized.Request.Env["REMOTE_ADDR"]); + Assert.Equal("[Filtered]", sanitized.Request.Other!["access_token"]); + Assert.Equal("preserved", sanitized.Request.Other["safe"]); Assert.Null(sanitized.Request.Cookies); Assert.Equal("https://central.example/api/agents", sanitized.Request.Url); Assert.Null(sanitized.Request.QueryString); @@ -94,7 +100,7 @@ public sealed class SentryTelemetryPrivacyTests "contexts": { "trace": { "type": "trace", "data": { "api_key": "SENTINEL_TRANSACTION_TRACE_411", "safe": "preserved" } }, "response": { "type": "response", "cookies": "session=SENTINEL_TRANSACTION_RESPONSE_COOKIE_411", "headers": { "Set-Cookie": "SENTINEL_TRANSACTION_SET_COOKIE_411", "Content-Type": "application/json" }, "data": { "token": "SENTINEL_TRANSACTION_RESPONSE_BODY_411" } }, - "custom": { "clientSecret": "SENTINEL_TRANSACTION_CONTEXT_411", "mode": "safe" } + "custom": { "clientSecret": "SENTINEL_TRANSACTION_CONTEXT_411", "mode": "safe", "metadata": [ { "token": "SENTINEL_CONTEXT_ARRAY_411" } ] } }, "breadcrumbs": [ { "message": "request", "type": "http", "category": "http.client", "data": { "Authorization": "SENTINEL_TRANSACTION_BREADCRUMB_411", "method": "GET" } } @@ -123,6 +129,7 @@ public sealed class SentryTelemetryPrivacyTests Assert.Null(sanitized.Contexts.Response.Data); Assert.Equal("[Filtered]", ((IDictionary)sanitized.Contexts["custom"])["clientSecret"]); Assert.Equal("safe", ((IDictionary)sanitized.Contexts["custom"])["mode"]); + Assert.Equal("[Filtered]", ((IDictionary)sanitized.Contexts["custom"])["metadata"]); Assert.Equal("[Filtered]", sanitized.Breadcrumbs.Single().Data!["Authorization"]); Assert.Equal("GET", sanitized.Breadcrumbs.Single().Data!["method"]); Assert.Equal("[Filtered]", sanitized.Tags["api_key"]);