fix(config): align GatewayConfigPathRules with Secrets-005 (symlinks, drive-relative, device/loopback/local-share spellings) (scadaproj#52)
ci / nightly-windev (push) Has been skipped
ci / windows-x86 (push) Successful in 1m30s
ci / java (push) Successful in 2m17s
ci / portable (push) Failing after 4m20s
ci / nightly-windev (push) Has been skipped
ci / windows-x86 (push) Successful in 1m30s
ci / java (push) Successful in 2m17s
ci / portable (push) Failing after 4m20s
Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using ZB.MOM.WW.Configuration;
|
||||
using ZB.MOM.WW.MxGateway.Server.Configuration;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Tests.Configuration;
|
||||
|
||||
/// <summary>A fact that is skipped (visibly) on anything but Windows.</summary>
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
internal sealed class WindowsOnlyFactAttribute : FactAttribute
|
||||
{
|
||||
public WindowsOnlyFactAttribute()
|
||||
{
|
||||
if (!OperatingSystem.IsWindows())
|
||||
{
|
||||
Skip = "Windows path semantics; run on a Windows host.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Mirrors the ZB.MOM.WW.Secrets Secrets-005 tests (scadaproj#45 / #52): the store-path rules compare
|
||||
/// locations, not spellings. Symlink cases run wherever links can be created; Windows-only spellings
|
||||
/// (drive-relative, device prefix, loopback admin share, local share, 8.3) are skipped elsewhere.
|
||||
/// </summary>
|
||||
public sealed class GatewayConfigPathSpellingTests : IDisposable
|
||||
{
|
||||
private readonly string _base;
|
||||
|
||||
public GatewayConfigPathSpellingTests()
|
||||
{
|
||||
_base = Path.Combine(Path.GetTempPath(), $"mxgw-spelling-{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(Path.Combine(_base, "app", "data"));
|
||||
Directory.CreateDirectory(Path.Combine(_base, "outside"));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.Delete(_base, recursive: true);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
// best-effort temp cleanup
|
||||
}
|
||||
}
|
||||
|
||||
private static bool Inside(string value, string root)
|
||||
{
|
||||
ValidationBuilder builder = new();
|
||||
GatewayConfigPathRules.AddIfUnderContentRoot(value, root, "inside", builder);
|
||||
return !builder.IsValid;
|
||||
}
|
||||
|
||||
private static bool NotRooted(string value)
|
||||
{
|
||||
ValidationBuilder builder = new();
|
||||
GatewayConfigPathRules.AddIfNotRooted(value, "relative", builder);
|
||||
return !builder.IsValid;
|
||||
}
|
||||
|
||||
private static bool TryLink(string link, string target)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.CreateSymbolicLink(link, target);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
return false; // Windows without the symlink privilege
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Verifies a store reached through a symlink to the content root is inside it.</summary>
|
||||
[Fact]
|
||||
public void StoreThroughSymlinkIntoContentRoot_IsInside()
|
||||
{
|
||||
string root = Path.Combine(_base, "app");
|
||||
string link = Path.Combine(_base, "alias");
|
||||
if (!TryLink(link, root))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Assert.True(Inside(Path.Combine(root, "data", "secrets.db"), root)); // positive control
|
||||
Assert.True(Inside(Path.Combine(link, "data", "secrets.db"), root));
|
||||
}
|
||||
|
||||
/// <summary>Verifies a content root reached through a "current" link still contains its store.</summary>
|
||||
[Fact]
|
||||
public void ContentRootThroughCurrentSymlink_StillContainsStore()
|
||||
{
|
||||
string real = Path.Combine(_base, "app");
|
||||
string current = Path.Combine(_base, "current");
|
||||
if (!TryLink(current, real))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Assert.True(Inside(Path.Combine(real, "data", "secrets.db"), current));
|
||||
Assert.True(Inside(Path.Combine(current, "data", "secrets.db"), real));
|
||||
}
|
||||
|
||||
/// <summary>Verifies a store spelled through the content root stays refused even if a link leads out.</summary>
|
||||
[Fact]
|
||||
public void StoreSpelledThroughContentRoot_StaysRefused_EvenIfLinkLeadsOut()
|
||||
{
|
||||
string root = Path.Combine(_base, "app");
|
||||
string link = Path.Combine(root, "durable");
|
||||
if (!TryLink(link, Path.Combine(_base, "outside")))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Assert.True(Inside(Path.Combine(link, "secrets.db"), root));
|
||||
Assert.False(Inside(Path.Combine(_base, "outside", "secrets.db"), root)); // control
|
||||
}
|
||||
|
||||
/// <summary>Verifies a not-yet-created store is compared as written.</summary>
|
||||
[Fact]
|
||||
public void NotYetCreatedStore_IsComparedAsWritten()
|
||||
{
|
||||
string root = Path.Combine(_base, "app");
|
||||
Assert.True(Inside(Path.Combine(root, "new", "deeper", "secrets.db"), root));
|
||||
Assert.False(Inside(Path.Combine(_base, "outside", "new", "secrets.db"), root));
|
||||
}
|
||||
|
||||
/// <summary>Verifies drive-relative and root-relative paths are not fully qualified on Windows.</summary>
|
||||
[WindowsOnlyFact]
|
||||
public void DriveAndRootRelativePaths_AreNotAbsoluteOnWindows()
|
||||
{
|
||||
Assert.True(NotRooted(@"C:secrets.db"));
|
||||
Assert.True(NotRooted(@"\secrets.db"));
|
||||
Assert.False(NotRooted(@"C:\data\secrets.db"));
|
||||
Assert.False(NotRooted(@"\\server\share\secrets.db"));
|
||||
}
|
||||
|
||||
/// <summary>Verifies device-prefixed and loopback admin share spellings are inside on Windows.</summary>
|
||||
[WindowsOnlyFact]
|
||||
public void DevicePrefixedAndLoopbackShareSpellings_AreInsideOnWindows()
|
||||
{
|
||||
string root = Path.Combine(_base, "app");
|
||||
string full = Path.GetFullPath(Path.Combine(root, "data", "secrets.db"));
|
||||
string drive = full[..1];
|
||||
string rest = full[3..];
|
||||
|
||||
Assert.True(Inside(@"\\?\" + full, root));
|
||||
Assert.True(Inside(@"\\.\" + full, root));
|
||||
Assert.True(Inside($@"\\localhost\{drive}$\{rest}", root));
|
||||
Assert.True(Inside($@"\\127.0.0.1\{drive}$\{rest}", root));
|
||||
Assert.True(Inside($@"\\?\UNC\localhost\{drive}$\{rest}", root));
|
||||
}
|
||||
|
||||
/// <summary>Verifies an 8.3 short-name spelling is inside on Windows (skips quietly if 8.3 is disabled).</summary>
|
||||
[WindowsOnlyFact]
|
||||
public void ShortNameSpelling_IsInsideOnWindows()
|
||||
{
|
||||
string longDir = Path.Combine(_base, "Application Directory");
|
||||
Directory.CreateDirectory(longDir);
|
||||
string? shortDir = ShortName(longDir);
|
||||
if (shortDir is null || !shortDir.Contains('~'))
|
||||
{
|
||||
return; // 8.3 names disabled on this volume
|
||||
}
|
||||
|
||||
Assert.True(Inside(Path.Combine(shortDir, "secrets.db"), longDir));
|
||||
Assert.True(Inside(Path.Combine(longDir, "secrets.db"), shortDir));
|
||||
}
|
||||
|
||||
/// <summary>Verifies an ordinary local share that points at the content root is inside on Windows.</summary>
|
||||
[WindowsOnlyFact]
|
||||
public void OrdinaryLocalShareToContentRoot_IsInsideOnWindows()
|
||||
{
|
||||
string root = Path.Combine(_base, "app");
|
||||
string share = "mxgwtest" + Guid.NewGuid().ToString("N")[..8];
|
||||
ShareInfo2 info = new() { shi2_netname = share, shi2_max_uses = uint.MaxValue, shi2_path = root };
|
||||
if (NetShareAdd(null, 2, ref info, out _) != 0)
|
||||
{
|
||||
return; // 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 string? ShortName(string path)
|
||||
{
|
||||
char[] buffer = new char[1024];
|
||||
uint n = GetShortPathNameW(path, buffer, (uint)buffer.Length);
|
||||
return n == 0 ? null : new string(buffer, 0, (int)n);
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = 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;
|
||||
}
|
||||
|
||||
[DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
|
||||
private static extern uint NetShareAdd(string? server, uint level, ref ShareInfo2 buf, out uint parmErr);
|
||||
|
||||
[DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
|
||||
private static extern uint NetShareDel(string? server, string netName, uint reserved);
|
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
|
||||
private static extern uint GetShortPathNameW(string longPath, [Out] char[] shortPath, uint size);
|
||||
}
|
||||
Reference in New Issue
Block a user