From 7cda8dbb81eadfdefed90fd42c1c4289ae73a9de Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 15:30:13 -0500 Subject: [PATCH] perf(opcua-acl): #1033 flag-off revalidation answers local items before the pass memo With Security:Acl:Enabled=false a revalidation pass over local Raw/UNS items is an unconditional allow; it still paid a (session, ns, id) memo entry and the full Permits walk per item. Measured over 10k items: ~6.5-7 ms and ~1.4 MB per pass -> ~0.5-1 ms and ~0.2 KB. IsLocalNodeUngated mirrors exactly what Permits concludes there and excludes the one wiring where it differs (mapped resolver without the projected predicate). The same shortcut inside the per-request Permits was measured and NOT taken: a 1,000-node flag-off Read stayed ~1.5-1.9 ms either way (SDK read dominates). Co-Authored-By: Claude --- docs/security.md | 4 ++- .../OtOpcUaNodeManager.cs | 26 ++++++++++++++++ .../NodeManagerStage6EnforcementTests.cs | 31 +++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/docs/security.md b/docs/security.md index 7ee195ab2..ec51b263e 100644 --- a/docs/security.md +++ b/docs/security.md @@ -848,7 +848,9 @@ Behaviours worth knowing before enabling it: the creation gate's own seam (`Permits` → `AuthorizeIfRequired`), which bypasses local grants — so an ungranted (e.g. Anonymous) subscription admitted with the flag off is **never** stood down by a tick or a deploy; only gRPC-data projected / imported-condition items, gated in both modes, can lapse (#972 — between - `8ad03e9c5` and the fix, the pass enforced grants with the flag off). Without the timer a key that expired by the + `8ad03e9c5` and the fix, the pass enforced grants with the flag off). With the flag off a local Raw/UNS item is + answered allow before the pass's memo (#1033: a 10k-item pass ~6.5–7 ms / ~1.4 MB ⇒ ~0.5–1 ms / ~0.2 KB; the same + shortcut in the per-request `Permits` was measured and not taken — a 1,000-node Read did not change). Without the timer a key that expired by the clock kept streaming until a deployment happened to land — **indefinitely** on a fleet that has been deployed once and left running, which is exactly the shape a production fleet settles into. diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs index 9e9edb8ac..31369a431 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs @@ -6286,6 +6286,29 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage (_authorizer is not null || _mappedSubjectResolver is null); } + /// + /// #1033: whether the revalidation pass's CreateMonitoredItems verdict on is an + /// unconditional allow — the node is in this manager's own Raw or UNS namespace and Security:Acl:Enabled is + /// off. That is exactly what concludes there for a non-Browse operation, minus its lookups: + /// the imported-condition classifier answers only namespace [4], the projected predicate + /// () only [2]/[3], and AuthorizeIfRequired then takes the + /// localBypass. The one wiring where the full path differs — a mapped-subject resolver with no projected + /// predicate, which asks the resolver about every node and denies the local ones it cannot place — is excluded, so + /// that configuration keeps its existing verdict. + /// + /// + /// Measured (10k flag-off items): a pass went from ~6.5–7 ms and ~1.4 MB (the pass-local memo's tuple keys and + /// dictionary growth, plus the per-item authorizer walk) to ~0.5–1 ms and ~0.2 KB. The same shortcut in + /// itself was measured too and NOT taken: a 1,000-node flag-off Read stayed ~1.5–1.9 ms either + /// way (the SDK read dominates). + /// + /// The target node. + /// when the decision is an allow without consulting the authorizer. + private bool IsLocalNodeUngated(NodeId nodeId) => + _authorizer is { Enabled: false } authorizer + && (nodeId.NamespaceIndex == NamespaceIndexes[0] || nodeId.NamespaceIndex == NamespaceIndexes[1]) + && (authorizer.MappedSubjectResolver is null || authorizer.MappedNamespacePredicate is not null); + /// Guard against a corrupt (cyclic) parent chain in . private const int MaxUnsAncestry = 64; @@ -9290,6 +9313,9 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage // `Security:Acl:Enabled=false` bypass (AuthorizeIfRequired), the mapped/imported classifiers, and the // no-authorizer case. The bypass-less `IsAllowedCached` used here before enforced grants with the flag off, // standing every ungranted subscription (Anonymous included) down on the next tick or deploy. + // #1033: the flag-off answer for a local node needs no memo entry (the tuple key + dictionary growth were most of a + // 10k-item pass's ~1.4 MB / ~43 ms). + if (IsLocalNodeUngated(nodeId)) return true; if (identity is null || (identity.ApiKeyId ?? identity.Principal) is not { Length: > 0 } sessionKey || !nodeId.TryGetValue(out string identifier) || identifier.Length == 0) diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/NodeManagerStage6EnforcementTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/NodeManagerStage6EnforcementTests.cs index 6ba6452a6..ad1e778f4 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/NodeManagerStage6EnforcementTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/NodeManagerStage6EnforcementTests.cs @@ -840,6 +840,37 @@ public sealed class NodeManagerStage6EnforcementTests : IDisposable finally { await host.DisposeAsync(); } } + /// + /// #1033: with the flag off a revalidation pass over local-namespace items is an unconditional allow, so it must not + /// pay the pass-local memo (a tuple key + dictionary slot per item) or the per-item authorizer walk. Measured over + /// 10k items: ~6.5–7 ms and ~1.4 MB per pass before, ~0.5–1 ms and ~0.2 KB after. Budgeted here on allocation (a + /// 2,000-item pass allocated ~290 KB before). + /// is the control that the pass still decides when the flag is on. + /// + [Fact] + public async Task A_flag_off_revalidation_pass_over_local_items_allocates_almost_nothing() + { + var (host, server) = await BootAsync(); + try + { + var nm = server.NodeManager!; + nm.Authorizer = Authorizer(enabled: false, GrantOn(NodePermissions.None)); + const int n = 2_000; + var items = new List(n); + for (var i = 0; i < n; i++) + items.Add(TrackItem(nm, Variable(nm, $"Plant/Modbus/dev1/T{i}"), i % 2 == 0 ? Identity() : AnonymousIdentity())); + + nm.RevalidateMonitoredItems(); // warm-up + var before = GC.GetAllocatedBytesForCurrentThread(); + for (var pass = 0; pass < 5; pass++) nm.RevalidateMonitoredItems(); + var perPass = (GC.GetAllocatedBytesForCurrentThread() - before) / 5; + + items.ShouldAllBe(i => i.Denials == 0); + perPass.ShouldBeLessThan(8 * 1024, $"a flag-off pass over {n} local items allocated {perPass} bytes"); + } + finally { await host.DisposeAsync(); } + } + // ───────────────────────────── helpers ───────────────────────────── /// An Anonymous session's identity, as the activation handler builds it (reserved principal, no key).