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
This commit is contained in:
Arnaud Dartois committed 2026-07-22 20:36:04 +00:00
1 parent deb4af9412
commit 0f2d556c26
1 file changed
+19 -8
@@ -221,18 +221,29 @@ public class SettingsController : ControllerBase
return Accepted(new { message = "Config reload broadcast to all connected agents" });
}
/// <summary>
/// Sensitive key patterns whose values must never be returned to the client.
/// Centralized list — add new patterns here as needed.
/// </summary>
private static readonly string[] SensitiveKeyPatterns =
["password", "token", "secret", "apikey", "api_key", "credential", "jwt", "privatekey", "connectionstring"];
/// <summary>
/// Sensitive value type patterns whose values must never be returned to the client.
/// </summary>
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;
}
}