fix(security): per-instance credential references (#835) and confined OpcUaClient user-certificate path (#836)
#835: inside one driver namespace, a secret:<ns>/<X>/... reference whose first segment names ANOTHER existing same-type driver instance (id or name, case-insensitive; another-instance match beats an own-name match, so renaming one's driver after a victim's unlocks nothing) is refused at save (DriverConfigSaveGate.ValidateAsync), deploy (CredentialReferenceOtherInstance, Heidenhain included), Test Connect (TestDriverConnect.DriverInstanceId, ConfigDb read off the mailbox) and live browse (BrowserSessionService, fail-closed without a ConfigDb). One rule: DriverCredentialPolicy.DescribeOtherInstanceProblems + DriverInstanceIdentities. Shared names and the driver's own id stay valid; env: is not instance-scoped. Running drivers are unchanged (warn-only window: the deploy gate and the upgrade preflight / /raw banner list them). #836: OpcUaClient UserCertificatePath is a relative path inside Drivers:OpcUaClient:UserCertificateDirectory (default <LocalAppData>/OtOpcUa/pki/user-certificates). Syntactic rule (CredentialFilePaths, a DriverCredentialPolicy path field) at every #815 seam; at load time ResolveSecretRefsAsync joins, canonicalises and requires lexical AND link-resolved containment before the PFX password is resolved, and LoadCertificate refuses an unresolved path. A running driver's pre-#836 absolute path keeps loading with a rate-limited warning (LegacyCredentialReferenceTracker.ReportPath). Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
+35
-3
@@ -45,7 +45,10 @@ public sealed class OpcUaClientBrowserCertificateAuthTests
|
||||
ex.Message.ShouldNotContain("does not support");
|
||||
}
|
||||
|
||||
/// <summary>A valid PFX whose password is a <c>secret:</c> ref resolves through the injected store.</summary>
|
||||
/// <summary>
|
||||
/// A valid PFX (a relative path inside the user-certificate directory — lmxopcua#836) whose password is a
|
||||
/// <c>secret:</c> ref resolves through the injected store.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task A_certificate_identity_is_built_with_its_secret_password_resolved()
|
||||
{
|
||||
@@ -55,12 +58,13 @@ public sealed class OpcUaClientBrowserCertificateAuthTests
|
||||
var opts = new OpcUaClientDriverOptions
|
||||
{
|
||||
AuthType = OpcUaAuthType.Certificate,
|
||||
UserCertificatePath = path,
|
||||
UserCertificatePath = Path.GetFileName(path),
|
||||
UserCertificatePassword = "secret:opcuaclient/browse-cert",
|
||||
};
|
||||
|
||||
var identity = await OpcUaClientDriverBrowser.BuildBrowseUserIdentityAsync(
|
||||
opts, new OneSecret("opcuaclient/browse-cert", "s3cret-pw"), TestContext.Current.CancellationToken);
|
||||
opts, new OneSecret("opcuaclient/browse-cert", "s3cret-pw"), TestContext.Current.CancellationToken,
|
||||
userCertificateDirectory: Path.GetDirectoryName(path));
|
||||
|
||||
identity.TokenType.ShouldBe(UserTokenType.Certificate);
|
||||
}
|
||||
@@ -70,6 +74,34 @@ public sealed class OpcUaClientBrowserCertificateAuthTests
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// lmxopcua#836: the browse is a hard-fail path — an absolute <c>UserCertificatePath</c> (any PFX the service account
|
||||
/// can read) is refused before it is loaded or its password resolved, even when the file exists.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task The_browse_refuses_an_absolute_user_certificate_path()
|
||||
{
|
||||
var path = WritePfx("s3cret-pw");
|
||||
try
|
||||
{
|
||||
var opts = new OpcUaClientDriverOptions
|
||||
{
|
||||
AuthType = OpcUaAuthType.Certificate,
|
||||
UserCertificatePath = path,
|
||||
UserCertificatePassword = "s3cret-pw",
|
||||
};
|
||||
|
||||
var ex = await Should.ThrowAsync<ZB.MOM.WW.OtOpcUa.Core.Abstractions.CredentialReferenceException>(() =>
|
||||
OpcUaClientDriverBrowser.BuildBrowseUserIdentityAsync(
|
||||
opts, null, TestContext.Current.CancellationToken, userCertificateDirectory: Path.GetDirectoryName(path)));
|
||||
ex.Message.ShouldContain("lmxopcua#836");
|
||||
}
|
||||
finally
|
||||
{
|
||||
try { File.Delete(path); } catch { /* best-effort */ }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>With no secret store, a <c>secret:</c> ref fails closed instead of being sent as the literal.</summary>
|
||||
[Fact]
|
||||
public async Task A_secret_ref_with_no_secret_store_fails_closed()
|
||||
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// lmxopcua#836: the user-certificate identity loads only a PFX inside the node's user-certificate directory — the
|
||||
/// authored <c>UserCertificatePath</c> is relative, resolved (and link-checked) by <c>ResolveSecretRefsAsync</c>, which
|
||||
/// both the running driver and the AdminUI browser call before building the identity.
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class OpcUaClientUserCertificatePathTests : IDisposable
|
||||
{
|
||||
private readonly string _dir = Path.Combine(Path.GetTempPath(), "otopcua-836-ucert-" + Guid.NewGuid().ToString("N"));
|
||||
private readonly string _outside = Path.Combine(Path.GetTempPath(), "otopcua-836-outside-" + Guid.NewGuid().ToString("N"));
|
||||
|
||||
public OpcUaClientUserCertificatePathTests()
|
||||
{
|
||||
Directory.CreateDirectory(Path.Combine(_dir, "plant1"));
|
||||
Directory.CreateDirectory(_outside);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var d in new[] { _dir, _outside })
|
||||
try { Directory.Delete(d, recursive: true); } catch (IOException) { } catch (UnauthorizedAccessException) { }
|
||||
}
|
||||
|
||||
private static void WritePfx(string path)
|
||||
{
|
||||
using var rsa = RSA.Create(2048);
|
||||
var req = new CertificateRequest("CN=Issue836", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
||||
using var cert = req.CreateSelfSigned(DateTimeOffset.UtcNow.AddMinutes(-5), DateTimeOffset.UtcNow.AddHours(1));
|
||||
File.WriteAllBytes(path, cert.Export(X509ContentType.Pfx, "pw"));
|
||||
}
|
||||
|
||||
private static OpcUaClientDriverOptions Cert(string path) =>
|
||||
new() { AuthType = OpcUaAuthType.Certificate, UserCertificatePath = path, UserCertificatePassword = "pw" };
|
||||
|
||||
[Fact]
|
||||
public async Task A_relative_path_inside_the_directory_resolves_and_loads()
|
||||
{
|
||||
WritePfx(Path.Combine(_dir, "plant1", "user.pfx"));
|
||||
|
||||
var resolved = await OpcUaUserIdentityFactory.ResolveSecretRefsAsync(
|
||||
Cert("plant1/user.pfx"), null, TestContext.Current.CancellationToken, userCertificateDirectory: _dir);
|
||||
|
||||
resolved.UserCertificatePath.ShouldBe(Path.Combine(Path.GetFullPath(_dir), "plant1", "user.pfx"));
|
||||
OpcUaUserIdentityFactory.Build(resolved).TokenType.ShouldBe(Opc.Ua.UserTokenType.Certificate);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("../outside.pfx")]
|
||||
[InlineData("plant1/../../outside.pfx")]
|
||||
public async Task Traversal_is_refused_before_any_load(string path) =>
|
||||
await Should.ThrowAsync<CredentialReferenceException>(() => OpcUaUserIdentityFactory.ResolveSecretRefsAsync(
|
||||
Cert(path), null, TestContext.Current.CancellationToken, userCertificateDirectory: _dir));
|
||||
|
||||
[Fact]
|
||||
public async Task An_absolute_path_is_refused_on_the_hard_fail_path_even_inside_the_directory()
|
||||
{
|
||||
var abs = Path.Combine(_dir, "plant1", "user.pfx");
|
||||
WritePfx(abs);
|
||||
var ex = await Should.ThrowAsync<CredentialReferenceException>(() => OpcUaUserIdentityFactory.ResolveSecretRefsAsync(
|
||||
Cert(abs), null, TestContext.Current.CancellationToken, userCertificateDirectory: _dir));
|
||||
ex.Message.ShouldContain("lmxopcua#836");
|
||||
ex.Message.ShouldNotContain(abs);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_symlink_escaping_the_directory_is_refused()
|
||||
{
|
||||
var victim = Path.Combine(_outside, "victim.pfx");
|
||||
WritePfx(victim);
|
||||
try
|
||||
{
|
||||
File.CreateSymbolicLink(Path.Combine(_dir, "user.pfx"), victim);
|
||||
}
|
||||
catch (Exception e) when (e is UnauthorizedAccessException or IOException)
|
||||
{
|
||||
return; // no symlink privilege (Windows): covered lexically above
|
||||
}
|
||||
|
||||
await Should.ThrowAsync<CredentialReferenceException>(() => OpcUaUserIdentityFactory.ResolveSecretRefsAsync(
|
||||
Cert("user.pfx"), null, TestContext.Current.CancellationToken, userCertificateDirectory: _dir));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task The_running_driver_keeps_loading_a_legacy_absolute_path_and_warns()
|
||||
{
|
||||
var abs = Path.Combine(_outside, "legacy.pfx");
|
||||
WritePfx(abs);
|
||||
var warnings = new List<string>();
|
||||
var tracker = new LegacyCredentialReferenceTracker(DriverCredentialPolicy.OpcUaClientScope, "ouc", warnings.Add);
|
||||
|
||||
var resolved = await OpcUaUserIdentityFactory.ResolveSecretRefsAsync(
|
||||
Cert(abs), null, TestContext.Current.CancellationToken, legacy: tracker, userCertificateDirectory: _dir);
|
||||
|
||||
resolved.UserCertificatePath.ShouldBe(abs);
|
||||
OpcUaUserIdentityFactory.Build(resolved).TokenType.ShouldBe(Opc.Ua.UserTokenType.Certificate);
|
||||
warnings.ShouldHaveSingleItem().ShouldContain("lmxopcua#836");
|
||||
warnings[0].ShouldNotContain(abs);
|
||||
tracker.Diagnostics.ShouldContainKey(LegacyCredentialReferenceTracker.DiagnosticsKeyPrefix + "UserCertificatePath");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task The_legacy_tracker_does_not_relax_a_relative_path_that_escapes()
|
||||
{
|
||||
var tracker = new LegacyCredentialReferenceTracker(DriverCredentialPolicy.OpcUaClientScope, "ouc", _ => { });
|
||||
await Should.ThrowAsync<CredentialReferenceException>(() => OpcUaUserIdentityFactory.ResolveSecretRefsAsync(
|
||||
Cert("../x.pfx"), null, TestContext.Current.CancellationToken, legacy: tracker, userCertificateDirectory: _dir));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_non_certificate_auth_type_leaves_the_path_alone()
|
||||
{
|
||||
var options = new OpcUaClientDriverOptions { AuthType = OpcUaAuthType.Anonymous, UserCertificatePath = "/etc/stale.pfx" };
|
||||
(await OpcUaUserIdentityFactory.ResolveSecretRefsAsync(options, null, TestContext.Current.CancellationToken, userCertificateDirectory: _dir))
|
||||
.ShouldBeSameAs(options);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadCertificate_refuses_an_unresolved_relative_path()
|
||||
{
|
||||
Should.Throw<InvalidOperationException>(() => OpcUaUserIdentityFactory.LoadCertificate(Cert("plant1/user.pfx")))
|
||||
.Message.ShouldContain("not been resolved");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task The_path_is_checked_before_any_secret_is_read()
|
||||
{
|
||||
var reads = 0;
|
||||
var options = Cert("/etc/x.pfx") with { UserCertificatePassword = "secret:opcuaclient/pfx-pw" };
|
||||
await Should.ThrowAsync<CredentialReferenceException>(() => OpcUaUserIdentityFactory.ResolveSecretRefsAsync(
|
||||
options, new CountingResolver(() => reads++), TestContext.Current.CancellationToken, userCertificateDirectory: _dir));
|
||||
reads.ShouldBe(0);
|
||||
}
|
||||
|
||||
private sealed class CountingResolver(Action onRead) : ZB.MOM.WW.Secrets.Abstractions.ISecretResolver
|
||||
{
|
||||
public Task<string?> GetAsync(ZB.MOM.WW.Secrets.Abstractions.SecretName name, CancellationToken ct)
|
||||
{
|
||||
onRead();
|
||||
return Task.FromResult<string?>("pw");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user