fix(sessions): restrict the worker named pipe to the gateway's account (#187)
On Windows each worker pipe is created via NamedPipeServerStreamAcl with a protected DACL granting FullControl only to the gateway process user (the worker is launched under the same account) and denying NETWORK logons. On non-Windows hosts (test matrix) the pipe uses PipeOptions.CurrentUserOnly. Tests assert the factory ACL and the ACL read back from the created pipe (Windows), and that a same-user client still connects.
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
using System.IO.Pipes;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Security.AccessControl;
|
||||
using System.Security.Principal;
|
||||
using ZB.MOM.WW.MxGateway.Server.Sessions;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Tests.Gateway.Sessions;
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the gateway↔worker pipe is restricted to the gateway's own account (#187).
|
||||
/// The ACL assertions need Windows; the Unix assertion covers the macOS/Linux test matrix.
|
||||
/// </summary>
|
||||
public sealed class WorkerPipeSecurityTests
|
||||
{
|
||||
|
||||
/// <summary>Verifies the factory grants only the given user and denies network logons.</summary>
|
||||
[WindowsFact]
|
||||
[SupportedOSPlatform("windows")]
|
||||
public void Create_GrantsOnlyTheUserAndDeniesNetwork()
|
||||
{
|
||||
SecurityIdentifier user = new("S-1-5-21-1000-2000-3000-1001");
|
||||
|
||||
PipeSecurity security = WorkerPipeSecurity.Create(user);
|
||||
|
||||
Assert.True(security.AreAccessRulesProtected);
|
||||
Assert.Equal(user, security.GetOwner(typeof(SecurityIdentifier)));
|
||||
AssertOnlyUserAllowedAndNetworkDenied(security, user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the pipe the factory actually creates carries the restricted ACL — read back from
|
||||
/// the kernel object, not from the descriptor handed to it.
|
||||
/// </summary>
|
||||
[WindowsFact]
|
||||
[SupportedOSPlatform("windows")]
|
||||
public void CreatePipe_OnWindows_AppliesCurrentUserOnlyAcl()
|
||||
{
|
||||
using WindowsIdentity identity = WindowsIdentity.GetCurrent();
|
||||
SecurityIdentifier user = identity.User!;
|
||||
|
||||
using NamedPipeServerStream pipe = SessionWorkerClientFactory.CreatePipe(NewPipeName());
|
||||
PipeSecurity applied = pipe.GetAccessControl();
|
||||
|
||||
AssertOnlyUserAllowedAndNetworkDenied(applied, user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the pipe still accepts a client running as the same user — the worker is launched
|
||||
/// under the gateway's own account, so the restriction must not break the session handshake.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task CreatePipe_AcceptsClientRunningAsTheSameUser()
|
||||
{
|
||||
string pipeName = NewPipeName();
|
||||
using NamedPipeServerStream server = SessionWorkerClientFactory.CreatePipe(pipeName);
|
||||
using NamedPipeClientStream client = new(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
|
||||
using CancellationTokenSource timeout = new(TimeSpan.FromSeconds(10));
|
||||
|
||||
Task accept = server.WaitForConnectionAsync(timeout.Token);
|
||||
await client.ConnectAsync(timeout.Token);
|
||||
await accept;
|
||||
|
||||
Assert.True(server.IsConnected);
|
||||
Assert.True(client.IsConnected);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies non-Windows pipes are created current-user-only. On Unix .NET enforces this by
|
||||
/// checking the connecting peer's credentials at accept time (the socket file mode is not
|
||||
/// changed), so the observable contract is the option itself.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void UnixPipeOptions_AreAsynchronousAndCurrentUserOnly()
|
||||
{
|
||||
Assert.True(WorkerPipeSecurity.UnixPipeOptions.HasFlag(PipeOptions.CurrentUserOnly));
|
||||
Assert.True(WorkerPipeSecurity.UnixPipeOptions.HasFlag(PipeOptions.Asynchronous));
|
||||
}
|
||||
|
||||
[SupportedOSPlatform("windows")]
|
||||
private static void AssertOnlyUserAllowedAndNetworkDenied(PipeSecurity security, SecurityIdentifier user)
|
||||
{
|
||||
SecurityIdentifier everyoneSid = new(WellKnownSidType.WorldSid, null);
|
||||
SecurityIdentifier anonymousSid = new(WellKnownSidType.AnonymousSid, null);
|
||||
SecurityIdentifier networkSid = new(WellKnownSidType.NetworkSid, null);
|
||||
List<PipeAccessRule> rules = security
|
||||
.GetAccessRules(includeExplicit: true, includeInherited: true, typeof(SecurityIdentifier))
|
||||
.Cast<PipeAccessRule>()
|
||||
.ToList();
|
||||
|
||||
PipeAccessRule[] allows = rules.Where(r => r.AccessControlType == AccessControlType.Allow).ToArray();
|
||||
PipeAccessRule allow = Assert.Single(allows);
|
||||
Assert.Equal(user, allow.IdentityReference);
|
||||
Assert.Equal(PipeAccessRights.FullControl, allow.PipeAccessRights & PipeAccessRights.FullControl);
|
||||
|
||||
PipeAccessRule deny = Assert.Single(rules, r => r.AccessControlType == AccessControlType.Deny);
|
||||
Assert.Equal(networkSid, deny.IdentityReference);
|
||||
|
||||
Assert.DoesNotContain(rules, r => r.IdentityReference == everyoneSid && r.AccessControlType == AccessControlType.Allow);
|
||||
Assert.DoesNotContain(rules, r => r.IdentityReference == anonymousSid && r.AccessControlType == AccessControlType.Allow);
|
||||
}
|
||||
|
||||
private static string NewPipeName() => $"mxgw-acl-{Guid.NewGuid():N}";
|
||||
}
|
||||
|
||||
/// <summary>A fact that runs only on Windows.</summary>
|
||||
public sealed class WindowsFactAttribute : FactAttribute
|
||||
{
|
||||
/// <summary>Initializes a new instance of the <see cref="WindowsFactAttribute"/> class.</summary>
|
||||
public WindowsFactAttribute()
|
||||
{
|
||||
if (!OperatingSystem.IsWindows())
|
||||
{
|
||||
Skip = "Requires Windows.";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user