fix(acl): close the #808 review findings — reference variables, owner folder chain, NUL keys, unknown ids (#808)

Review of af5c9744:
1. UNS reference variables (s=Area/Line/Equip/Eff) of a moved or renamed
   equipment stayed readable/writable under the OLD names' grants: they now
   resolve as ReferenceVariable and are authorized at their split path only
   while it equals their live equipment folder's placement (live chain ==
   generation chain) + one name; otherwise denied (Read and Write).
2. TryPlaceScriptedCondition now also requires the owner folder's live chain
   to match the generation (AuthorizeScriptedCondition gains ownerFolderChain),
   so a moved equipment's alarm/children/commands follow its folder.
3. Identifiers containing NUL are denied before the decision cache, keeping
   client ids disjoint from the NUL-prefixed resolved-scope keys (a client
   could otherwise poison a real folder's cached verdict for its key shard).
4. An id no live node holds is Unknown: placed at [\0unknown] so only
   Cluster / Namespace-uns grants decide it (a removed opaque id can no longer
   split onto a same-named narrow grant; narrow grantees get no NodeDeleted for
   removed UNS folders/vtags/conditions — documented); 4-segment
   reference-shaped ids keep their split.
5. A wire test drives placements from DeploymentArtifact.ParseDataPlaneAuthority.

Baseline updates: #797 owner tests (unit + wire) and Sdk2RepeatedAlarmAuthority
now install folder scopes; Sdk2HistoryAuthority's UNS alias used the RAW path
as its UNS id (never minted in production) and now carries EqA/HistoryAlias.
Each new guard was falsified (reverting it fails its test).
This commit is contained in:
Joseph Doherty
2026-11-10 23:44:11 -05:00
parent af5c9744b7
commit e39a125069
11 changed files with 557 additions and 64 deletions
@@ -2304,6 +2304,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
// authority. Never a fallback to splitting the opaque id.
var native = false;
string? owner = null;
List<string>? ownerChain = null;
lock (Lock)
{
var key = MapKey(condition.NodeId);
@@ -2311,12 +2312,11 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
{
native = true;
}
else if (_scriptedAlarmOwners.TryGetValue(key, out var binding)
&& ReferenceEquals(binding.Alarm, condition)
&& _alarmConditions.TryGetValue(key, out var live)
&& ReferenceEquals(live, condition))
else
{
owner = binding.EquipmentId;
owner = BoundScriptedOwner(key, condition);
// Gitea #808 review: the owner's folder must also sit where the generation deploys it.
ownerChain = BoundScriptedOwnerChain(key, condition);
}
}
@@ -2324,7 +2324,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
? _authorizer!.Authorize(
identity, AlarmOperationFor(operation), condition.NodeId, RealmOf(condition.NodeId))
: _authorizer!.AuthorizeScriptedCondition(
identity, AlarmOperationFor(operation), condition.NodeId, owner);
identity, AlarmOperationFor(operation), condition.NodeId, owner, ownerChain);
}
// The activation-time AlarmAck role is honoured only while the key that earned it is
@@ -4224,7 +4224,10 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
/// </para>
/// <list type="bullet">
/// <item><b>UNS reference variables</b> — <c>s=Area/Line/Equipment/EffectiveName</c>, NAMES: the id IS the
/// ACL path (<see cref="UnsAclSubjectKind.Path"/>), so a <c>Tag</c> grant keeps matching it.</item>
/// ACL path, so a <c>Tag</c> grant keeps matching it — but only while it equals its live equipment folder's
/// placement + one name (<see cref="UnsAclSubjectKind.ReferenceVariable"/>, #808 review).</item>
/// <item><b>Anything no live node holds</b> (removed, never deployed, probed):
/// <see cref="UnsAclSubjectKind.Unknown"/> — decided only by a realm-wide grant.</item>
/// <item><b>Hierarchy folders and what they own</b> — Area / Line / Equipment folders under their opaque
/// entity ids (<c>s=UA-…</c>/<c>s=UL-…</c>/<c>s=EQ-…</c>), and an equipment's virtual tags and their
/// sub-folders (<c>s=EQ-…/Name</c>): <see cref="UnsAclSubjectKind.Folder"/>, with the live id ancestry.</item>
@@ -4263,21 +4266,18 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
if (node is null)
{
// Not a folder or variable: a scripted condition (s=<id>) or one of its instance-scoped children
// (s=<id>/<browse path>, #629). Anything else is an unknown id and keeps the split.
// (s=<id>/<browse path>, #629). Anything else is held by NO live node — removed, never deployed, or a
// probe — and is Unknown: only a realm-wide grant decides it (review of #808: splitting a removed opaque
// id let it match a narrow grant on an unrelated entity of the same name).
var conditionId = slash < 0 ? identifier : identifier[..slash];
var conditionKey = MapKey(AddressSpaceRealm.Uns, conditionId);
if (!_alarmConditions.TryGetValue(conditionKey, out var condition)) return UnsAclSubject.Path;
if (!_alarmConditions.TryGetValue(conditionKey, out var condition)) return UnsAclSubject.Unknown;
lock (Lock)
{
if (_nativeAlarmNodeIds.Contains(conditionKey)) return UnsAclSubject.Path;
var owner = _scriptedAlarmOwners.TryGetValue(conditionKey, out var binding)
&& ReferenceEquals(binding.Alarm, condition)
&& _alarmConditions.TryGetValue(conditionKey, out var live)
&& ReferenceEquals(live, condition)
? binding.EquipmentId
: null;
return UnsAclSubject.ScriptedCondition(conditionId, owner);
return UnsAclSubject.ScriptedCondition(
conditionId, BoundScriptedOwner(conditionKey, condition), BoundScriptedOwnerChain(conditionKey, condition));
}
}
@@ -4292,11 +4292,30 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
break;
}
if (owning is null || !firstSegment.SequenceEqual(owning.NodeId.IdentifierAsString)) return UnsAclSubject.Path;
// No live hierarchy folder above it: a shape only test rigs mint (a UNS node hung straight under the root).
if (owning is null) return UnsAclSubject.Path;
// Its live ancestry, root-first: every live hierarchy folder up to the first non-UNS-folder (the root).
var chain = LiveHierarchyChain(owning);
// A node whose first id segment is NOT its equipment's id is a UNS reference variable
// (s=Area/Line/Equipment/EffectiveName): authorized at its split path, but only while that path is where the
// current generation places the equipment it hangs under (review of #808 — a moved or renamed equipment's
// variables keep their OLD-name ids until re-pointed, and the old names' grants must not keep them).
return firstSegment.SequenceEqual(owning.NodeId.IdentifierAsString)
? UnsAclSubject.Folder(chain, isFolderItself: ReferenceEquals(node, owning))
: UnsAclSubject.ReferenceVariable(chain);
}
/// <summary>
/// The live ancestry of <paramref name="folder"/>, root-first and ending at it: every live hierarchy folder (a
/// slash-free UNS folder id) up to the first node that is not one (the shared root).
/// </summary>
/// <param name="folder">A live hierarchy folder.</param>
/// <returns>The id chain.</returns>
private List<string> LiveHierarchyChain(NodeState folder)
{
var chain = new List<string>(3);
current = owning;
NodeState? current = folder;
for (var depth = 0; current is not null && depth < MaxUnsAncestry; depth++, current = ParentOf(current))
{
if (LiveUnsFolderId(current) is not { } id || id.Contains('/')) break;
@@ -4304,9 +4323,30 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
}
chain.Reverse();
return UnsAclSubject.Folder(chain, isFolderItself: ReferenceEquals(node, owning));
return chain;
}
/// <summary>The <c>EquipmentId</c> the exact live <paramref name="condition"/> is bound to, or null. Call under <c>Lock</c>.</summary>
private string? BoundScriptedOwner(RealmKey key, ConditionState condition) =>
_scriptedAlarmOwners.TryGetValue(key, out var binding)
&& ReferenceEquals(binding.Alarm, condition)
&& _alarmConditions.TryGetValue(key, out var live)
&& ReferenceEquals(live, condition)
? binding.EquipmentId
: null;
/// <summary>
/// The live folder-id ancestry of the equipment folder a BOUND scripted condition hangs under (the #808 folder
/// rule applied to the #797 owner), or null when unbound or no longer under that folder. Call under <c>Lock</c>.
/// </summary>
private List<string>? BoundScriptedOwnerChain(RealmKey key, ConditionState condition) =>
BoundScriptedOwner(key, condition) is { } owner
&& ParentOf(condition) is FolderState parent
&& LiveUnsFolderId(parent) is { } parentId
&& string.Equals(parentId, owner, StringComparison.Ordinal)
? LiveHierarchyChain(parent)
: null;
/// <summary>The node's parent in the instance tree, or null for a type/root node.</summary>
/// <param name="node">The node.</param>
/// <returns>Its parent.</returns>