From 0f2d556c26b61342e248e2df4fed2521a0c615f1 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Wed, 22 Jul 2026 20:36:04 +0000 Subject: [PATCH] fix(cqrs): robust RedactValue with centralized sensitive key patterns - Move sensitive patterns to typed arrays (SensitiveKeyPatterns, SensitiveValueTypePatterns) - Add missing patterns: apikey, api_key, credential, jwt, privatekey, connectionstring - Use LINQ Any() for cleaner matching logic --- .../Controllers/SettingsController.cs | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Heimdall.Web/Controllers/SettingsController.cs b/src/Heimdall.Web/Controllers/SettingsController.cs index 48c5c27..bf36583 100644 --- a/src/Heimdall.Web/Controllers/SettingsController.cs +++ b/src/Heimdall.Web/Controllers/SettingsController.cs @@ -221,18 +221,29 @@ public class SettingsController : ControllerBase return Accepted(new { message = "Config reload broadcast to all connected agents" }); } + /// + /// Sensitive key patterns whose values must never be returned to the client. + /// Centralized list — add new patterns here as needed. + /// + private static readonly string[] SensitiveKeyPatterns = + ["password", "token", "secret", "apikey", "api_key", "credential", "jwt", "privatekey", "connectionstring"]; + + /// + /// Sensitive value type patterns whose values must never be returned to the client. + /// + private static readonly string[] SensitiveValueTypePatterns = + ["password", "token", "secret", "key", "credential"]; + private static string RedactValue(string? valueType, string value, string? key = null) { - if (valueType is not null && valueType.Contains("password", StringComparison.OrdinalIgnoreCase)) + if (key is not null && SensitiveKeyPatterns.Any(p => + key.Contains(p, StringComparison.OrdinalIgnoreCase))) return "***REDACTED***"; - if (valueType is not null && valueType.Contains("token", StringComparison.OrdinalIgnoreCase)) - return "***REDACTED***"; - if (key is not null && key.Contains("password", StringComparison.OrdinalIgnoreCase)) - return "***REDACTED***"; - if (key is not null && key.Contains("token", StringComparison.OrdinalIgnoreCase)) - return "***REDACTED***"; - if (key is not null && key.Contains("secret", StringComparison.OrdinalIgnoreCase)) + + if (valueType is not null && SensitiveValueTypePatterns.Any(p => + valueType.Contains(p, StringComparison.OrdinalIgnoreCase))) return "***REDACTED***"; + return value; } }