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
@@ -173,6 +173,34 @@ public sealed class ReferenceAuditorTests : IDisposable
Assert.Equal(ReferenceStatus.Missing, findings.Single(f => f.SecretName == "api/key").Status);
}
[Theory]
[InlineData("${secret:}")]
[InlineData("${secret: }")]
[InlineData("prefix ${secret:unterminated")]
[InlineData("${secret:a${secret:b}")]
public async Task Malformed_token_yields_Malformed_finding_matching_the_expander(string value)
{
SecretsSession session = await Open(EnvKey(), Config(("Bad:Ref", value), ("Good:Ref", "${secret:api/key}")));
IReadOnlyList<ReferenceFinding> findings =
await new ReferenceAuditor().AuditAsync(session, CancellationToken.None);
ReferenceFinding bad = findings.Single(f => f.Status == ReferenceStatus.Malformed);
Assert.Equal(["Bad:Ref"], bad.ConfigPaths);
Assert.Equal(ReferenceStatus.Missing, findings.Single(f => f.SecretName == "api/key").Status);
}
[Fact]
public async Task Well_formed_tokens_produce_no_Malformed_finding()
{
SecretsSession session = await Open(EnvKey(), Config(("A", "x ${secret:a/b} y ${secret:c}")));
IReadOnlyList<ReferenceFinding> findings =
await new ReferenceAuditor().AuditAsync(session, CancellationToken.None);
Assert.DoesNotContain(findings, f => f.Status == ReferenceStatus.Malformed);
}
[Fact]
public async Task Comment_keys_are_skipped_like_the_expander()
{
@@ -139,6 +139,50 @@ public sealed class SecretsStorePathSpellingTests : IDisposable
Assert.True(Inside(Path.Combine(longDir, "secrets.db"), shortDir!));
}
[SkippableFact]
public void An_ordinary_local_share_pointing_at_the_content_root_is_inside_on_windows()
{
Skip.IfNot(OperatingSystem.IsWindows(), "Windows path semantics");
string root = Path.Combine(_base, "app");
string share = "zbtest" + Guid.NewGuid().ToString("N")[..8];
Skip.IfNot(CreateShare(share, root), "cannot create an SMB share here (needs Administrators)");
try
{
Assert.True(Inside($@"\\localhost\{share}\data\secrets.db", root));
Assert.True(Inside($@"\\{Environment.MachineName}\{share}\secrets.db", root));
Assert.False(Inside($@"\\localhost\{share}\secrets.db", Path.Combine(_base, "outside")));
}
finally
{
_ = NetShareDel(null, share, 0);
}
}
private static bool CreateShare(string name, string path)
{
var info = new ShareInfo2 { shi2_netname = name, shi2_type = 0, shi2_max_uses = uint.MaxValue, shi2_path = path };
return NetShareAdd(null, 2, ref info, out _) == 0;
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
private struct ShareInfo2
{
public string shi2_netname;
public uint shi2_type;
public string? shi2_remark;
public uint shi2_permissions;
public uint shi2_max_uses;
public uint shi2_current_uses;
public string shi2_path;
public string? shi2_passwd;
}
[System.Runtime.InteropServices.DllImport("netapi32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
private static extern uint NetShareAdd(string? server, uint level, ref ShareInfo2 buf, out uint parmErr);
[System.Runtime.InteropServices.DllImport("netapi32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
private static extern uint NetShareDel(string? server, string netName, uint reserved);
private static string? ShortName(string path)
{
char[] buffer = new char[1024];
@@ -27,6 +27,16 @@ public class SecretNameTests
public void RejectsRootedOrEmptySegment(string input)
=> Assert.Throws<ArgumentException>(() => new SecretName(input));
[Fact]
public void AcceptsExactlyMaxLength_AndRejectsOneOver()
{
Assert.Equal(450, SecretName.MaxLength);
Assert.Equal(450, new SecretName(new string('a', 450)).Value.Length);
Assert.Throws<ArgumentException>(() => new SecretName(new string('a', 451)));
// Length is measured after trim/lowercase normalization.
Assert.Equal(450, new SecretName(" " + new string('A', 450) + " ").Value.Length);
}
[Fact]
public void DefaultInstance_ThrowsOnValue()
=> Assert.Throws<InvalidOperationException>(() => _ = default(SecretName).Value);