fix(secrets): audit malformed ${secret:} tokens, cap SecretName at 450, resolve local UNC shares (scadaproj#52)

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 15:38:36 -05:00
parent 4a3c4e9b7e
commit ebc493361e
8 changed files with 236 additions and 6 deletions
@@ -57,6 +57,13 @@ public sealed class ReferenceAuditFlow : IInteractiveFlow
continue;
}
if (finding.Status == ReferenceStatus.Malformed)
{
console.MarkupLineInterpolated(
$"[red]Malformed ${{secret:}} token[/] at {string.Join(", ", finding.ConfigPaths)} — the app refuses to start with it; give it a name and a closing '}}'.");
continue;
}
if (!IsSeedable(finding.Status))
{
continue; // Ok (nothing to do) or PresentUnverified (unreachable on a KEK-capable session).
@@ -132,7 +139,7 @@ public sealed class ReferenceAuditFlow : IInteractiveFlow
{
ReferenceStatus.Ok => "green",
ReferenceStatus.PresentUnverified => "yellow",
_ => "red", // Missing / Tombstoned / Undecryptable / InvalidName
_ => "red", // Missing / Tombstoned / Undecryptable / InvalidName / Malformed
};
// A deterministic "N Status" count-by-status roll-up, ordered by the status enum.
@@ -30,6 +30,13 @@ public enum ReferenceStatus
/// unfilled placeholder like <c>REPLACE ME</c>, and so on), so it cannot be looked up at all.
/// </summary>
InvalidName,
/// <summary>
/// A <c>${secret:</c> marker that does not start a complete, named reference (<c>${secret:}</c>,
/// <c>${secret: }</c>, an unterminated <c>${secret:name</c>, or a marker swallowed inside another
/// match). <c>SecretReferenceExpander</c> refuses to start with such a value, so the audit surfaces it.
/// </summary>
Malformed,
}
/// <summary>
@@ -49,6 +56,12 @@ public sealed class ReferenceAuditor
// the references the expander would resolve at startup. Do NOT diverge from the expander's regex.
private static readonly Regex TokenPattern = new(@"\$\{secret:([^}]+)\}", RegexOptions.Compiled);
// Pinned to SecretReferenceExpander.TokenMarker.
private const string TokenMarker = "${secret:";
/// <summary>The <see cref="ReferenceFinding.SecretName"/> used for a malformed-token finding.</summary>
public const string MalformedFindingName = "${secret:...} (malformed)";
/// <summary>
/// Walks every configuration value, collects each distinct <c>${secret:NAME}</c> reference with the
/// config key paths that use it, and classifies each name against the session's store. References are
@@ -79,8 +92,29 @@ public sealed class ReferenceAuditor
continue;
}
foreach (Match match in TokenPattern.Matches(value))
MatchCollection matches = TokenPattern.Matches(value);
// Mirror the expander's fail-closed shape check: every marker must start one complete,
// non-blank reference, otherwise the app refuses to start.
if (CountMarkers(value) != matches.Count
|| matches.Any(m => string.IsNullOrWhiteSpace(m.Groups[1].Value)))
{
if (!groups.TryGetValue(MalformedFindingName, out ReferenceGroup? bad))
{
bad = new ReferenceGroup(null) { Malformed = true };
groups[MalformedFindingName] = bad;
}
bad.Paths.Add(entry.Key);
}
foreach (Match match in matches)
{
if (string.IsNullOrWhiteSpace(match.Groups[1].Value))
{
continue;
}
string raw = match.Groups[1].Value.Trim();
ReferenceGroup group = ResolveGroup(groups, raw);
group.Paths.Add(entry.Key);
@@ -90,7 +124,9 @@ public sealed class ReferenceAuditor
var findings = new List<ReferenceFinding>(groups.Count);
foreach ((string key, ReferenceGroup group) in groups)
{
ReferenceStatus status = group.Name is { } name
ReferenceStatus status = group.Malformed
? ReferenceStatus.Malformed
: group.Name is { } name
? await ClassifyAsync(session, name, ct).ConfigureAwait(false)
: ReferenceStatus.InvalidName;
findings.Add(new ReferenceFinding(key, status, [.. group.Paths]));
@@ -131,6 +167,19 @@ public sealed class ReferenceAuditor
// A configuration key is a comment/documentation key when its leaf segment (after the last ':') starts
// with '_'. Duplicated from SecretReferenceExpander.IsCommentKey — keep the two in lockstep.
private static int CountMarkers(string value)
{
int count = 0;
for (int at = value.IndexOf(TokenMarker, StringComparison.Ordinal);
at >= 0;
at = value.IndexOf(TokenMarker, at + TokenMarker.Length, StringComparison.Ordinal))
{
count++;
}
return count;
}
private static bool IsCommentKey(string key)
{
int lastSeparator = key.LastIndexOf(':');
@@ -177,5 +226,7 @@ public sealed class ReferenceAuditor
public SortedSet<string> Paths { get; } = new(StringComparer.Ordinal);
public SecretName? Name { get; } = name;
public bool Malformed { get; init; }
}
}